Seqanswers Leaderboard Ad

Collapse

Announcement

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

  • Changing the strand in a Bam/Sam file

    My sequencing data has been generated using the dUTP method, which means that when viewed in IGV or other browsers the reads are shown as being in the opposite strand from which they are generated. This is usually not an issue but now I need to prepare some figures (using the fantastic bioconductor package gviz) where the direction of the reads is the key. The question is, how to reverse the strandness of the alignments in a bam/sam file?

    I have been using a combination of bamToBed | awk | bedToBam but I loose the information on split reads, that is, a split read will become multiple reads. There must a more elegant way of doing it but I am missing it.

  • #2
    There are a couple ways to do that. If the version of awk on your system is actually gawk, you can use the bitwise operators (namely "or()") to test the strand and then swap things accordingly. Some systems come with mawk, which lacks that. So, you can always use python:

    Code:
    #!/usr/bin/env python
    import csv
    import sys
    
    f = csv.reader(sys.stdin, dialect="excel-tab")
    of = csv.writer(sys.stdout, dialect="excel-tab")
    for line in f :
        #Don't try to modify the header!!!
        if(line[0][0] == "@") :
            of.writerow(line)
            continue
        #Swap strand
        if((int(line[1]) & 0x10) == 0x10) :
            line[1] = int(line[1]) - 0x10
        else :
            line[1] = int(line[1]) + 0x10
        of.writerow(line)
    For paired-end data, you could swap the flag that indicates the mate's orientation in the same manner. If you happen to have pysam installed, this example code could be even shorter.
    Last edited by dpryan; 11-14-2013, 01:49 AM. Reason: Had a 1 were I should have had a 0!

    Comment


    • #3
      also in awk

      Thanks a lot dpryan. In the mean time some folks in lab helped to come up with a awk solution:

      Code:
      #!/usr/bin/awk -f
      
      BEGIN{
      	FS="\t";OFS="\t";}
      {
      	if($2=="16"){
      		$2="0";}
      	else{
      		if($2=="0"){
      			$2="16";}
      		}
      print $_;
      }

      Comment


      • #4
        That's pretty similar to the awk solution I had in mind. If you end up with paired-end reads or things with secondary alignments, then you end up having to check the actual bits in the flag. Otherwise, that'll work!

        Comment


        • #5
          Originally posted by dpryan View Post
          That's pretty similar to the awk solution I had in mind. If you end up with paired-end reads or things with secondary alignments, then you end up having to check the actual bits in the flag. Otherwise, that'll work!

          Thankfully this is single read and I only have the flag "0" and "16" on that field

          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 on Modified Bases...
            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
          55 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 04-10-2024, 10:19 PM
          0 responses
          51 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