JAL-1705 align CDS and peptide products to transcripts
[jalview.git] / src / jalview / ext / ensembl / EnsemblSeqProxy.java
index a2be17b..869a702 100644 (file)
@@ -30,8 +30,6 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map.Entry;
 
-import com.stevesoft.pat.Regex;
-
 /**
  * Base class for Ensembl sequence fetchers
  * 
@@ -39,12 +37,8 @@ import com.stevesoft.pat.Regex;
  */
 public abstract class EnsemblSeqProxy extends EnsemblRestClient
 {
-  // TODO modify to accept other species e.g. ENSMUSTnnn
-  private static final Regex TRANSCRIPT_REGEX = new Regex(
-          "(ENST)[0-9]{11}$");
-
   private static final List<String> CROSS_REFERENCES = Arrays
-          .asList(new String[] { "CCDS" });
+          .asList(new String[] { "CCDS", "Uniprot/SWISSPROT" });
 
   protected static final String CONSEQUENCE_TYPE = "consequence_type";
 
@@ -163,6 +157,11 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       }
     }
 
+    if (alignment == null)
+    {
+      return null;
+    }
+
     /*
      * fetch and transfer genomic sequence features,
      * fetch protein product and add as cross-reference
@@ -344,7 +343,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
     int mappedDnaLength = getCdsRanges(dnaSeq, ranges);
 
     int proteinLength = proteinSeq.getLength();
-    List<int[]> proteinRange = new ArrayList<int[]>();
+    int proteinEnd = proteinLength;
     int proteinStart = 1;
 
     /*
@@ -356,15 +355,20 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       proteinStart = 2;
       proteinLength--;
     }
-    proteinRange.add(new int[] { proteinStart, proteinLength });
+    List<int[]> proteinRange = new ArrayList<int[]>();
 
     /*
      * dna length should map to protein (or protein plus stop codon)
      */
     int codesForResidues = mappedDnaLength / 3;
-    if (codesForResidues == proteinLength
-            || codesForResidues == (proteinLength + 1))
+    if (codesForResidues == (proteinLength + 1))
+    {
+      MappingUtils.unmapStopCodon(ranges, mappedDnaLength);
+      codesForResidues--;
+    }
+    if (codesForResidues == proteinLength)
     {
+      proteinRange.add(new int[] { proteinStart, proteinEnd });
       return new MapList(ranges, proteinRange, 3, 1);
     }
     return null;
@@ -389,14 +393,14 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
     {
       return 0;
     }
+    SequenceOntologyI so = SequenceOntologyFactory.getInstance();
     int mappedDnaLength = 0;
     for (SequenceFeature sf : sfs)
     {
       /*
        * process a CDS feature (or a sub-type of CDS)
        */
-      if (SequenceOntologyFactory.getInstance().isA(sf.getType(),
-              SequenceOntologyI.CDS))
+      if (so.isA(sf.getType(), SequenceOntologyI.CDS))
       {
         int phase = 0;
         try {
@@ -411,7 +415,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
          */
         int begin = sf.getBegin();
         int end = sf.getEnd();
-        if (ranges.isEmpty() && phase > 0)
+        if (ranges.isEmpty())
         {
           begin += phase;
           if (begin > end)
@@ -1129,9 +1133,4 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
             || SequenceOntologyFactory.getInstance().isA(featureType,
                     SequenceOntologyI.TRANSCRIPT);
   }
-
-  public static boolean isTranscriptIdentifier(String query)
-  {
-    return query == null ? false : TRANSCRIPT_REGEX.search(query);
-  }
 }