JAL-2629 add ability to add background frequencies to a HMM
authortva <tva@10.205.251.175>
Fri, 28 Jun 2019 15:49:41 +0000 (16:49 +0100)
committertva <tva@10.205.251.175>
Fri, 28 Jun 2019 15:49:41 +0000 (16:49 +0100)
resources/lang/Messages.properties
src/jalview/gui/PopupMenu.java
src/jalview/io/CountReader.java [new file with mode: 0644]
src/jalview/schemes/HMMMatchScoreColourScheme.java

index 33d9359..33395d2 100644 (file)
@@ -99,6 +99,7 @@ action.edit_group = Edit Group
 action.border_colour = Border colour
 action.edit_new_group = Edit New Group
 action.hide_sequences = Hide Sequences
+action.add_background_frequencies = Add Background Frequencies
 action.sequences = Sequences
 action.ids = IDS
 action.ids_sequences = IDS and sequences
@@ -201,6 +202,7 @@ label.colourScheme_purine/pyrimidine = Purine/Pyrimidine
 label.colourScheme_nucleotide = Nucleotide
 label.colourScheme_hmmer-uniprot = HMMER profile v global background
 label.colourScheme_hmmer-alignment = HMMER profile v alignment background
+label.colourScheme_hmm_match_score = HMM Match Score
 label.colourScheme_t-coffeescores = T-Coffee Scores
 label.colourScheme_rnahelices = By RNA Helices
 label.colourScheme_sequenceid = Sequence ID Colour
index dcf0209..8c87aa0 100644 (file)
@@ -34,10 +34,12 @@ import jalview.datamodel.Annotation;
 import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
+import jalview.datamodel.ResidueCount;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.ColourMenuHelper.ColourChangeListener;
+import jalview.io.CountReader;
 import jalview.io.FileFormatI;
 import jalview.io.FileFormats;
 import jalview.io.FormatAdapter;
@@ -56,6 +58,8 @@ import jalview.util.UrlLink;
 import java.awt.Color;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.IOException;
+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
@@ -532,6 +536,36 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
         }
       }
 
+      if (seq.hasHMMProfile())
+      {
+        menuItem = new JMenuItem(MessageManager
+                .getString("action.add_background_frequencies"));
+        menuItem.addActionListener(new ActionListener()
+        {
+          @Override
+          public void actionPerformed(ActionEvent e)
+          {
+            try
+            {
+              ResidueCount counts = CountReader.getBackgroundFrequencies(ap,
+                      seq);
+              if (counts != null)
+              {
+                seq.getHMM().setBackgroundFrequencies(counts);
+                ap.alignFrame.buildColourMenu();
+              }
+            } catch (MalformedURLException e1)
+            {
+              e1.printStackTrace();
+            } catch (IOException e1)
+            {
+              e1.printStackTrace();
+            }
+          }
+        });
+        add(menuItem);
+      }
+
       menuItem = new JMenuItem(
               MessageManager.getString("action.hide_sequences"));
       menuItem.addActionListener(new ActionListener()
diff --git a/src/jalview/io/CountReader.java b/src/jalview/io/CountReader.java
new file mode 100644 (file)
index 0000000..eea3dae
--- /dev/null
@@ -0,0 +1,63 @@
+package jalview.io;
+
+import jalview.bin.Jalview;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.ResidueCount;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignmentPanel;
+import jalview.gui.Desktop;
+import jalview.gui.JvOptionPane;
+import jalview.util.MessageManager;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+
+import javax.swing.JFileChooser;
+
+public class CountReader
+{
+  public static ResidueCount getBackgroundFrequencies(AlignmentPanel ap, SequenceI seq) throws MalformedURLException, IOException
+  {
+    JFileChooser bkgdFreqChooser = new JFileChooser();
+    
+    bkgdFreqChooser.showOpenDialog(ap);
+    
+    File file = bkgdFreqChooser.getSelectedFile();
+    if (file == null)
+    {
+      return null;
+    }
+    
+    IdentifyFile identifier = new IdentifyFile();
+    FileFormatI format = null;
+    try
+    {
+      format = identifier.identify(file.getPath(), DataSourceType.FILE);
+    } catch (Exception e)
+    {
+
+    }
+    
+    if (format == null)
+    {
+      if (!Jalview.isHeadlessMode())
+      {
+        JvOptionPane.showInternalMessageDialog(Desktop.desktop,
+                MessageManager.getString("label.couldnt_read_data") + " in "
+                        + file + "\n"
+                        + AppletFormatAdapter.getSupportedFormats(),
+                MessageManager.getString("label.couldnt_read_data"),
+                JvOptionPane.WARNING_MESSAGE);
+      }
+    }
+
+    FileParse parser = new FileParse(file.getPath(), DataSourceType.FILE);
+    AlignmentI al = new FormatAdapter().readFromFile(parser, format);
+    parser.close();
+    
+    ResidueCount counts = new ResidueCount(al.getSequences());
+    
+    return counts;
+  }
+}
index 36f66d0..cc8740b 100644 (file)
@@ -141,11 +141,24 @@ public class HMMMatchScoreColourScheme extends ResidueColourScheme
       symbol = Character.toUpperCase(symbol);
     }
 
-    double llr = Math
-            .log(getHmm().getMatchEmissionProbability(column, symbol)
-                    / hmm.getBackgroundFrequencies().get(symbol));
+    double prob = 0;
+    if (hmm.getBackgroundFrequencies().containsKey(symbol))
+    {
+      double llr = Math
+              .log(getHmm().getMatchEmissionProbability(column, symbol)
+                      / hmm.getBackgroundFrequencies().get(symbol));
+
+      if (!probabilities.get(symbol).containsKey(format(llr)))
+      {
+        return Color.green;
+      }
 
-    double prob = probabilities.get(symbol).get(format(llr));
+      prob = probabilities.get(symbol).get(format(llr));
+    }
+    else
+    {
+      return new Color(140, 140, 140);
+    }
 
     Color colour = Color.ORANGE;
     if (prob >= 0.5)
@@ -224,13 +237,14 @@ public class HMMMatchScoreColourScheme extends ResidueColourScheme
   }
 
   /**
-   * Answers true if the sequence collection has an HMM consensus sequence, else
-   * false
+   * Answers true if the sequence collection has an HMM consensus sequence and
+   * that the first HMM sequence contains background frequencies, else false
    */
   @Override
   public boolean isApplicableTo(AnnotatedCollectionI ac)
   {
-    return !ac.getHmmSequences().isEmpty();
+    return !ac.getHmmSequences().isEmpty() && ac.getHmmSequences().get(0)
+            .getHMM().getBackgroundFrequencies() != null;
   }
 
   protected Map<Character, Float> getFrequencies()