001package org.biojavax.bio.phylo;
002import java.io.*;
003import java.lang.*;
004import java.util.*;
005import java.util.ArrayList;
006import java.util.List;
007import java.util.Map;
008
009import org.biojava.utils.process.ExternalProcess;
010import org.biojavax.bio.phylo.io.nexus.*;
011import org.jgrapht.*;
012import org.jgrapht.generate.*;
013import org.jgrapht.graph.*;
014
015
016public class ParsimonyTreeMethod {
017        
018        
019        public static void MP(TaxaBlock t, CharactersBlock ch){
020        
021                int NTax = t.getDimensionsNTax();
022                int NChar = ch.getDimensionsNChar();
023                List labels = t.getTaxLabels();
024                
025                String [] seq = new String[NTax];
026                //WeightedGraph<String, DefaultWeightedEdge> [] jgrapht;
027                
028                //writing number of taxa & length of sequences to the phylip input file
029                try{
030                        FileWriter fw = new FileWriter(new File("C:\\Program Files\\phylip3.67\\exe\\temp.txt"), true);
031                        fw.write(NTax + "     " + NChar + "\n");
032                        fw.close();
033                }catch(IOException e){
034                        System.out.println("Error in Writing Temp_File(1)!");
035                }
036
037
038                for(int i = 0; i < NTax; i++){
039                        seq[i] = "";
040                }
041
042                int name_len = 0; // variable for finding the longest taxa name (for alignment)
043
044                for (Iterator i = labels.iterator(); i.hasNext(); ) {
045                        
046                        String taxa = (String)i.next();
047                            List matrix = ch.getMatrixData(taxa);
048                        
049                        if(name_len < taxa.length())
050                                name_len = taxa.length();
051        
052                        for (Iterator j = matrix.iterator(); j.hasNext(); ) {                     
053                                Object elem = j.next();                         
054                                        
055                                if (elem instanceof Set) {
056                                                Set data = (Set)elem;
057                                } else if (elem instanceof List) {
058                                                List data = (List)elem;
059                                } else {
060                                                String data = elem.toString();                                    
061                                        if(data != null && data != " ")
062                                                seq[labels.indexOf(taxa)] += data;
063                                }
064                        }
065                }
066
067                //writing taxa name & sequence to the phylip input file     
068                for(Iterator i = labels.iterator(); i.hasNext(); ) {
069
070                        String taxa = (String)i.next();
071
072                        try{
073                                FileWriter fw = new FileWriter(new File("C:\\Program Files\\phylip3.67\\exe\\temp.txt"), true);
074                                fw.write(taxa);
075
076                                for(int j = 0; j < name_len - taxa.length(); j++) 
077                                        fw.write(" ");
078                                fw.write("     " + seq[labels.indexOf(taxa)] + "\n");
079                                fw.close();
080                        }catch(IOException e){
081                                System.out.println("Error in Writing Temp_File(2)!");
082                        }
083                }
084
085                try{
086                        FileWriter fw = new FileWriter(new File("C:\\Program Files\\phylip3.67\\exe\\temp.txt"), true);
087                        fw.write("\n \n");
088                        fw.close();
089                }catch(IOException e){
090                        System.out.println("Error in Writing Temp_File(1)!");
091                }
092
093
094                ExternalProcess ep = new ExternalProcess();
095                Object [] cmd = new Object[2];
096                cmd[0] = "C:\\Program Files\\phylip3.67\\exe\\dnapars";
097                cmd[1] = "Y";
098                //cmd[2] = "F";
099                StringWriter output = new StringWriter();
100                
101                //System.out.println(ep.joinCommands(cmd));
102                
103                try{
104                        try{
105                                try{
106                                        try{
107                                                try{
108                                                        ep.execute(ep.joinCommands(cmd), "C:\\Program Files\\phylip3.67\\exe\\temp.txt",  output, null);        
109                                                } catch (IOException ie){}
110                                        }catch (InterruptedException ite){}
111                                }catch(NullPointerException ne){}
112                        }catch(SecurityException se){}
113                        }catch(IllegalArgumentException iae){}
114                
115
116        }       
117}
118