Unconfigured Ad

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

    #1

    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

                  • SEQadmin2
                    Multiomic Liquid Biopsy: What DNA/RNA Panels Reveal That DNA Alone Can’t
                    by SEQadmin2



                    Tumors contain complex mixtures of cell populations that evolve in response to treatment and host-specific factors. Tissue biopsies provide only static snapshots of a tumor’s state. But what if we could follow that evolution over time using a minimally invasive technique?

                    Liquid biopsies can detect tumor-derived material circulating in the bloodstream, providing a way to study treatment response, detect emerging resistance, and discover new biomarkers. Circulating
                    ...
                    10-14-2026, 09:55 AM
                  • SEQadmin2
                    New Genomics Technologies Take Aim at Long-Standing Limits
                    by SEQadmin2


                    Researchers using sequencing and genomics tools often have to make trade-offs. They can choose between speed or scale, short reads or long-range information, or targeted panels or a view of the whole transcriptome. New technologies that have been released this year are built to address those tough choices.

                    We asked six companies the same four questions to learn about their latest products. The new technologies bring a lot to the table, including rethinking sequencing
                    ...
                    09-28-2026, 10:25 AM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by SEQadmin2, 10-22-2026, 11:13 AM
                  0 responses
                  29 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-22-2026, 10:48 AM
                  0 responses
                  22 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-15-2026, 10:52 AM
                  0 responses
                  39 views
                  0 reactions
                  Last Post SEQadmin2  
                  Started by SEQadmin2, 10-14-2026, 05:39 AM
                  0 responses
                  45 views
                  0 reactions
                  Last Post SEQadmin2  
                  Working...