JAL-1705 tests added, minor bugfix and refactoring
[jalview.git] / src / jalview / ext / ensembl / EnsemblSeqProxy.java
index fb0b01c..e241874 100644 (file)
@@ -296,29 +296,62 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    */
   protected MapList mapCdsToProtein(SequenceI dnaSeq, SequenceI proteinSeq)
   {
-    SequenceFeature[] sfs = dnaSeq.getSequenceFeatures();
-    if (sfs == null)
+    List<int[]> ranges = new ArrayList<int[]>(50);
+
+    int mappedDnaLength = getCdsRanges(dnaSeq, ranges);
+
+    int proteinLength = proteinSeq.getLength();
+    List<int[]> proteinRange = new ArrayList<int[]>();
+    int proteinStart = 1;
+
+    /*
+     * incomplete start codon may mean X at start of peptide
+     * we ignore both for mapping purposes
+     */
+    if (proteinSeq.getCharAt(0) == 'X')
     {
-      return null;
+      proteinStart = 2;
+      proteinLength--;
     }
+    proteinRange.add(new int[] { proteinStart, proteinLength });
 
-    List<int[]> ranges = new ArrayList<int[]>(50);
-    SequenceOntology so = SequenceOntology.getInstance();
-
-    int mappedDnaLength = 0;
-    
     /*
-     * Map CDS columns of dna to peptide. No need to worry about reverse strand
-     * dna here since the retrieved sequence is as transcribed (reverse
-     * complement for reverse strand), i.e in the same sense as the peptide. 
+     * dna length should map to protein (or protein plus stop codon)
      */
-    boolean fivePrimeIncomplete = false;
+    int codesForResidues = mappedDnaLength / 3;
+    if (codesForResidues == proteinLength
+            || codesForResidues == (proteinLength + 1))
+    {
+      return new MapList(ranges, proteinRange, 3, 1);
+    }
+    return null;
+  }
+
+  /**
+   * Adds CDS ranges to the ranges list, and returns the total length mapped.
+   * 
+   * No need to worry about reverse strand dna here since the retrieved sequence
+   * is as transcribed (reverse complement for reverse strand), i.e in the same
+   * sense as the peptide.
+   * 
+   * @param dnaSeq
+   * @param ranges
+   * @return
+   */
+  protected int getCdsRanges(SequenceI dnaSeq, List<int[]> ranges)
+  {
+    SequenceFeature[] sfs = dnaSeq.getSequenceFeatures();
+    if (sfs == null)
+    {
+      return 0;
+    }
+    int mappedDnaLength = 0;
     for (SequenceFeature sf : sfs)
     {
       /*
        * process a CDS feature (or a sub-type of CDS)
        */
-      if (so.isA(sf.getType(), SequenceOntology.CDS))
+      if (SequenceOntology.getInstance().isA(sf.getType(), SequenceOntology.CDS))
       {
         int phase = 0;
         try {
@@ -335,7 +368,6 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
         int end = sf.getEnd();
         if (ranges.isEmpty() && phase > 0)
         {
-          fivePrimeIncomplete = true;
           begin += phase;
           if (begin > end)
           {
@@ -346,26 +378,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
         mappedDnaLength += Math.abs(end - begin) + 1;
       }
     }
-    int proteinLength = proteinSeq.getLength();
-    List<int[]> proteinRange = new ArrayList<int[]>();
-    int proteinStart = 1;
-    if (fivePrimeIncomplete && proteinSeq.getCharAt(0) == 'X')
-    {
-      proteinStart = 2;
-      proteinLength--;
-    }
-    proteinRange.add(new int[] { proteinStart, proteinLength });
-
-    /*
-     * dna length should map to protein (or protein plus stop codon)
-     */
-    int codesForResidues = mappedDnaLength / 3;
-    if (codesForResidues == proteinLength
-            || codesForResidues == (proteinLength + 1))
-    {
-      return new MapList(ranges, proteinRange, 3, 1);
-    }
-    return null;
+    return mappedDnaLength;
   }
 
   /**
@@ -834,7 +847,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       peptide = peptide.getDatasetSequence();
     }
   
-    mapExonsToProtein(dnaSeq, peptide, dnaToProtein);
+    mapExonFeaturesToProtein(dnaSeq, peptide, dnaToProtein);
 
     LinkedHashMap<Integer, String[][]> variants = buildDnaVariantsMap(
             dnaSeq, dnaToProtein);
@@ -852,7 +865,6 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
               residue);
       if (!peptideVariants.isEmpty())
       {
-        Collections.sort(peptideVariants);
         String desc = StringUtils.listToDelimitedString(peptideVariants,
                 ", ");
         SequenceFeature sf = new SequenceFeature(
@@ -875,7 +887,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    * @param peptide
    * @param dnaToProtein
    */
-  static int mapExonsToProtein(SequenceI dnaSeq, SequenceI peptide,
+  static int mapExonFeaturesToProtein(SequenceI dnaSeq, SequenceI peptide,
           MapList dnaToProtein)
   {
     SequenceFeature[] sfs = dnaSeq.getSequenceFeatures();
@@ -930,6 +942,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       return variants;
     }
   
+    int dnaStart = dnaSeq.getStart();
     int[] lastCodon = null;
     int lastPeptidePostion = 0;
   
@@ -985,7 +998,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
         for (int codonPos = 0; codonPos < 3; codonPos++)
         {
           String nucleotide = String.valueOf(dnaSeq
-                  .getCharAt(codon[codonPos] - 1));
+                  .getCharAt(codon[codonPos] - dnaStart));
           if (codon[codonPos] == dnaCol)
           {
             /*
@@ -1011,11 +1024,11 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   }
 
   /**
-   * Returns a non-redundant list of all peptide translations generated by the
-   * given dna variants, excluding the current residue value
+   * Returns a sorted, non-redundant list of all peptide translations generated
+   * by the given dna variants, excluding the current residue value
    * 
    * @param codonVariants
-   *          an array of base values for codon positions 1, 2, 3
+   *          an array of base values (acgtACGT) for codon positions 1, 2, 3
    * @param residue
    *          the current residue translation
    * @return
@@ -1036,13 +1049,37 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
           String peptide = codon.contains("-") ? "-" : ResidueProperties
                   .codonTranslate(codon);
           if (peptide != null && !result.contains(peptide)
-                  && !peptide.equals(residue))
+                  && !peptide.equalsIgnoreCase(residue))
           {
             result.add(peptide);
           }
         }
       }
     }
+
+    /*
+     * sort alphabetically with STOP at the end
+     */
+    Collections.sort(result, new Comparator<String>()
+    {
+
+      @Override
+      public int compare(String o1, String o2)
+      {
+        if ("STOP".equals(o1))
+        {
+          return 1;
+        }
+        else if ("STOP".equals(o2))
+        {
+          return -1;
+        }
+        else
+        {
+          return o1.compareTo(o2);
+        }
+      }
+    });
     return result;
   }