Last active
December 28, 2015 20:19
-
-
Save davetang/7556434 to your computer and use it in GitHub Desktop.
Script that takes as input a BED file stream and outputs the stream to its corresponding chromosome. Do not use this script in parallel.
This file contains hidden or 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
#!/bin/env perl | |
use strict; | |
use warnings; | |
#hash for filehandles | |
my %fh = (); | |
#read from stream | |
while (<>){ | |
my ($chr, @rest) = split(/\t/); | |
if (exists $fh{$chr}){ | |
print {$fh{$chr}} "$_"; | |
} else { | |
#open output filehandle for each chromosome | |
open(my $fh, ">>", "$chr.bed") || die("Could not open $chr.bed for output: $!\n"); | |
#store file handle in hash | |
$fh{$chr} = $fh; | |
print {$fh{$chr}} "$_"; | |
} | |
} | |
#close all filehandles | |
foreach my $fh (keys %fh){ | |
close $fh{$fh}; | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment