Skip to content

MuscleShellBuilder

dellch edited this page Jul 20, 2018 · 4 revisions
package edu.ufl.flmnh;

import edu.ufl.flmnh.WorkflowBuilder.AbstractActor;

final class MuscleShellBuilder extends ShellBuilder {

	/* Should be configurable in the future. */
	private static final String MUSCLE_BINARY = "/opt/muscle-3.7/bin/muscle";

	private static final String FASTA_INPUT_FILE_NAME = "seqs.fa";
	private static final int MAX_ITERATIONS_DEFAULT = 16;

	/* Package private constants. */
	static final String PROGRAM_NAME = "muscle";
	static final String ALIGN_OUTPUT_FILE_NAME = "muscleout";

	/*
	 * The following is based on Muscle 3.6 Manual, which can be downloaded 
	 * from http://www.drive5.com/muscle/muscle.pdf.
	 * 
	 * <program name="muscle">
	 *     <input name="fastaInput" optional="false">
	 *         <comment>Muscle only accepts FASTA format input</comment>
	 *         <accept-type name="fasta"/>
	 *     </input>
	 *     
	 *     <parameter name="maxIterations" optional="true" default="16" xsi:type="sp:IntegerParameter">
	 *         <comment>Maximum number of iterations</comment>
	 *     </parameter>
	 *     
	 *     <parameter name="findDiagonals" optional="true" default="false" xsi:type="sp:BooleanParameter">
	 *         <comment>Enable optimization by finding common words (6-mers in a compressed amino acid 
	 *         		alphabet) between the two sequences as seeds for diagonals</comment>
	 *     </parameter>
	 *     
	 *     <output name="alignOutput" type="fasta">
	 *     	   <alternative-type name="clw"/>
	 *     	   <alternative-type name="/>
	 *         <alternative-type name="phylip-sequential"/>
	 *         <alternative-type name="phylip-interleaved"/>
	 *         <alternative-type name="msf"/>
	 *     </output>
	 * </program>
	 * 
	 */
	public String doBuild(AbstractActor aa) {
		if(!aa.program.equals(PROGRAM_NAME)) return null;
		makeRoot();

		//-----Deal with inputs, note we don't need to deal with embedded values-----
		//-----because it has already been extracted.-----

		// This input is mandatory.
		String fastaInputType = aa.inputTypes.get("fastaInput");
		if(fastaInputType == null) {
			// We must get this input from another actor.
			// TODO: find and process input from other actors.

		} else if(fastaInputType.equals("fasta")) {
			// User provided an input file with correct file format. we 
			// know this file is in the directory one level up. Copy it
			// here and rename it to FASTA_INPUT_FILE_NAME.
			builder.append("cp ../");
			builder.append(aa.name);
			builder.append(".fastaInput ");
			builder.append(FASTA_INPUT_FILE_NAME);
			builder.append("; ");
		} else
			// Perhaps user provide an input file with wrong file format?
			return null;

		//-----Deal with parameters-----

		// This parameter is optional.
		int maxIterations = MAX_ITERATIONS_DEFAULT;
		boolean maxIterSet = (aa.parameterValues.
				get("maxIterations") == null);
		if(maxIterSet) {
			try {
				maxIterations = Integer.parseInt(aa.
						parameterValues.get("maxIterations"));
			} catch (NumberFormatException e) {
				maxIterations = MAX_ITERATIONS_DEFAULT;
			}	
		}

		//-----Deal with outputs-----

		String alignOutputType = aa.outputTypes.get("alignOutput");
		if(alignOutputType == null)
			alignOutputType = "fasta";

		// Now build the command.
		builder.append(MUSCLE_BINARY);
		builder.append(" -in ");
		builder.append(FASTA_INPUT_FILE_NAME);
		if(maxIterSet) {
			builder.append(" -maxiters ");
			builder.append(maxIterations);
		}
		if(alignOutputType.equals("fasta"))
			builder.append(" -out ");
		else if(alignOutputType.equals("))
			builder.append(" out ");
		else if(alignOutputType.equals("clw"))
			builder.append(" -clwout ");
		else if(alignOutputType.equals("phylip-interleaved"))
			builder.append(" -phyiout ");
		else if(alignOutputType.equals("phylip-sequential"))
			builder.append(" -physout ");
		else // Oops, we can't recognize this type.
			return null;

		builder.append(ALIGN_OUTPUT_FILE_NAME);
		builder.append(" >std.out 2>&1; ");
		builder.append("tar czf ");
		builder.append(aa.name);
		builder.append(".tar.gz ");
		builder.append(ALIGN_OUTPUT_FILE_NAME);
		builder.append(" std.out; ");

		//System.out.println(builder.toString());
		return builder.toString();
	}

}
Clone this wiki locally