modified: Makefile
[GalaxyCodeBases.git] / perl / etc / WoodyMiaoLin / PbeBefore2015 / convert_fasta_to_nexus.pl
blob03adea906a1a8bab7a840bb1f14d421711df6d26
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
5 my $i = shift;
6 my $o = shift;
8 open my $i1, "<", "$i";
9 open O, ">", "$o";
11 my $nchar;
12 my %dna = %{&readfasta($i1)};
13 close $i1;
14 my $ntax = keys %dna;
16 print O "#NEXUS
17 BEGIN DATA;
18 Dimensions ntax=$ntax nchar=$nchar;
19 Format datatype=DNA gap=- missing=?;
20 Matrix\n";
22 foreach (sort keys %dna) {
23 print O "$_\t$dna{$_}\n";
25 print O ";\nEND;\n";
26 close O;
28 sub readfasta {
29 my $in = $_[0];
30 my %fa;
31 while (<$in>) {
32 $/ = ">";
33 my $seq = <$in>;
34 chomp $seq;
35 $seq =~ s/\s//g;
36 $/ = "\n";
37 $nchar = length $seq unless $nchar;
38 s/>//;
39 /^(\S+)\s/;
40 $fa{$1} = $seq;
42 return \%fa;