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

            • 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-22-2026, 11:13 AM
            0 responses
            26 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-22-2026, 10:48 AM
            0 responses
            20 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-15-2026, 10:52 AM
            0 responses
            36 views
            0 reactions
            Last Post SEQadmin2  
            Started by SEQadmin2, 10-14-2026, 05:39 AM
            0 responses
            43 views
            0 reactions
            Last Post SEQadmin2  
            Working...