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
                                Advancing Precision Medicine for Rare Diseases in Children
                                by seqadmin




                                Many organizations study rare diseases, but few have a mission as impactful as Rady Children’s Institute for Genomic Medicine (RCIGM). “We are all about changing outcomes for children,” explained Dr. Stephen Kingsmore, President and CEO of the group. The institute’s initial goal was to provide rapid diagnoses for critically ill children and shorten their diagnostic odyssey, a term used to describe the long and arduous process it takes patients to obtain an accurate...
                                12-16-2024, 07:57 AM
                              • seqadmin
                                Recent Advances in Sequencing Technologies
                                by seqadmin



                                Innovations in next-generation sequencing technologies and techniques are driving more precise and comprehensive exploration of complex biological systems. Current advancements include improved accessibility for long-read sequencing and significant progress in single-cell and 3D genomics. This article explores some of the most impactful developments in the field over the past year.

                                Long-Read Sequencing
                                Long-read sequencing has seen remarkable advancements,...
                                12-02-2024, 01:49 PM

                              ad_right_rmr

                              Collapse

                              News

                              Collapse

                              Topics Statistics Last Post
                              Started by seqadmin, 12-17-2024, 10:28 AM
                              0 responses
                              33 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 12-13-2024, 08:24 AM
                              0 responses
                              49 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 12-12-2024, 07:41 AM
                              0 responses
                              34 views
                              0 likes
                              Last Post seqadmin  
                              Started by seqadmin, 12-11-2024, 07:45 AM
                              0 responses
                              46 views
                              0 likes
                              Last Post seqadmin  
                              Working...
                              X