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
                    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 on Modified Bases...
                    Yesterday, 07:01 AM
                  • seqadmin
                    Current Approaches to Protein Sequencing
                    by seqadmin


                    Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
                    04-04-2024, 04:25 PM

                  ad_right_rmr

                  Collapse

                  News

                  Collapse

                  Topics Statistics Last Post
                  Started by seqadmin, 04-11-2024, 12:08 PM
                  0 responses
                  37 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 04-10-2024, 10:19 PM
                  0 responses
                  41 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 04-10-2024, 09:21 AM
                  0 responses
                  35 views
                  0 likes
                  Last Post seqadmin  
                  Started by seqadmin, 04-04-2024, 09:00 AM
                  0 responses
                  54 views
                  0 likes
                  Last Post seqadmin  
                  Working...
                  X