From: TZVanaalten Date: Fri, 18 Aug 2017 13:47:49 +0000 (+0100) Subject: JAL-2668 HMMFile export method moved to print method X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=97d3ad401eb2bc5dba791f31616b87d88ea47e41;p=jalview.git JAL-2668 HMMFile export method moved to print method --- diff --git a/src/jalview/hmmer/HMMAlignThread.java b/src/jalview/hmmer/HMMAlignThread.java index f714153..1c2450c 100644 --- a/src/jalview/hmmer/HMMAlignThread.java +++ b/src/jalview/hmmer/HMMAlignThread.java @@ -410,6 +410,25 @@ public class HMMAlignThread implements Runnable } } + /** + * Runs hmmalign, and waits for the results to be imported before continuing + */ + public void hmmalignWaitTillComplete() + { + Thread loader = new Thread(this); + loader.start(); + + while (loader.isAlive()) + { + try + { + Thread.sleep(500); + } catch (Exception ex) + { + } + } + + } } diff --git a/src/jalview/hmmer/HMMBuildThread.java b/src/jalview/hmmer/HMMBuildThread.java index e910b1f..8bdb008 100644 --- a/src/jalview/hmmer/HMMBuildThread.java +++ b/src/jalview/hmmer/HMMBuildThread.java @@ -217,7 +217,7 @@ public class HMMBuildThread implements Runnable { name = af.getTitle(); } - if (name == null) + if (name == null || name == "" || name == " " || name == " ") { name = "Alignment"; } diff --git a/src/jalview/io/HMMFile.java b/src/jalview/io/HMMFile.java index b2a6f8b..e114f52 100644 --- a/src/jalview/io/HMMFile.java +++ b/src/jalview/io/HMMFile.java @@ -5,11 +5,7 @@ import jalview.datamodel.HiddenMarkovModel; import jalview.datamodel.SequenceI; import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -26,12 +22,12 @@ public class HMMFile extends AlignFile implements AlignmentFileReaderI, AlignmentFileWriterI { // HMM to store file data - private HiddenMarkovModel hmm = new HiddenMarkovModel(); + private HiddenMarkovModel hmm; // number of possible transitions - private final int NUMBER_OF_TRANSITIONS = 7; + private static final int NUMBER_OF_TRANSITIONS = 7; - private final String NEW_LINE = "\n"; + private static final String NL = "\n"; //number of symbols in the alphabet used in the hidden Markov model int numberOfSymbols; @@ -43,18 +39,29 @@ public class HMMFile extends AlignFile private final String EMPTY = ""; //This is a line that needs to be added to each HMMER� file. It is purely for readability. - private static final String TRANSITIONTYPELINE = "m->m m->i m->d i->m i->i d->m d->d"; + private static final String TRANSITIONTYPELINE = " m->m m->i m->d i->m i->i d->m d->d"; /** - * Constructor for HMMFile, parses immediately + * Parses immediately. + * + * @param inFile + * @param type + * @throws IOException + */ + public HMMFile(String inFile, DataSourceType type) throws IOException + { + super(inFile, type); + } + + /** + * Parses immediately. * * @param source * @throws IOException */ public HMMFile(FileParse source) throws IOException { - super(false, source); - parse(); + super(source); } /** @@ -77,6 +84,16 @@ public class HMMFile extends AlignFile } /** + * For testing, do not use. + * + * @param br + */ + HMMFile(BufferedReader br) + { + dataIn = br; + } + + /** * Returns the HMM produced by reading in a HMMER3 file. * * @return @@ -114,6 +131,7 @@ public class HMMFile extends AlignFile @Override public void parse() throws IOException { + hmm = new HiddenMarkovModel(); parseFileProperties(dataIn); parseModel(dataIn); } @@ -126,6 +144,7 @@ public class HMMFile extends AlignFile public void parse(BufferedReader br) throws IOException { + hmm = new HiddenMarkovModel(); parseFileProperties(br); parseModel(br); } @@ -329,49 +348,6 @@ public class HMMFile extends AlignFile return list; } - - /** - * Writes a HMM to a file/ - * - * @param exportLocation - * Filename, URL or Pasted String to write to. - * @throws FileNotFoundException - * @throws UnsupportedEncodingException - * - **/ - - public void exportFile(String exportLocation) throws IOException - { - PrintWriter writer = new PrintWriter(exportLocation); - appendFileProperties(writer); - appendModel(writer); - writer.println("//"); - - writer.close(); - - } - - /** - * Writes a HMM to a file/ - * - * @param exportLocation - * Filename, URL or Pasted String to write to. - * @throws FileNotFoundException - * @throws UnsupportedEncodingException - * - **/ - - public void exportFile(File exportLocation) throws IOException - { - PrintWriter writer = new PrintWriter(exportLocation); - appendFileProperties(writer); - appendModel(writer); - writer.println("//"); - - writer.close(); - - } - /** * Returns a string to be added to the StringBuilder containing the entire * output String. @@ -473,21 +449,18 @@ public class HMMFile extends AlignFile } /** - * Appends the hidden Markov model data to the StringBuilder containing the - * output - * - * @param file - * The StringBuilder containing the output. + * Returns a string containing the model data. */ - void appendModel(PrintWriter writer) + String getModelAsString() { + StringBuffer output = new StringBuffer(); String symbolLine = "HMM"; List charSymbols = hmm.getSymbols(); List strSymbols; strSymbols = charListToStringList(charSymbols); symbolLine += addData(11, 9, strSymbols); - writer.println(symbolLine); - writer.println(TRANSITIONTYPELINE); + output.append(symbolLine); + output.append(NL + TRANSITIONTYPELINE); int length = hmm.getLength(); @@ -524,7 +497,7 @@ public class HMMFile extends AlignFile } - writer.println(matchLine); + output.append(NL + matchLine); String insertLine = EMPTY; List strInserts; @@ -534,7 +507,7 @@ public class HMMFile extends AlignFile strInserts = doubleListToStringList(doubleInserts); insertLine += addData(17, 9, strInserts); - writer.println(insertLine); + output.append(NL + insertLine); String transitionLine = EMPTY; List strTransitions; @@ -544,47 +517,45 @@ public class HMMFile extends AlignFile strTransitions = doubleListToStringList(doubleTransitions); transitionLine += addData(17, 9, strTransitions); - writer.println(transitionLine); + output.append(NL + transitionLine); } + return output.toString(); } /** - * Appends the hidden Markov model file properties to the StringBuilder - * containing the output - * - * @param file - * The StringBuilder containing the output. + * Returns a String containing the HMM file properties */ - void appendFileProperties(PrintWriter writer) + String getFilePropertiesAsString() { + StringBuffer output = new StringBuffer(); String line; - writer.println(hmm.getFileHeader()); + output.append(hmm.getFileHeader()); line = String.format("%-5s %1s", "NAME", hmm.getName()); - writer.println((line)); + output.append(NL + line); if (hmm.getAccessionNumber() != null) { line = String.format("%-5s %1s", "ACC", hmm.getAccessionNumber()); - writer.println((line)); + output.append(NL + line); } if (hmm.getDescription() != null) { line = String.format("%-5s %1s", "DESC", hmm.getDescription()); - writer.println((line)); + output.append(NL + line); } line = String.format("%-5s %1s", "LENG", hmm.getLength()); - writer.println((line)); + output.append(NL + line); if (hmm.getMaxInstanceLength() != null) { line = String.format("%-5s %1s", "MAXL", hmm.getMaxInstanceLength()); - writer.println((line)); + output.append(NL + line); } line = String.format("%-5s %1s", "ALPH", hmm.getAlphabetType()); - writer.println((line)); + output.append(NL + line); boolean status; String statusStr; @@ -593,83 +564,84 @@ public class HMMFile extends AlignFile statusStr = HiddenMarkovModel.findStringFromBoolean(status); line = String.format("%-5s %1s", "RF", statusStr); - writer.println((line)); + output.append(NL + line); status = hmm.maskValueIsActive(); statusStr = HiddenMarkovModel.findStringFromBoolean(status); line = String.format("%-5s %1s", "MM", statusStr); - writer.println((line)); + output.append(NL + line); status = hmm.consensusResidueIsActive(); statusStr = HiddenMarkovModel.findStringFromBoolean(status); line = String.format("%-5s %1s", "CONS", statusStr); - writer.println((line)); + output.append(NL + line); status = hmm.consensusStructureIsActive(); statusStr = HiddenMarkovModel.findStringFromBoolean(status); line = String.format("%-5s %1s", "CS", statusStr); - writer.println((line)); + output.append(NL + line); status = hmm.mapIsActive(); statusStr = HiddenMarkovModel.findStringFromBoolean(status); line = String.format("%-5s %1s", "MAP", statusStr); - writer.println((line)); + output.append(NL + line); if (hmm.getDate() != null) { line = String.format("%-5s %1s", "DATE", hmm.getDate()); - writer.println((line)); + output.append(NL + line); } if (hmm.getNumberOfSequences() != null) { line = String.format("%-5s %1s", "NSEQ", hmm.getNumberOfSequences()); - writer.println((line)); + output.append(NL + line); } if (hmm.getEffectiveNumberOfSequences() != null) { line = String.format("%-5s %1s", "EFFN", hmm.getEffectiveNumberOfSequences()); - writer.println((line)); + output.append(NL + line); } if (hmm.getCheckSum() != null) { line = String.format("%-5s %1s", "CKSUM", hmm.getCheckSum()); - writer.println((line)); + output.append(NL + line); } if (hmm.getGatheringThreshold() != null) { line = String.format("%-5s %1s", "GA", hmm.getGatheringThreshold()); - writer.println((line)); + output.append(NL + line); } if (hmm.getTrustedCutoff() != null) { line = String.format("%-5s %1s", "TC", hmm.getTrustedCutoff()); - writer.println((line)); + output.append(NL + line); } if (hmm.getNoiseCutoff() != null) { line = String.format("%-5s %1s", "NC", hmm.getNoiseCutoff()); - writer.println((line)); + output.append(NL + line); } if (hmm.getMSV() != null) { line = String.format("%-19s %18s", "STATS LOCAL MSV", hmm.getMSV()); - writer.println((line)); + output.append(NL + line); line = String.format("%-19s %18s", "STATS LOCAL VITERBI", hmm.getViterbi()); - writer.println((line)); + output.append(NL + line); line = String.format("%-19s %18s", "STATS LOCAL FORWARD", hmm.getForward()); - writer.println((line)); + output.append(NL + line); } + return output.toString(); } @@ -687,12 +659,25 @@ public class HMMFile extends AlignFile } - // TODO do this @Override public String print(SequenceI[] seqs, boolean jvsuffix) { + return print(); + } - return null; + /** + * Prints the .hmm file to a String. + * + * @return + */ + public String print() + { + StringBuffer output = new StringBuffer(); + output.append(getFilePropertiesAsString()); + output.append(NL); + output.append(getModelAsString()); + output.append(NL + "//"); + return output.toString(); } /** @@ -747,5 +732,6 @@ public class HMMFile extends AlignFile i++; } } + }