JAL-2210 explicit ‘doNotAdd’ list to prevent sequences retrieved from being added...
authorJim Procter <jprocter@issues.jalview.org>
Mon, 3 Oct 2016 08:16:14 +0000 (09:16 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Mon, 3 Oct 2016 08:16:14 +0000 (09:16 +0100)
src/jalview/analysis/CrossRef.java

index 71b0aa0..59ba8a3 100644 (file)
@@ -422,13 +422,16 @@ public class CrossRef
     if (retrieved != null)
     {
       boolean addedXref = false;
+      List<SequenceI> newDsSeqs = new ArrayList<SequenceI>(), doNotAdd = new ArrayList<SequenceI>();
+
       for (SequenceI retrievedSequence : retrieved)
       {
         // dataset gets contaminated ccwith non-ds sequences. why ??!
         // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
         SequenceI retrievedDss = retrievedSequence.getDatasetSequence() == null ? retrievedSequence
                 : retrievedSequence.getDatasetSequence();
-        addedXref |= importCrossRefSeq(cf, dss, retrievedDss);
+        addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
+                retrievedDss);
       }
       if (!addedXref)
       {
@@ -441,7 +444,17 @@ public class CrossRef
           // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
           SequenceI retrievedDss = retrievedSequence.getDatasetSequence() == null ? retrievedSequence
                   : retrievedSequence.getDatasetSequence();
-          addedXref |= importCrossRefSeq(cf, dss, retrievedDss);
+          addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
+                  retrievedDss);
+        }
+      }
+      for (SequenceI newToSeq : newDsSeqs)
+      {
+        if (!doNotAdd.contains(newToSeq)
+                && dataset.findIndex(newToSeq) == -1)
+        {
+          dataset.addSequence(newToSeq);
+          matcher.add(newToSeq);
         }
       }
     }
@@ -494,6 +507,7 @@ public class CrossRef
    * @return true if retrieveSequence was imported
    */
   private boolean importCrossRefSeq(AlignedCodonFrame cf,
+          List<SequenceI> newDsSeqs, List<SequenceI> doNotAdd,
           SequenceI sourceSequence, SequenceI retrievedSequence)
   {
     /**
@@ -502,7 +516,6 @@ public class CrossRef
      */
     boolean imported = false;
     DBRefEntry[] dbr = retrievedSequence.getDBRefs();
-    List<SequenceI> newDsSeqs = new ArrayList<SequenceI>();
     if (dbr != null)
     {
       for (DBRefEntry dbref : dbr)
@@ -574,9 +587,15 @@ public class CrossRef
                    */
                   for (DBRefEntry ref : toRefs)
                   {
+                    if (dbref.getSrcAccString().equals(
+                            ref.getSrcAccString()))
+                    {
+                      continue; // avoid overwriting the ref on source sequence
+                    }
                     matched.addDBRef(ref); // add or update mapping
                   }
                 }
+                doNotAdd.add(map.getTo());
                 map.setTo(matched);
 
                 /*
@@ -634,15 +653,6 @@ public class CrossRef
         dataset.addSequence(retrievedSequence);
         matcher.add(retrievedSequence);
       }
-      for (SequenceI newToSeq : newDsSeqs)
-      {
-
-        if (dataset.findIndex(newToSeq) == -1)
-        {
-          dataset.addSequence(newToSeq);
-          matcher.add(newToSeq);
-        }
-      }
     }
     return imported;
   }