Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NearyJL78
    Member
    • Feb 2011
    • 14

    #1

    Query bam files, awk question

    Hello,

    I am trying to generate four column files for methylation analysis from my NGS alignments against the human genome (single and paired-end). The file needs to contain the following information for any read that aligns:

    1 - chr# 2 - start position of alignment 3 - stop position of alignment 4 - strand (+/-)

    And nothing else.

    Currently I am trying to awk the bam files to obtain the info, and it seems to work well enough except for strand info. I am either obtaining all positive or all negative strand reads, even though I can see I have alignments to both. I don't know if this is a problem with my awk command or what. If I recall correctly, the PE reads all came out positive strand and the single reads in another run all came out negative. (possibly it was the reverse pattern)

    So I have two questions. First, is my awk command roughly correct? Do you have better suggestions or any idea what I am doing wrong. Second, can you think of any other way to generate this 4 column file? (comma separated preferably)

    Here is the awk for the PE reads:

    samtools view -f 0x0002 -X yourbamfilehere | gawk '{ printf "%s,%s,%s,%s\n", $3, $4, $4 + length($10), (index($2,"r") == 0) }' > yourcsvfilenamehere

    Thanks so much! JN
  • Richard Finney
    Senior Member
    • Feb 2009
    • 701

    #2
    The length($10) may mis-count indels, hard/soft clips, etc.

    Note the use of commas in printf strings for "comma separated value " output.

    Example using awk "and" function to get at bit for strand in FLAG field...

    samtools view sample.bam | awk '{if (and($2,0x10)==0x10) printf "%s,%s,%s,%s\n",$3,$4,$4+length($10),"-"; else printf "%s,%s,%s,%s\n",$3,$4,$4+length($10),"+"}'


    check that strand is correct label.

    Comment

    • adamdeluca
      Member
      • Jul 2010
      • 95

      #3
      would something like this work (using bedtools)?

      Code:
      bamToBed -i in.bam | cut -f1,2,3,6 > out.bed
      Last edited by adamdeluca; 05-02-2011, 03:58 PM.

      Comment

      • NearyJL78
        Member
        • Feb 2011
        • 14

        #4
        Thank you for your help! Both suggestions make sense and while I haven't used bedfiles I think maybe I will give it a try :-)

        Comment

        • steven
          Senior Member
          • Aug 2009
          • 269

          #5
          Originally posted by Richard Finney View Post

          samtools view sample.bam | awk '{if (and($2,0x10)==0x10) printf "%s,%s,%s,%s\n",$3,$4,$4+length($10),"-"; else printf "%s,%s,%s,%s\n",$3,$4,$4+length($10),"+"}'
          With awk i like to redefine the output field separator to avoid the printf and to use the "condition?iftrue:else" form when the condition is long and the actions short

          samtools view sample.bam | awk '{OFS="\t"; print $3,$4,$4+length($10),(and($2,0x10)==0x10)?"-":"+"}'

          (should work the same -not tested)

          but anyway the BEDtools/cut option (above) is probably the best one. Let us know if you have any issue.

          Comment

          • NearyJL78
            Member
            • Feb 2011
            • 14

            #6
            Bedtools was definitely the easiest. I'm going to stick with that, thanks everyone!

            Comment

            Latest Articles

            Collapse

            • kinpict.com
              Reply to 16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
              by kinpict.com
              The part about this topic feels surprisingly grounded, and the details on the page is the bit that made me slow down. I’d keep <a href="https://kinpict.com/">ai family photo generator</a> bookmarked for later.
              Yesterday, 06:37 PM
            • SEQadmin2
              16S/ITS Profiling vs. Shotgun Metagenomics: What Changes in Your Workflow?
              by SEQadmin2





              NGS has revolutionized clinical microbiology by offering new ways to identify and surveil pathogens without prior knowledge of the target. One popular approach is 16S rRNA and ITS amplicon sequencing, which targets specific microbial markers. But studies are increasingly incorporating shotgun metagenomics, a technique that sequences all DNA from a sample without targeting specific microbial markers. This approach allows more detailed taxonomic classification,...
              10-30-2026, 06:46 AM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by SEQadmin2, 11-12-2026, 07:59 AM
            0 responses
            29 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 11-05-2026, 09:25 AM
            0 responses
            36 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 11-03-2026, 11:54 AM
            2 responses
            60 views
            0 reactions
            Last Post kinpict.com  
            Started by SEQadmin2, 10-28-2026, 09:57 AM
            0 responses
            176 views
            0 reactions
            Last Post SEQadmin2  
            Working...