Seqanswers Leaderboard Ad

Collapse

Announcement

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

  • 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

  • #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


    • #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


      • #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


        • #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


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

            Comment

            Latest Articles

            Collapse

            • seqadmin
              Essential Discoveries and Tools in Epitranscriptomics
              by seqadmin




              The field of epigenetics has traditionally concentrated more on DNA and how changes like methylation and phosphorylation of histones impact gene expression and regulation. However, our increased understanding of RNA modifications and their importance in cellular processes has led to a rise in epitranscriptomics research. “Epitranscriptomics brings together the concepts of epigenetics and gene expression,” explained Adrien Leger, PhD, Principal Research Scientist...
              Yesterday, 07:01 AM
            • seqadmin
              Current Approaches to Protein Sequencing
              by seqadmin


              Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
              04-04-2024, 04:25 PM

            ad_right_rmr

            Collapse

            News

            Collapse

            Topics Statistics Last Post
            Started by seqadmin, 04-11-2024, 12:08 PM
            0 responses
            58 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-10-2024, 10:19 PM
            0 responses
            54 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-10-2024, 09:21 AM
            0 responses
            45 views
            0 likes
            Last Post seqadmin  
            Started by seqadmin, 04-04-2024, 09:00 AM
            0 responses
            55 views
            0 likes
            Last Post seqadmin  
            Working...
            X