# HG changeset patch # User ldelisle # Date 1666168226 0 # Node ID 07695b197e6c54a2ec5e82cb491d81e5e94d5e6d Uploaded diff -r 000000000000 -r 07695b197e6c revertR2orientationInBam/.shed.yml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/revertR2orientationInBam/.shed.yml Wed Oct 19 08:30:26 2022 +0000 @@ -0,0 +1,6 @@ +categories: + - SAM +description: Revert the mapped orientation of R2 mates in a bam. +name: revertr2orientationinbam +owner: lldelisle +long_description: This tool is very useful when you have paired-end stranded RNA-seq. Using this tool prior to a bedtools genome coverage allow to have strand specific coverage using both mates. It uses samtools to convert input to sam format and then awk to modify the flag "reverse strand" for the second mate of pairs. diff -r 000000000000 -r 07695b197e6c revertR2orientationInBam/revertR2orientationInBam.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/revertR2orientationInBam/revertR2orientationInBam.sh Wed Oct 19 08:30:26 2022 +0000 @@ -0,0 +1,28 @@ +#!/bin/bash + +inputBam=$1 +outputBam=$2 + +samtools view -h "$inputBam" | awk -v OFS="\t" '{ + # Process only non header lines + if(!($1~/^@/)){ + # SAM flag is field 2 + n=$2 + # Change only the second in pair flag is 128 + d=128 + q=(n-n%d)/d+(n<0) + if(q%2==1){ + # Evaluate the strand reverse strand flag is 16 + d=16 + q=(n-n%d)/d+(n<0) + if(q%2==1){ + # It is reverse it is now forward + $2-=16 + }else{ + # It is forward it is now reverse + $2+=16 + } + } + } + print +}' | samtools view -b - > "$outputBam" \ No newline at end of file diff -r 000000000000 -r 07695b197e6c revertR2orientationInBam/revertR2orientationInBam.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/revertR2orientationInBam/revertR2orientationInBam.xml Wed Oct 19 08:30:26 2022 +0000 @@ -0,0 +1,40 @@ + + Revert the mapped orientation of R2 mates in a bam. + + samtools + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 000000000000 -r 07695b197e6c revertR2orientationInBam/test-data/input.sam --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/revertR2orientationInBam/test-data/input.sam Wed Oct 19 08:30:26 2022 +0000 @@ -0,0 +1,7 @@ +@HD VN:1.4 SO:coordinate +@SQ SN:chr1 LN:195471971 +1 163 chr1 3151308 255 43M = 3151560 295 GTCCCAAAAAGCAGAGAGAAATATCTCTCTGGGCCTTATAGCA AAAAAEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE NH:i:1 HI:i:1 AS:i:82 nM:i:1 +1 83 chr1 3151560 255 43M = 3151308 -295 CTGCTGGAAAAACTGTTCGTTTTAGGAAATAAGGGCGAGTGCG EEEEEEAEEEEEEEEEAEEEEEEEEEEEEEEEEEEEEEAAAAA NH:i:1 HI:i:1 AS:i:82 nM:i:1 +2 137 chr1 3155261 255 9S20M * 0 0 TGTGGGTTTTACACACGGGAGGAATCGGG AAAAA/EEE/EE6EAEE6EAE6/EEEAEE NH:i:1 HI:i:1 AS:i:19 nM:i:0 +3 99 chr1 3195817 255 43M = 3195911 137 ATCTTACTCTGTTTCACAGATAGGTGCTGAGGTTAAATTTTGC AAAAAEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE NH:i:1 HI:i:1 AS:i:84 nM:i:0 +3 147 chr1 3195911 255 43M = 3195817 -137 GGTGTAGGTTCTTAGACGCAAGAAATCTCAGGTAGAGTTTGGC EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEAAAAA NH:i:1 HI:i:1 AS:i:84 nM:i:0 diff -r 000000000000 -r 07695b197e6c revertR2orientationInBam/test-data/output.bam Binary file revertR2orientationInBam/test-data/output.bam has changed