1#!/usr/local/bin/perl 2 3#--- 4# Initialize parameters 5#--- 6$sampleStart = $ARGV[0]; 7$sampleEnd = $ARGV[1]; 8if(( $ARGV[0] eq '' ) || ($ARGV[1] eq '')) { die "Usage: fastaSample sampleStart sampleEnd\n"; } 9 10$sampleStart--; 11$sampleEnd--; 12$sampleLen = $sampleEnd - $sampleStart + 1; 13 14#--- 15# Read fasta file 16#--- 17for($lineNum=0 ; $l = <STDIN> ; $lineNum++ ) { 18 if( $l =~/^>/ ) { 19 # Sample if not empty 20 if( $seq ne "" ) { 21 $s = substr( $seq, $sampleStart, $sampleLen); 22 print "$s\n"; 23 } 24 # New sequence 25 $seq = ""; 26 } else { 27 chomp($l); 28 $seq .= $l; 29 } 30} 31 32# Sample if not empty 33if( $seq ne "" ) { 34 $s = substr( $seq, $sampleStart, $sampleLen); 35 print "$s\n"; 36} 37 38$len = length($seq); 39print STDERR "Lines: $lineNum\n"; 40print STDERR "Sequence size: $len\n"; 41print STDERR "Sample size: $sampleLen\n"; 42