Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / src / jalview / ext / ensembl / EnsemblSeqProxy.java
index fb01708..7f2fa59 100644 (file)
  */
 package jalview.ext.ensembl;
 
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.json.simple.parser.ParseException;
+
 import jalview.analysis.AlignmentUtils;
 import jalview.analysis.Dna;
-import jalview.bin.Cache;
+import jalview.bin.Console;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
@@ -40,18 +51,6 @@ import jalview.util.Comparison;
 import jalview.util.DBRefUtils;
 import jalview.util.IntRangeComparator;
 import jalview.util.MapList;
-import jalview.util.Platform;
-
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-
-import org.json.simple.parser.ParseException;
 
 /**
  * Base class for Ensembl sequence fetchers
@@ -171,7 +170,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
      * fetch and transfer genomic sequence features,
      * fetch protein product and add as cross-reference
      */
-    for (int i = 0, n = allIds.size(); i < n; i++) 
+    for (int i = 0, n = allIds.size(); i < n; i++)
     {
       addFeaturesAndProduct(allIds.get(i), alignment);
     }
@@ -209,20 +208,19 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
        */
       SequenceI genomicSequence = null;
       EnsemblFeatures gffFetcher = new EnsemblFeatures(getDomain());
-      EnsemblFeatureType[] features = getFeaturesToFetch();      
-      
-      Platform.timeCheck("ESP.getsequencerec1", Platform.TIME_MARK);     
+      EnsemblFeatureType[] features = getFeaturesToFetch();
+
+      // Platform.timeCheck("ESP.getsequencerec1", Platform.TIME_MARK);
 
-      
       AlignmentI geneFeatures = gffFetcher.getSequenceRecords(accId,
               features);
       if (geneFeatures != null && geneFeatures.getHeight() > 0)
       {
         genomicSequence = geneFeatures.getSequenceAt(0);
       }
-      
-      Platform.timeCheck("ESP.getsequencerec2", Platform.TIME_MARK);     
-      
+
+      // Platform.timeCheck("ESP.getsequencerec2", Platform.TIME_MARK);
+
       if (genomicSequence != null)
       {
         /*
@@ -236,7 +234,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
            * fetch and map protein product, and add it as a cross-reference
            * of the retrieved sequence
            */
-            Platform.timeCheck("ESP.transferFeatures", Platform.TIME_MARK);      
+          // Platform.timeCheck("ESP.transferFeatures", Platform.TIME_MARK);
           addProteinProduct(querySeq);
         }
       }
@@ -245,7 +243,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       System.err.println(
               "Error transferring Ensembl features: " + e.getMessage());
     }
-    Platform.timeCheck("ESP.addfeat done", Platform.TIME_MARK);          
+    // Platform.timeCheck("ESP.addfeat done", Platform.TIME_MARK);
   }
 
   /**
@@ -297,8 +295,8 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
         List<DBRefEntry> uprots = DBRefUtils.selectRefs(ds.getDBRefs(),
                 new String[]
                 { DBRefSource.UNIPROT });
-        List<DBRefEntry> upxrefs = DBRefUtils.selectRefs(querySeq.getDBRefs(),
-                new String[]
+        List<DBRefEntry> upxrefs = DBRefUtils
+                .selectRefs(querySeq.getDBRefs(), new String[]
                 { DBRefSource.UNIPROT });
         if (uprots != null)
         {
@@ -314,7 +312,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
 
               if (upx.size() > 1)
               {
-                Cache.log.warn(
+                Console.warn(
                         "Implementation issue - multiple uniprot acc on product sequence.");
               }
             }
@@ -340,8 +338,8 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
          * copy exon features to protein, compute peptide variants from dna 
          * variants and add as features on the protein sequence ta-da
          */
-        AlignmentUtils.computeProteinFeatures(querySeq, proteinSeq,
-                mapList);
+        // JAL-3187 render on the fly instead
+        // AlignmentUtils.computeProteinFeatures(querySeq, proteinSeq, mapList);
       }
     } catch (Exception e)
     {
@@ -358,47 +356,47 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    */
   protected void getCrossReferences(SequenceI seq)
   {
-         
-      Platform.timeCheck("ESP. getdataseq ", Platform.TIME_MARK);        
 
-         
+    // Platform.timeCheck("ESP. getdataseq ", Platform.TIME_MARK);
+
     while (seq.getDatasetSequence() != null)
     {
       seq = seq.getDatasetSequence();
     }
 
-    Platform.timeCheck("ESP. getxref ", Platform.TIME_MARK);     
+    // Platform.timeCheck("ESP. getxref ", Platform.TIME_MARK);
 
     EnsemblXref xrefFetcher = new EnsemblXref(getDomain(), getDbSource(),
             getEnsemblDataVersion());
     List<DBRefEntry> xrefs = xrefFetcher.getCrossReferences(seq.getName());
-    
+
     for (int i = 0, n = xrefs.size(); i < n; i++)
     {
-//        Platform.timeCheck("ESP. getxref + " + (i) + "/" + n, Platform.TIME_MARK);     
-        // BH 2019.01.25 this next method was taking 174 ms PER addition for a 266-reference example.
-        //    DBRefUtils.ensurePrimaries(seq) 
-        //        was at the end of seq.addDBRef, so executed after ever addition!
-        //        This method was moved to     seq.getPrimaryDBRefs()
+      // Platform.timeCheck("ESP. getxref + " + (i) + "/" + n,
+      // Platform.TIME_MARK);
+      // BH 2019.01.25 this next method was taking 174 ms PER addition for a
+      // 266-reference example.
+      // DBRefUtils.ensurePrimaries(seq)
+      // was at the end of seq.addDBRef, so executed after ever addition!
+      // This method was moved to seq.getPrimaryDBRefs()
       seq.addDBRef(xrefs.get(i));
     }
 
-//    System.out.println("primaries are " + seq.getPrimaryDBRefs().toString());
+    // System.out.println("primaries are " + seq.getPrimaryDBRefs().toString());
     /*
      * and add a reference to itself
      */
-    
-//    Platform.timeCheck("ESP. getxref self ", Platform.TIME_MARK);      
+
+    // Platform.timeCheck("ESP. getxref self ", Platform.TIME_MARK);
 
     DBRefEntry self = new DBRefEntry(getDbSource(), getEnsemblDataVersion(),
-    seq.getName());
+            seq.getName());
 
-//    Platform.timeCheck("ESP. getxref self add ", Platform.TIME_MARK);          
+    // Platform.timeCheck("ESP. getxref self add ", Platform.TIME_MARK);
 
     seq.addDBRef(self);
-    
-    Platform.timeCheck("ESP. seqprox done ", Platform.TIME_MARK);        
 
+    // Platform.timeCheck("ESP. seqprox done ", Platform.TIME_MARK);
   }
 
   /**
@@ -419,11 +417,11 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       inProgress = false;
       throw new JalviewException("ENSEMBL Rest API not available.");
     }
-       Platform.timeCheck("EnsemblSeqProx.fetchSeq ", Platform.TIME_MARK);
+    // Platform.timeCheck("EnsemblSeqProx.fetchSeq ", Platform.TIME_MARK);
 
     List<SequenceI> seqs = parseSequenceJson(ids);
     if (seqs == null)
-       return alignment;
+      return alignment;
 
     if (seqs.isEmpty())
     {
@@ -486,11 +484,11 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
        * for now, assumes only one sequence returned; refactor if needed
        * in future to handle a JSONArray with more than one
        */
-       
-       Platform.timeCheck("ENS seqproxy", Platform.TIME_MARK);
-      Map<String, Object> val = (Map<String, Object>) getJSON(null, ids, -1, MODE_MAP, null);
+      // Platform.timeCheck("ENS seqproxy", Platform.TIME_MARK);
+      Map<String, Object> val = (Map<String, Object>) getJSON(null, ids, -1,
+              MODE_MAP, null);
       if (val == null)
-         return null;
+        return null;
       Object s = val.get("desc");
       String desc = s == null ? null : s.toString();
       s = val.get("id");
@@ -513,7 +511,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       System.err.println("Error processing JSON response: " + e.toString());
       // ignore
     }
-       Platform.timeCheck("ENS seqproxy2", Platform.TIME_MARK);
+    // Platform.timeCheck("ENS seqproxy2", Platform.TIME_MARK);
     return result;
   }
 
@@ -693,9 +691,9 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    */
   protected abstract List<SequenceFeature> getIdentifyingFeatures(
           SequenceI seq, String accId);
-  
+
   int bhtest = 0;
-  
+
   /**
    * Transfers the sequence feature to the target sequence, locating its start
    * and end range based on the mapping. Features which do not overlap the
@@ -717,7 +715,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
 
     if (mappedRange != null)
     {
-//      Platform.timeCheck(null, Platform.TIME_SET);
+      // Platform.timeCheck(null, Platform.TIME_SET);
       String group = sf.getFeatureGroup();
       if (".".equals(group))
       {
@@ -725,10 +723,11 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       }
       int newBegin = Math.min(mappedRange[0], mappedRange[1]);
       int newEnd = Math.max(mappedRange[0], mappedRange[1]);
-//      Platform.timeCheck(null, Platform.TIME_MARK);
+      // Platform.timeCheck(null, Platform.TIME_MARK);
       bhtest++;
       // 280 ms/1000 here:
-      SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd, group, sf.getScore());
+      SequenceFeature copy = new SequenceFeature(sf, newBegin, newEnd,
+              group, sf.getScore());
       // 0.175 ms here:
       targetSequence.addSequenceFeature(copy);
 
@@ -736,7 +735,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
        * for sequence_variant on reverse strand, have to convert the allele
        * values to their complements
        */
-      if (!forwardStrand && SequenceOntologyFactory.getInstance()
+      if (!forwardStrand && SequenceOntologyFactory.getSequenceOntology()
               .isA(sf.getType(), SequenceOntologyI.SEQUENCE_VARIANT))
       {
         reverseComplementAlleles(copy);
@@ -765,18 +764,6 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
     String comp = complement.toString();
     sf.setValue(Gff3Helper.ALLELES, comp);
     sf.setDescription(comp);
-
-    /*
-     * replace value of "alleles=" in sf.ATTRIBUTES as well
-     * so 'output as GFF' shows reverse complement alleles
-     */
-    String atts = sf.getAttributes();
-    if (atts != null)
-    {
-      atts = atts.replace(Gff3Helper.ALLELES + "=" + alleles,
-              Gff3Helper.ALLELES + "=" + comp);
-      sf.setAttributes(atts);
-    }
   }
 
   /**
@@ -828,26 +815,24 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       return false;
     }
 
-//    long start = System.currentTimeMillis();
+    // long start = System.currentTimeMillis();
     List<SequenceFeature> sfs = sourceSequence.getFeatures()
             .getPositionalFeatures();
     MapList mapping = getGenomicRangesFromFeatures(sourceSequence,
             accessionId, targetSequence.getStart());
     if (mapping == null)
-    { 
+    {
       return false;
     }
 
-
-    Platform.timeCheck("ESP. xfer " + sfs.size(), Platform.TIME_MARK);   
-
+    // Platform.timeCheck("ESP. xfer " + sfs.size(), Platform.TIME_MARK);
 
     boolean result = transferFeatures(sfs, targetSequence, mapping,
             accessionId);
-//    System.out.println("transferFeatures (" + (sfs.size()) + " --> "
-//            + targetSequence.getFeatures().getFeatureCount(true) + ") to "
-//            + targetSequence.getName() + " took "
-//            + (System.currentTimeMillis() - start) + "ms");
+    // System.out.println("transferFeatures (" + (sfs.size()) + " --> "
+    // + targetSequence.getFeatures().getFeatureCount(true) + ") to "
+    // + targetSequence.getName() + " took "
+    // + (System.currentTimeMillis() - start) + "ms");
     return result;
   }
 
@@ -875,14 +860,15 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
     SequenceFeatures.sortFeatures(sfs, forwardStrand);
 
     boolean transferred = false;
-    
+
     for (int i = 0, n = sfs.size(); i < n; i++)
     {
 
-       if ((i%1000) == 0) {
-//         Platform.timeCheck("Feature " + bhtest, Platform.TIME_GET);
-       Platform.timeCheck("ESP. xferFeature + " + (i) + "/" + n, Platform.TIME_MARK);    
-       }
+      // if ((i%1000) == 0) {
+      //// Platform.timeCheck("Feature " + bhtest, Platform.TIME_GET);
+      // Platform.timeCheck("ESP. xferFeature + " + (i) + "/" + n,
+      // Platform.TIME_MARK);
+      // }
 
       SequenceFeature sf = sfs.get(i);
       if (retainFeature(sf, parentId))
@@ -919,8 +905,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   protected boolean featureMayBelong(SequenceFeature sf, String identifier)
   {
     String parent = (String) sf.getValue(PARENT);
-    if (parent != null
-            && !parent.equalsIgnoreCase(identifier))
+    if (parent != null && !parent.equalsIgnoreCase(identifier))
     {
       // this genomic feature belongs to a different transcript
       return false;
@@ -984,7 +969,7 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   public static boolean isTranscript(String featureType)
   {
     return SequenceOntologyI.NMD_TRANSCRIPT_VARIANT.equals(featureType)
-            || SequenceOntologyFactory.getInstance().isA(featureType,
+            || SequenceOntologyFactory.getSequenceOntology().isA(featureType,
                     SequenceOntologyI.TRANSCRIPT);
   }
 }