package org.cipres.kepler.sandbox; import java.io.File; import java.io.IOException; import javax.swing.JOptionPane; import org.apache.log4j.Logger; import org.cipres.datatypes.PhyloDataset; import org.cipres.guigen.ServiceCommandPanel; import org.cipres.kepler.DisplayObject; import org.cipres.kepler.SubsetChooser; import org.cipres.helpers.JRun; import org.cipres.kepler.registry.*; import org.cipres.util.CipresFile; import org.cipres.CipresIDL.api1.DataMatrix; import org.cipres.CipresIDL.api1.Tree; import forester.atv.ATVjframe; public class ServiceRunner { private static Logger logger = Logger.getLogger(ServiceRunner.class.getName()); CipresKeplerRegistry reg = Globals.getInstance().getRegistry(); public void editAlignment_Seaview(File inputNexusFile) throws InterruptedException, IOException { ActorInfo seaViewActor = reg.getActor("AlignmentEditor_Seaview"); File appFile = new File(seaViewActor.getAppPath()); String[] args = { inputNexusFile.getAbsolutePath() }; JRun jrun = new JRun(); jrun.setExecutable("open -a " + appFile.getName()); jrun.setArguments(args); //jrun.setStandardOutputStream(System.out); jrun.setOutputFilename(reg.getDefaultStdOutDir() + "StdOut.log"); jrun.setErrorFilename(reg.getDefaultStdErrDir() + "StdErr.log"); jrun.setWorkingDirPath(appFile.getParent()); jrun.setWaitForExecution(true); jrun.execute(); } public void showTreeForester(File inputNexusFile) throws Exception { PhyloDataset pdTrees = new PhyloDataset(); pdTrees.initialize(inputNexusFile); for(int i = 0; i< pdTrees.getTrees().length; i++){ Tree tree = pdTrees.getTrees()[i]; forester.tree.Tree displayedTree = new forester.tree.Tree(tree.m_newick); ATVjframe atvframe = new ATVjframe(displayedTree); atvframe.setTitle(tree.m_name + " from file " + inputNexusFile.getName()); atvframe.showWhole(); } } public File runClustal() throws Exception { return runClustal(null, null); } public File runClustal(String inputFastaFilename, String outputNexusFileName) throws Exception { ActorInfo clustalActor = reg.getActor(KeplerTest.CLUSTAL_ACTOR_NAME); ServiceCommandPanel pnl = new ServiceCommandPanel(new File(clustalActor.getGuiXmlFile())); pnl.setDefaultInFileDirectory(reg.getDefaultInputFileDir()); if(inputFastaFilename!=null){ pnl.setParameterValue("infile", inputFastaFilename); } if(outputNexusFileName!=null){ pnl.setParameterValue("outfile", outputNexusFileName); } pnl.showServicePanelAsDialog(); File appFile = new File(clustalActor.getAppPath()); String args[] = { pnl.getCmdBlock() }; JRun jrun = new JRun(); jrun.setExecutable(appFile.getAbsolutePath()); jrun.setArguments(args); jrun.setStandardOutputStream(System.out); jrun.setOutputFilename(reg.getDefaultStdOutDir() + "StdOut.log"); jrun.setErrorFilename(reg.getDefaultStdErrDir() + "StdErr.log"); jrun.setWorkingDirPath(appFile.getParent()); jrun.setWaitForExecution(true); System.out.println(jrun.execute()); logger.info("Clustal Finished with exit code " + jrun.getExitCode()); File retFile = new File(pnl .getParameterValue("outfile")); return PhyloFileUtils.removeSymbolsFromNexus(retFile); } public File runPaupConsensusTrees(File inputNexusFile, File outputNexusFile) throws Exception { ActorInfo paupActor = reg.getActor(KeplerTest.CONSENSUS_TREE_PAUP); //get paup commands using guigen ServiceCommandPanel pnl = new ServiceCommandPanel( new File(paupActor.getGuiXmlFile())); pnl.setParameterValue("infile",inputNexusFile.getAbsolutePath()); pnl.setParameterValue("outfile",outputNexusFile.getAbsolutePath()); pnl.showServicePanelAsDialog(); //this command has paup: //1. import input and write to output //2. execute contree and append trees to output paupExecute("EXECUTE " + inputNexusFile.getAbsolutePath() + ";" + " EXPORT File=" + pnl.getParameterValue("outfile") + " FORMAT = NEXUS TREES=NO REPLACE=YES;" + " CLEARTREES NOWARN=YES;" + " SET WARNRESET=NO; " + pnl.getCmdBlock() + " TreeFile =" + pnl.getParameterValue("outfile") + " APPEND=YES;"); //PhyloDataset pdTrees = new PhyloDataset(); //pdTrees.initialize(new cipresFile(pnl.getParameterValue("outfile"))); //pdTrees.writeToNexus(outputNexusFile); return new File(pnl.getParameterValue("outfile")); } public File runPaupInfer(File inputNexusFile, File outputNexusFile, String PaupCommands) throws Exception { ActorInfo paupActor = reg.getActor(KeplerTest.INFER_TREE_PARS_PAUP); String outFilePath; if(PaupCommands == null) { //get paup commands using guigen ServiceCommandPanel pnl = new ServiceCommandPanel( new File(paupActor.getGuiXmlFile())); pnl.setParameterValue("infile",inputNexusFile.getAbsolutePath()); pnl.setParameterValue("outfile",outputNexusFile.getAbsolutePath()); pnl.showServicePanelAsDialog(); PaupCommands = pnl.getCmdBlock(); outFilePath = pnl.getParameterValue("outfile"); } else { outFilePath = outputNexusFile.getAbsolutePath(); } //if path to output file is > 64 chars paup will throw an error //so, we have give paup only the filename, which causes it to write to //its own dir, then rename the file to the location specified by the //outFilePath File outfile = new File(outFilePath); File paupApp = new File(paupActor.getAppPath()); File paupOut = new File(paupApp.getParent() + Globals.getInstance().getDirSep() + outfile.getName()); String commands = PaupCommands + "\n EXPORT File=" + outfile.getName() + " FORMAT=NEXUS REPLACE=YES;\n" + " SAVETREES File=" + outfile.getName() + " FORMAT=NEXUS BRLENS=YES MAXDECIMALS=2" + " APPEND=YES;"; paupExecute(commands); //now move outfile to its proper location //outfile is currently located in same dir as paup if(!outfile.getAbsolutePath().equalsIgnoreCase(paupOut.getAbsolutePath())){ paupOut.renameTo(outfile); } return new File(outFilePath); } public static void paupExecute(String commands) throws Exception{ CipresFile cmdFile = new CipresFile(KeplerTest.PAUP_CMD_FILE); cmdFile.fillFromString("#NEXUS\nBEGIN PAUP;\n" + commands + " QUIT;\nEND;"); logger.info("Running paupExecute on commands" + commands); CipresKeplerRegistry registry = Globals.getInstance().getRegistry(); ActorInfo paupActor = registry.getActor(KeplerTest.CONSENSUS_TREE_PAUP); //run paup File appFile = new File(paupActor.getAppPath()); String args[] = { cmdFile.getAbsolutePath() }; JRun jrun = new JRun(); jrun.setExecutable(appFile.getAbsolutePath()); jrun.setArguments(args); jrun.setStandardOutputStream(System.out); jrun.setOutputFilename(registry.getDefaultStdOutDir() + "StdOut.log"); jrun.setErrorFilename(registry.getDefaultStdErrDir() + "StdErr.log"); jrun.setWorkingDirPath(appFile.getParent()); jrun.setWaitForExecution(true); System.out.println(jrun.execute()); System.out.println("Paup Finished with exit code " + jrun.getExitCode()); } //Works, but not real uselss until bug in ReadNexusServer is fixed public File subsetTrees(File inputNexusFile, File outputNexusFile) throws Exception { PhyloDataset pdIn = new PhyloDataset(); pdIn.initialize(inputNexusFile); if(pdIn.getTrees()==null){ JOptionPane.showMessageDialog(null, "There are no trees to subset"); return inputNexusFile; } DisplayObject[] displayObjects = new DisplayObject[pdIn.getTrees().length]; Tree tree = null; String treeName; int j; for (int i = 0; i < pdIn.getTrees().length; i++) { tree = pdIn.getTrees()[i]; if(tree.m_name==null || tree.m_name.trim().length()==0){ j = i + 1; treeName = "Tree" + j; } else { treeName = tree.m_name; } displayObjects[i] = new DisplayObject(new Integer(i), treeName); } SubsetChooser sc = new SubsetChooser(displayObjects); DisplayObject[] displayObjectsSubset = sc.showSubsetChooserAsDialog(); // create new PhyloDataset comprised of the subset and write it to nexus PhyloDataset pdOut; Integer jInt; Tree[] treesOut = new Tree[displayObjectsSubset.length]; for (int i = 0; i < displayObjectsSubset.length; i++) { jInt = (Integer) displayObjectsSubset[i].getObject(); j = jInt.intValue(); treesOut[i] = pdIn.getTrees()[j]; } pdOut = new PhyloDataset(null, treesOut, pdIn.getTaxaInfo()); pdOut.writeToNexus(outputNexusFile); return outputNexusFile; } public File subsetNexusByTaxa(File inputNexusFile, File outputNexusFile) throws Exception { PhyloDataset pdIn = new PhyloDataset(); pdIn.initialize(inputNexusFile); if(pdIn.getTaxaInfo()==null){ JOptionPane.showMessageDialog(null, "There are no taxa to subset"); return inputNexusFile; } DisplayObject[] displayObjects = new DisplayObject[pdIn.getTaxaInfo().length]; for (int i = 0; i < pdIn.getTaxaInfo().length; i++) { displayObjects[i] = new DisplayObject(new Integer(i), pdIn .getTaxaInfo()[i]); } SubsetChooser sc = new SubsetChooser(displayObjects); DisplayObject[] displayObjectsSubset = sc.showSubsetChooserAsDialog(); // create new PhyloDataset comprised of the subset and write it to nexus PhyloDataset pdOut; DataMatrix dmIn = pdIn.getDataMatrix(); DataMatrix dmOut = new DataMatrix(); String[] taxa = new String[displayObjectsSubset.length]; short[][] matrix = new short[displayObjectsSubset.length][pdIn .getDataMatrix().m_numCharacters]; Integer jInt; int j = 0; for (int i = 0; i < displayObjectsSubset.length; i++) { jInt = (Integer) displayObjectsSubset[i].getObject(); j = jInt.intValue(); taxa[i] = pdIn.getTaxaInfo()[j]; for (int k = 0; k < dmIn.m_matrix[0].length; k++) { matrix[i][k] = dmIn.m_matrix[j][k]; } } dmOut.m_matrix = matrix; dmOut.m_symbols = dmIn.m_symbols; dmOut.m_charStateLookup = dmIn.m_charStateLookup; dmOut.m_datatype = dmIn.m_datatype; dmOut.m_numCharacters = dmIn.m_numCharacters; dmOut.m_numStates = dmIn.m_numStates; dmOut.m_charStateLookup = dmIn.m_charStateLookup; pdOut = new PhyloDataset(dmOut, pdIn.getTrees(), taxa); pdOut.writeToNexus(outputNexusFile); return outputNexusFile; } }