formatting
[jalview.git] / src / jalview / analysis / CrossRef.java
index f355d1f..6c63f1c 100644 (file)
@@ -1,6 +1,24 @@
+/*\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
+ *\r
+ * This file is part of Jalview.\r
+ *\r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License\r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
+ *\r
+ * Jalview is distributed in the hope that it will be useful, but\r
+ * WITHOUT ANY WARRANTY; without even the implied warranty\r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR\r
+ * PURPOSE.  See the GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
 package jalview.analysis;\r
 \r
 import java.util.Enumeration;\r
+import java.util.List;\r
 import java.util.Vector;\r
 import java.util.Hashtable;\r
 \r
@@ -49,19 +67,19 @@ public class CrossRef
   public static Hashtable classifyDbRefs(DBRefEntry[] rfs)\r
   {\r
     Hashtable classes = new Hashtable();\r
-    classes.put(DBRefSource.PROTEINDBS, jalview.util.DBRefUtils.selectRefs(\r
-            rfs, DBRefSource.PROTEINDBS));\r
+    classes.put(DBRefSource.PROTEINDBS,\r
+            jalview.util.DBRefUtils.selectRefs(rfs, DBRefSource.PROTEINDBS));\r
     classes.put(DBRefSource.DNACODINGDBS, jalview.util.DBRefUtils\r
             .selectRefs(rfs, DBRefSource.DNACODINGDBS));\r
-    classes.put(DBRefSource.DOMAINDBS, jalview.util.DBRefUtils.selectRefs(\r
-            rfs, DBRefSource.DOMAINDBS));\r
+    classes.put(DBRefSource.DOMAINDBS,\r
+            jalview.util.DBRefUtils.selectRefs(rfs, DBRefSource.DOMAINDBS));\r
     // classes.put(OTHER, )\r
     return classes;\r
   }\r
 \r
   /**\r
    * @param dna\r
-   *                true if seqs are DNA seqs\r
+   *          true if seqs are DNA seqs\r
    * @param seqs\r
    * @return a list of sequence database cross reference source types\r
    */\r
@@ -75,7 +93,7 @@ public class CrossRef
    * any of the direct DBRefEntrys on the given sequences.\r
    * \r
    * @param dna\r
-   *                true if seqs are DNA seqs\r
+   *          true if seqs are DNA seqs\r
    * @param seqs\r
    * @return a list of sequence database cross reference source types\r
    */\r
@@ -86,36 +104,41 @@ public class CrossRef
     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
+      if (seqs[s] != null)\r
       {\r
-        if (!refs.contains(rfs[r].getSource()))\r
+\r
+        SequenceI dss = seqs[s];\r
+        while (dss.getDatasetSequence() != null)\r
         {\r
-          refs.addElement(rfs[r].getSource());\r
+          dss = dss.getDatasetSequence();\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,\r
-                null); // don't need to specify codon frame for mapping here\r
-        Enumeration lr = rseqs.elements();\r
-        while (lr.hasMoreElements())\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
-          SequenceI rs = (SequenceI) lr.nextElement();\r
-          DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRef());\r
-          for (int r = 0; rfs != null && r < rfs.length; r++)\r
+          // search for references to this sequence's direct references.\r
+          DBRefEntry[] lrfs = CrossRef\r
+                  .findXDbRefs(!dna, seqs[s].getDBRef());\r
+          Vector rseqs = new Vector();\r
+          CrossRef.searchDatasetXrefs(seqs[s], !dna, lrfs, dataset, rseqs,\r
+                  null); // don't need to specify codon frame for mapping here\r
+          Enumeration lr = rseqs.elements();\r
+          while (lr.hasMoreElements())\r
           {\r
-            if (!refs.contains(rfs[r].getSource()))\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
-              refs.addElement(rfs[r].getSource());\r
+              if (!refs.contains(rfs[r].getSource()))\r
+              {\r
+                refs.addElement(rfs[r].getSource());\r
+              }\r
             }\r
           }\r
         }\r
@@ -158,7 +181,9 @@ public class CrossRef
       {\r
         if (cdna[c].getSource().equals(DBRefSource.EMBLCDS))\r
         {\r
-          // retrieve CDS dataset sequences\r
+          System.err\r
+                  .println("TODO: unimplemented sequence retrieval for coding region sequence.");\r
+          // TODO: 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
@@ -194,7 +219,7 @@ public class CrossRef
    * @param dna\r
    * @param source\r
    * @param dataset\r
-   *                alignment to search for product sequences.\r
+   *          alignment to search for product sequences.\r
    * @return products (as dataset sequences)\r
    */\r
   public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,\r
@@ -261,7 +286,7 @@ public class CrossRef
           // xrefs on this sequence.\r
           if (dataset != null)\r
           {\r
-            found |= searchDataset(dss, xrfs[r], dataset, rseqs, cf);\r
+            found |= searchDataset(dss, xrfs[r], dataset, rseqs, cf); // ,false,!dna);\r
             if (found)\r
               xrfs[r] = null; // we've recovered seqs for this one.\r
           }\r
@@ -305,7 +330,10 @@ public class CrossRef
             xrfs = t;\r
             try\r
             {\r
-              retrieved = sftch.getSequences(xrfs); // problem here is we don't know which of xrfs resulted in which retrieved element\r
+              retrieved = sftch.getSequences(xrfs); // problem here is we don't\r
+              // know which of xrfs\r
+              // resulted in which\r
+              // retrieved element\r
             } catch (Exception e)\r
             {\r
               System.err\r
@@ -318,32 +346,39 @@ public class CrossRef
               for (int rs = 0; rs < retrieved.length; rs++)\r
               {\r
                 // TODO: examine each sequence for 'redundancy'\r
-                jalview.datamodel.DBRefEntry[] dbr = retrieved[rs].getDBRef();\r
+                jalview.datamodel.DBRefEntry[] dbr = retrieved[rs]\r
+                        .getDBRef();\r
                 if (dbr != null && dbr.length > 0)\r
                 {\r
                   for (int di = 0; di < dbr.length; di++)\r
                   {\r
-                    // find any entry where we should put in the sequence being cross-referenced into the map\r
+                    // find any entry where we should put in the sequence being\r
+                    // cross-referenced into the map\r
                     jalview.datamodel.Mapping map = dbr[di].getMap();\r
                     if (map != null)\r
                     {\r
                       if (map.getTo() != null && map.getMap() != null)\r
                       {\r
-                        // should search the local dataset to find any existing candidates for To !\r
+                        // should search the local dataset to find any existing\r
+                        // candidates for To !\r
                         try\r
                         {\r
-                          // compare ms with dss and replace with dss in mapping if map is congruent\r
+                          // compare ms with dss and replace with dss in mapping\r
+                          // if map is congruent\r
                           SequenceI ms = map.getTo();\r
                           int sf = map.getMap().getToLowest();\r
                           int st = map.getMap().getToHighest();\r
                           SequenceI mappedrg = ms.getSubSequence(sf, st);\r
                           SequenceI loc = dss.getSubSequence(sf, st);\r
-                          if (mappedrg.getLength()>0 && mappedrg.getSequenceAsString().equals(\r
-                                  loc.getSequenceAsString()))\r
+                          if (mappedrg.getLength() > 0\r
+                                  && mappedrg.getSequenceAsString().equals(\r
+                                          loc.getSequenceAsString()))\r
                           {\r
                             System.err\r
                                     .println("Mapping updated for retrieved crossreference");\r
-                            // method to update all refs of existing To on retrieved sequence with dss and merge any props on To onto dss.\r
+                            // method to update all refs of existing To on\r
+                            // retrieved sequence with dss and merge any props\r
+                            // on To onto dss.\r
                             map.setTo(dss);\r
                           }\r
                         } catch (Exception e)\r
@@ -414,7 +449,7 @@ public class CrossRef
    * @param xrf\r
    * @param dataset\r
    * @param rseqs\r
-   *                set of unique sequences\r
+   *          set of unique sequences\r
    * @param cf\r
    * @return true if one or more unique sequences were found and added\r
    */\r
@@ -433,10 +468,10 @@ public class CrossRef
    * @param xrf\r
    * @param dataset\r
    * @param rseqs\r
-   * @param direct -\r
-   *                search all references or only subset\r
+   * @param direct\r
+   *          - search all references or only subset\r
    * @param dna\r
-   *                search dna or protein xrefs (if direct=false)\r
+   *          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
@@ -444,6 +479,7 @@ public class CrossRef
           boolean direct, boolean dna)\r
   {\r
     boolean found = false;\r
+    SequenceI[] typer = new SequenceI[1];\r
     if (dataset == null)\r
       return false;\r
     if (dataset.getSequences() == null)\r
@@ -451,69 +487,82 @@ public class CrossRef
       System.err.println("Empty dataset sequence set - NO VECTOR");\r
       return false;\r
     }\r
-    Enumeration e = dataset.getSequences().elements();\r
-    while (e.hasMoreElements())\r
+    List<SequenceI> ds;\r
+    synchronized (ds = dataset.getSequences())\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
+      for (SequenceI nxt : ds)\r
+        if (nxt != null)\r
         {\r
-          // look for direct or indirect references in common\r
-          DBRefEntry[] poss = null, cands = null;\r
-          if (direct)\r
+          if (nxt.getDatasetSequence() != null)\r
           {\r
-            cands = jalview.util.DBRefUtils.searchRefs(poss = nxt\r
-                    .getDBRef(), xrf);\r
+            System.err\r
+                    .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");\r
           }\r
-          else\r
+          if (nxt != sequenceI && nxt != sequenceI.getDatasetSequence())\r
           {\r
-            cands = jalview.util.DBRefUtils.searchRefs(poss = CrossRef\r
-                    .findXDbRefs(dna, nxt.getDBRef()), xrf);\r
-          }\r
-          if (cands != null)\r
-          {\r
-            if (!rseqs.contains(nxt))\r
+            // check if this is the correct sequence type\r
+            {\r
+              typer[0] = nxt;\r
+              boolean isDna = jalview.util.Comparison.isNucleotide(typer);\r
+              if ((direct && isDna == dna) || (!direct && isDna != dna))\r
+              {\r
+                // skip this sequence because it is same molecule type\r
+                continue;\r
+              }\r
+            }\r
+\r
+            // look for direct or indirect references in common\r
+            DBRefEntry[] poss = nxt.getDBRef(), cands = null;\r
+            if (direct)\r
+            {\r
+              cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);\r
+            }\r
+            else\r
+            {\r
+              poss = CrossRef.findXDbRefs(dna, poss); //\r
+              cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);\r
+            }\r
+            if (cands != null)\r
             {\r
-              rseqs.addElement(nxt);\r
-              boolean foundmap = cf != null; // don't search if we aren't given\r
-              // a codon map object\r
-              for (int r = 0; foundmap && r < cands.length; r++)\r
+              if (!rseqs.contains(nxt))\r
               {\r
-                if (cands[r].hasMap())\r
+                rseqs.addElement(nxt);\r
+                boolean foundmap = cf != null; // don't search if we aren't\r
+                                               // given\r
+                // a codon map object\r
+                for (int r = 0; foundmap && r < cands.length; r++)\r
                 {\r
-                  if (cands[r].getMap().getTo() != null\r
-                          && cands[r].getMap().getMap().getFromRatio() != cands[r]\r
-                                  .getMap().getMap().getToRatio())\r
+                  if (cands[r].hasMap())\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
-                    }\r
-                    else\r
+                    if (cands[r].getMap().getTo() != null\r
+                            && cands[r].getMap().getMap().getFromRatio() != cands[r]\r
+                                    .getMap().getMap().getToRatio())\r
                     {\r
-                      // map should be from protein seq to its coding dna\r
-                      cf.addMap(nxt, sequenceI, cands[r].getMap().getMap()\r
-                              .getInverse());\r
+                      foundmap = true;\r
+                      // get sense of map correct for adding to product\r
+                      // alignment.\r
+                      if (dna)\r
+                      {\r
+                        // map is from dna seq to a protein product\r
+                        cf.addMap(sequenceI, nxt, cands[r].getMap()\r
+                                .getMap());\r
+                      }\r
+                      else\r
+                      {\r
+                        // map should be from protein seq to its coding dna\r
+                        cf.addMap(nxt, sequenceI, cands[r].getMap()\r
+                                .getMap().getInverse());\r
+                      }\r
                     }\r
                   }\r
                 }\r
+                // TODO: add mapping between sequences if necessary\r
+                found = true;\r
               }\r
-              // TODO: add mapping between sequences if necessary\r
-              found = true;\r
             }\r
-          }\r
 \r
+          }\r
         }\r
-      }\r
     }\r
     return found;\r
   }\r
@@ -525,8 +574,8 @@ public class CrossRef
    * @param dna\r
    * @param seqs\r
    * @param dataset\r
-   * @param fake -\r
-   *                don't actually build lists - just get types\r
+   * @param fake\r
+   *          - don't actually build lists - just get types\r
    * @return public static Object[] buildXProductsList(boolean dna, SequenceI[]\r
    *         seqs, AlignmentI dataset, boolean fake) { String types[] =\r
    *         jalview.analysis.CrossRef.findSequenceXrefTypes( dna, seqs,\r
@@ -535,18 +584,19 @@ public class CrossRef
    *         System.out.println("Type: " + types[t]); SequenceI[] prod =\r
    *         jalview.analysis.CrossRef.findXrefSequences(seqs, dna, types[t]);\r
    *         System.out.println("Found " + ((prod == null) ? "no" : "" +\r
-   *         prod.length) + " products"); if (prod!=null) { for (int p=0; p<prod.length;\r
-   *         p++) { System.out.println("Prod "+p+":\r
-   *         "+prod[p].getDisplayId(true)); } } }\r
-   *  } else { System.out.println("Trying getProducts for\r
-   * "+al.getSequenceAt(0).getDisplayId(true)); System.out.println("Search DS\r
-   * Xref for: "+(dna ? "dna" : "prot")); // have a bash at finding the products\r
-   * amongst all the retrieved sequences. SequenceI[] prod =\r
-   * jalview.analysis.CrossRef.findXrefSequences(al .getSequencesArray(), dna,\r
-   * null, ds); System.out.println("Found " + ((prod == null) ? "no" : "" +\r
-   * prod.length) + " products"); if (prod!=null) { // select non-equivalent\r
-   * sequences from dataset list for (int p=0; p<prod.length; p++) {\r
-   * System.out.println("Prod "+p+": "+prod[p].getDisplayId(true)); } }\r
-   *  } }\r
+   *         prod.length) + " products"); if (prod!=null) { for (int p=0;\r
+   *         p<prod.length; p++) { System.out.println("Prod "+p+":\r
+   *         "+prod[p].getDisplayId(true)); } } } } else {\r
+   *         System.out.println("Trying getProducts for\r
+   *         "+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\r
+   *         sequences. SequenceI[] prod =\r
+   *         jalview.analysis.CrossRef.findXrefSequences(al\r
+   *         .getSequencesArray(), dna, null, ds); System.out.println("Found " +\r
+   *         ((prod == null) ? "no" : "" + prod.length) + " products"); if\r
+   *         (prod!=null) { // select non-equivalent sequences from dataset list\r
+   *         for (int p=0; p<prod.length; p++) { System.out.println("Prod "+p+":\r
+   *         "+prod[p].getDisplayId(true)); } } } }\r
    */\r
-}
\ No newline at end of file
+}\r