Mercurial > repos > ldelisle > revertr2orientationinbam
changeset 0:07695b197e6c draft
Uploaded
author | ldelisle |
---|---|
date | Wed, 19 Oct 2022 08:30:26 +0000 |
parents | |
children | 2c86ad672f52 |
files | revertR2orientationInBam/.shed.yml revertR2orientationInBam/revertR2orientationInBam.sh revertR2orientationInBam/revertR2orientationInBam.xml revertR2orientationInBam/test-data/input.sam revertR2orientationInBam/test-data/output.bam |
diffstat | 5 files changed, 81 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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.
--- /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
--- /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 @@ +<tool id="revertR2orientationInBam" name="revertR2orientationInBam" version="0.0.1" profile="16.04"> + <description> Revert the mapped orientation of R2 mates in a bam.</description> + <requirements> + <requirement type="package" version="1.9">samtools</requirement> + </requirements> + <stdio> + <!-- Anything other than zero is an error --> + <exit_code range="1:" /> + <exit_code range=":-1" /> + <!-- In case the return code has not been set propery check stderr too --> + <regex match="Error:" /> + <regex match="Exception:" /> + </stdio> + <command> +<![CDATA[ + bash $__tool_directory__/revertR2orientationInBam.sh $input $output +]]> + </command> + <inputs> + <!-- note unsorted bam includes all derived bam types (inlcuding bam which is sorted) --> + <param name="input" type="data" format="sam,bam" label="Select a sam or bam file."/> + </inputs> + <outputs> + <data format="bam" name="output" label="$input.name with R2 orientation reversed"/> + </outputs> + + <tests> + <test> + <param name="input" value="input.sam"/> + <output name="output" file="output.bam" ftype="bam"/> + </test> + </tests> + <help> +<![CDATA[ + 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. +]]> + </help> +</tool>
--- /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