Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • krespim
    Member
    • Jul 2012
    • 49

    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.
  • dpryan
    Devon Ryan
    • Jul 2011
    • 3478

    #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

    • krespim
      Member
      • Jul 2012
      • 49

      #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

      • dpryan
        Devon Ryan
        • Jul 2011
        • 3478

        #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

        • krespim
          Member
          • Jul 2012
          • 49

          #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

          • SEQadmin2
            Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
            by SEQadmin2


            Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

            The systematic characterization of the human proteome has
            ...
            07-20-2026, 11:48 AM
          • SEQadmin2
            Advanced Sequencing Platforms Tackle Neuroscience’s Toughest Genomics Problems
            by SEQadmin2



            Genomics studies in neuroscience face a special challenge due to the brain’s complexity and scarcity of samples. Mapping changes in cell type and state using conventional next-generation sequencing methods remains challenging. Advances in technologies like single-cell sequencing, spatial transcriptomics, and long-read sequencing have opened the door to deeper studies of the brain and diseases like Alzheimer’s, amyotrophic lateral sclerosis (ALS), and schizophrenia.
            ...
            07-09-2026, 11:10 AM
          • SEQadmin2
            Cancer Drug Resistance: The Lingering Barrier to Rising Survival
            by SEQadmin2



            Cancer survival rates have significantly increased in the last few decades in the United States, reaching a combined 70% 5-year survival rate by 2021. Behind this number, there are years of research to find new therapies, drug targets, and early detection methods. But there is one core challenge that keeps slowing down these advances, and it’s about drug resistance.

            There is no single reason why many patients don’t respond to treatment as expected. Cancer is...
            07-08-2026, 05:17 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by SEQadmin2, Yesterday, 12:17 PM
          0 responses
          11 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-23-2026, 11:41 AM
          0 responses
          11 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-20-2026, 11:10 AM
          0 responses
          23 views
          0 reactions
          Last Post SEQadmin2  
          Started by SEQadmin2, 07-13-2026, 10:26 AM
          0 responses
          37 views
          0 reactions
          Last Post SEQadmin2  
          Working...