All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.bio.seq.alignment.muscle.Muscle Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.bio.seq.alignment.muscle;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import com.hfg.bio.seq.BioSequence;
import com.hfg.bio.seq.format.FASTA;
import com.hfg.util.Executor;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;

//==============================================================================
/**
 Wrapper for a Muscle multiple sequence alignment.
 
Command-line executable is downloadable from
https://www.drive5.com/muscle
@author J. Alex Taylor, hairyfatguy.com */ //============================================================================== // com.hfg XML/HTML Coding Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // 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 GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //============================================================================== public class Muscle { private MuscleSettings mSettings; private static MuscleSettings sDefaultSettings; //########################################################################### // CONSTRUCTORS //########################################################################### //--------------------------------------------------------------------------- public Muscle() { this(null); } //--------------------------------------------------------------------------- public Muscle(MuscleSettings inSettings) { mSettings = inSettings != null ? inSettings : getDefaultSettings(); } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- public MuscleSettings getSettings() { return mSettings; } //--------------------------------------------------------------------------- public MuscleOutput run(List inQueries) throws IOException { preflight(); String cmd = generateCmd(); Executor executor = new Executor(); executor.setSTDIN(generateSTDIN(inQueries)); executor.setCommand(cmd); int exitStatus = executor.exec(); MuscleOutput output = new MuscleOutput() .setExitStatus(exitStatus) .setStdErr(executor.getSTDERR()) .setStdOut(executor.getSTDOUT()) .setExecutedCmd(cmd) .setMuscleVersion(getVersion()); return output; } //########################################################################### // PRIVATE METHODS //########################################################################### //--------------------------------------------------------------------------- public static void setDefaultSettings(MuscleSettings inValue) { sDefaultSettings = inValue; } //--------------------------------------------------------------------------- public static MuscleSettings getDefaultSettings() { return (sDefaultSettings != null ? sDefaultSettings : new MuscleSettings()); } //--------------------------------------------------------------------------- private File getExecutable() { return (getSettings().getExecutable() != null ? new File(getSettings().getExecutableDir(), getSettings().getExecutable()) : null); } //--------------------------------------------------------------------------- private void preflight() throws IOException { File exe = getExecutable(); if (null == exe) { throw new IOException("No Muscle executable was specified!"); } else if (! exe.exists()) { File exeDir = exe.getParentFile(); if (exeDir != null && !exeDir.exists()) { throw new IOException("The Muscle executable directory " + StringUtil.singleQuote(exeDir.getPath()) + " doesn't exist!"); } throw new IOException("The Muscle executable " + StringUtil.singleQuote(exe.getPath()) + " doesn't exist!"); } } //--------------------------------------------------------------------------- private String generateCmd() { StringBuilderPlus cmd = new StringBuilderPlus(getExecutable().getPath()).setDelimiter(" "); if (getSettings().getOutputFormat() != null) { cmd.delimitedAppend("-" + getSettings().getOutputFormat().name()); } if (getSettings().getCommandLineParams() != null) { cmd.delimitedAppend(getSettings().getCommandLineParams()); } return cmd.toString(); } //--------------------------------------------------------------------------- private InputStream generateSTDIN(Collection inQuery) { FASTA fasta = new FASTA(); // TODO return new ByteArrayInputStream(fasta.write(inQuery).getBytes()); } //--------------------------------------------------------------------------- private String getVersion() { String version = null; Executor executor = new Executor(); executor.setCommand(getExecutable() + " -version"); int exitStatus = executor.exec(); if (0 == exitStatus) { // MUSCLE v3.8.31 by Robert C. Edgar version = executor.getSTDOUT().split("\\s+")[1]; } return version; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy