Header Leaderboard Ad

Collapse

Changing the strand in a Bam/Sam file

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
            Improved Targeted Sequencing: A Comprehensive Guide to Amplicon Sequencing
            by seqadmin



            Amplicon sequencing is a targeted approach that allows researchers to investigate specific regions of the genome. This technique is routinely used in applications such as variant identification, clinical research, and infectious disease surveillance. The amplicon sequencing process begins by designing primers that flank the regions of interest. The DNA sequences are then amplified through PCR (typically multiplex PCR) to produce amplicons complementary to the targets. RNA targets...
            03-21-2023, 01:49 PM
          • seqadmin
            Targeted Sequencing: Choosing Between Hybridization Capture and Amplicon Sequencing
            by seqadmin




            Targeted sequencing is an effective way to sequence and analyze specific genomic regions of interest. This method enables researchers to focus their efforts on their desired targets, as opposed to other methods like whole genome sequencing that involve the sequencing of total DNA. Utilizing targeted sequencing is an attractive option for many researchers because it is often faster, more cost-effective, and only generates applicable data. While there are many approaches...
            03-10-2023, 05:31 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by seqadmin, Yesterday, 01:40 PM
          0 responses
          7 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 03-29-2023, 11:44 AM
          0 responses
          12 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 03-24-2023, 02:45 PM
          0 responses
          20 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 03-22-2023, 12:26 PM
          0 responses
          28 views
          0 likes
          Last Post seqadmin  
          Working...
          X