From 88141010d441b5fa5fcf43bb3dcbecbbc1721139 Mon Sep 17 00:00:00 2001 From: TZVanaalten Date: Fri, 11 Aug 2017 16:09:39 +0100 Subject: [PATCH] JAL-2629 add ability to build a HMM from a group --- src/jalview/datamodel/Alignment.java | 53 ++++++++++---------------- src/jalview/datamodel/AlignmentI.java | 4 +- src/jalview/datamodel/Sequence.java | 41 ++++++++++++++++++++ src/jalview/datamodel/SequenceGroup.java | 18 +++++---- src/jalview/datamodel/SequenceI.java | 7 ++++ src/jalview/hmmer/HMMBuildThread.java | 57 ++++++++++++++++++++-------- src/jalview/hmmer/HMMERCommands.java | 20 ++++++++-- src/jalview/workers/InformationThread.java | 2 +- 8 files changed, 137 insertions(+), 65 deletions(-) diff --git a/src/jalview/datamodel/Alignment.java b/src/jalview/datamodel/Alignment.java index 8ca859f..38d0092 100755 --- a/src/jalview/datamodel/Alignment.java +++ b/src/jalview/datamodel/Alignment.java @@ -30,7 +30,6 @@ import jalview.util.MessageManager; import java.util.ArrayList; import java.util.Collections; import java.util.Enumeration; -import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.List; @@ -1961,54 +1960,40 @@ public class Alignment implements AlignmentI hiddenCols = cols; } + @Override - public Map getHMMConsensusSequences(boolean remove) + public List getHMMConsensusSequences(boolean remove) { - Map seqs = new HashMap<>(); + List seqs = new ArrayList<>(); int position = 0; - for (SequenceI seq : sequences) + int seqsRemoved = 0; + boolean endReached = false; + + while (!endReached) { + SequenceI seq = sequences.get(position); if (seq.isHMMConsensusSequence()) { - seqs.put(position, seq); if (remove) { - sequences.set(position, null); + sequences.remove(position); + seqsRemoved++; + seq.setPreviousPosition(seqsRemoved + position - 1); } - - } - position++; - } - boolean endReached = false; - int pos = 0; - while (!endReached) - { - if (sequences.get(pos) == null) - { - sequences.remove(pos); + else + { + position++; + } + seqs.add(seq); } else { - pos++; - } - if (pos >= sequences.size()) - { - endReached = true; + position++; } - } - - return seqs; - } - @Override - public List getHMMConsensusSequences() - { - List seqs = new ArrayList<>(); - for (SequenceI seq : sequences) - { - if (seq.isHMMConsensusSequence()) + if (position >= sequences.size()) { - seqs.add(seq); + endReached = true; } } return seqs; diff --git a/src/jalview/datamodel/AlignmentI.java b/src/jalview/datamodel/AlignmentI.java index edd5b66..58df1b0 100755 --- a/src/jalview/datamodel/AlignmentI.java +++ b/src/jalview/datamodel/AlignmentI.java @@ -590,10 +590,8 @@ public interface AlignmentI extends AnnotatedCollectionI public int[] getVisibleStartAndEndIndex(List hiddenCols); public void setHiddenColumns(HiddenColumns cols); - - public Map getHMMConsensusSequences(boolean remove); - public List getHMMConsensusSequences(); + public List getHMMConsensusSequences(boolean remove); diff --git a/src/jalview/datamodel/Sequence.java b/src/jalview/datamodel/Sequence.java index 54fa3b0..0da30ba 100755 --- a/src/jalview/datamodel/Sequence.java +++ b/src/jalview/datamodel/Sequence.java @@ -51,12 +51,16 @@ public class Sequence extends ASequence implements SequenceI private char[] sequence; + int previousPosition; + String description; int start; int end; + boolean hasInfo; + HiddenMarkovModel hmm; boolean isHMMConsensusSequence = false; @@ -1529,6 +1533,43 @@ public class Sequence extends ASequence implements SequenceI this.isHMMConsensusSequence = isHMMConsensusSequence; } + @Override + public boolean hasHMMAnnotation() + { + return hasInfo; + /* + if (annotation == null) + { + return false; + } + + for (AlignmentAnnotation annot : annotation) + { + if (annot.label.contains("_HMM")) + { + return true; + } + } + return false; + */ + } + + @Override + public void setHasInfo(boolean status) + { + hasInfo = true; + } + + @Override + public int getPreviousPosition() + { + return previousPosition; + } + @Override + public void setPreviousPosition(int previousPosition) + { + this.previousPosition = previousPosition; + } } diff --git a/src/jalview/datamodel/SequenceGroup.java b/src/jalview/datamodel/SequenceGroup.java index eeff135..5cf07e2 100755 --- a/src/jalview/datamodel/SequenceGroup.java +++ b/src/jalview/datamodel/SequenceGroup.java @@ -51,8 +51,6 @@ public class SequenceGroup implements AnnotatedCollectionI boolean colourText = false; - HiddenMarkovModel hmm; - /** * True if the group is defined as a group on the alignment, false if it is * just a selection. @@ -578,8 +576,12 @@ public class SequenceGroup implements AnnotatedCollectionI endRes + 1, showSequenceLogo); if (information != null) { - // _updateInformationRow(cnsns, sequences.size()); TODO don't know what - // to do here + HiddenMarkovModel hmm = information.sequenceRef.getHMM(); + + ProfilesI info = AAFrequency.calculateInformation(hmm, + (endRes + 1) - startRes, startRes, endRes + 1, + showHMMSequenceLogo, ignoreBelowBackground); + _updateInformationRow(info, sequences.size()); upd = true; } if (consensus != null) @@ -1170,12 +1172,12 @@ public class SequenceGroup implements AnnotatedCollectionI if (information == null) { information = new AlignmentAnnotation("", "", new Annotation[1], 0f, - 100f, AlignmentAnnotation.BAR_GRAPH); + 6.25f, AlignmentAnnotation.BAR_GRAPH); information.hasText = true; - information.autoCalculated = true; + information.autoCalculated = false; information.groupRef = this; - information.label = "Consensus for " + getName(); - information.description = "Percent Identity"; + information.label = getName() + "_HMM"; + information.description = "Information content, measured in bits"; } return information; } diff --git a/src/jalview/datamodel/SequenceI.java b/src/jalview/datamodel/SequenceI.java index 243d688..271f25a 100755 --- a/src/jalview/datamodel/SequenceI.java +++ b/src/jalview/datamodel/SequenceI.java @@ -487,5 +487,12 @@ public interface SequenceI extends ASequenceI void setIsHMMConsensusSequence(boolean isHMMConsensusSequence); + boolean hasHMMAnnotation(); + + void setHasInfo(boolean status); + + int getPreviousPosition(); + + void setPreviousPosition(int previousPosition); } diff --git a/src/jalview/hmmer/HMMBuildThread.java b/src/jalview/hmmer/HMMBuildThread.java index c5e1816..fb904f9 100644 --- a/src/jalview/hmmer/HMMBuildThread.java +++ b/src/jalview/hmmer/HMMBuildThread.java @@ -2,6 +2,7 @@ package jalview.hmmer; import jalview.bin.Cache; import jalview.datamodel.AlignmentI; +import jalview.datamodel.SequenceGroup; import jalview.datamodel.SequenceI; import jalview.gui.AlignFrame; import jalview.gui.Preferences; @@ -12,7 +13,9 @@ import jalview.util.MessageManager; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.Map; +import java.util.List; + +import javax.swing.JOptionPane; public class HMMBuildThread implements Runnable { @@ -20,28 +23,40 @@ public class HMMBuildThread implements Runnable AlignFrame af; AlignmentI alignment; + SequenceGroup group; + + boolean canRun = true; + File hmmTemp = null; File stoTemp = null; long barID; - - Map hmmSeqs; public HMMBuildThread(AlignFrame af) { this.af = af; + if (af.getViewport().getSelectionGroup() != null) + { + group = af.getViewport().getSelectionGroup(); + } alignment = af.getViewport().getAlignment(); + } @Override public void run() { barID = System.currentTimeMillis(); - System.out.println(System.getProperty("java.io.tmpdir")); af.setProgressBar(MessageManager.getString("status.running_hmmbuild"), barID); cmds.HMMERFOLDER = Cache.getProperty(Preferences.HMMER_PATH); + if (alignment == null && group == null) + { + JOptionPane.showMessageDialog(af, + MessageManager.getString("label.no_sequence_data")); + return; + } try { hmmTemp = File.createTempFile("hmm", ".hmm"); @@ -57,12 +72,24 @@ public class HMMBuildThread implements Runnable { try { - hmmSeqs = alignment.getHMMConsensusSequences(true); - if (!alignment.isAligned()) + List seqs = alignment + .getHMMConsensusSequences(true); + cmds.setHmmSeqs(seqs); + if (group != null) { - alignment.padGaps(); + SequenceI[] array = group.getSelectionAsNewSequences(alignment); + + cmds.exportData(array, stoTemp, null, null); + } + else + { + if (!alignment.isAligned()) + { + alignment.padGaps(); + } + cmds.exportData(alignment.getSequencesArray(), stoTemp, null, + null); } - cmds.exportData(alignment.getSequencesArray(), stoTemp, null, null); } catch (FileNotFoundException e) { // TODO Auto-generated catch block @@ -88,7 +115,7 @@ public class HMMBuildThread implements Runnable } } catch (Exception e) { - + e.printStackTrace(); } finally { af.setProgressBar(MessageManager.getString("status.running_hmmbuild"), @@ -109,19 +136,17 @@ public class HMMBuildThread implements Runnable private void importData() throws IOException, InterruptedException { + cmds.addHMMConsensusSequences(af); af.loadJalviewDataFile(hmmTemp.getAbsolutePath(), DataSourceType.FILE, FileFormat.HMMER3, null); - for (Map.Entry entry : hmmSeqs.entrySet()) - { - SequenceI seq = entry.getValue(); - Integer pos = entry.getKey(); - cmds.addHMMConsensusSequence(af, seq, pos); - } - af.alignPanel.alignmentChanged(); hmmTemp.delete(); stoTemp.delete(); } + public boolean canRun() + { + return canRun; + } diff --git a/src/jalview/hmmer/HMMERCommands.java b/src/jalview/hmmer/HMMERCommands.java index daa61cb..3b5472a 100644 --- a/src/jalview/hmmer/HMMERCommands.java +++ b/src/jalview/hmmer/HMMERCommands.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Hashtable; +import java.util.List; public class HMMERCommands { @@ -47,6 +48,8 @@ public class HMMERCommands Hashtable hash = new Hashtable(); + List hmmSeqs; + public void uniquifySequences(SequenceI[] seqs) { hash = jalview.analysis.SeqsetUtils.uniquify(seqs, true); @@ -121,16 +124,27 @@ public class HMMERCommands } } - public void addHMMConsensusSequence(AlignFrame af, SequenceI seq, - Integer position) + public void addHMMConsensusSequences(AlignFrame af) { - seq.getHMM().initHMMSequence(af, position); AlignmentI al = af.getViewport().getAlignment(); + for (SequenceI seq : hmmSeqs) + { + Integer position = seq.getPreviousPosition(); + al.getSequences().add(position, seq); + } af.getViewport().setAlignment(al); af.alignPanel.adjustAnnotationHeight(); af.getViewport().updateSequenceIdColours(); af.buildSortByAnnotationScoresMenu(); } + public List getHmmSeqs() + { + return hmmSeqs; + } + public void setHmmSeqs(List hmmSeqs) + { + this.hmmSeqs = hmmSeqs; + } } diff --git a/src/jalview/workers/InformationThread.java b/src/jalview/workers/InformationThread.java index 54e5198..67dd838 100644 --- a/src/jalview/workers/InformationThread.java +++ b/src/jalview/workers/InformationThread.java @@ -116,7 +116,7 @@ public class InformationThread extends AlignCalcWorker { int width = alignment.getWidth(); - List hmmSeqs = alignment.getHMMConsensusSequences(); + List hmmSeqs = alignment.getHMMConsensusSequences(false); int index = 0; for (SequenceI seq : hmmSeqs) { -- 1.7.10.2