view snpEff_2_1a/snpEff_2_1a/scripts/fastaSplit.pl @ 3:c052639fa666 default tip

Uploaded
author pcingola
date Fri, 20 Apr 2012 11:22:59 -0400
parents
children
line wrap: on
line source

#!/usr/bin/perl

#------------------------------------------------------------------------------
# Split a fasta file (create one file per sequence)
#
#
#------------------------------------------------------------------------------

use strict;

#------------------------------------------------------------------------------
# Write fasta file
#------------------------------------------------------------------------------
sub writeSeq($$) {
	my($name, $seq) = @_;
	$name = "chr" . $name . ".fa";
	print "Writing to $name\n";
	open OUT, "> $name";
	print OUT $seq;
	close OUT;
}

#------------------------------------------------------------------------------
# Main
#------------------------------------------------------------------------------

my($seq, $name) = ('', '');
my($lineNum, $l, $newName);
#---
# Read fasta file
#---
for($lineNum=0 ; $l = <STDIN> ; $lineNum++ ) {
	if( $l =~/^>\s*(.*?)\s+.*/ ) {
		$newName = $1;
		if( $seq ne "" ) { writeSeq($name, $seq); } 
		# New sequence
		$name = $newName;
		$seq = $l;
	} else { $seq .= $l; }
}

if( $seq ne "" ) { writeSeq($name, $seq); }