/* Copyright (c) 2009 Peter Troshin * Copyright (c) 2013 Alexander Sherstnev * * Java Bioinformatics Analysis Web Services (JABAWS) * @version: 2.5 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache * License for more details. * * A copy of the license is in apache_license.txt. It is also available here: * @see: http://www.apache.org/licenses/LICENSE-2.0.txt * * Any republication or derived work distributed in source code form * must include this copyright and license notice. */ package compbio.runner.msa; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import javax.xml.bind.ValidationException; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import compbio.data.sequence.Alignment; import compbio.engine.AsyncExecutor; import compbio.engine.Configurator; import compbio.engine.FilePuller; import compbio.engine.SyncExecutor; import compbio.engine.client.ConfExecutable; import compbio.engine.client.ConfiguredExecutable; import compbio.engine.client.Executable; import compbio.engine.client.RunConfiguration; import compbio.engine.cluster.drmaa.JobRunner; import compbio.engine.local.LocalRunner; import compbio.metadata.AllTestSuit; import compbio.metadata.ChunkHolder; import compbio.metadata.JobExecutionException; import compbio.metadata.JobStatus; import compbio.metadata.JobSubmissionException; import compbio.metadata.LimitsManager; import compbio.metadata.PresetManager; import compbio.metadata.ResultNotAvailableException; import compbio.metadata.RunnerConfig; import compbio.runner.msa.Tcoffee; import compbio.runner.predictors.Jpred; import compbio.util.FileWatcher; import compbio.util.SysPrefs; public class TcoffeeTester { private Tcoffee tcoffee; public static final String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() + File.separator; public static String test_output = "tcoffee.out"; public static String test_input = CURRENT_DIRECTORY + "testsrc" + File.separator + "testdata" + File.separator + "TO1381.fasta"; @BeforeMethod(groups = {AllTestSuit.test_group_cluster, AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows}) public void init() { tcoffee = new Tcoffee(); tcoffee.setInput(test_input); tcoffee.setOutput(test_output); tcoffee.setError("tcoffee.progress"); //System.out.println("Tcoffee has been configured!"); } @Test(groups = { AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows }) public void RunLocally() { try { ConfiguredExecutable ctcoffee = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Local); // matrix does not appear to work // ctcoffee.getParameters().setParam("-matrix","BLOSUM62"); SyncExecutor sexecutor = Configurator.getSyncEngine(ctcoffee); sexecutor.executeJob(); ConfiguredExecutable al = sexecutor.waitForResult(); Alignment align = al.getResults(); assertNotNull(align); // LocalRunner sexecutor = new LocalRunner(ctcoffee); // sexecutor.executeJob(); // ConfiguredExecutable al = sexecutor.waitForResult(); // Alignment align = al.getResults(); // assertNotNull(align); } catch (JobSubmissionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (JobExecutionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (ResultNotAvailableException e) { e.printStackTrace(); fail(e.getMessage()); } } @Test(groups = {AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows}) public void readStatistics() { try { Tcoffee tcoffee = new Tcoffee().setInput(AllTestSuit.test_input); ConfiguredExecutable confTcoffee = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Local); AsyncExecutor sexec = Configurator.getAsyncEngine(confTcoffee); String jobId = sexec.submitJob(confTcoffee); String file = confTcoffee.getWorkDirectory() + File.separator + tcoffee.getError(); FilePuller fw = FilePuller.newFilePuller(file, FileWatcher.MIN_CHUNK_SIZE_BYTES); int count = 0; long position = 0; fw.waitForFile(4); while (!(sexec.getJobStatus(jobId) == JobStatus.FINISHED || sexec.getJobStatus(jobId) == JobStatus.FAILED || sexec.getJobStatus(jobId) == JobStatus.UNDEFINED) || fw.hasMoreData()) { if (fw.hasMoreData()) { ChunkHolder ch = fw.pull(position); String chunk = ch.getChunk(); position = ch.getNextPosition(); System.out.print(chunk); } count++; } assertTrue(count > 1); ConfiguredExecutable al = sexec.getResults(jobId); assertNotNull(al.getResults()); } catch (JobSubmissionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (ResultNotAvailableException e) { e.printStackTrace(); fail(e.getMessage()); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } } @Test(groups = { AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows }) public void SetNcore() { Tcoffee tc = new Tcoffee(); assertEquals(tc.getParameters(null).size(), 3); tc.setNCore(2); assertEquals(2, tc.getNCore()); assertEquals(tc.getParameters(null).size(), 3); tc.setNCore(4); assertEquals(4, tc.getNCore()); assertEquals(tc.getParameters(null).size(), 3); } @Test(groups = { AllTestSuit.test_group_cluster, AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows }) public void Persistance() { try { Tcoffee tcoffee = new Tcoffee(); tcoffee.setError("errrr.txt"); tcoffee.setInput(AllTestSuit.test_input); tcoffee.setOutput("outtt.txt"); assertEquals(tcoffee.getInput(), AllTestSuit.test_input); assertEquals(tcoffee.getError(), "errrr.txt"); assertEquals(tcoffee.getOutput(), "outtt.txt"); ConfiguredExecutable ctcofee = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Local); SyncExecutor sexec = Configurator.getSyncEngine(ctcofee); sexec.executeJob(); ConfiguredExecutable al = sexec.waitForResult(); assertNotNull(al.getResults()); // Save run configuration assertTrue(ctcofee.saveRunConfiguration()); // See if loaded configuration is the same as saved RunConfiguration loadedRun = RunConfiguration.load(new FileInputStream(new File(ctcofee.getWorkDirectory(), RunConfiguration.rconfigFile))); assertEquals(((ConfExecutable) ctcofee).getRunConfiguration(), loadedRun); // Load run configuration as ConfExecutable ConfiguredExecutable resurrectedCTcoffee = (ConfiguredExecutable) ctcofee .loadRunConfiguration(new FileInputStream(new File(ctcofee.getWorkDirectory(), RunConfiguration.rconfigFile))); assertNotNull(resurrectedCTcoffee); // See in details whether executables are the same assertEquals(resurrectedCTcoffee.getExecutable(), tcoffee); // Finally rerun the job in the new task directory ConfiguredExecutable restcoffee = Configurator.configureExecutable(resurrectedCTcoffee.getExecutable(), Executable.ExecProvider.Local); sexec = Configurator.getSyncEngine(restcoffee,Executable.ExecProvider.Local); sexec.executeJob(); al = sexec.waitForResult(); assertNotNull(al); } catch (JobSubmissionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (JobExecutionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (FileNotFoundException e) { e.printStackTrace(); fail(e.getMessage()); } catch (IOException e) { e.printStackTrace(); fail(e.getMessage()); } catch (ResultNotAvailableException e) { e.printStackTrace(); fail(e.getMessage()); } } @Test(groups = { AllTestSuit.test_group_runner }) public void ConfigurationLoading() { try { RunnerConfig tcoffeeConfig = ConfExecutable.getRunnerOptions(Tcoffee.class); assertNotNull(tcoffeeConfig); assertTrue(tcoffeeConfig.getArguments().size() > 0); PresetManager tcoffeePresets = ConfExecutable.getRunnerPresets(Tcoffee.class); assertNotNull(tcoffeePresets); assertTrue(tcoffeePresets.getPresets().size() > 0); tcoffeePresets.validate(tcoffeeConfig); LimitsManager tcoffeeLimits = ConfExecutable.getRunnerLimits(Tcoffee.class); assertNotNull(tcoffeeLimits); assertTrue(tcoffeeLimits.getLimits().size() > 0); tcoffeeLimits.validate(tcoffeePresets); } catch (FileNotFoundException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (IOException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } catch (ValidationException e) { e.printStackTrace(); fail(e.getLocalizedMessage()); } } // disabled @Test(enabled=false,groups = { AllTestSuit.test_group_cluster, AllTestSuit.test_group_runner, AllTestSuit.test_group_non_windows }) public void RunOnCluster() { try { ConfiguredExecutable cmafft = Configurator.configureExecutable(tcoffee, Executable.ExecProvider.Cluster); JobRunner sexecutor = (JobRunner) Configurator.getSyncEngine(cmafft, Executable.ExecProvider.Cluster); sexecutor.executeJob(); ConfiguredExecutable al = sexecutor.waitForResult(); Alignment align = al.getResults(); assertNotNull(align); } catch (JobSubmissionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (JobExecutionException e) { e.printStackTrace(); fail(e.getMessage()); } catch (ResultNotAvailableException e) { e.printStackTrace(); fail(e.getMessage()); } } }