Merge branch 'feature/JAL-3187linkedFeatures' into feature/JAL-3251biotypedMappings
[jalview.git] / src / jalview / datamodel / AlignedCodonFrame.java
index ec11fc1..7845ddc 100644 (file)
@@ -34,89 +34,14 @@ import java.util.List;
 public class AlignedCodonFrame
 {
 
-  /*
-   * Data bean to hold mappings from one sequence to another
-   */
-  public class SequenceToSequenceMapping
-  {
-    private SequenceI fromSeq;
-
-    private Mapping mapping;
-
-    SequenceToSequenceMapping(SequenceI from, Mapping map)
-    {
-      this.fromSeq = from;
-      this.mapping = map;
-    }
-
-    /**
-     * Readable representation for debugging only, not guaranteed not to change
-     */
-    @Override
-    public String toString()
-    {
-      return String.format("From %s %s", fromSeq.getName(),
-              mapping.toString());
-    }
-
-    /**
-     * Returns a hashCode derived from the hashcodes of the mappings and fromSeq
-     * 
-     * @see SequenceToSequenceMapping#hashCode()
-     */
-    @Override
-    public int hashCode()
-    {
-      return (fromSeq == null ? 0 : fromSeq.hashCode() * 31)
-              + mapping.hashCode();
-    }
-
-    /**
-     * Answers true if the objects hold the same mapping between the same two
-     * sequences
-     * 
-     * @see Mapping#equals
-     */
-    @Override
-    public boolean equals(Object obj)
-    {
-      if (!(obj instanceof SequenceToSequenceMapping))
-      {
-        return false;
-      }
-      SequenceToSequenceMapping that = (SequenceToSequenceMapping) obj;
-      if (this.mapping == null)
-      {
-        return that.mapping == null;
-      }
-      // TODO: can simplify by asserting fromSeq is a dataset sequence
-      return (this.fromSeq == that.fromSeq
-              || (this.fromSeq != null && that.fromSeq != null
-                      && this.fromSeq.getDatasetSequence() != null
-                      && this.fromSeq.getDatasetSequence() == that.fromSeq
-                              .getDatasetSequence()))
-              && this.mapping.equals(that.mapping);
-    }
-
-    public SequenceI getFromSeq()
-    {
-      return fromSeq;
-    }
-
-    public Mapping getMapping()
-    {
-      return mapping;
-    }
-  }
-
-  private List<SequenceToSequenceMapping> mappings;
+  private List<SequenceMapping> mappings;
 
   /**
    * Constructor
    */
   public AlignedCodonFrame()
   {
-    mappings = new ArrayList<SequenceToSequenceMapping>();
+    mappings = new ArrayList<>();
   }
 
   /**
@@ -158,7 +83,7 @@ public class AlignedCodonFrame
      * note that 'adding' a duplicate map does nothing; this protects against
      * creating duplicate mappings in AlignedCodonFrame
      */
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.fromSeq == fromSeq && ssm.mapping.to == toSeq)
       {
@@ -172,15 +97,15 @@ public class AlignedCodonFrame
      */
     Mapping mp = new Mapping(toSeq, map);
     mp.setMappedFromId(mapFromId);
-    mappings.add(new SequenceToSequenceMapping(fromSeq, mp));
+    mappings.add(new SequenceMapping(fromSeq, mp));
   }
 
   public SequenceI[] getdnaSeqs()
   {
     // TODO return a list instead?
     // return dnaSeqs;
-    List<SequenceI> seqs = new ArrayList<SequenceI>();
-    for (SequenceToSequenceMapping ssm : mappings)
+    List<SequenceI> seqs = new ArrayList<>();
+    for (SequenceMapping ssm : mappings)
     {
       seqs.add(ssm.fromSeq);
     }
@@ -190,8 +115,8 @@ public class AlignedCodonFrame
   public SequenceI[] getAaSeqs()
   {
     // TODO not used - remove?
-    List<SequenceI> seqs = new ArrayList<SequenceI>();
-    for (SequenceToSequenceMapping ssm : mappings)
+    List<SequenceI> seqs = new ArrayList<>();
+    for (SequenceMapping ssm : mappings)
     {
       seqs.add(ssm.mapping.to);
     }
@@ -200,8 +125,8 @@ public class AlignedCodonFrame
 
   public MapList[] getdnaToProt()
   {
-    List<MapList> maps = new ArrayList<MapList>();
-    for (SequenceToSequenceMapping ssm : mappings)
+    List<MapList> maps = new ArrayList<>();
+    for (SequenceMapping ssm : mappings)
     {
       maps.add(ssm.mapping.map);
     }
@@ -210,8 +135,8 @@ public class AlignedCodonFrame
 
   public Mapping[] getProtMappings()
   {
-    List<Mapping> maps = new ArrayList<Mapping>();
-    for (SequenceToSequenceMapping ssm : mappings)
+    List<Mapping> maps = new ArrayList<>();
+    for (SequenceMapping ssm : mappings)
     {
       maps.add(ssm.mapping);
     }
@@ -225,16 +150,20 @@ public class AlignedCodonFrame
    * @param seq
    * @return
    */
-  public Mapping getMappingForSequence(SequenceI seq)
+  public Mapping getMappingForSequence(SequenceI seq, boolean cdsOnly)
   {
     SequenceI seqDs = seq.getDatasetSequence();
     seqDs = seqDs != null ? seqDs : seq;
 
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.fromSeq == seqDs || ssm.mapping.to == seqDs)
       {
-        return ssm.mapping;
+        if (!cdsOnly || ssm.fromSeq.getName().startsWith("CDS")
+                || ssm.mapping.to.getName().startsWith("CDS"))
+        {
+          return ssm.mapping;
+        }
       }
     }
     return null;
@@ -250,7 +179,7 @@ public class AlignedCodonFrame
   public SequenceI getAaForDnaSeq(SequenceI dnaSeqRef)
   {
     SequenceI dnads = dnaSeqRef.getDatasetSequence();
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.fromSeq == dnaSeqRef || ssm.fromSeq == dnads)
       {
@@ -268,7 +197,7 @@ public class AlignedCodonFrame
   public SequenceI getDnaForAaSeq(SequenceI aaSeqRef)
   {
     SequenceI aads = aaSeqRef.getDatasetSequence();
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.mapping.to == aaSeqRef || ssm.mapping.to == aads)
       {
@@ -306,7 +235,7 @@ public class AlignedCodonFrame
   {
     int[] codon;
     SequenceI ds = seq.getDatasetSequence();
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.fromSeq == seq || ssm.fromSeq == ds)
       {
@@ -357,7 +286,7 @@ public class AlignedCodonFrame
      */
     MapList ml = null;
     int i = 0;
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.fromSeq == seq)
       {
@@ -384,7 +313,7 @@ public class AlignedCodonFrame
     /*
      * Search mapped protein ('to') sequences first.
      */
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.fromSeq == seq || ssm.fromSeq == seq.getDatasetSequence())
       {
@@ -402,7 +331,7 @@ public class AlignedCodonFrame
     /*
      * Then try mapped dna sequences.
      */
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.mapping.to == seq
               || ssm.mapping.to == seq.getDatasetSequence())
@@ -441,7 +370,7 @@ public class AlignedCodonFrame
     {
       return null;
     }
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       /*
        * try mapping from target to query
@@ -485,9 +414,9 @@ public class AlignedCodonFrame
   {
     MapList ml = null;
     SequenceI dnaSeq = null;
-    List<char[]> result = new ArrayList<char[]>();
+    List<char[]> result = new ArrayList<>();
 
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       if (ssm.mapping.to == protein
               && ssm.mapping.getMap().getFromRatio() == 3)
@@ -524,12 +453,12 @@ public class AlignedCodonFrame
    */
   public List<Mapping> getMappingsFromSequence(SequenceI seq)
   {
-    List<Mapping> result = new ArrayList<Mapping>();
-    List<SequenceI> related = new ArrayList<SequenceI>();
+    List<Mapping> result = new ArrayList<>();
+    List<SequenceI> related = new ArrayList<>();
     SequenceI seqDs = seq.getDatasetSequence();
     seqDs = seqDs != null ? seqDs : seq;
 
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       final Mapping mapping = ssm.mapping;
       if (ssm.fromSeq == seqDs)
@@ -589,7 +518,7 @@ public class AlignedCodonFrame
     /*
      * check for replaceable DNA ('map from') sequences
      */
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     {
       SequenceI dna = ssm.fromSeq;
       if (dna instanceof SequenceDummy
@@ -682,7 +611,7 @@ public class AlignedCodonFrame
     }
     SequenceI ds = seq.getDatasetSequence();
 
-    for (SequenceToSequenceMapping ssm : mappings)
+    for (SequenceMapping ssm : mappings)
     /*
      * 'from' sequences
      */
@@ -738,7 +667,7 @@ public class AlignedCodonFrame
     SequenceI dssTo = toSeq.getDatasetSequence() == null ? toSeq
             : toSeq.getDatasetSequence();
 
-    for (SequenceToSequenceMapping mapping : mappings)
+    for (SequenceMapping mapping : mappings)
     {
       SequenceI from = mapping.fromSeq;
       SequenceI to = mapping.mapping.to;
@@ -754,7 +683,7 @@ public class AlignedCodonFrame
   /**
    * Returns a hashcode derived from the list of sequence mappings
    * 
-   * @see SequenceToSequenceMapping#hashCode()
+   * @see SequenceMapping#hashCode()
    * @see AbstractList#hashCode()
    */
   @Override
@@ -779,7 +708,7 @@ public class AlignedCodonFrame
     return this.mappings.equals(((AlignedCodonFrame) obj).mappings);
   }
 
-  public List<SequenceToSequenceMapping> getMappings()
+  public List<SequenceMapping> getMappings()
   {
     return mappings;
   }