Header Leaderboard Ad

Collapse

Changing the strand in a Bam/Sam file

Collapse

Announcement

Collapse

SEQanswers June Challenge Has Begun!

The competition has begun! We're giving away a $50 Amazon gift card to the member who answers the most questions on our site during the month. We want to encourage our community members to share their knowledge and help each other out by answering questions related to sequencing technologies, genomics, and bioinformatics. The competition is open to all members of the site, and the winner will be announced at the beginning of July. Best of luck!

For a list of the official rules, visit (https://www.seqanswers.com/forum/sit...wledge-and-win)
See more
See less
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

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by seqadmin, Yesterday, 01:08 PM
          0 responses
          6 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 06-01-2023, 08:56 PM
          0 responses
          12 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 06-01-2023, 07:33 AM
          0 responses
          132 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-31-2023, 07:50 AM
          0 responses
          170 views
          0 likes
          Last Post seqadmin  
          Working...
          X