JAL-2110 remove hardwired detection of search context from the dataset ‘isNucleotide...
[jalview.git] / src / jalview / analysis / CrossRef.java
index 9fd87df..d96ab58 100644 (file)
@@ -54,12 +54,6 @@ public class CrossRef
   private AlignmentI dataset;
 
   /*
-   * true if we are searching for cross-references from nucleotide,
-   * i.e. for protein sequences, false if the reverse
-   */
-  private boolean fromDna;
-
-  /*
    * the sequences for which we are seeking cross-references
    */
   private SequenceI[] fromSeqs;
@@ -76,7 +70,6 @@ public class CrossRef
   public CrossRef(SequenceI[] seqs, AlignmentI ds)
   {
     fromSeqs = seqs;
-    fromDna = ds.isNucleotide();
     dataset = ds.getDataset() == null ? ds : ds.getDataset();
   }
 
@@ -88,16 +81,20 @@ public class CrossRef
    * reference from another sequence in the dataset which has a cross-reference
    * to a direct DBRefEntry on the given sequence</li>
    * </ul>
+   * 
+   * @param dna
+   *          - when true, cross-references *from* dna returned. When false,
+   *          cross-references *from* protein are returned
    * @return
    */
-  public List<String> findXrefSourcesForSequences()
+  public List<String> findXrefSourcesForSequences(boolean dna)
   {
     List<String> sources = new ArrayList<String>();
     for (SequenceI seq : fromSeqs)
     {
       if (seq != null)
       {
-        findXrefSourcesForSequence(seq, sources);
+        findXrefSourcesForSequence(seq, dna, sources);
       }
     }
     return sources;
@@ -117,7 +114,8 @@ public class CrossRef
    * @param sources
    *          a list of sources to add matches to
    */
-  void findXrefSourcesForSequence(SequenceI seq, List<String> sources)
+  void findXrefSourcesForSequence(SequenceI seq, boolean fromDna,
+          List<String> sources)
   {
     /*
      * first find seq's xrefs (dna-to-peptide or peptide-to-dna)
@@ -136,7 +134,7 @@ public class CrossRef
        * find sequences in the alignment which xref one of these DBRefs
        * i.e. is xref-ed to a common sequence identifier
        */
-      searchDatasetXrefs(seq, lrfs, rseqs, null);
+      searchDatasetXrefs(fromDna, seq, lrfs, rseqs, null);
 
       /*
        * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
@@ -194,7 +192,7 @@ public class CrossRef
    * @param source
    * @return cross-referenced sequences (as dataset sequences)
    */
-  public Alignment findXrefSequences(String source)
+  public Alignment findXrefSequences(String source, boolean fromDna)
   {
 
     List<SequenceI> rseqs = new ArrayList<SequenceI>();
@@ -226,7 +224,7 @@ public class CrossRef
          *  which have a dbref to an accession id for this sequence,
          *  and add them to the results
          */
-        found = searchDatasetXrefs(dss, lrfs, rseqs, cf);
+        found = searchDatasetXrefs(fromDna, dss, lrfs, rseqs, cf);
       }
       if (xrfs == null && !found)
       {
@@ -297,7 +295,7 @@ public class CrossRef
                   + xref.getAccessionId());
           if (matchedSeq != null)
           {
-            if (constructMapping(seq, matchedSeq, xref, cf))
+            if (constructMapping(seq, matchedSeq, xref, cf, fromDna))
             {
               found = true;
             }
@@ -308,7 +306,7 @@ public class CrossRef
         {
           // do a bit more work - search for sequences with references matching
           // xrefs on this sequence.
-          found = searchDataset(dss, xref, rseqs, cf, false);
+          found = searchDataset(fromDna, dss, xref, rseqs, cf, false);
         }
         if (found)
         {
@@ -337,7 +335,7 @@ public class CrossRef
 
         if (retrieved != null)
         {
-          updateDbrefMappings(seq, xrfs, retrieved, cf);
+          updateDbrefMappings(seq, xrfs, retrieved, cf, fromDna);
           for (SequenceI retrievedSequence : retrieved)
           {
             SequenceI retrievedDss = retrievedSequence.getDatasetSequence() == null ? retrievedSequence
@@ -595,8 +593,8 @@ public class CrossRef
    * @param retrieved
    * @param acf
    */
-  void updateDbrefMappings(SequenceI mapFrom,
-          DBRefEntry[] xrefs, SequenceI[] retrieved, AlignedCodonFrame acf)
+  void updateDbrefMappings(SequenceI mapFrom, DBRefEntry[] xrefs,
+          SequenceI[] retrieved, AlignedCodonFrame acf, boolean fromDna)
   {
     SequenceIdMatcher matcher = new SequenceIdMatcher(retrieved);
     for (DBRefEntry xref : xrefs)
@@ -612,7 +610,7 @@ public class CrossRef
         }
         for (SequenceI seq : matches)
         {
-          constructMapping(mapFrom, seq, xref, acf);
+          constructMapping(mapFrom, seq, xref, acf, fromDna);
         }
       }
     }
@@ -639,7 +637,7 @@ public class CrossRef
    * @return
    */
   boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
-          DBRefEntry xref, AlignedCodonFrame mappings)
+          DBRefEntry xref, AlignedCodonFrame mappings, boolean fromDna)
   {
     MapList mapping = null;
 
@@ -700,12 +698,15 @@ public class CrossRef
    * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
    * based on source and accession string only - Map and Version are nulled.
    * 
+   * @param fromDna
+   *          - true if context was searching from Dna sequences, false if
+   *          context was searching from Protein sequences
    * @param sequenceI
    * @param lrfs
    * @param rseqs
    * @return true if matches were found.
    */
-  private boolean searchDatasetXrefs(SequenceI sequenceI,
+  private boolean searchDatasetXrefs(boolean fromDna, SequenceI sequenceI,
           DBRefEntry[] lrfs, List<SequenceI> rseqs, AlignedCodonFrame cf)
   {
     boolean found = false;
@@ -719,7 +720,7 @@ public class CrossRef
       // add in wildcards
       xref.setVersion(null);
       xref.setMap(null);
-      found |= searchDataset(sequenceI, xref, rseqs, cf, false);
+      found |= searchDataset(fromDna, sequenceI, xref, rseqs, cf, false);
     }
     return found;
   }
@@ -728,6 +729,9 @@ public class CrossRef
    * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
    * associated sequence to rseqs
    * 
+   * @param fromDna
+   *          true if context was searching for refs *from* dna sequence, false
+   *          if context was searching for refs *from* protein sequence
    * @param sequenceI
    *          a sequence to ignore (start point of search)
    * @param xrf
@@ -740,8 +744,9 @@ public class CrossRef
    *          - search all references or only subset
    * @return true if relationship found and sequence added.
    */
-  boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
-          List<SequenceI> rseqs, AlignedCodonFrame cf, boolean direct)
+  boolean searchDataset(boolean fromDna, SequenceI sequenceI,
+          DBRefEntry xrf, List<SequenceI> rseqs, AlignedCodonFrame cf,
+          boolean direct)
   {
     boolean found = false;
     if (dataset == null)