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;
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;
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);
}
/**
}
/**
+ * For testing, do not use.
+ *
+ * @param br
+ */
+ HMMFile(BufferedReader br)
+ {
+ dataIn = br;
+ }
+
+ /**
* Returns the HMM produced by reading in a HMMER3 file.
*
* @return
@Override
public void parse() throws IOException
{
+ hmm = new HiddenMarkovModel();
parseFileProperties(dataIn);
parseModel(dataIn);
}
public void parse(BufferedReader br) throws IOException
{
+ hmm = new HiddenMarkovModel();
parseFileProperties(br);
parseModel(br);
}
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.
}
/**
- * 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<Character> charSymbols = hmm.getSymbols();
List<String> 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();
}
- writer.println(matchLine);
+ output.append(NL + matchLine);
String insertLine = EMPTY;
List<String> strInserts;
strInserts = doubleListToStringList(doubleInserts);
insertLine += addData(17, 9, strInserts);
- writer.println(insertLine);
+ output.append(NL + insertLine);
String transitionLine = EMPTY;
List<String> strTransitions;
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;
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();
}
}
- // 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();
}
/**
i++;
}
}
+
}