package jalview.io;
+import jalview.api.AlignExportSettingI;
+import jalview.api.AlignmentViewPanel;
import jalview.datamodel.HMMNode;
import jalview.datamodel.HiddenMarkovModel;
import jalview.datamodel.SequenceI;
import java.io.BufferedReader;
-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;
/**
- * Adds capability to read in and write out HMMER3 files. Currently only supports HMMER3/f.
+ * Adds capability to read in and write out HMMER3 files. .
*
*
* @author TZVanaalten
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 final String NEW_LINE = "\n";
+ private static final int NUMBER_OF_TRANSITIONS = 7;
- // file header
- String fileHeader;
+ private 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";
+ //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";
+
+ /**
+ * Parses immediately.
+ *
+ * @param inFile
+ * @param type
+ * @throws IOException
+ */
+ public HMMFile(String inFile, DataSourceType type) throws IOException
+ {
+ super(inFile, type);
+ }
/**
- * Constructor for HMMFile
+ * Parses immediately.
+ *
* @param source
* @throws IOException
*/
public HMMFile(FileParse source) throws IOException
{
- super(false, source);
+ super(source);
}
/**
}
/**
+ * Constructor for HMMFile used for exporting.
+ *
+ * @param hmm
+ * @param exportImmediately
+ */
+ public HMMFile(HiddenMarkovModel markov)
+ {
+ hmm = markov;
+ }
+
+ /**
+ * 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
{
- parseFileProperties(dataIn);
- parseModel(dataIn);
+ try
+ {
+ hmm = new HiddenMarkovModel();
+ parseFileProperties(dataIn);
+ parseModel(dataIn);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Reads the data from HMM file into the HMM field on this object.
+ *
+ * @throws IOException
+ */
+
+ public void parse(BufferedReader br) throws IOException
+ {
+ hmm = new HiddenMarkovModel();
+ parseFileProperties(br);
+ parseModel(br);
}
void parseFileProperties(BufferedReader input) throws IOException
{
boolean readingFile = true;
- fileHeader = input.readLine();
+ hmm.setFileHeader(input.readLine());
String line = input.readLine();
while (readingFile)
{
// properties)
{
readingFile = false;
- hmm.fillSymbols(parser);
+ fillSymbols(parser);
numberOfSymbols = hmm.getNumberOfSymbols();
}
else if ("STATS".equals(next))
*/
void parseModel(BufferedReader input) throws IOException
{
- for (int i = 0; i < hmm.getLength() + 1; i++)
+ String line = input.readLine();
+ int node = 0;
+ while (!"//".equals(line))
{
hmm.getNodes().add(new HMMNode());
String next;
- String line;
- line = input.readLine();
Scanner matchReader = new Scanner(line);
next = matchReader.next();
- if (next.equals(COMPO) || i > 0)
+ if (next.equals(COMPO) || node > 0)
{
// stores match emission line in list
List<Double> matches = new ArrayList<>();
matches = fillList(matchReader, numberOfSymbols);
- hmm.getNodes().get(i).setMatchEmissions(matches);
- if (i > 0)
+ hmm.getNodes().get(node).setMatchEmissions(matches);
+ if (node > 0)
{
- parseAnnotations(matchReader, i);
+ parseAnnotations(matchReader, node);
}
}
matchReader.close();
Scanner insertReader = new Scanner(line);
List<Double> inserts = new ArrayList<>();
inserts = fillList(insertReader, numberOfSymbols);
- hmm.getNodes().get(i).setInsertEmissions(inserts);
+ hmm.getNodes().get(node).setInsertEmissions(inserts);
insertReader.close();
// stores state transition line in list
Scanner transitionReader = new Scanner(line);
List<Double> transitions = new ArrayList<>();
transitions = fillList(transitionReader, NUMBER_OF_TRANSITIONS);
- hmm.getNodes().get(i).setStateTransitions(transitions);
+ hmm.getNodes().get(node).setStateTransitions(transitions);
transitionReader.close();
+ line = input.readLine();
+ node++;
}
}
*/
void parseAnnotations(Scanner scanner, int index)
{
- if (hmm.mapIsActive())
+ if (hmm.mapIsActive() && scanner.hasNext())
{
int column;
column = scanner.nextInt();
- hmm.getNodes().get(index).setAlignmentColumn(column);
- hmm.getNodeLookup().put(column, index);
+ hmm.getNodes().get(index).setAlignmentColumn(column - 1);
+ hmm.getNodeLookup().put(column - 1, index);
}
else
{
scanner.next();
}
- char consensusR;
- consensusR = charValue(scanner.next());
- hmm.getNodes().get(index).setConsensusResidue(consensusR);
+ if (scanner.hasNext())
+ {
+ char consensusR;
+ consensusR = charValue(scanner.next());
+ hmm.getNodes().get(index).setConsensusResidue(consensusR);
+ }
+ if (scanner.hasNext())
+ {
char reference;
reference = charValue(scanner.next());
hmm.getNodes().get(index).setReferenceAnnotation(reference);
+ }
-
+ if (scanner.hasNext())
+ {
char value;
value = charValue(scanner.next());
hmm.getNodes().get(index).setMaskValue(value);
-
- char consensusS;
- consensusS = charValue(scanner.next());
- hmm.getNodes().get(index).setConsensusStructure(consensusS);
+ }
+ if (scanner.hasNext())
+ {
+ char consensusS;
+ consensusS = charValue(scanner.next());
+ hmm.getNodes().get(index).setConsensusStructure(consensusS);
+ }
}
+
/**
* Fills a list of doubles based on an input line.
*
* @param numberOfElements
* The number of elements in the list to be filled.
* @return filled list Returns the list of doubles.
+ * @throws IOException
*/
static List<Double> fillList(Scanner input,
- int numberOfElements)
+ int numberOfElements) throws IOException
{
List<Double> list = new ArrayList<>();
for (int i = 0; i < numberOfElements; i++)
list.add(prob);
}
}
+ if (list.size() < numberOfElements)
+ {
+ throw new IOException("Incomplete data");
+ }
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
- {
- StringBuilder file = new StringBuilder();
- appendFileProperties(file);
- appendModel(file);
- file.append("//");
-
- PrintWriter output = new PrintWriter(exportLocation);
- output.append(file);
- output.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(StringBuilder file)
+ String getModelAsString()
{
+ StringBuilder output = new StringBuilder();
String symbolLine = "HMM";
List<Character> charSymbols = hmm.getSymbols();
List<String> strSymbols;
strSymbols = charListToStringList(charSymbols);
symbolLine += addData(11, 9, strSymbols);
- file.append(symbolLine + NEW_LINE);
- file.append(TRANSITIONTYPELINE + NEW_LINE);
+ output.append(symbolLine);
+ output.append(NL).append(TRANSITIONTYPELINE);
int length = hmm.getLength();
List<String> strMatches;
List<Double> doubleMatches;
- doubleMatches = hmm.getNode(node).getMatchEmissions();
- convertListToLogSpace(doubleMatches);
+ doubleMatches = convertListToLogSpace(
+ hmm.getNode(node).getMatchEmissions());
strMatches = doubleListToStringList(doubleMatches);
matchLine += addData(10, 9, strMatches);
if (node != 0)
{
- matchLine += SPACE + hmm.getNodeAlignmentColumn(node);
+ matchLine += SPACE + (hmm.getNodeAlignmentColumn(node) + 1);
matchLine += SPACE + hmm.getConsensusResidue(node);
matchLine += SPACE + hmm.getReferenceAnnotation(node);
- matchLine += SPACE + hmm.getMaskedValue(node);
- matchLine += SPACE + hmm.getConsensusStructure(node);
+ if (hmm.getFileHeader().contains("HMMER3/f"))
+ {
+ matchLine += SPACE + hmm.getMaskedValue(node);
+ matchLine += SPACE + hmm.getConsensusStructure(node);
+ }
}
- file.append(matchLine + NEW_LINE);
+ output.append(NL).append(matchLine);
String insertLine = EMPTY;
List<String> strInserts;
List<Double> doubleInserts;
- doubleInserts = hmm.getNode(node).getInsertEmissions();
- convertListToLogSpace(doubleInserts);
+ doubleInserts = convertListToLogSpace(
+ hmm.getNode(node).getInsertEmissions());
strInserts = doubleListToStringList(doubleInserts);
insertLine += addData(17, 9, strInserts);
- file.append(insertLine + NEW_LINE);
+ output.append(NL).append(insertLine);
String transitionLine = EMPTY;
List<String> strTransitions;
List<Double> doubleTransitions;
- doubleTransitions = hmm.getNode(node).getStateTransitions();
- convertListToLogSpace(doubleTransitions);
+ doubleTransitions = convertListToLogSpace(
+ hmm.getNode(node).getStateTransitions());
strTransitions = doubleListToStringList(doubleTransitions);
transitionLine += addData(17, 9, strTransitions);
- file.append(transitionLine + NEW_LINE);
+ output.append(NL).append(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(StringBuilder file)
+ String getFilePropertiesAsString()
{
+ StringBuffer output = new StringBuffer();
String line;
- file.append(fileHeader + NEW_LINE);
+ output.append(hmm.getFileHeader());
line = String.format("%-5s %1s", "NAME", hmm.getName());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
if (hmm.getAccessionNumber() != null)
{
line = String.format("%-5s %1s", "ACC", hmm.getAccessionNumber());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getDescription() != null)
{
line = String.format("%-5s %1s", "DESC", hmm.getDescription());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
line = String.format("%-5s %1s", "LENG", hmm.getLength());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
if (hmm.getMaxInstanceLength() != null)
{
line = String.format("%-5s %1s", "MAXL", hmm.getMaxInstanceLength());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
line = String.format("%-5s %1s", "ALPH", hmm.getAlphabetType());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
boolean status;
String statusStr;
statusStr = HiddenMarkovModel.findStringFromBoolean(status);
line = String.format("%-5s %1s", "RF",
statusStr);
- file.append((line + NEW_LINE));
+ output.append(NL + line);
status = hmm.maskValueIsActive();
statusStr = HiddenMarkovModel.findStringFromBoolean(status);
line = String.format("%-5s %1s", "MM",
statusStr);
- file.append((line + NEW_LINE));
+ output.append(NL + line);
status = hmm.consensusResidueIsActive();
statusStr = HiddenMarkovModel.findStringFromBoolean(status);
line = String.format("%-5s %1s", "CONS",
statusStr);
- file.append((line + NEW_LINE));
+ output.append(NL + line);
status = hmm.consensusStructureIsActive();
statusStr = HiddenMarkovModel.findStringFromBoolean(status);
line = String.format("%-5s %1s", "CS",
statusStr);
- file.append((line + NEW_LINE));
+ output.append(NL + line);
status = hmm.mapIsActive();
statusStr = HiddenMarkovModel.findStringFromBoolean(status);
line = String.format("%-5s %1s", "MAP",
statusStr);
- file.append((line + NEW_LINE));
+ output.append(NL + line);
if (hmm.getDate() != null)
{
line = String.format("%-5s %1s", "DATE", hmm.getDate());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getNumberOfSequences() != null)
{
line = String.format("%-5s %1s", "NSEQ", hmm.getNumberOfSequences());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getEffectiveNumberOfSequences() != null)
{
line = String.format("%-5s %1s", "EFFN",
hmm.getEffectiveNumberOfSequences());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getCheckSum() != null)
{
line = String.format("%-5s %1s", "CKSUM", hmm.getCheckSum());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getGatheringThreshold() != null)
{
line = String.format("%-5s %1s", "GA", hmm.getGatheringThreshold());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getTrustedCutoff() != null)
{
line = String.format("%-5s %1s", "TC", hmm.getTrustedCutoff());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getNoiseCutoff() != null)
{
line = String.format("%-5s %1s", "NC", hmm.getNoiseCutoff());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
if (hmm.getMSV() != null)
{
line = String.format("%-19s %18s", "STATS LOCAL MSV", hmm.getMSV());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
line = String.format("%-19s %18s", "STATS LOCAL VITERBI",
hmm.getViterbi());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
line = String.format("%-19s %18s", "STATS LOCAL FORWARD",
hmm.getForward());
- file.append((line + NEW_LINE));
+ output.append(NL + line);
}
+ return output.toString();
}
@Override
public String print(SequenceI[] seqs, boolean jvsuffix)
{
+ if (seqs[0].getHMM() != null)
+ {
+ hmm = seqs[0].getHMM();
+ }
+ 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();
}
/**
*
* @param list
*/
- void convertListToLogSpace(List<Double> list)
+ List<Double> convertListToLogSpace(List<Double> list)
{
+ List<Double> convertedList = new ArrayList<>();
for (int i = 0; i < list.size(); i++)
{
double prob = list.get(i);
double logProb = -1 * Math.log(prob);
- list.set(i, logProb);
+ convertedList.add(logProb);
}
+ return convertedList;
+
+ }
+
+ /**
+ * Returns the HMM sequence produced by reading a .hmm file.
+ */
+ @Override
+ public SequenceI[] getSeqsAsArray()
+ {
+ SequenceI hmmSeq = hmm.initHMMSequence();
+ SequenceI[] seq = new SequenceI[1];
+ seq[0] = hmmSeq;
+ return seq;
}
+
+ /**
+ * Fills symbol array and adds each symbol to an index lookup
+ *
+ * @param parser
+ * The scanner scanning the symbol line in the file.
+ */
+ public void fillSymbols(Scanner parser)
+ {
+ int i = 0;
+ while (parser.hasNext())
+ {
+ String strSymbol = parser.next();
+ char[] symbol = strSymbol.toCharArray();
+ hmm.getSymbols().add(symbol[0]);
+ hmm.setSymbolIndex(symbol[0], i);
+ i++;
+ }
+ }
+
+ @Override
+ public void setNewlineString(String newLine)
+ {
+ NL = newLine;
+ }
+
+ @Override
+ public void setExportSettings(AlignExportSettingI exportSettings)
+ {
+
+ }
+
+ @Override
+ public void configureForView(AlignmentViewPanel viewpanel)
+ {
+
+ }
+
+ @Override
+ public boolean hasWarningMessage()
+ {
+ return false;
+ }
+
+ @Override
+ public String getWarningMessage()
+ {
+ return "warning message";
+ }
+
}