JAL-2480 renamed java.util.RangeComparator to IntRangeComparator
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 12 May 2017 13:22:03 +0000 (14:22 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Fri, 12 May 2017 13:22:03 +0000 (14:22 +0100)
src/jalview/ext/ensembl/EnsemblSeqProxy.java
src/jalview/ext/rbvi/chimera/AtomSpecModel.java
src/jalview/util/RangeComparator.java [deleted file]

index 2d0e3fe..dda77d7 100644 (file)
@@ -30,6 +30,7 @@ import jalview.datamodel.DBRefSource;
 import jalview.datamodel.Mapping;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.SequenceFeatures;
 import jalview.exceptions.JalviewException;
 import jalview.io.FastaFile;
 import jalview.io.FileParse;
@@ -37,8 +38,8 @@ import jalview.io.gff.SequenceOntologyFactory;
 import jalview.io.gff.SequenceOntologyI;
 import jalview.util.Comparison;
 import jalview.util.DBRefUtils;
+import jalview.util.IntRangeComparator;
 import jalview.util.MapList;
-import jalview.util.RangeComparator;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
@@ -46,7 +47,6 @@ import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 
 /**
@@ -536,8 +536,10 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   protected MapList getGenomicRangesFromFeatures(SequenceI sourceSequence,
           String accId, int start)
   {
-    SequenceFeature[] sfs = sourceSequence.getSequenceFeatures();
-    if (sfs == null)
+    // SequenceFeature[] sfs = sourceSequence.getSequenceFeatures();
+    List<SequenceFeature> sfs = sourceSequence.getFeatures()
+            .getPositionalFeatures();
+    if (sfs.isEmpty())
     {
       return null;
     }
@@ -607,7 +609,8 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
      * a final sort is needed since Ensembl returns CDS sorted within source
      * (havana / ensembl_havana)
      */
-    Collections.sort(regions, new RangeComparator(direction == 1));
+    Collections.sort(regions, direction == 1 ? IntRangeComparator.ASCENDING
+            : IntRangeComparator.DESCENDING);
 
     List<int[]> to = Arrays.asList(new int[] { start,
         start + mappedLength - 1 });
@@ -765,8 +768,10 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
       return false;
     }
 
-    // long start = System.currentTimeMillis();
-    SequenceFeature[] sfs = sourceSequence.getSequenceFeatures();
+    long start = System.currentTimeMillis();
+    // SequenceFeature[] sfs = sourceSequence.getSequenceFeatures();
+    List<SequenceFeature> sfs = sourceSequence.getFeatures()
+            .getPositionalFeatures();
     MapList mapping = getGenomicRangesFromFeatures(sourceSequence,
             accessionId, targetSequence.getStart());
     if (mapping == null)
@@ -776,10 +781,10 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
 
     boolean result = transferFeatures(sfs, targetSequence, mapping,
             accessionId);
-    // System.out.println("transferFeatures (" + (sfs.length) + " --> "
-    // + targetSequence.getSequenceFeatures().length + ") 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;
   }
 
@@ -788,13 +793,13 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
    * converted using the mapping. Features which do not overlap are ignored.
    * Features whose parent is not the specified identifier are also ignored.
    * 
-   * @param features
+   * @param sfs
    * @param targetSequence
    * @param mapping
    * @param parentId
    * @return
    */
-  protected boolean transferFeatures(SequenceFeature[] features,
+  protected boolean transferFeatures(List<SequenceFeature> sfs,
           SequenceI targetSequence, MapList mapping, String parentId)
   {
     final boolean forwardStrand = mapping.isFromForwardStrand();
@@ -804,10 +809,10 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
      * position descending if reverse strand) so as to add them in
      * 'forwards' order to the target sequence
      */
-    sortFeatures(features, forwardStrand);
+    SequenceFeatures.sortFeatures(sfs, forwardStrand);
 
     boolean transferred = false;
-    for (SequenceFeature sf : features)
+    for (SequenceFeature sf : sfs)
     {
       if (retainFeature(sf, parentId))
       {
@@ -819,33 +824,6 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
   }
 
   /**
-   * Sort features by start position ascending (if on forward strand), or end
-   * position descending (if on reverse strand)
-   * 
-   * @param features
-   * @param forwardStrand
-   */
-  protected static void sortFeatures(SequenceFeature[] features,
-          final boolean forwardStrand)
-  {
-    Arrays.sort(features, new Comparator<SequenceFeature>()
-    {
-      @Override
-      public int compare(SequenceFeature o1, SequenceFeature o2)
-      {
-        if (forwardStrand)
-        {
-          return Integer.compare(o1.getBegin(), o2.getBegin());
-        }
-        else
-        {
-          return Integer.compare(o2.getEnd(), o1.getEnd());
-        }
-      }
-    });
-  }
-
-  /**
    * Answers true if the feature type is one we want to keep for the sequence.
    * Some features are only retrieved in order to identify the sequence range,
    * and may then be discarded as redundant information (e.g. "CDS" feature for
@@ -887,35 +865,30 @@ public abstract class EnsemblSeqProxy extends EnsemblRestClient
 
   /**
    * Returns a (possibly empty) list of features on the sequence which have the
-   * specified sequence ontology type (or a sub-type of it), and the given
+   * specified sequence ontology term (or a sub-type of it), and the given
    * identifier as parent
    * 
    * @param sequence
-   * @param type
+   * @param term
    * @param parentId
    * @return
    */
   protected List<SequenceFeature> findFeatures(SequenceI sequence,
-          String type, String parentId)
+          String term, String parentId)
   {
     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
 
-    SequenceFeature[] sfs = sequence.getSequenceFeatures();
-    if (sfs != null)
+    List<SequenceFeature> sfs = sequence.getFeatures()
+            .getFeaturesByOntology(term);
+    for (SequenceFeature sf : sfs)
     {
-      SequenceOntologyI so = SequenceOntologyFactory.getInstance();
-      for (SequenceFeature sf : sfs)
+      String parent = (String) sf.getValue(PARENT);
+      if (parent != null && parent.equals(parentId))
       {
-        if (so.isA(sf.getType(), type))
-        {
-          String parent = (String) sf.getValue(PARENT);
-          if (parent.equals(parentId))
-          {
-            result.add(sf);
-          }
-        }
+        result.add(sf);
       }
     }
+
     return result;
   }
 
index d62cc3c..f3c9c1e 100644 (file)
@@ -1,6 +1,6 @@
 package jalview.ext.rbvi.chimera;
 
-import jalview.util.RangeComparator;
+import jalview.util.IntRangeComparator;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -107,7 +107,7 @@ public class AtomSpecModel
         /*
          * sort ranges into ascending start position order
          */
-        Collections.sort(rangeList, new RangeComparator(true));
+        Collections.sort(rangeList, IntRangeComparator.ASCENDING);
 
         int start = rangeList.isEmpty() ? 0 : rangeList.get(0)[0];
         int end = rangeList.isEmpty() ? 0 : rangeList.get(0)[1];
diff --git a/src/jalview/util/RangeComparator.java b/src/jalview/util/RangeComparator.java
deleted file mode 100644 (file)
index f911a9b..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-package jalview.util;
-
-import java.util.Comparator;
-
-/**
- * A comparator to order [from, to] ranges into ascending or descending order of
- * their start position
- */
-public class RangeComparator implements Comparator<int[]>
-{
-  boolean forwards;
-
-  public RangeComparator(boolean forward)
-  {
-    forwards = forward;
-  }
-
-  @Override
-  public int compare(int[] o1, int[] o2)
-  {
-    int compared = Integer.compare(o1[0], o2[0]);
-    return forwards ? compared : -compared;
-  }
-
-}
\ No newline at end of file