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
            Latest Developments in Precision Medicine
            by seqadmin



            Technological advances have led to drastic improvements in the field of precision medicine, enabling more personalized approaches to treatment. This article explores four leading groups that are overcoming many of the challenges of genomic profiling and precision medicine through their innovative platforms and technologies.

            Somatic Genomics
            “We have such a tremendous amount of genetic diversity that exists within each of us, and not just between us as individuals,”...
            05-24-2024, 01:16 PM
          • seqadmin
            Recent Advances in Sequencing Analysis Tools
            by seqadmin


            The sequencing world is rapidly changing due to declining costs, enhanced accuracies, and the advent of newer, cutting-edge instruments. Equally important to these developments are improvements in sequencing analysis, a process that converts vast amounts of raw data into a comprehensible and meaningful form. This complex task requires expertise and the right analysis tools. In this article, we highlight the progress and innovation in sequencing analysis by reviewing several of the...
            05-06-2024, 07:48 AM

          ad_right_rmr

          Collapse

          News

          Collapse

          Topics Statistics Last Post
          Started by seqadmin, 05-24-2024, 07:15 AM
          0 responses
          16 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-23-2024, 10:28 AM
          0 responses
          18 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-23-2024, 07:35 AM
          0 responses
          22 views
          0 likes
          Last Post seqadmin  
          Started by seqadmin, 05-22-2024, 02:06 PM
          0 responses
          11 views
          0 likes
          Last Post seqadmin  
          Working...
          X