1#!/usr/local/bin/perl
2
3#------------------------------------------------------------------------------
4# Split a fasta file (create one file per sequence)
5#
6#																Pablo Cingolani
7#------------------------------------------------------------------------------
8
9use strict;
10
11#------------------------------------------------------------------------------
12# Write fasta file
13#------------------------------------------------------------------------------
14sub writeSeq($$) {
15	my($name, $seq) = @_;
16	if( $name !~ /^chr/ ) { $name = "chr$name"; }
17	$name = "$name.fa";
18	print "Writing to $name\n";
19	open OUT, "> $name";
20	print OUT $seq;
21	close OUT;
22}
23
24#------------------------------------------------------------------------------
25# Main
26#------------------------------------------------------------------------------
27
28my($seq, $name) = ('', '');
29my($lineNum, $l, $newName);
30
31# Read fasta file
32for($lineNum=0 ; $l = <STDIN> ; $lineNum++ ) {
33	if( $l =~/^>\s*(.*?)\s+.*/ ) {
34		$newName = $1;
35		if( $seq ne "" ) { writeSeq($name, $seq); }
36		# New sequence
37		$name = $newName;
38		$seq = $l;
39	} else { $seq .= $l; }
40}
41
42if( $seq ne "" ) { writeSeq($name, $seq); }
43