Skip to content

Instantly share code, notes, and snippets.

@rhallPB
Created September 10, 2014 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhallPB/7275c48d6f166e1410df to your computer and use it in GitHub Desktop.
Save rhallPB/7275c48d6f166e1410df to your computer and use it in GitHub Desktop.
Coverage bed to csv file to be used in CeleraToGephi script
#!/usr/bin/perl
print "Name,MedianCoverage,ReferenceLength,MapsTo\n";
use List::Util qw(sum max);
open FBed, $ARGV[0];
open Fnuc, $ARGV[1];
while(<FBed>){
if (!/^track/){
chomp;
split;
$length{$_[0]} = $_[2];
push(@{$coverageArray{$_[0]}},$_[4]);
}
}
while(<Fnuc>){
if(/^ +[0-9]+/){
split;
$contig = $_[12];
$ref = $_[11];
@out = split(/\|/,$_[12]);
$map{$out[0]} = $_[11];
}
}
foreach $contig (sort keys %length){
$medianCov = median(@{$coverageArray{$contig}});
print $contig.','.$medianCov.','.$length{$contig}.",".$map{$contig}."\n";
}
sub mean
{
return sum(@_)/@_;;
}
sub median
{
my @vals = sort {$a <=> $b} @_;
my $len = @vals;
if($len%2) #odd?
{
return $vals[int($len/2)];
}
else #even
{
return ($vals[int($len/2)-1] + $vals[int($len/2)])/2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment