Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fabrice
    Member
    • Oct 2009
    • 86

    #1

    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.
  • fabrice
    Member
    • Oct 2009
    • 86

    #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

    • fabrice
      Member
      • Oct 2009
      • 86

      #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

      • kmcarr
        Senior Member
        • May 2008
        • 1181

        #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

        • fabrice
          Member
          • Oct 2009
          • 86

          #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

          • chenyao
            Member
            • Jul 2011
            • 74

            #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

            • kmcarr
              Senior Member
              • May 2008
              • 1181

              #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

              • chenyao
                Member
                • Jul 2011
                • 74

                #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

                • swbarnes2
                  Senior Member
                  • May 2008
                  • 910

                  #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

                  • fabrice
                    Member
                    • Oct 2009
                    • 86

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

                    Comment

                    • swbarnes2
                      Senior Member
                      • May 2008
                      • 910

                      #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

                      • fabrice
                        Member
                        • Oct 2009
                        • 86

                        #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

                        • kmcarr
                          Senior Member
                          • May 2008
                          • 1181

                          #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

                          • fabrice
                            Member
                            • Oct 2009
                            • 86

                            #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

                            • kmcarr
                              Senior Member
                              • May 2008
                              • 1181

                              #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

                              • 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-15-2026, 10:52 AM
                              0 responses
                              26 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 10-14-2026, 05:39 AM
                              0 responses
                              33 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 10-13-2026, 09:56 AM
                              0 responses
                              31 views
                              0 reactions
                              Last Post SEQadmin2  
                              Started by SEQadmin2, 10-09-2026, 11:14 AM
                              0 responses
                              28 views
                              0 reactions
                              Last Post SEQadmin2  
                              Working...