1#!/usr/local/bin/perl
2
3#-------------------------------------------------------------------------------
4#
5# Split a WIG file by chromosome
6#
7#
8#															Pablo Cingolani
9#-------------------------------------------------------------------------------
10
11$chrPrev = "";
12
13# Parse STDIN
14while( $l = <STDIN> ) {
15	$chr = "";
16
17	if( $l =~ /^variableStep\s+chrom=(\S+)/ ) {
18		$chr = $1;
19
20		# Open new chromosome file?
21		if( $chr ne $chrPrev ) {
22			# Close old file
23			close OUT if $chrPrev ne "";
24
25			# Open new file
26			$file = "$chr.wig";
27			print STDERR "Creating new file '$file'\n";
28			open OUT, ">$file";
29
30			$chrPrev = $chr;
31		}
32	}
33
34	print OUT $l;
35}
36
37close OUT;
38