source: release/1.0.0/outsplit.pl @ 174

Revision 174, 1.0 KB checked in by admin, 6 years ago (diff)
  • Property svn:eol-style set to native
Line 
1#! /usr/bin/perl -w
2# Copyright (2005) Reed A. Cartwright.  All rights reserved.
3#
4# outsplit.pl is used to extract sequences from Fasta and Phylip files
5#
6# usage: perl -w outsplit.pl <file> <id>
7#
8# if <id> is "all" it it creates a directory and
9# creates a new file for each alignment
10#
11# Distributed under the same license as DAWG
12#
13
14use strict;
15use File::Basename;
16use File::Path;
17use File::Spec::Functions;
18
19my ($file, $id) = @ARGV;
20
21local $/;
22
23open( FILE, $file) or die("Error opening $file.");
24
25my $text = <FILE>;
26
27close(FILE);
28
29my @blocks = split(/\[DataSet \d+\].*\n/, $text);
30
31if($id ne 'all')
32{
33        print $blocks[$id];     
34}
35else
36{
37        my ($name,$dir,$ext) = fileparse($file, qr{\..*});
38        my $outdir = catdir($dir, $name);
39        print "Creating directory $outdir\n";
40        mkpath($outdir) unless (-d $outdir);
41        chdir($outdir) or die("Unable to change directory.");
42       
43        foreach my $i (1..$#blocks)
44        {
45                my $out = "${name}_$i$ext";
46                print "Creating file $out\n";
47                open(OUT, ">$out") or die("Unable to open file.");
48                print OUT $blocks[0];
49                print OUT $blocks[$i];
50                close(OUT);
51        }
52}
Note: See TracBrowser for help on using the repository browser.