|
Revision 201, 1.3 kB
(checked in by cholt, 7 months ago)
|
added split_fasta
|
- Property svn:executable set to
*
|
| Line | |
|---|
| 1 |
#! /usr/bin/perl -w |
|---|
| 2 |
|
|---|
| 3 |
use strict "vars"; |
|---|
| 4 |
use strict "refs"; |
|---|
| 5 |
|
|---|
| 6 |
use FindBin; |
|---|
| 7 |
use lib "$FindBin::Bin/../lib"; |
|---|
| 8 |
use vars qw($LOG $CMD_ARGS); |
|---|
| 9 |
|
|---|
| 10 |
use Iterator::Fasta; |
|---|
| 11 |
|
|---|
| 12 |
my $usage = " |
|---|
| 13 |
Usage: |
|---|
| 14 |
|
|---|
| 15 |
split_fasta [count] <fasta_input> |
|---|
| 16 |
|
|---|
| 17 |
This script splits a multi-fasta file into the number of files specified by count. |
|---|
| 18 |
|
|---|
| 19 |
"; |
|---|
| 20 |
#----------------------------------------------------------------------------- |
|---|
| 21 |
#----------------------------------- MAIN ------------------------------------ |
|---|
| 22 |
#----------------------------------------------------------------------------- |
|---|
| 23 |
if (@ARGV != 2){ |
|---|
| 24 |
print $usage; |
|---|
| 25 |
exit; |
|---|
| 26 |
} |
|---|
| 27 |
|
|---|
| 28 |
#variables that are persistent outside of try block |
|---|
| 29 |
my $count = shift @ARGV; |
|---|
| 30 |
die "ERROR: the count nust be set to a value greater than 1.\n"if ($count <= 1); |
|---|
| 31 |
my $infile = shift @ARGV; |
|---|
| 32 |
die "ERROR: The file \'$infile\' does not exist.\n" if(! -e $infile); |
|---|
| 33 |
|
|---|
| 34 |
my $fasta_iterator; |
|---|
| 35 |
$fasta_iterator = new Iterator::Fasta($infile); |
|---|
| 36 |
|
|---|
| 37 |
my @files; |
|---|
| 38 |
my $i = 0; |
|---|
| 39 |
while (my $fasta = $fasta_iterator->nextEntry()) { |
|---|
| 40 |
if(! defined($files[$i])){ |
|---|
| 41 |
my $outfile = $infile; |
|---|
| 42 |
$outfile =~ s/\.fasta$//; |
|---|
| 43 |
$outfile .= "_part_$i.fasta"; |
|---|
| 44 |
open(my $fh, "> $outfile"); |
|---|
| 45 |
push(@files, $fh); |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
my $fh = $files[$i]; |
|---|
| 49 |
|
|---|
| 50 |
print $fh $fasta ."\n"; |
|---|
| 51 |
|
|---|
| 52 |
$i++; |
|---|
| 53 |
$i = 0 if ($i >= $count); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
foreach my $fh (@files){ |
|---|
| 57 |
close($fh); |
|---|
| 58 |
} |
|---|