Seqanswers Leaderboard Ad

Collapse

Announcement

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

  • How to get mapped reads ID from bam files using samtools?

    I produced the bam file using paire-end data.
    I want to get the mapped (not care mate mapped or not) reads ID from a bam file. How can I do this?

    Thanks.

  • #2
    Can I do like this?

    java -jar /usr/local/bin/ViewSam.jar VALIDATION_STRINGENCY=LENIENT INPUT=your_mapped_reads.sam ALIGNMENT_STATUS=Aligned PF_STATUS=All > your_Aligned_reads.sam

    Comment


    • #3
      It seems that there is not a flag to set which reads mapped or not.
      samtools view -f

      6. In a string FLAG, each character represents one bit with
      p=0x1 (paired), P=0x2 (properly paired), u=0x4 (unmapped),
      U=0x8 (mate unmapped), r=0x10 (reverse), R=0x20 (mate reverse)
      1=0x40 (first), 2=0x80 (second), s=0x100 (not primary),
      f=0x200 (failure) and d=0x400 (duplicate). Note that `-x' and
      `-X' are samtools-C specific. Picard and older samtools do not
      support HEX or string flags.

      Comment


      • #4
        Originally posted by fabrice View Post
        It seems that there is not a flag to set which reads mapped or not.
        samtools view -f

        6. In a string FLAG, each character represents one bit with
        p=0x1 (paired), P=0x2 (properly paired), u=0x4 (unmapped),
        U=0x8 (mate unmapped), r=0x10 (reverse), R=0x20 (mate reverse)
        1=0x40 (first), 2=0x80 (second), s=0x100 (not primary),
        f=0x200 (failure) and d=0x400 (duplicate). Note that `-x' and
        `-X' are samtools-C specific. Picard and older samtools do not
        support HEX or string flags.
        There is a flag for unmapped though. If that bit flag is not set then the read is mapped. Instead of using the '-f' required flag parameter with samtools view use the '-F' filter flag parameter to remove unmapped reads.

        Code:
        # samtools view -F 4 -b -o <out.bam> <in.bam>
        The out.bam file will contain only reads which mapped, regardless of whether their mates mapped or not.

        Comment


        • #5
          kmcarr,
          Thank you.
          Yes. Now I can get the mapped reads.
          I do not understand why the reads number from "samtools view -F 4" is different to the "samtools flagstat" report mapped number.
          The number from "samtools view -F 4" is bigger.

          Comment


          • #6
            I am not sure -F 4 will get mapped reads.

            -F will skip alignments with bits present in INT,

            4 is the unmapped reads?

            Comment


            • #7
              Originally posted by chenyao View Post
              I am not sure -F 4 will get mapped reads.

              -F will skip alignments with bits present in INT,

              4 is the unmapped reads?
              Yes, 4 (in decimal) or 0x0004 (in HEX) indicates that the query read is unmapped. If you use the filter option (-F 4) you will remove unmapped reads, hence the output will only contain mapped reads. By contrast if you use the required option (-f 4) you will only output unmapped reads.

              Comment


              • #8
                It's a pair-end data, how about one read mapped and the other not. Did -F 4 will retain the mapped reads without considering mate? If so , how can I extract paired mapping reads?

                Comment


                • #9
                  Originally posted by chenyao View Post
                  It's a pair-end data, how about one read mapped and the other not. Did -F 4 will retain the mapped reads without considering mate? If so , how can I extract paired mapping reads?
                  -F 4 will get all mapped reads, regardless of what the mate looked like. When the 8 flag is present, the mate was unmapped. so -F 12 should get all reads where both ends mapped. -F 4 will get all the mapped reads, and since unmapped mates will have a 4, it won't get those.

                  Comment


                  • #10
                    For pair-end data, samtools flagstat how to count?

                    Comment


                    • #11
                      Originally posted by fabrice View Post
                      For pair-end data, samtools flagstat how to count?
                      Flagstat will count a number of things, like how many reads there are total, how many aligned, how many aligned in proper pairs, etc.

                      If you want to count how many read have or don't have particular flags, the -c option in samtools view will just count reads, rather than outputting all the reads that meet the flagging requirements. Or, output them all with filters set in view, and do a wc -l to count the number of lines in the file. Adjust for headers, of course.

                      Comment


                      • #12
                        swbarnes2,

                        Thank you for your reply.

                        It seems my question is not clear.

                        I used samtools view -F 4, which give a number how many reads are mapped.

                        Then I used samtools flagstat, it also report a number how many reads are mapped. But in mu case, this two number is not equal. So I just wonder how samtools flagstat report a number is mapped. My mapped file is from eland.

                        Originally posted by swbarnes2 View Post
                        Flagstat will count a number of things, like how many reads there are total, how many aligned, how many aligned in proper pairs, etc.

                        If you want to count how many read have or don't have particular flags, the -c option in samtools view will just count reads, rather than outputting all the reads that meet the flagging requirements. Or, output them all with filters set in view, and do a wc -l to count the number of lines in the file. Adjust for headers, of course.

                        Comment


                        • #13
                          Originally posted by fabrice View Post
                          swbarnes2,

                          Thank you for your reply.

                          It seems my question is not clear.

                          I used samtools view -F 4, which give a number how many reads are mapped.

                          Then I used samtools flagstat, it also report a number how many reads are mapped. But in mu case, this two number is not equal. So I just wonder how samtools flagstat report a number is mapped. My mapped file is from eland.
                          Originally posted by swbarnes2 View Post
                          Flagstat will count a number of things, like how many reads there are total, how many aligned, how many aligned in proper pairs, etc.

                          If you want to count how many read have or don't have particular flags, the -c option in samtools view will just count reads, rather than outputting all the reads that meet the flagging requirements. Or, output them all with filters set in view, and do a wc -l to count the number of lines in the file. Adjust for headers, of course.
                          To be precise flagstat (or for that matter counting lines in a .sam file) is not counting reads, it is reporting how many alignments are in the file. If the parameters used for your mapping allowed for reporting multiple alignments per read then the numbers reported by flagstat (or #smtools view | wc -l) may be greater than the number of input reads.

                          This still does not explain the problem fabrice is having where he is seeing a larger number of alignments by counting the lines in his .sam file vs. what is reported by flagstat. I don't have an answer for that one, but I do have a question, what version of samtools are you using?

                          Comment


                          • #14
                            Thanks.

                            samtools is 0.1.17.

                            I expect that samtools flagstat will give bigger number than samtools view -F 4. But no, in my case.



                            Originally posted by kmcarr View Post
                            To be precise flagstat (or for that matter counting lines in a .sam file) is not counting reads, it is reporting how many alignments are in the file. If the parameters used for your mapping allowed for reporting multiple alignments per read then the numbers reported by flagstat (or #smtools view | wc -l) may be greater than the number of input reads.

                            This still does not explain the problem fabrice is having where he is seeing a larger number of alignments by counting the lines in his .sam file vs. what is reported by flagstat. I don't have an answer for that one, but I do have a question, what version of samtools are you using?

                            Comment


                            • #15
                              Originally posted by fabrice View Post
                              Thanks.

                              samtools is 0.1.17.

                              I expect that samtools flagstat will give bigger number than samtools view -F 4. But no, in my case.
                              They should give the same number, and that is the case with the handful of bam files I have checked myself. Again the # reported for mapped with flagstat is not the number of reads which mapped but the number of alignments reported in the file. When output as a sam text file there is one line for each alignment which you count with wc -l. The numbers should be equal.

                              Comment

                              Latest Articles

                              Collapse

                              • seqadmin
                                Strategies for Sequencing Challenging Samples
                                by seqadmin


                                Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
                                03-22-2024, 06:39 AM
                              • seqadmin
                                Techniques and Challenges in Conservation Genomics
                                by seqadmin



                                The field of conservation genomics centers on applying genomics technologies in support of conservation efforts and the preservation of biodiversity. This article features interviews with two researchers who showcase their innovative work and highlight the current state and future of conservation genomics.

                                Avian Conservation
                                Matthew DeSaix, a recent doctoral graduate from Kristen Ruegg’s lab at The University of Colorado, shared that most of his research...
                                03-08-2024, 10:41 AM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by seqadmin, 03-27-2024, 06:37 PM
                              0 responses
                              12 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 03-27-2024, 06:07 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 03-22-2024, 10:03 AM
                              0 responses
                              53 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 03-21-2024, 07:32 AM
                              0 responses
                              69 views
                              0 likes
                              Last Post seqadmin  
                              Working...
                              X