package org.cipres.kepler.sandbox; import java.io.File; import org.cipres.util.CipresFile; public class PhyloFileUtils { //for creating actor to translate NexusToPhylip public static File NexusToPhylip(File inputNexusFile, String outputPhylipFilename) throws Exception { if(!inputNexusFile.canRead()){ throw new Exception("Cannot Read " + inputNexusFile.getAbsolutePath()); } String commands = " EXECUTE " + inputNexusFile.getAbsolutePath() + ";" + " EXPORT File=" + outputPhylipFilename + " FORMAT=PHYLIP TREES=YES REPLACE=YES;\n"; ServiceRunner.paupExecute(commands); return new File(outputPhylipFilename); } //for creating actor to translate PhylipToNexus public static File PhylipToNexus(File inputPhylipFile, String outputNexusFilename) throws Exception { if(!inputPhylipFile.canRead()){ throw new Exception("Cannot Read " + inputPhylipFile.getAbsolutePath()); } String commands = " TONEXUS FORMAT=PHYLIP FromFile=" + inputPhylipFile.getAbsolutePath() + " ToFile=" + outputNexusFilename + " REPLACE=YES;\n" + " EXECUTE" + outputNexusFilename + ";" + " SAVETREES File=" + outputNexusFilename + " FORMAT=NEXUS APPEND=YES;"; ServiceRunner.paupExecute(commands); return new File(outputNexusFilename); } //THIS NEXT METHOD SHOULD BE USED BY THE CLUSTAL ACTOR public static File removeSymbolsFromNexus(File inputNexusFile) throws Exception { CipresFile cfIn = new CipresFile(inputNexusFile.getAbsolutePath()); CipresFile cfOut = new CipresFile(KeplerTest.CLUSTAL_NEXUS_OUT); String content = cfIn.getContent(); //find beginnning of symbols statement int pos1 = content.toLowerCase().indexOf("symbols"); //find end of symbols statement (will be the 2nd quotation mark after "symbols") int pos2 = content.indexOf("\"", pos1); pos2 = content.indexOf("\"", pos2+1); String symbolsString = content.substring(pos1, pos2); cfOut.fillFromString(content.substring(0,pos1) + content.substring(pos1 + symbolsString.length() + 1, content.length())); return new File(cfOut.getAbsolutePath()); } }