5 die "Usage: $0 <in bam> <out path>\n" if @ARGV<2;
9 my $infilename = (split /\//,$in)[-1];
10 $infilename =~ s/\.bam$//i;
12 my $SAMTOOLS='samtools';
13 system($SAMTOOLS) == -1 and $SAMTOOLS='/usr/local/bin/samtools';
16 open H
, '-|', "$SAMTOOLS view -H $in" or die "Error opening $in: $!\n";
20 my ($type,$chr,$len)=split /\t/;
21 next unless $type eq '@SQ';
22 $chr=(split(':',$chr))[1];
24 if ($str =~ /gb\|([\w.]+)\|/) {
31 print "$str, $chr, $len";
32 die if exists $Chr2ID{$chr};
37 for my $k (keys %Chr2ID) {
38 my $out = "$outp/$infilename.$Chr2ID{$k}.bam";
39 open $Chr2FH{$k},'|-', "$SAMTOOLS view -bS - >$out" or die "Error opening $out: $!\n";
40 print { $Chr2FH{$k} } @HEADS;
42 open I
, '-|', "$SAMTOOLS view $in" or die "Error opening $in: $!\n";
44 my @items = split /\t/;
45 next if $items[2] eq '*';
46 my $fh = $Chr2FH{$items[2]} or die;
50 close $Chr2FH{$_} for keys %Chr2FH;