Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • serhatg
    Member
    • Mar 2014
    • 10

    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
  • GenoMax
    Senior Member
    • Feb 2008
    • 7142

    #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

    • serhatg
      Member
      • Mar 2014
      • 10

      #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

      • GenoMax
        Senior Member
        • Feb 2008
        • 7142

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

        Comment

        • dpryan
          Devon Ryan
          • Jul 2011
          • 3478

          #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

          • serhatg
            Member
            • Mar 2014
            • 10

            #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

            • dpryan
              Devon Ryan
              • Jul 2011
              • 3478

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

              Comment

              • rnaeye
                Member
                • May 2011
                • 80

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

                Comment

                • syfo
                  Just a member
                  • Nov 2012
                  • 103

                  #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

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, Yesterday, 11:58 AM
                  0 responses
                  13 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 06-05-2026, 10:09 AM
                  0 responses
                  25 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 06-04-2026, 08:59 AM
                  0 responses
                  35 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 06-02-2026, 12:03 PM
                  0 responses
                  60 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...