JAL-1705 various refactoring towards Uniprot-to-Ensembl fetching
[jalview.git] / src / jalview / analysis / CrossRef.java
index 21fd08d..d45750e 100644 (file)
@@ -27,6 +27,7 @@ import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.DBRefSource;
 import jalview.datamodel.Mapping;
 import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.util.DBRefUtils;
 import jalview.ws.SequenceFetcher;
@@ -45,6 +46,27 @@ import java.util.Vector;
  */
 public class CrossRef
 {
+  /*
+   * A sub-class that ignores Parent attribute when comparing sequence 
+   * features. This avoids 'duplicate' CDS features that only
+   * differ in their parent Transcript ids.
+   */
+  class MySequenceFeature extends SequenceFeature
+  {
+    private SequenceFeature feat;
+
+    MySequenceFeature(SequenceFeature sf)
+    {
+      this.feat = sf;
+    }
+
+    @Override
+    public boolean equals(Object o)
+    {
+      return feat.equals(o, true);
+    }
+  }
+
   /**
    * Select just the DNA or protein references for a protein or dna sequence
    * 
@@ -337,8 +359,11 @@ public class CrossRef
                               + seq.getName());
               e.printStackTrace();
             }
+
             if (retrieved != null)
             {
+              List<SequenceFeature> copiedFeatures = new ArrayList<SequenceFeature>();
+              CrossRef me = new CrossRef();
               for (int rs = 0; rs < retrieved.length; rs++)
               {
                 // TODO: examine each sequence for 'redundancy'
@@ -369,12 +394,41 @@ public class CrossRef
                                   && mappedrg.getSequenceAsString().equals(
                                           loc.getSequenceAsString()))
                           {
-                            System.err
-                                    .println("Mapping updated for retrieved crossreference");
+                            String msg = "Mapping updated from "
+                                    + ms.getName()
+                                    + " to retrieved crossreference "
+                                    + dss.getName();
+                            System.out.println(msg);
                             // method to update all refs of existing To on
                             // retrieved sequence with dss and merge any props
                             // on To onto dss.
                             map.setTo(dss);
+                            /*
+                             * copy sequence features as well, avoiding
+                             * duplication (e.g. from 2 transcripts)
+                             */
+                            SequenceFeature[] sfs = ms
+                                    .getSequenceFeatures();
+                            if (sfs != null)
+                            {
+                              for (SequenceFeature feat : sfs)
+                              {
+                                /* 
+                                 * we override the equality test here (but not
+                                 * elsewhere) to ignore Parent attribute
+                                 * TODO not quite working yet!
+                                 */
+                                if (!copiedFeatures
+                                        .contains(me.new MySequenceFeature(
+                                                feat)))
+                                {
+                                  dss.addSequenceFeature(feat);
+                                  copiedFeatures.add(feat);
+                                }
+                              }
+                            }
+                            cf.addMap(retrieved[rs].getDatasetSequence(),
+                                    dss, map.getMap());
                           }
                         } catch (Exception e)
                         {