Last active
October 25, 2024 14:13
-
-
Save taoliu/a44244f98ae2ac0d37282f15167cf69a to your computer and use it in GitHub Desktop.
Convert samtools depth output to bedGraph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# A script to convert samtools (>v1.3) depth (with -aa option) output | |
# to bedGraph by merging continous positions with the same value. | |
if ( $#ARGV < 0 ) { | |
print "Need 1 parameter! $0 <samtools depth output (with -aa)>\n"; | |
exit (); | |
} | |
$f = $ARGV[ 0 ]; | |
open( IN,$f); | |
$_ = <IN>; | |
chomp; | |
( $chr,$s,$v ) = split; | |
$e = $s; | |
$s -= 1; | |
while( <IN> ){ | |
chomp; | |
( $c_chr,$c_s,$c_v ) = split; | |
if( $c_chr eq $chr && $c_v != $v ) { | |
print join( "\t",( $chr, $s, $e, $v ) ),"\n"; | |
$s = $c_s - 1; | |
$v = $c_v; | |
$e = $c_s } | |
elsif( $c_chr eq $chr && $c_v == $v ) { | |
$e = $c_s; | |
} | |
elsif( $c_chr ne $chr ) { | |
print join( "\t",( $chr,$s,$e,$v ) ),"\n"; | |
$chr = $c_chr; | |
$s = $c_s-1; | |
$v = $c_v; | |
$e = $c_s; | |
} | |
} | |
# last region | |
print join( "\t",( $chr,$s,$e,$v ) ),"\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, this script may have a bug that the last chr does not print out.