JAL-1264 adding show/hide annotation options to applet
[jalview.git] / src / jalview / analysis / AlignmentUtils.java
index 74406dc..6f0125d 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  */
 package jalview.analysis;
 
-import jalview.datamodel.AlignedCodon;
-import jalview.datamodel.AlignedCodonFrame;
-import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.AlignmentI;
-import jalview.datamodel.Mapping;
-import jalview.datamodel.SearchResults;
-import jalview.datamodel.Sequence;
-import jalview.datamodel.SequenceI;
-import jalview.schemes.ResidueProperties;
-import jalview.util.MapList;
-
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -42,6 +33,18 @@ import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;
 
+import jalview.datamodel.AlignedCodon;
+import jalview.datamodel.AlignedCodonFrame;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.Mapping;
+import jalview.datamodel.SearchResults;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
+import jalview.schemes.ResidueProperties;
+import jalview.util.MapList;
+
 /**
  * grab bag of useful alignment manipulation operations Expect these to be
  * refactored elsewhere at some point.
@@ -53,15 +56,6 @@ public class AlignmentUtils
 {
 
   /**
-   * Represents the 3 possible results of trying to map one alignment to
-   * another.
-   */
-  public enum MappingResult
-  {
-    Mapped, NotMapped, AlreadyMapped
-  }
-
-  /**
    * given an existing alignment, create a new alignment including all, or up to
    * flankSize additional symbols from each sequence's dataset sequence
    * 
@@ -214,51 +208,95 @@ public class AlignmentUtils
 
   /**
    * Build mapping of protein to cDNA alignment. Mappings are made between
-   * sequences which have the same name and compatible lengths. Any new mappings
-   * are added to the protein alignment. Has a 3-valued result: either Mapped
-   * (at least one sequence mapping was created), AlreadyMapped (all possible
-   * sequence mappings already exist), or NotMapped (no possible sequence
-   * mappings exist).
+   * sequences where the cDNA translates to the protein sequence. Any new
+   * mappings are added to the protein alignment. Returns true if any mappings
+   * either already exist or were added, else false.
    * 
    * @param proteinAlignment
    * @param cdnaAlignment
    * @return
    */
-  public static MappingResult mapProteinToCdna(
+  public static boolean mapProteinToCdna(
           final AlignmentI proteinAlignment,
           final AlignmentI cdnaAlignment)
   {
     if (proteinAlignment == null || cdnaAlignment == null)
     {
-      return MappingResult.NotMapped;
+      return false;
     }
 
-    boolean mappingPossible = false;
-    boolean mappingPerformed = false;
+    Set<SequenceI> mappedDna = new HashSet<SequenceI>();
+    Set<SequenceI> mappedProtein = new HashSet<SequenceI>();
 
-    List<SequenceI> thisSeqs = proteinAlignment.getSequences();
-  
     /*
-     * Build a look-up of cDNA sequences by name, for matching purposes.
+     * First pass - map sequences where cross-references exist. This include
+     * 1-to-many mappings to support, for example, variant cDNA.
      */
-    Map<String, List<SequenceI>> cdnaSeqs = cdnaAlignment
-            .getSequencesByName();
-  
+    boolean mappingPerformed = mapProteinToCdna(proteinAlignment,
+            cdnaAlignment, mappedDna, mappedProtein, true);
+
+    /*
+     * Second pass - map sequences where no cross-references exist. This only
+     * does 1-to-1 mappings and assumes corresponding sequences are in the same
+     * order in the alignments.
+     */
+    mappingPerformed |= mapProteinToCdna(proteinAlignment, cdnaAlignment,
+            mappedDna, mappedProtein, false);
+    return mappingPerformed;
+  }
+
+  /**
+   * Make mappings between compatible sequences (where the cDNA translation
+   * matches the protein).
+   * 
+   * @param proteinAlignment
+   * @param cdnaAlignment
+   * @param mappedDna
+   *          a set of mapped DNA sequences (to add to)
+   * @param mappedProtein
+   *          a set of mapped Protein sequences (to add to)
+   * @param xrefsOnly
+   *          if true, only map sequences where xrefs exist
+   * @return
+   */
+  protected static boolean mapProteinToCdna(
+          final AlignmentI proteinAlignment,
+          final AlignmentI cdnaAlignment, Set<SequenceI> mappedDna,
+          Set<SequenceI> mappedProtein, boolean xrefsOnly)
+  {
+    boolean mappingPerformed = false;
+    List<SequenceI> thisSeqs = proteinAlignment.getSequences();
     for (SequenceI aaSeq : thisSeqs)
     {
+      boolean proteinMapped = false;
       AlignedCodonFrame acf = new AlignedCodonFrame();
-      List<SequenceI> candidates = cdnaSeqs.get(aaSeq.getName());
-      if (candidates == null)
+
+      for (SequenceI cdnaSeq : cdnaAlignment.getSequences())
       {
         /*
-         * No cDNA sequence with matching name, so no mapping possible for this
-         * protein sequence
+         * Always try to map if sequences have xref to each other; this supports
+         * variant cDNA or alternative splicing for a protein sequence.
+         * 
+         * If no xrefs, try to map progressively, assuming that alignments have
+         * mappable sequences in corresponding order. These are not
+         * many-to-many, as that would risk mixing species with similar cDNA
+         * sequences.
          */
-        continue;
-      }
-      mappingPossible = true;
-      for (SequenceI cdnaSeq : candidates)
-      {
+        if (xrefsOnly && !CrossRef.haveCrossRef(aaSeq, cdnaSeq))
+        {
+          continue;
+        }
+
+        /*
+         * Don't map non-xrefd sequences more than once each. This heuristic
+         * allows us to pair up similar sequences in ordered alignments.
+         */
+        if (!xrefsOnly
+                && (mappedProtein.contains(aaSeq) || mappedDna
+                        .contains(cdnaSeq)))
+        {
+          continue;
+        }
         if (!mappingExists(proteinAlignment.getCodonFrames(),
                 aaSeq.getDatasetSequence(), cdnaSeq.getDatasetSequence()))
         {
@@ -267,25 +305,18 @@ public class AlignmentUtils
           {
             acf.addMap(cdnaSeq, aaSeq, map);
             mappingPerformed = true;
+            proteinMapped = true;
+            mappedDna.add(cdnaSeq);
+            mappedProtein.add(aaSeq);
           }
         }
       }
-      proteinAlignment.addCodonFrame(acf);
-    }
-
-    /*
-     * If at least one mapping was possible but none was done, then the
-     * alignments are already as mapped as they can be.
-     */
-    if (mappingPossible && !mappingPerformed)
-    {
-      return MappingResult.AlreadyMapped;
-    }
-    else
-    {
-      return mappingPerformed ? MappingResult.Mapped
-              : MappingResult.NotMapped;
+      if (proteinMapped)
+      {
+        proteinAlignment.addCodonFrame(acf);
+      }
     }
+    return mappingPerformed;
   }
 
   /**
@@ -311,7 +342,8 @@ public class AlignmentUtils
   /**
    * Build a mapping (if possible) of a protein to a cDNA sequence. The cDNA
    * must be three times the length of the protein, possibly after ignoring
-   * start and/or stop codons. Returns null if no mapping is determined.
+   * start and/or stop codons, and must translate to the protein. Returns null
+   * if no mapping is determined.
    * 
    * @param proteinSeqs
    * @param cdnaSeq
@@ -321,34 +353,41 @@ public class AlignmentUtils
           SequenceI cdnaSeq)
   {
     /*
-     * Here we handle either dataset sequence set (desktop) or absent (applet)
+     * Here we handle either dataset sequence set (desktop) or absent (applet).
+     * Use only the char[] form of the sequence to avoid creating possibly large
+     * String objects.
      */
     final SequenceI proteinDataset = proteinSeq.getDatasetSequence();
-    String aaSeqString = proteinDataset != null ? proteinDataset
-            .getSequenceAsString() : proteinSeq.getSequenceAsString();
+    char[] aaSeqChars = proteinDataset != null ? proteinDataset
+            .getSequence() : proteinSeq.getSequence();
     final SequenceI cdnaDataset = cdnaSeq.getDatasetSequence();
-    String cdnaSeqString = cdnaDataset != null ? cdnaDataset
-            .getSequenceAsString() : cdnaSeq.getSequenceAsString();
-    if (aaSeqString == null || cdnaSeqString == null)
+    char[] cdnaSeqChars = cdnaDataset != null ? cdnaDataset.getSequence()
+            : cdnaSeq.getSequence();
+    if (aaSeqChars == null || cdnaSeqChars == null)
     {
       return null;
     }
 
-    final int mappedLength = 3 * aaSeqString.length();
-    int cdnaLength = cdnaSeqString.length();
+    /*
+     * cdnaStart/End, proteinStartEnd are base 1 (for dataset sequence mapping)
+     */
+    final int mappedLength = 3 * aaSeqChars.length;
+    int cdnaLength = cdnaSeqChars.length;
     int cdnaStart = 1;
     int cdnaEnd = cdnaLength;
     final int proteinStart = 1;
-    final int proteinEnd = aaSeqString.length();
+    final int proteinEnd = aaSeqChars.length;
 
     /*
      * If lengths don't match, try ignoring stop codon.
      */
-    if (cdnaLength != mappedLength)
+    if (cdnaLength != mappedLength && cdnaLength > 2)
     {
-      for (Object stop : ResidueProperties.STOP)
+      String lastCodon = String.valueOf(cdnaSeqChars, cdnaLength - 3, 3)
+              .toUpperCase();
+      for (String stop : ResidueProperties.STOP)
       {
-        if (cdnaSeqString.toUpperCase().endsWith((String) stop))
+        if (lastCodon.equals(stop))
         {
           cdnaEnd -= 3;
           cdnaLength -= 3;
@@ -361,24 +400,68 @@ public class AlignmentUtils
      * If lengths still don't match, try ignoring start codon.
      */
     if (cdnaLength != mappedLength
-            && cdnaSeqString.toUpperCase().startsWith(
+            && cdnaLength > 2
+            && String.valueOf(cdnaSeqChars, 0, 3).toUpperCase()
+                    .equals(
                     ResidueProperties.START))
     {
       cdnaStart += 3;
       cdnaLength -= 3;
     }
 
-    if (cdnaLength == mappedLength)
+    if (cdnaLength != mappedLength)
     {
-      MapList map = new MapList(new int[]
-      { cdnaStart, cdnaEnd }, new int[]
-      { proteinStart, proteinEnd }, 3, 1);
-      return map;
+      return null;
     }
-    else
+    if (!translatesAs(cdnaSeqChars, cdnaStart - 1, aaSeqChars))
     {
       return null;
     }
+    MapList map = new MapList(new int[]
+    { cdnaStart, cdnaEnd }, new int[]
+    { proteinStart, proteinEnd }, 3, 1);
+    return map;
+  }
+
+  /**
+   * Test whether the given cdna sequence, starting at the given offset,
+   * translates to the given amino acid sequence, using the standard translation
+   * table. Designed to fail fast i.e. as soon as a mismatch position is found.
+   * 
+   * @param cdnaSeqChars
+   * @param cdnaStart
+   * @param aaSeqChars
+   * @return
+   */
+  protected static boolean translatesAs(char[] cdnaSeqChars, int cdnaStart,
+          char[] aaSeqChars)
+  {
+    int aaResidue = 0;
+    for (int i = cdnaStart; i < cdnaSeqChars.length - 2
+            && aaResidue < aaSeqChars.length; i += 3, aaResidue++)
+    {
+      String codon = String.valueOf(cdnaSeqChars, i, 3);
+      final String translated = ResidueProperties.codonTranslate(
+              codon);
+      /*
+       * ? allow X in protein to match untranslatable in dna ?
+       */
+      final char aaRes = aaSeqChars[aaResidue];
+      if ((translated == null || "STOP".equals(translated)) && aaRes == 'X')
+      {
+        continue;
+      }
+      if (translated == null
+              || !(aaRes == translated.charAt(0)))
+      {
+        // debug
+        // System.out.println(("Mismatch at " + i + "/" + aaResidue + ": "
+        // + codon + "(" + translated + ") != " + aaRes));
+        return false;
+      }
+    }
+    // fail if we didn't match all of the aa sequence
+    return (aaResidue == aaSeqChars.length);
   }
 
   /**
@@ -405,7 +488,7 @@ public class AlignmentUtils
     // TODO there may be one AlignedCodonFrame per dataset sequence, or one with
     // all mappings. Would it help to constrain this?
     List<AlignedCodonFrame> mappings = al.getCodonFrame(seq);
-    if (mappings == null)
+    if (mappings == null || mappings.isEmpty())
     {
       return false;
     }
@@ -902,4 +985,231 @@ public class AlignmentUtils
       seqProduct.put(protein, codon.product);
     }
   }
+
+  /**
+   * Returns true if a cDNA/Protein mapping either exists, or could be made,
+   * between at least one pair of sequences in the two alignments. Currently,
+   * the logic is:
+   * <ul>
+   * <li>One alignment must be nucleotide, and the other protein</li>
+   * <li>At least one pair of sequences must be already mapped, or mappable</li>
+   * <li>Mappable means the nucleotide translation matches the protein sequence</li>
+   * <li>The translation may ignore start and stop codons if present in the
+   * nucleotide</li>
+   * </ul>
+   * 
+   * @param al1
+   * @param al2
+   * @return
+   */
+  public static boolean isMappable(AlignmentI al1, AlignmentI al2)
+  {
+    /*
+     * Require one nucleotide and one protein
+     */
+    if (al1.isNucleotide() == al2.isNucleotide())
+    {
+      return false;
+    }
+    AlignmentI dna = al1.isNucleotide() ? al1 : al2;
+    AlignmentI protein = dna == al1 ? al2 : al1;
+    Set<AlignedCodonFrame> mappings = protein.getCodonFrames();
+    for (SequenceI dnaSeq : dna.getSequences())
+    {
+      for (SequenceI proteinSeq : protein.getSequences())
+      {
+        if (isMappable(dnaSeq, proteinSeq, mappings))
+        {
+          return true;
+        }
+      }
+    }
+    return false;
+  }
+
+  /**
+   * Returns true if the dna sequence is mapped, or could be mapped, to the
+   * protein sequence.
+   * 
+   * @param dnaSeq
+   * @param proteinSeq
+   * @param mappings
+   * @return
+   */
+  public static boolean isMappable(SequenceI dnaSeq, SequenceI proteinSeq,
+          Set<AlignedCodonFrame> mappings)
+  {
+    SequenceI dnaDs = dnaSeq.getDatasetSequence() == null ? dnaSeq : dnaSeq.getDatasetSequence();
+    SequenceI proteinDs = proteinSeq.getDatasetSequence() == null ? proteinSeq
+            : proteinSeq.getDatasetSequence();
+    
+    /*
+     * Already mapped?
+     */
+    for (AlignedCodonFrame mapping : mappings) {
+      if ( proteinDs == mapping.getAaForDnaSeq(dnaDs)) {
+        return true;
+      }
+    }
+
+    /*
+     * Just try to make a mapping (it is not yet stored), test whether
+     * successful.
+     */
+    return mapProteinToCdna(proteinDs, dnaDs) != null;
+  }
+
+  /**
+   * Finds any reference annotations associated with the sequences in
+   * sequenceScope, that are not already added to the alignment, and adds them
+   * to the 'candidates' map. Also populates a lookup table of annotation
+   * labels, keyed by calcId, for use in constructing tooltips or the like.
+   * 
+   * @param sequenceScope
+   *          the sequences to scan for reference annotations
+   * @param labelForCalcId
+   *          (optional) map to populate with label for calcId
+   * @param candidates
+   *          map to populate with annotations for sequence
+   * @param al
+   *          the alignment to check for presence of annotations
+   */
+  public static void findAddableReferenceAnnotations(
+          List<SequenceI> sequenceScope, Map<String, String> labelForCalcId,
+          final Map<SequenceI, List<AlignmentAnnotation>> candidates,
+          AlignmentI al)
+  {
+    if (sequenceScope == null)
+    {
+      return;
+    }
+  
+    /*
+     * For each sequence in scope, make a list of any annotations on the
+     * underlying dataset sequence which are not already on the alignment.
+     * 
+     * Add to a map of { alignmentSequence, <List of annotations to add> }
+     */
+    for (SequenceI seq : sequenceScope)
+    {
+      SequenceI dataset = seq.getDatasetSequence();
+      if (dataset == null)
+      {
+        continue;
+      }
+      AlignmentAnnotation[] datasetAnnotations = dataset.getAnnotation();
+      if (datasetAnnotations == null)
+      {
+        continue;
+      }
+      final List<AlignmentAnnotation> result = new ArrayList<AlignmentAnnotation>();
+      for (AlignmentAnnotation dsann : datasetAnnotations)
+      {
+        /*
+         * Find matching annotations on the alignment. If none is found, then
+         * add this annotation to the list of 'addable' annotations for this
+         * sequence.
+         */
+        final Iterable<AlignmentAnnotation> matchedAlignmentAnnotations = al
+                .findAnnotations(seq, dsann.getCalcId(),
+                        dsann.label);
+        if (!matchedAlignmentAnnotations.iterator().hasNext())
+        {
+          result.add(dsann);
+          if (labelForCalcId != null)
+          {
+            labelForCalcId.put(dsann.getCalcId(), dsann.label);
+          }
+        }
+      }
+      /*
+       * Save any addable annotations for this sequence
+       */
+      if (!result.isEmpty())
+      {
+        candidates.put(seq, result);
+      }
+    }
+  }
+
+  /**
+   * Adds annotations to the top of the alignment annotations, in the same order
+   * as their related sequences.
+   * 
+   * @param annotations
+   *          the annotations to add
+   * @param alignment
+   *          the alignment to add them to
+   * @param selectionGroup
+   *          current selection group (or null if none)
+   */
+  public static void addReferenceAnnotations(
+          Map<SequenceI, List<AlignmentAnnotation>> annotations,
+          final AlignmentI alignment, final SequenceGroup selectionGroup)
+  {
+    for (SequenceI seq : annotations.keySet())
+    {
+      for (AlignmentAnnotation ann : annotations.get(seq))
+      {
+        AlignmentAnnotation copyAnn = new AlignmentAnnotation(ann);
+        int startRes = 0;
+        int endRes = ann.annotations.length;
+        if (selectionGroup != null)
+        {
+          startRes = selectionGroup.getStartRes();
+          endRes = selectionGroup.getEndRes();
+        }
+        copyAnn.restrict(startRes, endRes);
+  
+        /*
+         * Add to the sequence (sets copyAnn.datasetSequence), unless the
+         * original annotation is already on the sequence.
+         */
+        if (!seq.hasAnnotation(ann))
+        {
+          seq.addAlignmentAnnotation(copyAnn);
+        }
+        // adjust for gaps
+        copyAnn.adjustForAlignment();
+        // add to the alignment and set visible
+        alignment.addAnnotation(copyAnn);
+        copyAnn.visible = true;
+      }
+    }
+  }
+
+  /**
+   * Set visibility of alignment annotations of specified types (labels), for
+   * specified sequences. This supports controls like
+   * "Show all secondary structure", "Hide all Temp factor", etc.
+   * 
+   * @al the alignment to scan for annotations
+   * @param types
+   *          the types (labels) of annotations to be updated
+   * @param forSequences
+   *          if not null, only annotations linked to one of these sequences are
+   *          in scope for update; if null, acts on all sequence annotations
+   * @param anyType
+   *          if this flag is true, 'types' is ignored (label not checked)
+   * @param doShow
+   *          if true, set visibility on, else set off
+   */
+  public static void showOrHideSequenceAnnotations(AlignmentI al,
+          Collection<String> types, List<SequenceI> forSequences,
+          boolean anyType, boolean doShow)
+  {
+    for (AlignmentAnnotation aa : al
+            .getAlignmentAnnotation())
+    {
+      if (anyType || types.contains(aa.label))
+      {
+        if ((aa.sequenceRef != null)
+                && (forSequences == null || forSequences
+                        .contains(aa.sequenceRef)))
+        {
+          aa.visible = doShow;
+        }
+      }
+    }
+  }
 }