Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read count in a specific region [samtools]

    Dear all,

    I'd like to get the number of reads in a specified region with samtools. Is there a function which does that ? The method shouldn't be OS dependent.

    Thanks in advance,
    Serhat

  • #2
    Discussion of next-gen sequencing related bioinformatics: resources, algorithms, open source efforts, etc


    Using an indexed BAM file

    Code:
    $ samtools view input.bam chr1:1234-2345 | wc -l

    Comment


    • #3
      Originally posted by GenoMax View Post
      http://seqanswers.com/forums/showthread.php?t=33357

      Using an indexed BAM file

      Code:
      $ samtools view input.bam chr1:1234-2345 | wc -l
      Thanks for your answer GenoMax;however, I am looking for a method which is not depended on the OS.

      P.S. I am using samtools libraries in a cpp project.
      Last edited by serhatg; 09-01-2014, 03:44 AM.

      Comment


      • #4
        I missed the programmatic part of your original question. Someone else should be along with the right answer.

        Comment


        • #5
          I guess that someone is me.

          With the older samtools 0.1.19 API, you can just use the bam_fetch() function and give it a function to just increment a counter with each call.

          That function doesn't exist in htslib, so you either need to roll your own version:

          Code:
          //N.B., there are some functions here you'll need to replace
          int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
          {
              int ret;
              bam_iter_t iter;
              bam1_t *b;
              b = bam_init1();
              iter = bam_iter_query(idx, tid, beg, end);
              while ((ret = bam_iter_read(fp, iter, b)) >= 0) func(b, data);
              bam_iter_destroy(iter);
              bam_destroy1(b);
              return (ret == -1)? 0 : ret;
          }
          Alternatively, you can statically compile in libbam.a from the newer version of samtools and the API should be more or less the same as the previous version (annoyingly, there's little documentation at the moment).

          Comment


          • #6
            Originally posted by dpryan View Post
            I guess that someone is me.

            With the older samtools 0.1.19 API, you can just use the bam_fetch() function and give it a function to just increment a counter with each call.

            That function doesn't exist in htslib, so you either need to roll your own version:

            Code:
            //N.B., there are some functions here you'll need to replace
            int bam_fetch(bamFile fp, const bam_index_t *idx, int tid, int beg, int end, void *data, bam_fetch_f func)
            {
                int ret;
                bam_iter_t iter;
                bam1_t *b;
                b = bam_init1();
                iter = bam_iter_query(idx, tid, beg, end);
                while ((ret = bam_iter_read(fp, iter, b)) >= 0) func(b, data);
                bam_iter_destroy(iter);
                bam_destroy1(b);
                return (ret == -1)? 0 : ret;
            }
            Alternatively, you can statically compile in libbam.a from the newer version of samtools and the API should be more or less the same as the previous version (annoyingly, there's little documentation at the moment).

            Thank you for the answer. I was planning to use exactly the same method; however, then I thought there might be a smarter way or a method which I am not aware of. Apparently, I will need to stick to this method.

            Thanks again

            Comment


            • #7
              Well there might be a smarter way, but this is at least the simplest way

              Comment


              • #8
                You can call samtools command from a Perl script that also counts the lines.

                Comment


                • #9
                  Originally posted by GenoMax View Post
                  samtools view input.bam chr1:1234-2345 | wc -l
                  Originally posted by rnaeye View Post
                  You can call samtools command from a Perl script that also counts the lines.
                  On a side note, you can use the "samtools view -c" option instead of counting "yourself" the number of lines.

                  Comment

                  Latest Articles

                  Collapse

                  • seqadmin
                    Best Practices for Single-Cell Sequencing Analysis
                    by seqadmin



                    While isolating and preparing single cells for sequencing was historically the bottleneck, recent technological advancements have shifted the challenge to data analysis. This highlights the rapidly evolving nature of single-cell sequencing. The inherent complexity of single-cell analysis has intensified with the surge in data volume and the incorporation of diverse and more complex datasets. This article explores the challenges in analysis, examines common pitfalls, offers...
                    Today, 07:15 AM
                  • seqadmin
                    Latest Developments in Precision Medicine
                    by seqadmin



                    Technological advances have led to drastic improvements in the field of precision medicine, enabling more personalized approaches to treatment. This article explores four leading groups that are overcoming many of the challenges of genomic profiling and precision medicine through their innovative platforms and technologies.

                    Somatic Genomics
                    “We have such a tremendous amount of genetic diversity that exists within each of us, and not just between us as individuals,”...
                    05-24-2024, 01:16 PM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by seqadmin, Today, 08:18 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, Today, 08:04 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 06-03-2024, 06:55 AM
                  0 responses
                  13 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 05-30-2024, 03:16 PM
                  0 responses
                  27 views
                  0 likes
                  Last Post seqadmin  
                  Working...
                  X