Skip to content

Instantly share code, notes, and snippets.

@davetang
Last active December 28, 2015 20:19
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 davetang/7556434 to your computer and use it in GitHub Desktop.
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.
#!/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