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
                    Recent Advances in Sequencing Analysis Tools
                    by seqadmin


                    The sequencing world is rapidly changing due to declining costs, enhanced accuracies, and the advent of newer, cutting-edge instruments. Equally important to these developments are improvements in sequencing analysis, a process that converts vast amounts of raw data into a comprehensible and meaningful form. This complex task requires expertise and the right analysis tools. In this article, we highlight the progress and innovation in sequencing analysis by reviewing several of the...
                    05-06-2024, 07:48 AM
                  • seqadmin
                    Essential Discoveries and Tools in Epitranscriptomics
                    by seqadmin




                    The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
                    04-22-2024, 07:01 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by seqadmin, 05-10-2024, 06:35 AM
                  0 responses
                  19 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 05-09-2024, 02:46 PM
                  0 responses
                  21 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 05-07-2024, 06:57 AM
                  0 responses
                  19 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 05-06-2024, 07:17 AM
                  0 responses
                  21 views
                  0 likes
                  Last Post seqadmin  
                  Working...
                  X