3 import jalview.datamodel.HMMNode;
4 import jalview.datamodel.HiddenMarkovModel;
5 import jalview.datamodel.SequenceI;
7 import java.io.BufferedReader;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.io.UnsupportedEncodingException;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Scanner;
18 * reads in and writes out a HMMER standard file
24 public class HMMFile extends AlignFile
25 implements AlignmentFileReaderI, AlignmentFileWriterI
27 // HMM to store file data
28 private HiddenMarkovModel hmm = new HiddenMarkovModel();
33 // number of possible transitions
34 private final int NUMBER_OF_TRANSITIONS = 7;
36 private final String NEW_LINE = "\n";
44 private final String SPACE = " ";
46 private final String COMPO = "COMPO";
48 private final String EMPTY = "";
50 public HMMFile(FileParse source) throws IOException
60 public HiddenMarkovModel getHMM()
65 public void setHMM(HiddenMarkovModel model)
70 public String getName()
76 * reads data from HMM file
81 public void parse() throws IOException
83 parseFileProperties(dataIn);
90 * imports file properties from hmm file
93 * buffered reader used to read in file
96 void parseFileProperties(BufferedReader input) throws IOException
98 boolean readingFile = true;
99 fileHeader = input.readLine();
100 String line = input.readLine();
105 Scanner parser = new Scanner(line);
106 String next = parser.next();
107 if ("HMM".equals(next)) // indicates start of HMM data (end of file
111 hmm.fillSymbols(parser);
112 numberOfSymbols = hmm.getNumberOfSymbols();
114 else if ("STATS".equals(next))
120 value = parser.next() + SPACE + SPACE + parser.next();
121 hmm.addFileProperty(key, value);
126 String value = parser.next();
127 while (parser.hasNext())
129 value = value + SPACE + parser.next();
131 hmm.addFileProperty(key, value);
135 line = input.readLine();
145 * parses the model data from the hmm file
148 * buffered reader used to read file
149 * @throws IOException
151 void parseModel(BufferedReader input) throws IOException
153 for (int i = 0; i < hmm.getLength() + 1; i++)
155 hmm.getNodes().add(new HMMNode());
158 line = input.readLine();
159 Scanner matchReader = new Scanner(line);
160 next = matchReader.next();
161 if (next.equals(COMPO) || i > 0)
163 // stores match emission line in list
164 List<Double> matches = new ArrayList<>();
165 matches = fillList(matchReader, numberOfSymbols);
166 hmm.getNodes().get(i).setMatchEmissions(matches);
169 parseAnnotations(matchReader, i);
173 // stores insert emission line in list
174 line = input.readLine();
175 Scanner insertReader = new Scanner(line);
176 List<Double> inserts = new ArrayList<>();
177 inserts = fillList(insertReader, numberOfSymbols);
178 hmm.getNodes().get(i).setInsertEmissions(inserts);
179 insertReader.close();
181 // stores state transition line in list
182 line = input.readLine();
183 Scanner transitionReader = new Scanner(line);
184 List<Double> transitions = new ArrayList<>();
185 transitions = fillList(transitionReader, NUMBER_OF_TRANSITIONS);
186 hmm.getNodes().get(i).setStateTransitions(transitions);
187 transitionReader.close();
193 * parses annotations on match emission line
196 * scanner which is processing match emission line
198 * index of node which is beign scanned
200 void parseAnnotations(Scanner scanner, int index)
202 if (hmm.mapIsActive())
205 column = scanner.nextInt();
206 hmm.getNodes().get(index).setAlignmentColumn(column);
207 hmm.getNodeLookup().put(column, index);
215 consensusR = charValue(scanner.next());
216 hmm.getNodes().get(index).setConsensusResidue(consensusR);
219 reference = charValue(scanner.next());
220 hmm.getNodes().get(index).setReferenceAnnotation(reference);
224 value = charValue(scanner.next());
225 hmm.getNodes().get(index).setMaskValue(value);
228 consensusS = charValue(scanner.next());
229 hmm.getNodes().get(index).setConsensusStructure(consensusS);
236 * scanner for line containing data to be transferred to list
237 * @param numberOfElements
238 * number of elements in the list to be filled
239 * @return filled list
241 static List<Double> fillList(Scanner input,
242 int numberOfElements)
244 List<Double> list = new ArrayList<>();
245 for (int i = 0; i < numberOfElements; i++)
248 String next = input.next();
249 if (next.contains("*")) // state transitions to or from delete states
250 // occasionally have values of -infinity. These
251 // values are represented by an * in the .hmm
252 // file, and by a null value in the
253 // HiddenMarkovModel class
255 list.add(Double.NEGATIVE_INFINITY);
259 list.add(Double.valueOf(next));
267 * writes a HiddenMarkovModel to a file
269 * @param exportLocation
270 * Filename, URL or Pasted String to write to
271 * @throws FileNotFoundException
272 * @throws UnsupportedEncodingException
276 public void exportFile(String exportLocation) throws IOException
278 StringBuilder file = new StringBuilder();
279 appendFileProperties(file);
284 PrintWriter output = new PrintWriter(exportLocation);
290 String addData(int initialColumnSeparation,
291 int columnSeparation, List<String> data)
295 for (String value : data)
299 line += String.format("%" + initialColumnSeparation + "s", value);
303 line += String.format("%" + columnSeparation + "s", value);
310 List<String> charListToStringList(List<Character> list)
312 List<String> strList = new ArrayList<>();
313 for (char value : list)
315 String strValue = Character.toString(value);
316 strList.add(strValue);
321 List<String> doubleListToStringList(List<Double> list,
324 List<String> strList = new ArrayList<>();
325 for (double value : list)
328 if (value == Double.NEGATIVE_INFINITY)
334 strValue = String.format("%.5f", value);
337 strList.add(strValue);
342 List<String> stringArrayToStringList(String[] array)
344 List<String> list = new ArrayList<>();
345 for (String value : array)
353 void appendModel(StringBuilder file)
355 String symbolLine = "HMM";
356 List<Character> charSymbols = hmm.getSymbols();
357 List<String> strSymbols;
358 strSymbols = charListToStringList(charSymbols);
359 symbolLine += addData(11, 9, strSymbols);
360 file.append(symbolLine + NEW_LINE);
362 String transitionTypeLine = "";
363 List<String> transitionTypes;
364 transitionTypes = stringArrayToStringList(hmm.getTransitionTypes());
365 transitionTypeLine += addData(16, 9, transitionTypes);
366 file.append(transitionTypeLine + NEW_LINE);
368 int length = hmm.getLength();
370 for (int node = 0; node <= length; node++)
375 matchLine = String.format("%7s", "COMPO");
379 matchLine = String.format("%7s", node);
382 List<String> strMatches;
383 List<Double> doubleMatches;
384 doubleMatches = hmm.getNode(node).getMatchEmissions();
385 strMatches = doubleListToStringList(doubleMatches, 5);
386 matchLine += addData(10, 9, strMatches);
391 matchLine += SPACE + hmm.getNodeAlignmentColumn(node);
392 matchLine += SPACE + hmm.getConsensusResidue(node);
393 matchLine += SPACE + hmm.getReferenceAnnotation(node);
394 matchLine += SPACE + hmm.getMaskedValue(node);
395 matchLine += SPACE + hmm.getConsensusStructure(node);
399 file.append(matchLine + NEW_LINE);
401 String insertLine = EMPTY;
402 List<String> strInserts;
403 List<Double> doubleInserts;
404 doubleInserts = hmm.getNode(node).getInsertEmissions();
405 strInserts = doubleListToStringList(doubleInserts, 5);
406 insertLine += addData(17, 9, strInserts);
408 file.append(insertLine + NEW_LINE);
410 String transitionLine = EMPTY;
411 List<String> strTransitions;
412 List<Double> doubleTransitions;
413 doubleTransitions = hmm.getNode(node).getStateTransitions();
414 strTransitions = doubleListToStringList(doubleTransitions, 5);
415 transitionLine += addData(17, 9, strTransitions);
417 file.append(transitionLine + NEW_LINE);
421 void appendFileProperties(StringBuilder file)
425 file.append(fileHeader + NEW_LINE);
427 line = String.format("%-5s %1s", "NAME", hmm.getName());
428 file.append((line + NEW_LINE));
430 if (hmm.getAccessionNumber() != null)
432 line = String.format("%-5s %1s", "ACC", hmm.getAccessionNumber());
433 file.append((line + NEW_LINE));
436 if (hmm.getDescription() != null)
438 line = String.format("%-5s %1s", "DESC", hmm.getDescription());
439 file.append((line + NEW_LINE));
441 line = String.format("%-5s %1s", "LENG", hmm.getLength());
442 file.append((line + NEW_LINE));
444 if (hmm.getMaxInstanceLength() != null)
446 line = String.format("%-5s %1s", "MAXL", hmm.getMaxInstanceLength());
447 file.append((line + NEW_LINE));
449 line = String.format("%-5s %1s", "ALPH", hmm.getAlphabetType());
450 file.append((line + NEW_LINE));
455 status = hmm.referenceAnnotationIsActive();
456 statusStr = HiddenMarkovModel.findStringFromBoolean(status);
457 line = String.format("%-5s %1s", "RF",
459 file.append((line + NEW_LINE));
461 status = hmm.maskValueIsActive();
462 statusStr = HiddenMarkovModel.findStringFromBoolean(status);
463 line = String.format("%-5s %1s", "MM",
465 file.append((line + NEW_LINE));
467 status = hmm.consensusResidueIsActive();
468 statusStr = HiddenMarkovModel.findStringFromBoolean(status);
469 line = String.format("%-5s %1s", "CONS",
471 file.append((line + NEW_LINE));
473 status = hmm.consensusStructureIsActive();
474 statusStr = HiddenMarkovModel.findStringFromBoolean(status);
475 line = String.format("%-5s %1s", "CS",
477 file.append((line + NEW_LINE));
479 status = hmm.mapIsActive();
480 statusStr = HiddenMarkovModel.findStringFromBoolean(status);
481 line = String.format("%-5s %1s", "MAP",
483 file.append((line + NEW_LINE));
486 if (hmm.getDate() != null)
488 line = String.format("%-5s %1s", "DATE", hmm.getDate());
489 file.append((line + NEW_LINE));
491 if (hmm.getNumberOfSequences() != null)
493 line = String.format("%-5s %1s", "NSEQ", hmm.getNumberOfSequences());
494 file.append((line + NEW_LINE));
496 if (hmm.getEffectiveNumberOfSequences() != null)
498 line = String.format("%-5s %1s", "EFFN",
499 hmm.getEffectiveNumberOfSequences());
500 file.append((line + NEW_LINE));
502 if (hmm.getCheckSum() != null)
504 line = String.format("%-5s %1s", "CKSUM", hmm.getCheckSum());
505 file.append((line + NEW_LINE));
507 if (hmm.getGatheringThreshold() != null)
509 line = String.format("%-5s %1s", "GA", hmm.getGatheringThreshold());
510 file.append((line + NEW_LINE));
513 if (hmm.getTrustedCutoff() != null)
515 line = String.format("%-5s %1s", "TC", hmm.getTrustedCutoff());
516 file.append((line + NEW_LINE));
518 if (hmm.getNoiseCutoff() != null)
520 line = String.format("%-5s %1s", "NC", hmm.getNoiseCutoff());
521 file.append((line + NEW_LINE));
523 if (hmm.getMSV() != null)
525 line = String.format("%-19s %18s", "STATS LOCAL MSV", hmm.getMSV());
526 file.append((line + NEW_LINE));
528 line = String.format("%-19s %18s", "STATS LOCAL VITERBI",
530 file.append((line + NEW_LINE));
532 line = String.format("%-19s %18s", "STATS LOCAL FORWARD",
534 file.append((line + NEW_LINE));
540 char charValue(String string)
543 character = string.charAt(0);
548 public String print(SequenceI[] seqs, boolean jvsuffix)