JAL-1705 compute peptide variants from dna; add "consequence" feature on
[jalview.git] / src / jalview / analysis / CrossRef.java
index 9e69ca4..68f6c93 100644 (file)
-package jalview.analysis;\r
-\r
-import java.util.Enumeration;\r
-import java.util.Vector;\r
-import java.util.Hashtable;\r
-\r
-import jalview.datamodel.AlignedCodonFrame;\r
-import jalview.datamodel.Alignment;\r
-import jalview.datamodel.AlignmentI;\r
-import jalview.datamodel.DBRefSource;\r
-import jalview.datamodel.DBRefEntry;\r
-import jalview.datamodel.Sequence;\r
-import jalview.datamodel.SequenceI;\r
-import jalview.ws.ASequenceFetcher;\r
-import jalview.ws.SequenceFetcher;\r
-\r
-/**\r
- * Functions for cross-referencing sequence databases. user must first specify\r
- * if cross-referencing from protein or dna (set dna==true)\r
- * \r
- * @author JimP\r
- * \r
- */\r
-public class CrossRef\r
-{\r
-  /**\r
-   * get the DNA or protein references for a protein or dna sequence\r
-   * \r
-   * @param dna\r
-   * @param rfs\r
-   * @return\r
-   */\r
-  public static DBRefEntry[] findXDbRefs(boolean dna, DBRefEntry[] rfs)\r
-  {\r
-    if (dna)\r
-    {\r
-      rfs = jalview.util.DBRefUtils.selectRefs(rfs, DBRefSource.PROTEINDBS);\r
-    }\r
-    else\r
-    {\r
-      rfs = jalview.util.DBRefUtils.selectRefs(rfs,\r
-              DBRefSource.DNACODINGDBS); // could attempt to find other cross refs and return here - ie PDB xrefs (not dna, not protein seq)\r
-    }\r
-    return rfs;\r
-  }\r
-\r
-\r
-  public static Hashtable classifyDbRefs(DBRefEntry[] rfs)\r
-  {\r
-    Hashtable classes = new Hashtable();\r
-    classes.put(DBRefSource.PROTEINDBS, jalview.util.DBRefUtils.selectRefs(rfs, DBRefSource.PROTEINDBS));\r
-    classes.put(DBRefSource.DNACODINGDBS, jalview.util.DBRefUtils.selectRefs(rfs,\r
-            DBRefSource.DNACODINGDBS));\r
-    classes.put(DBRefSource.DOMAINDBS, jalview.util.DBRefUtils.selectRefs(rfs,\r
-            DBRefSource.DOMAINDBS));\r
-    // classes.put(OTHER, )\r
-    return classes;\r
-  }\r
-\r
-  /**\r
-   * @param dna\r
-   *          true if seqs are DNA seqs\r
-   * @param seqs\r
-   * @return a list of sequence database cross reference source types\r
-   */\r
-  public static String[] findSequenceXrefTypes(boolean dna, SequenceI[] seqs)\r
-  {\r
-    return findSequenceXrefTypes(dna, seqs, null);\r
-  }\r
-  /**\r
-   * Indirect references are references from other sequences from the dataset to any of the direct\r
-   * DBRefEntrys on the given sequences.\r
-   * @param dna\r
-   *          true if seqs are DNA seqs\r
-   * @param seqs\r
-   * @return a list of sequence database cross reference source types\r
-   */\r
-  public static String[] findSequenceXrefTypes(boolean dna, SequenceI[] seqs, AlignmentI dataset)\r
-  {\r
-    String[] dbrefs = null;\r
-    Vector refs = new Vector();\r
-    for (int s = 0; s < seqs.length; s++)\r
-    {\r
-      SequenceI dss = seqs[s];\r
-      while (dss.getDatasetSequence()!=null)\r
-      {\r
-        dss = dss.getDatasetSequence();\r
-      }\r
-      DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRef());\r
-      for (int r = 0; rfs != null && r < rfs.length; r++)\r
-      {\r
-        if (!refs.contains(rfs[r].getSource()))\r
-        {\r
-          refs.addElement(rfs[r].getSource());\r
-        }\r
-      }\r
-      if (dataset!=null)\r
-      {\r
-        // search for references to this sequence's direct references.\r
-        DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seqs[s].getDBRef());\r
-        Vector rseqs = new Vector();\r
-        CrossRef.searchDatasetXrefs(seqs[s], !dna, lrfs, dataset, rseqs, null); // don't need to specify codon frame for mapping here\r
-        Enumeration lr = rseqs.elements();\r
-        while (lr.hasMoreElements())\r
-        {\r
-          SequenceI rs = (SequenceI) lr.nextElement();\r
-          DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRef());\r
-          for (int r=0; rfs != null && r < rfs.length; r++)\r
-          {\r
-            if (!refs.contains(rfs[r].getSource()))\r
-            {\r
-              refs.addElement(rfs[r].getSource());\r
-            }\r
-          }\r
-        }\r
-      }\r
-    }\r
-    if (refs.size() > 0)\r
-    {\r
-      dbrefs = new String[refs.size()];\r
-      refs.copyInto(dbrefs);\r
-    }\r
-    return dbrefs;\r
-  }\r
-\r
-  /*\r
-   * if (dna) { if (rfs[r].hasMap()) { // most likely this is a protein cross\r
-   * reference if (!refs.contains(rfs[r].getSource())) {\r
-   * refs.addElement(rfs[r].getSource()); } } }\r
-   */\r
-  public static boolean hasCdnaMap(SequenceI[] seqs)\r
-  {\r
-    String[] reftypes = findSequenceXrefTypes(false, seqs);\r
-    for (int s = 0; s < reftypes.length; s++)\r
-    {\r
-      if (reftypes.equals(DBRefSource.EMBLCDS))\r
-      {\r
-        return true;\r
-        // no map\r
-      }\r
-    }\r
-    return false;\r
-  }\r
-\r
-  public static SequenceI[] getCdnaMap(SequenceI[] seqs)\r
-  {\r
-    Vector cseqs = new Vector();\r
-    for (int s = 0; s < seqs.length; s++)\r
-    {\r
-      DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRef());\r
-      for (int c = 0; c < cdna.length; c++)\r
-      {\r
-        if (cdna[c].getSource().equals(DBRefSource.EMBLCDS))\r
-        {\r
-          // retrieve CDS dataset sequences\r
-          // need global dataset sequence retriever/resolver to reuse refs\r
-          // and construct Mapping entry.\r
-          // insert gaps in CDS according to peptide gaps.\r
-          // add gapped sequence to cseqs\r
-        }\r
-      }\r
-    }\r
-    if (cseqs.size() > 0)\r
-    {\r
-      SequenceI[] rsqs = new SequenceI[cseqs.size()];\r
-      cseqs.copyInto(rsqs);\r
-      return rsqs;\r
-    }\r
-    return null;\r
-\r
-  }\r
-\r
-  /**\r
-   * \r
-   * @param dna\r
-   * @param seqs\r
-   * @return\r
-   */\r
-  public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,\r
-          String source)\r
-  {\r
-    return findXrefSequences(seqs, dna, source, null);\r
-  }\r
-\r
-  /**\r
-   * \r
-   * @param seqs\r
-   * @param dna\r
-   * @param source\r
-   * @param dataset\r
-   *          alignment to search for product sequences.\r
-   * @return products (as dataset sequences)\r
-   */\r
-  public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,\r
-          String source, AlignmentI dataset)\r
-  {\r
-    Vector rseqs = new Vector();\r
-    Alignment ral = null;\r
-    AlignedCodonFrame cf=new AlignedCodonFrame(dataset.getWidth()); // nominal width\r
-    for (int s = 0; s < seqs.length; s++)\r
-    {\r
-      SequenceI dss = seqs[s];\r
-      while (dss.getDatasetSequence()!=null)\r
-      {\r
-        dss = dss.getDatasetSequence();\r
-      }\r
-      boolean found = false;\r
-      DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRef());\r
-      if ((xrfs == null || xrfs.length == 0) && dataset!=null)\r
-      {\r
-        System.out.println("Attempting to find ds Xrefs refs.");\r
-        DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seqs[s].getDBRef()); // less ambiguous would be a 'find primary dbRefEntry' method.\r
-        found = CrossRef.searchDatasetXrefs(dss, !dna, lrfs, dataset, rseqs, cf);\r
-      }\r
-      for (int r = 0; xrfs!=null && r < xrfs.length; r++)\r
-      {\r
-        if (source != null && !source.equals(xrfs[r].getSource()))\r
-          continue;\r
-        if (xrfs[r].hasMap())\r
-        {\r
-          if (xrfs[r].getMap().getTo() != null)\r
-          {\r
-            Sequence rsq = new Sequence(xrfs[r].getMap().getTo());\r
-            rseqs.addElement(rsq);\r
-            if (xrfs[r].getMap().getMap().getFromRatio()!=xrfs[r].getMap().getMap().getToRatio())\r
-            {\r
-              // get sense of map correct for adding to product alignment.\r
-              if (dna)\r
-              {\r
-                // map is from dna seq to a protein product\r
-                cf.addMap(dss, rsq, xrfs[r].getMap().getMap());\r
-              } else {\r
-                // map should be from protein seq to its coding dna\r
-                cf.addMap(rsq, dss, xrfs[r].getMap().getMap().getInverse());\r
-              }\r
-            }\r
-            found = true;\r
-          }\r
-        }\r
-        else\r
-        {\r
-          // do a bit more work - search for sequences with references matching\r
-          // xrefs on this sequence.\r
-          if (dataset != null)\r
-          {\r
-            found = searchDataset(dss, xrfs[r], dataset, rseqs, cf);\r
-          }\r
-        }\r
-      }\r
-      if (!found)\r
-      {\r
-        if (xrfs != null && xrfs.length > 0)\r
-        {\r
-          // Try and get the sequence reference...\r
-          /*\r
-           * Ideal world - we ask for a sequence fetcher implementation here if\r
-           * (jalview.io.RunTimeEnvironment.getSequenceFetcher()) (\r
-           */\r
-          ASequenceFetcher sftch = new SequenceFetcher();\r
-          SequenceI[] retrieved = null;\r
-          int l = xrfs.length;\r
-          for (int r = 0; r < xrfs.length; r++)\r
-          {\r
-            // filter out any irrelevant or irretrievable references\r
-            if ((source != null && !source.equals(xrfs[r].getSource()))\r
-                    || !sftch.isFetchable(xrfs[r].getSource()))\r
-            {\r
-              l--;\r
-              xrfs[r] = null;\r
-            }\r
-          }\r
-          if (l > 0)\r
-          {\r
-            System.out\r
-            .println("Attempting to retrieve cross referenced sequences.");\r
-            DBRefEntry[] t = new DBRefEntry[l];\r
-            l = 0;\r
-            for (int r = 0; r < xrfs.length; r++)\r
-            {\r
-              if (xrfs[r] != null)\r
-                t[l++] = xrfs[r];\r
-            }\r
-            xrfs = t;\r
-            try\r
-            {\r
-              retrieved = sftch.getSequences(xrfs);\r
-            } catch (Exception e)\r
-            {\r
-              System.err\r
-              .println("Problem whilst retrieving cross references for Sequence : "\r
-                      + seqs[s].getName());\r
-              e.printStackTrace();\r
-            }\r
-            if (retrieved != null)\r
-            {\r
-              for (int rs = 0; rs < retrieved.length; rs++)\r
-              {\r
-                rseqs.addElement(retrieved[rs]);\r
-              }\r
-            }\r
-          }\r
-        }\r
-      }\r
-    }\r
-    if (rseqs.size() > 0)\r
-    {\r
-      SequenceI[] rsqs = new SequenceI[rseqs.size()];\r
-      rseqs.copyInto(rsqs);\r
-      ral = new Alignment(rsqs);\r
-      if (cf!=null && cf.getProtMappings()!=null)\r
-      {\r
-        ral.addCodonFrame(cf);\r
-      }\r
-    }\r
-    return ral;\r
-  }\r
-\r
-  /**\r
-   * find references to lrfs in the cross-reference set of each sequence in dataset (that is not equal to sequenceI)\r
-   * Identifies matching DBRefEntry based on source and accession string only - Map and Version are nulled.\r
-   * @param sequenceI\r
-   * @param lrfs\r
-   * @param dataset\r
-   * @param rseqs\r
-   * @return true if matches were found.\r
-   */\r
-  private static boolean searchDatasetXrefs(SequenceI sequenceI, boolean dna, DBRefEntry[] lrfs, AlignmentI dataset, Vector rseqs, AlignedCodonFrame cf)\r
-  {\r
-    boolean found=false;\r
-    if (lrfs==null)\r
-      return false;\r
-    for (int i=0;i<lrfs.length; i++)\r
-    {\r
-      DBRefEntry xref = new DBRefEntry(lrfs[i]);\r
-      // add in wildcards\r
-      xref.setVersion(null);\r
-      xref.setMap(null);\r
-      found = searchDataset(sequenceI, xref, dataset, rseqs, cf, false, dna);\r
-    }\r
-    return found;\r
-  }\r
-\r
-\r
-  /**\r
-   * search a given sequence dataset for references matching cross-references to\r
-   * the given sequence\r
-   * \r
-   * @param sequenceI\r
-   * @param xrf\r
-   * @param dna\r
-   * @param dataset\r
-   * @param rseqs\r
-   * @param cf \r
-   * @return true if sequences were found and added\r
-   */\r
-  public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,\r
-          AlignmentI dataset, Vector rseqs, AlignedCodonFrame cf)\r
-  {\r
-    return searchDataset(sequenceI, xrf,\r
-            dataset, rseqs, cf, true, false);\r
-  }\r
-  /**\r
-   * TODO: generalise to different protein classifications\r
-   * Search dataset for DBRefEntrys matching the given one (xrf) and add\r
-   * the associated sequence to rseq.\r
-   * @param sequenceI\r
-   * @param xrf\r
-   * @param dataset\r
-   * @param rseqs\r
-   * @param direct - search all references or only subset\r
-   * @param dna search dna or protein xrefs (if direct=false)\r
-   * @return true if relationship found and sequence added.\r
-   */\r
-  public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,\r
-          AlignmentI dataset, Vector rseqs, AlignedCodonFrame cf, boolean direct, boolean dna)\r
-  {\r
-    boolean found = false;\r
-    if (dataset==null)\r
-      return false;\r
-    Enumeration e = dataset.getSequences().elements();\r
-    while (e.hasMoreElements())\r
-    {\r
-      SequenceI nxt = (SequenceI) e.nextElement();\r
-      if (nxt != null)\r
-      {\r
-        if (nxt.getDatasetSequence() != null)\r
-        {\r
-          System.err\r
-          .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");\r
-        }\r
-        if (nxt != sequenceI && nxt != sequenceI.getDatasetSequence())\r
-        {\r
-          DBRefEntry[] poss=null, cands=null;\r
-          if (direct)\r
-          {\r
-            cands = jalview.util.DBRefUtils.searchRefs(poss=nxt\r
-                    .getDBRef(), xrf);\r
-          } else {\r
-            cands = jalview.util.DBRefUtils.searchRefs(\r
-                    poss=CrossRef.findXDbRefs(dna, nxt.getDBRef()), xrf);\r
-          }\r
-          if (cands != null)\r
-          {\r
-            rseqs.addElement(nxt);\r
-            boolean foundmap= cf!=null; // don't search if we aren't given a codon map object\r
-            for (int r=0; foundmap && r<cands.length; r++)\r
-            {\r
-              if (cands[r].hasMap())\r
-              {\r
-                if (cands[r].getMap().getTo()!=null && cands[r].getMap().getMap().getFromRatio()!=cands[r].getMap().getMap().getToRatio())\r
-                {\r
-                  foundmap=true;\r
-                  // get sense of map correct for adding to product alignment.\r
-                  if (dna)\r
-                  {\r
-                    // map is from dna seq to a protein product\r
-                    cf.addMap(sequenceI, nxt, cands[r].getMap().getMap()); \r
-                  } else {\r
-                    // map should be from protein seq to its coding dna\r
-                    cf.addMap(nxt, sequenceI, cands[r].getMap().getMap().getInverse());\r
-                  }\r
-                }\r
-              }\r
-            }\r
-          }\r
-          \r
-          // TODO: add mapping between sequences if necessary\r
-          found = true;\r
-        }\r
-      }\r
-    }\r
-    return found;\r
-  }\r
-\r
-  /**\r
-   * precalculate different products that can be found for seqs in dataset\r
-   * and return them.\r
-   * @param dna\r
-   * @param seqs\r
-   * @param dataset\r
-   * @param fake - don't actually build lists - just get types\r
-   * @return\r
-  public static Object[] buildXProductsList(boolean dna, SequenceI[] seqs, AlignmentI dataset, boolean fake)\r
-  {\r
-    String types[] = jalview.analysis.CrossRef.findSequenceXrefTypes(\r
-            dna, seqs, dataset);\r
-    if (types != null)\r
-    {\r
-      System.out.println("Xref Types for: "+(dna ? "dna" : "prot"));\r
-      for (int t = 0; t < types.length; t++)\r
-      {\r
-        System.out.println("Type: " + types[t]);\r
-        SequenceI[] prod = \r
-          jalview.analysis.CrossRef.findXrefSequences(seqs, dna, types[t]);\r
-        System.out.println("Found "\r
-                + ((prod == null) ? "no" : "" + prod.length)\r
-                + " products");\r
-        if (prod!=null)\r
-        {\r
-          for (int p=0; p<prod.length; p++)\r
-          {\r
-            System.out.println("Prod "+p+": "+prod[p].getDisplayId(true));\r
-          }\r
-        }\r
-      }\r
-\r
-    } else {\r
-      System.out.println("Trying getProducts for "+al.getSequenceAt(0).getDisplayId(true));\r
-      System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot"));\r
-      // have a bash at finding the products amongst all the retrieved sequences.\r
-      SequenceI[] prod = jalview.analysis.CrossRef.findXrefSequences(al\r
-              .getSequencesArray(), dna, null, ds);\r
-      System.out.println("Found "\r
-              + ((prod == null) ? "no" : "" + prod.length)\r
-              + " products");\r
-      if (prod!=null)\r
-      {\r
-        // select non-equivalent sequences from dataset list\r
-        for (int p=0; p<prod.length; p++)\r
-        {\r
-          System.out.println("Prod "+p+": "+prod[p].getDisplayId(true));\r
-        }\r
-      }\r
-\r
-    }\r
-  }\r
-   */\r
-}
\ No newline at end of file
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.analysis;
+
+import jalview.datamodel.AlignedCodonFrame;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentI;
+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.io.gff.SequenceOntology;
+import jalview.schemes.ResidueProperties;
+import jalview.util.DBRefUtils;
+import jalview.util.MapList;
+import jalview.util.MappingUtils;
+import jalview.util.StringUtils;
+import jalview.ws.SequenceFetcher;
+import jalview.ws.seqfetcher.ASequenceFetcher;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Vector;
+
+/**
+ * Functions for cross-referencing sequence databases. user must first specify
+ * if cross-referencing from protein or dna (set dna==true)
+ * 
+ * @author JimP
+ * 
+ */
+public class CrossRef
+{
+  /**
+   * Select just the DNA or protein references for a protein or dna sequence
+   * 
+   * @param fromDna
+   *          if true, select references from DNA (i.e. Protein databases), else
+   *          DNA database references
+   * @param refs
+   *          a set of references to select from
+   * @return
+   */
+  public static DBRefEntry[] findXDbRefs(boolean fromDna, DBRefEntry[] refs)
+  {
+    return DBRefUtils.selectRefs(refs, fromDna ? DBRefSource.PROTEINDBS
+            : DBRefSource.DNACODINGDBS);
+    // could attempt to find other cross
+    // refs here - ie PDB xrefs
+    // (not dna, not protein seq)
+  }
+
+  /**
+   * @param dna
+   *          true if seqs are DNA seqs
+   * @param seqs
+   * @return a list of sequence database cross reference source types
+   */
+  public static String[] findSequenceXrefTypes(boolean dna, SequenceI[] seqs)
+  {
+    return findSequenceXrefTypes(dna, seqs, null);
+  }
+
+  /**
+   * Indirect references are references from other sequences from the dataset to
+   * any of the direct DBRefEntrys on the given sequences.
+   * 
+   * @param dna
+   *          true if seqs are DNA seqs
+   * @param seqs
+   * @return a list of sequence database cross reference source types
+   */
+  public static String[] findSequenceXrefTypes(boolean dna,
+          SequenceI[] seqs, AlignmentI dataset)
+  {
+    String[] dbrefs = null;
+    List<String> refs = new ArrayList<String>();
+    for (SequenceI seq : seqs)
+    {
+      if (seq != null)
+      {
+        SequenceI dss = seq;
+        while (dss.getDatasetSequence() != null)
+        {
+          dss = dss.getDatasetSequence();
+        }
+        DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRef());
+        if (rfs != null)
+        {
+          for (DBRefEntry ref : rfs)
+          {
+            if (!refs.contains(ref.getSource()))
+            {
+              refs.add(ref.getSource());
+            }
+          }
+        }
+        if (dataset != null)
+        {
+          // search for references to this sequence's direct references.
+          DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRef());
+          List<SequenceI> rseqs = new ArrayList<SequenceI>();
+          CrossRef.searchDatasetXrefs(seq, !dna, lrfs, dataset, rseqs,
+                  null); // don't need to specify codon frame for mapping here
+          for (SequenceI rs : rseqs)
+          {
+            DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRef());
+            if (xrs != null)
+            {
+              for (DBRefEntry ref : xrs)
+              {
+                if (!refs.contains(ref.getSource()))
+                {
+                  refs.add(ref.getSource());
+                }
+              }
+            }
+            // looks like copy and paste - change rfs to xrs?
+            // for (int r = 0; rfs != null && r < rfs.length; r++)
+            // {
+            // if (!refs.contains(rfs[r].getSource()))
+            // {
+            // refs.add(rfs[r].getSource());
+            // }
+            // }
+          }
+        }
+      }
+    }
+    if (refs.size() > 0)
+    {
+      dbrefs = new String[refs.size()];
+      refs.toArray(dbrefs);
+    }
+    return dbrefs;
+  }
+
+  public static boolean hasCdnaMap(SequenceI[] seqs)
+  {
+    // TODO unused - remove?
+    String[] reftypes = findSequenceXrefTypes(false, seqs);
+    for (int s = 0; s < reftypes.length; s++)
+    {
+      if (reftypes.equals(DBRefSource.EMBLCDS))
+      {
+        return true;
+        // no map
+      }
+    }
+    return false;
+  }
+
+  public static SequenceI[] getCdnaMap(SequenceI[] seqs)
+  {
+    // TODO unused - remove?
+    Vector cseqs = new Vector();
+    for (int s = 0; s < seqs.length; s++)
+    {
+      DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRef());
+      for (int c = 0; c < cdna.length; c++)
+      {
+        if (cdna[c].getSource().equals(DBRefSource.EMBLCDS))
+        {
+          System.err
+                  .println("TODO: unimplemented sequence retrieval for coding region sequence.");
+          // TODO: retrieve CDS dataset sequences
+          // need global dataset sequence retriever/resolver to reuse refs
+          // and construct Mapping entry.
+          // insert gaps in CDS according to peptide gaps.
+          // add gapped sequence to cseqs
+        }
+      }
+    }
+    if (cseqs.size() > 0)
+    {
+      SequenceI[] rsqs = new SequenceI[cseqs.size()];
+      cseqs.copyInto(rsqs);
+      return rsqs;
+    }
+    return null;
+
+  }
+
+  /**
+   * 
+   * @param dna
+   * @param seqs
+   * @return
+   */
+  public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,
+          String source)
+  {
+    return findXrefSequences(seqs, dna, source, null);
+  }
+
+  /**
+   * 
+   * @param seqs
+   *          sequences whose xrefs are being retrieved
+   * @param dna
+   *          true if sequences are nucleotide
+   * @param source
+   * @param dataset
+   *          alignment to search for product sequences.
+   * @return products (as dataset sequences)
+   */
+  public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,
+          String source, AlignmentI dataset)
+  {
+    List<SequenceI> rseqs = new ArrayList<SequenceI>();
+    AlignedCodonFrame cf = new AlignedCodonFrame();
+    for (SequenceI seq : seqs)
+    {
+      SequenceI dss = seq;
+      while (dss.getDatasetSequence() != null)
+      {
+        dss = dss.getDatasetSequence();
+      }
+      boolean found = false;
+      DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRef());
+      if ((xrfs == null || xrfs.length == 0) && dataset != null)
+      {
+        System.out.println("Attempting to find ds Xrefs refs.");
+        // FIXME should be dss not seq here?
+        DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRef());
+        // less ambiguous would be a 'find primary dbRefEntry' method.
+        // filter for desired source xref here
+        found = CrossRef.searchDatasetXrefs(dss, !dna, lrfs, dataset,
+                rseqs, cf);
+      }
+      for (int r = 0; xrfs != null && r < xrfs.length; r++)
+      {
+        DBRefEntry xref = xrfs[r];
+        if (source != null && !source.equals(xref.getSource()))
+        {
+          continue;
+        }
+        if (xref.hasMap())
+        {
+          if (xref.getMap().getTo() != null)
+          {
+            SequenceI rsq = new Sequence(xref.getMap().getTo());
+            rseqs.add(rsq);
+            if (xref.getMap().getMap().getFromRatio() != xref
+                    .getMap().getMap().getToRatio())
+            {
+              // get sense of map correct for adding to product alignment.
+              if (dna)
+              {
+                // map is from dna seq to a protein product
+                cf.addMap(dss, rsq, xref.getMap().getMap());
+              }
+              else
+              {
+                // map should be from protein seq to its coding dna
+                cf.addMap(rsq, dss, xref.getMap().getMap().getInverse());
+              }
+
+              /*
+               * compute peptide variants from dna variants
+               */
+              rsq.createDatasetSequence();
+              computeProteinVariants(seq, rsq, xref.getMap().getMap());
+            }
+            found = true;
+          }
+        }
+        if (!found)
+        {
+          // do a bit more work - search for sequences with references matching
+          // xrefs on this sequence.
+          if (dataset != null)
+          {
+            found |= searchDataset(dss, xref, dataset, rseqs, cf); // ,false,!dna);
+            if (found)
+            {
+              xrfs[r] = null; // we've recovered seqs for this one.
+            }
+          }
+        }
+      }
+      if (!found)
+      {
+        if (xrfs != null && xrfs.length > 0)
+        {
+          // Try and get the sequence reference...
+          /*
+           * Ideal world - we ask for a sequence fetcher implementation here if
+           * (jalview.io.RunTimeEnvironment.getSequenceFetcher()) (
+           */
+          ASequenceFetcher sftch = new SequenceFetcher();
+          SequenceI[] retrieved = null;
+          int l = xrfs.length;
+          for (int r = 0; r < xrfs.length; r++)
+          {
+            // filter out any irrelevant or irretrievable references
+            if (xrfs[r] == null
+                    || ((source != null && !source.equals(xrfs[r]
+                            .getSource())) || !sftch.isFetchable(xrfs[r]
+                            .getSource())))
+            {
+              l--;
+              xrfs[r] = null;
+            }
+          }
+          if (l > 0)
+          {
+            System.out
+                    .println("Attempting to retrieve cross referenced sequences.");
+            DBRefEntry[] t = new DBRefEntry[l];
+            l = 0;
+            for (int r = 0; r < xrfs.length; r++)
+            {
+              if (xrfs[r] != null)
+              {
+                t[l++] = xrfs[r];
+              }
+            }
+            xrfs = t;
+            try
+            {
+              retrieved = sftch.getSequences(xrfs, !dna);
+              // problem here is we don't know which of xrfs resulted in which
+              // retrieved element
+            } catch (Exception e)
+            {
+              System.err
+                      .println("Problem whilst retrieving cross references for Sequence : "
+                              + seq.getName());
+              e.printStackTrace();
+            }
+            if (retrieved != null)
+            {
+              for (int rs = 0; rs < retrieved.length; rs++)
+              {
+                // TODO: examine each sequence for 'redundancy'
+                DBRefEntry[] dbr = retrieved[rs].getDBRef();
+                if (dbr != null && dbr.length > 0)
+                {
+                  for (int di = 0; di < dbr.length; di++)
+                  {
+                    // find any entry where we should put in the sequence being
+                    // cross-referenced into the map
+                    Mapping map = dbr[di].getMap();
+                    if (map != null)
+                    {
+                      if (map.getTo() != null && map.getMap() != null)
+                      {
+                        // should search the local dataset to find any existing
+                        // candidates for To !
+                        try
+                        {
+                          // compare ms with dss and replace with dss in mapping
+                          // if map is congruent
+                          SequenceI ms = map.getTo();
+                          int sf = map.getMap().getToLowest();
+                          int st = map.getMap().getToHighest();
+                          SequenceI mappedrg = ms.getSubSequence(sf, st);
+                          SequenceI loc = dss.getSubSequence(sf, st);
+                          if (mappedrg.getLength() > 0
+                                  && mappedrg.getSequenceAsString().equals(
+                                          loc.getSequenceAsString()))
+                          {
+                            System.err
+                                    .println("Mapping updated for retrieved crossreference");
+                            // method to update all refs of existing To on
+                            // retrieved sequence with dss and merge any props
+                            // on To onto dss.
+                            map.setTo(dss);
+                          }
+                        } catch (Exception e)
+                        {
+                          System.err
+                                  .println("Exception when consolidating Mapped sequence set...");
+                          e.printStackTrace(System.err);
+                        }
+                      }
+                    }
+                  }
+                }
+                retrieved[rs].updatePDBIds();
+                rseqs.add(retrieved[rs]);
+              }
+            }
+          }
+        }
+      }
+    }
+
+    Alignment ral = null;
+    if (rseqs.size() > 0)
+    {
+      SequenceI[] rsqs = new SequenceI[rseqs.size()];
+      rseqs.toArray(rsqs);
+      ral = new Alignment(rsqs);
+      if (cf != null && !cf.isEmpty())
+      {
+        ral.addCodonFrame(cf);
+      }
+    }
+    return ral;
+  }
+
+  /**
+   * find references to lrfs in the cross-reference set of each sequence in
+   * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
+   * based on source and accession string only - Map and Version are nulled.
+   * 
+   * @param sequenceI
+   * @param lrfs
+   * @param dataset
+   * @param rseqs
+   * @return true if matches were found.
+   */
+  private static boolean searchDatasetXrefs(SequenceI sequenceI,
+          boolean dna, DBRefEntry[] lrfs, AlignmentI dataset,
+          List<SequenceI> rseqs, AlignedCodonFrame cf)
+  {
+    boolean found = false;
+    if (lrfs == null)
+    {
+      return false;
+    }
+    for (int i = 0; i < lrfs.length; i++)
+    {
+      DBRefEntry xref = new DBRefEntry(lrfs[i]);
+      // add in wildcards
+      xref.setVersion(null);
+      xref.setMap(null);
+      found = searchDataset(sequenceI, xref, dataset, rseqs, cf, false, dna);
+    }
+    return found;
+  }
+
+  /**
+   * search a given sequence dataset for references matching cross-references to
+   * the given sequence
+   * 
+   * @param sequenceI
+   * @param xrf
+   * @param dataset
+   * @param rseqs
+   *          set of unique sequences
+   * @param cf
+   * @return true if one or more unique sequences were found and added
+   */
+  public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
+          AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf)
+  {
+    return searchDataset(sequenceI, xrf, dataset, rseqs, cf, true, false);
+  }
+
+  /**
+   * TODO: generalise to different protein classifications Search dataset for
+   * DBRefEntrys matching the given one (xrf) and add the associated sequence to
+   * rseq.
+   * 
+   * @param sequenceI
+   * @param xrf
+   * @param dataset
+   * @param rseqs
+   * @param direct
+   *          - search all references or only subset
+   * @param dna
+   *          search dna or protein xrefs (if direct=false)
+   * @return true if relationship found and sequence added.
+   */
+  public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
+          AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf,
+          boolean direct, boolean dna)
+  {
+    boolean found = false;
+    SequenceI[] typer = new SequenceI[1];
+    if (dataset == null)
+    {
+      return false;
+    }
+    if (dataset.getSequences() == null)
+    {
+      System.err.println("Empty dataset sequence set - NO VECTOR");
+      return false;
+    }
+    List<SequenceI> ds;
+    synchronized (ds = dataset.getSequences())
+    {
+      for (SequenceI nxt : ds)
+      {
+        if (nxt != null)
+        {
+          if (nxt.getDatasetSequence() != null)
+          {
+            System.err
+                    .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
+          }
+          if (nxt != sequenceI && nxt != sequenceI.getDatasetSequence())
+          {
+            // check if this is the correct sequence type
+            {
+              typer[0] = nxt;
+              boolean isDna = jalview.util.Comparison.isNucleotide(typer);
+              if ((direct && isDna == dna) || (!direct && isDna != dna))
+              {
+                // skip this sequence because it is same molecule type
+                continue;
+              }
+            }
+
+            // look for direct or indirect references in common
+            DBRefEntry[] poss = nxt.getDBRef(), cands = null;
+            if (direct)
+            {
+              cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
+            }
+            else
+            {
+              poss = CrossRef.findXDbRefs(dna, poss); //
+              cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
+            }
+            if (cands != null)
+            {
+              if (!rseqs.contains(nxt))
+              {
+                rseqs.add(nxt);
+                boolean foundmap = cf != null;
+                // don't search if we aren't given a codon map object
+                for (int r = 0; foundmap && r < cands.length; r++)
+                {
+                  if (cands[r].hasMap())
+                  {
+                    if (cands[r].getMap().getTo() != null
+                            && cands[r].getMap().getMap().getFromRatio() != cands[r]
+                                    .getMap().getMap().getToRatio())
+                    {
+                      foundmap = true;
+                      // get sense of map correct for adding to product
+                      // alignment.
+                      if (dna)
+                      {
+                        // map is from dna seq to a protein product
+                        cf.addMap(sequenceI, nxt, cands[r].getMap()
+                                .getMap());
+                      }
+                      else
+                      {
+                        // map should be from protein seq to its coding dna
+                        cf.addMap(nxt, sequenceI, cands[r].getMap()
+                                .getMap().getInverse());
+                      }
+                    }
+                  }
+                }
+                // TODO: add mapping between sequences if necessary
+                found = true;
+              }
+            }
+
+          }
+        }
+      }
+    }
+    return found;
+  }
+
+  /**
+   * Computes variants in peptide product generated by variants in dna, and adds
+   * them as sequence_variant features on the protein sequence. Returns the
+   * number of variant features added.
+   * 
+   * @param dnaSeq
+   * @param peptide
+   * @param dnaToProtein
+   */
+  protected static int computeProteinVariants(SequenceI dnaSeq,
+          SequenceI peptide, MapList dnaToProtein)
+  {
+    /*
+     * map from peptide position to all variant features of the codon for it
+     * LinkedHashMap ensures we add the peptide features in sequence order
+     */
+    LinkedHashMap<Integer, String[][]> variants = new LinkedHashMap<Integer, String[][]>();
+    SequenceOntology so = SequenceOntology.getInstance();
+  
+    SequenceFeature[] dnaFeatures = dnaSeq.getSequenceFeatures();
+    if (dnaFeatures == null)
+    {
+      return 0;
+    }
+  
+    int[] lastCodon = null;
+    int lastPeptidePostion = 0;
+
+    /*
+     * build a map of codon variations for peptides
+     */
+    for (SequenceFeature sf : dnaFeatures)
+    {
+      int dnaCol = sf.getBegin();
+      if (dnaCol != sf.getEnd())
+      {
+        // not handling multi-locus variant features
+        continue;
+      }
+      if (so.isSequenceVariant(sf.getType()))
+      {
+        int[] mapsTo = dnaToProtein.locateInTo(dnaCol, dnaCol);
+        if (mapsTo == null)
+        {
+          // feature doesn't lie within coding region
+          continue;
+        }
+        int peptidePosition = mapsTo[0];
+        String[][] codonVariants = variants.get(peptidePosition);
+        if (codonVariants == null)
+        {
+          codonVariants = new String[3][];
+          variants.put(peptidePosition, codonVariants);
+        }
+
+        /*
+         * extract dna variants to a string array
+         */
+        String alls = (String) sf.getValue("alleles");
+        if (alls == null)
+        {
+          continue;
+        }
+        String[] alleles = alls.split(",");
+
+        /*
+         * get this peptides codon positions e.g. [3, 4, 5] or [4, 7, 10]
+         */
+        int[] codon = peptidePosition == lastPeptidePostion ? lastCodon
+                : MappingUtils.flattenRanges(dnaToProtein.locateInFrom(
+                        peptidePosition, peptidePosition));
+        lastPeptidePostion = peptidePosition;
+        lastCodon = codon;
+
+        /*
+         * save nucleotide (and this variant) for each codon position
+         */
+        for (int codonPos = 0; codonPos < 3; codonPos++)
+        {
+          String nucleotide = String.valueOf(dnaSeq
+                  .getCharAt(codon[codonPos] - 1));
+          if (codon[codonPos] == dnaCol)
+          {
+            /*
+             * record current dna base and its alleles
+             */
+            String[] dnaVariants = new String[alleles.length + 1];
+            dnaVariants[0] = nucleotide;
+            System.arraycopy(alleles, 0, dnaVariants, 1, alleles.length);
+            codonVariants[codonPos] = dnaVariants;
+          }
+          else if (codonVariants[codonPos] == null)
+          {
+            /*
+             * record current dna base only 
+             * (at least until we find any variation and overwrite it)
+             */
+            codonVariants[codonPos] = new String[] { nucleotide };
+          }
+        }
+      }
+    }
+  
+    /*
+     * scan codon variations, compute peptide variants and add to peptide sequence
+     */
+    int count = 0;
+    for (Entry<Integer, String[][]> variant : variants.entrySet())
+    {
+      int peptidePos = variant.getKey();
+      String[][] codonVariants = variant.getValue();
+      String residue = String.valueOf(peptide.getCharAt(peptidePos - 1)); // 0-based
+      List<String> peptideVariants = computePeptideVariants(codonVariants,
+              residue);
+      if (!peptideVariants.isEmpty())
+      {
+        Collections.sort(peptideVariants);
+        String desc = StringUtils.listToDelimitedString(peptideVariants,
+                ", ");
+        SequenceFeature sf = new SequenceFeature(
+                SequenceOntology.SEQUENCE_VARIANT, desc, peptidePos,
+                peptidePos, Float.NaN, null);
+        peptide.getDatasetSequence().addSequenceFeature(sf);
+        count++;
+      }
+    }
+    return count;
+  }
+
+  /**
+   * Returns a non-redundant list of all peptide translations generated by the
+   * given dna variants, excluding the current residue value
+   * 
+   * @param codonVariants
+   *          an array of base values for codon positions 1, 2, 3
+   * @param residue
+   *          the current residue translation
+   * @return
+   */
+  protected static List<String> computePeptideVariants(
+          String[][] codonVariants, String residue)
+  {
+    List<String> result = new ArrayList<String>();
+    for (String base1 : codonVariants[0])
+    {
+      for (String base2 : codonVariants[1])
+      {
+        for (String base3 : codonVariants[2])
+        {
+          String codon = base1 + base2 + base3;
+          // TODO: report frameshift/insertion/deletion
+          // and multiple-base variants?!
+          String peptide = codon.contains("-") ? "-" : ResidueProperties
+                  .codonTranslate(codon);
+          if (peptide != null && !result.contains(peptide)
+                  && !peptide.equals(residue))
+          {
+            result.add(peptide);
+          }
+        }
+      }
+    }
+    return result;
+  }
+
+  /**
+   * Computes a list of all peptide variants given dna variants
+   * 
+   * @param dnaSeq
+   *          the coding dna sequence
+   * @param codonVariants
+   *          variant features for each codon position (null if no variant)
+   * @param residue
+   *          the canonical protein translation
+   * @return
+   */
+  protected static List<String> computePeptideVariants(SequenceI dnaSeq,
+          SequenceFeature[] codonVariants, String residue)
+  {
+    List<String> result = new ArrayList<String>();
+    int[][] dnaVariants = new int[3][];
+    for (int i = 0; i < 3; i++)
+    {
+
+    }
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  /**
+   * precalculate different products that can be found for seqs in dataset and
+   * return them.
+   * 
+   * @param dna
+   * @param seqs
+   * @param dataset
+   * @param fake
+   *          - don't actually build lists - just get types
+   * @return public static Object[] buildXProductsList(boolean dna, SequenceI[]
+   *         seqs, AlignmentI dataset, boolean fake) { String types[] =
+   *         jalview.analysis.CrossRef.findSequenceXrefTypes( dna, seqs,
+   *         dataset); if (types != null) { System.out.println("Xref Types for:
+   *         "+(dna ? "dna" : "prot")); for (int t = 0; t < types.length; t++) {
+   *         System.out.println("Type: " + types[t]); SequenceI[] prod =
+   *         jalview.analysis.CrossRef.findXrefSequences(seqs, dna, types[t]);
+   *         System.out.println("Found " + ((prod == null) ? "no" : "" +
+   *         prod.length) + " products"); if (prod!=null) { for (int p=0;
+   *         p<prod.length; p++) { System.out.println("Prod "+p+":
+   *         "+prod[p].getDisplayId(true)); } } } } else {
+   *         System.out.println("Trying getProducts for
+   *         "+al.getSequenceAt(0).getDisplayId(true));
+   *         System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot"));
+   *         // have a bash at finding the products amongst all the retrieved
+   *         sequences. SequenceI[] prod =
+   *         jalview.analysis.CrossRef.findXrefSequences(al
+   *         .getSequencesArray(), dna, null, ds); System.out.println("Found " +
+   *         ((prod == null) ? "no" : "" + prod.length) + " products"); if
+   *         (prod!=null) { // select non-equivalent sequences from dataset list
+   *         for (int p=0; p<prod.length; p++) { System.out.println("Prod "+p+":
+   *         "+prod[p].getDisplayId(true)); } } } }
+   */
+}