annotate phylogenies/makeNJst.pl @ 0:5b9a38ec4a39 draft default tip

First commit of old repositories
author osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
date Tue, 11 Mar 2014 12:19:13 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
1 #!/usr/bin/perl
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
2
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
3 #This script generates an R script to call NJst
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
4 #input is a table with treename<tab>newick tree
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
5 use strict;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
6 use Bio::TreeIO;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
7
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
8 my $filename = $ARGV[0];
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
9 my $outfile = $ARGV[1];
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
10 open FILE, $filename or die $!;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
11
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
12
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
13 my @splitline;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
14
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
15 print "require(phybase);\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
16 print "genetrees<-c(";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
17 my $counter=0;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
18 my $tree;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
19 while (<FILE>) {
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
20 chomp;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
21 #get a line from the data file
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
22 my $currentinput = "$_";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
23 @splitline = split(/\t/);
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
24 my $treename= $splitline[0];
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
25 $tree = $splitline[1];
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
26 unless($counter==0){
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
27 print ", ";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
28 }
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
29 $counter++;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
30 print "'$tree'";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
31 }
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
32 print ")\n"; #close genetree vector
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
33 print "taxaname<-c(";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
34 my $spnum = tree2spList($tree);
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
35 print ")\nspname<-taxaname\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
36 print "species.structure<-matrix(0,$spnum,$spnum)\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
37 print "diag(species.structure)<-1\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
38 print "\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
39 print "result<-NJst(genetrees,taxaname,spname,species.structure)\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
40 print "write(result, file='$outfile')\n";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
41 close FILE;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
42
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
43
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
44
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
45
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
46
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
47 #This script requires phybase R package
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
48 #NJst is a function used as follows
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
49 # genetrees<-c("(A:0.004,(B:0.003,(C:0.002,(D:0.001,E:0.001)
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
50 # :0.001):0.001):0.001);","(A:0.004,(B:0.003,(E:0.002,(D:0.001,C:0.001):0.001):0.001):0.001);","(A:0.004,(B:0.003,(C:0.002,(D:0.001,E:0.001):0.001):0.001):0.001);")
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
51 # taxaname<-c("A","B","C","D","E")
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
52 # spname<-taxaname
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
53 # species.structure<-matrix(0, 5, 5)
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
54 # diag(species.structure)<-1
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
55 #
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
56 # NJst(genetrees,taxaname, spname, species.structure)
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
57
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
58
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
59
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
60 sub tree2spList {
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
61 my $treefile=shift;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
62
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
63 my ($charactername, $characterstate);
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
64 my ($call, $sp_id, $char_id);
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
65
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
66 #Open treefile and get taxon names from tree
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
67 my $stringfh;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
68 open($stringfh, "<", \$treefile);
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
69
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
70 my $input = Bio::TreeIO->new(-format => 'newick', -fh => $stringfh);
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
71 my $tree = $input->next_tree;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
72
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
73 my @taxa = $tree->get_leaf_nodes;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
74 my @names = map { $_->id } @taxa;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
75
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
76 my $count=0;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
77 foreach(@names){
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
78 my $treespecies = $_;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
79 $treespecies =~ s/^\s+|\s+$//g ; #Trim leading and trailing whitespace
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
80 unless($count==0){
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
81 print ",";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
82 }
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
83 print "'$treespecies'";
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
84 $count++
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
85 }
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
86 return $count;
5b9a38ec4a39 First commit of old repositories
osiris_phylogenetics <ucsb_phylogenetics@lifesci.ucsb.edu>
parents:
diff changeset
87 } #end of tree2spList subroutine