JAL-2029 further fixes to get correct xrefs and xrefs on xrefs...
[jalview.git] / src / jalview / analysis / CrossRef.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.analysis;
22
23 import jalview.datamodel.AlignedCodonFrame;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.DBRefSource;
28 import jalview.datamodel.Mapping;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceI;
32 import jalview.util.DBRefUtils;
33 import jalview.util.MapList;
34 import jalview.ws.SequenceFetcher;
35 import jalview.ws.seqfetcher.ASequenceFetcher;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Vector;
40
41 /**
42  * Functions for cross-referencing sequence databases. user must first specify
43  * if cross-referencing from protein or dna (set dna==true)
44  * 
45  * @author JimP
46  * 
47  */
48 public class CrossRef
49 {
50   /*
51    * A sub-class that ignores Parent attribute when comparing sequence 
52    * features. This avoids 'duplicate' CDS features that only
53    * differ in their parent Transcript ids.
54    */
55   class MySequenceFeature extends SequenceFeature
56   {
57     private SequenceFeature feat;
58
59     MySequenceFeature(SequenceFeature sf)
60     {
61       this.feat = sf;
62     }
63
64     @Override
65     public boolean equals(Object o)
66     {
67       return feat.equals(o, true);
68     }
69   }
70
71   /**
72    * Select just the DNA or protein references for a protein or dna sequence
73    * 
74    * @param fromDna
75    *          if true, select references from DNA (i.e. Protein databases), else
76    *          DNA database references
77    * @param refs
78    *          a set of references to select from
79    * @return
80    */
81   public static DBRefEntry[] findXDbRefs(boolean fromDna, DBRefEntry[] refs)
82   {
83     return DBRefUtils.selectRefs(refs, fromDna ? DBRefSource.PROTEINDBS
84             : DBRefSource.DNACODINGDBS);
85     // could attempt to find other cross
86     // refs here - ie PDB xrefs
87     // (not dna, not protein seq)
88   }
89
90   /**
91    * @param dna
92    *          true if seqs are DNA seqs
93    * @param seqs
94    * @return a list of sequence database cross reference source types
95    */
96   public static String[] findSequenceXrefTypes(boolean dna, SequenceI[] seqs)
97   {
98     return findSequenceXrefTypes(dna, seqs, null);
99   }
100
101   /**
102    * Indirect references are references from other sequences from the dataset to
103    * any of the direct DBRefEntrys on the given sequences.
104    * 
105    * @param dna
106    *          true if seqs are DNA seqs
107    * @param seqs
108    * @return a list of sequence database cross reference source types
109    */
110   public static String[] findSequenceXrefTypes(boolean dna,
111           SequenceI[] seqs, AlignmentI dataset)
112   {
113     String[] dbrefs = null;
114     List<String> refs = new ArrayList<String>();
115     for (SequenceI seq : seqs)
116     {
117       if (seq != null)
118       {
119         SequenceI dss = seq;
120         while (dss.getDatasetSequence() != null)
121         {
122           dss = dss.getDatasetSequence();
123         }
124         DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRefs());
125         if (rfs != null)
126         {
127           for (DBRefEntry ref : rfs)
128           {
129             if (!refs.contains(ref.getSource()))
130             {
131               refs.add(ref.getSource());
132             }
133           }
134         }
135         if (dataset != null)
136         {
137           // search for references to this sequence's direct references.
138           DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRefs());
139           List<SequenceI> rseqs = new ArrayList<SequenceI>();
140           CrossRef.searchDatasetXrefs(seq, !dna, lrfs, dataset, rseqs,
141                   null); // don't need to specify codon frame for mapping here
142           for (SequenceI rs : rseqs)
143           {
144             DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRefs());
145             if (xrs != null)
146             {
147               for (DBRefEntry ref : xrs)
148               {
149                 if (!refs.contains(ref.getSource()))
150                 {
151                   refs.add(ref.getSource());
152                 }
153               }
154             }
155             // looks like copy and paste - change rfs to xrs?
156             // for (int r = 0; rfs != null && r < rfs.length; r++)
157             // {
158             // if (!refs.contains(rfs[r].getSource()))
159             // {
160             // refs.add(rfs[r].getSource());
161             // }
162             // }
163           }
164         }
165       }
166     }
167     if (refs.size() > 0)
168     {
169       dbrefs = new String[refs.size()];
170       refs.toArray(dbrefs);
171     }
172     return dbrefs;
173   }
174
175   public static boolean hasCdnaMap(SequenceI[] seqs)
176   {
177     // TODO unused - remove?
178     String[] reftypes = findSequenceXrefTypes(false, seqs);
179     for (int s = 0; s < reftypes.length; s++)
180     {
181       if (reftypes.equals(DBRefSource.EMBLCDS))
182       {
183         return true;
184         // no map
185       }
186     }
187     return false;
188   }
189
190   public static SequenceI[] getCdnaMap(SequenceI[] seqs)
191   {
192     // TODO unused - remove?
193     Vector cseqs = new Vector();
194     for (int s = 0; s < seqs.length; s++)
195     {
196       DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRefs());
197       for (int c = 0; c < cdna.length; c++)
198       {
199         if (cdna[c].getSource().equals(DBRefSource.EMBLCDS))
200         {
201           System.err
202                   .println("TODO: unimplemented sequence retrieval for coding region sequence.");
203           // TODO: retrieve CDS dataset sequences
204           // need global dataset sequence retriever/resolver to reuse refs
205           // and construct Mapping entry.
206           // insert gaps in CDS according to peptide gaps.
207           // add gapped sequence to cseqs
208         }
209       }
210     }
211     if (cseqs.size() > 0)
212     {
213       SequenceI[] rsqs = new SequenceI[cseqs.size()];
214       cseqs.copyInto(rsqs);
215       return rsqs;
216     }
217     return null;
218
219   }
220
221   /**
222    * 
223    * @param seqs
224    *          sequences whose xrefs are being retrieved
225    * @param dna
226    *          true if sequences are nucleotide
227    * @param source
228    * @param al
229    *          alignment to search for cross-referenced sequences (and possibly
230    *          add to)
231    * @param addedPeers
232    *          a list of sequences to add to if 'peers' to the original sequences
233    *          are found e.g. alternative protein products for a protein's gene
234    * @return products (as dataset sequences)
235    */
236   public static Alignment findXrefSequences(SequenceI[] seqs,
237           final boolean dna, final String source, AlignmentI al,
238           List<SequenceI> addedPeers)
239   {
240     AlignmentI dataset = al.getDataset() == null ? al : al.getDataset();
241     List<SequenceI> rseqs = new ArrayList<SequenceI>();
242     AlignedCodonFrame cf = new AlignedCodonFrame();
243     for (SequenceI seq : seqs)
244     {
245       SequenceI dss = seq;
246       while (dss.getDatasetSequence() != null)
247       {
248         dss = dss.getDatasetSequence();
249       }
250       boolean found = false;
251       DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRefs());
252       if ((xrfs == null || xrfs.length == 0) && dataset != null)
253       {
254         System.out.println("Attempting to find ds Xrefs refs.");
255         // FIXME should be dss not seq here?
256         DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRefs());
257         // less ambiguous would be a 'find primary dbRefEntry' method.
258         // filter for desired source xref here
259         found = CrossRef.searchDatasetXrefs(dss, !dna, lrfs, dataset,
260                 rseqs, cf);
261       }
262       for (int r = 0; xrfs != null && r < xrfs.length; r++)
263       {
264         DBRefEntry xref = xrfs[r];
265         if (source != null && !source.equals(xref.getSource()))
266         {
267           continue;
268         }
269         if (xref.hasMap())
270         {
271           if (xref.getMap().getTo() != null)
272           {
273             SequenceI rsq = new Sequence(xref.getMap().getTo());
274             rseqs.add(rsq);
275             if (xref.getMap().getMap().getFromRatio() != xref
276                     .getMap().getMap().getToRatio())
277             {
278               // get sense of map correct for adding to product alignment.
279               if (dna)
280               {
281                 // map is from dna seq to a protein product
282                 cf.addMap(dss, rsq, xref.getMap().getMap());
283               }
284               else
285               {
286                 // map should be from protein seq to its coding dna
287                 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse());
288               }
289             }
290             found = true;
291           }
292         }
293         if (!found)
294         {
295           // do a bit more work - search for sequences with references matching
296           // xrefs on this sequence.
297           if (dataset != null)
298           {
299             found |= searchDataset(dss, xref, dataset, rseqs, cf, false,
300                     !dna);
301             // ,false,!dna);
302             if (found)
303             {
304               xrfs[r] = null; // we've recovered seqs for this one.
305             }
306           }
307         }
308       }
309       if (!found)
310       {
311         if (xrfs != null && xrfs.length > 0)
312         {
313           // Try and get the sequence reference...
314           /*
315            * Ideal world - we ask for a sequence fetcher implementation here if
316            * (jalview.io.RunTimeEnvironment.getSequenceFetcher()) (
317            */
318           ASequenceFetcher sftch = new SequenceFetcher();
319           SequenceI[] retrieved = null;
320           int l = xrfs.length;
321           for (int r = 0; r < xrfs.length; r++)
322           {
323             // filter out any irrelevant or irretrievable references
324             if (xrfs[r] == null
325                     || ((source != null && !source.equals(xrfs[r]
326                             .getSource())) || !sftch.isFetchable(xrfs[r]
327                             .getSource())))
328             {
329               l--;
330               xrfs[r] = null;
331             }
332           }
333           if (l > 0)
334           {
335             System.out
336                     .println("Attempting to retrieve cross referenced sequences.");
337             DBRefEntry[] t = new DBRefEntry[l];
338             l = 0;
339             for (int r = 0; r < xrfs.length; r++)
340             {
341               if (xrfs[r] != null)
342               {
343                 t[l++] = xrfs[r];
344               }
345             }
346             xrfs = t;
347             try
348             {
349               retrieved = sftch.getSequences(xrfs, !dna);
350               // problem here is we don't know which of xrfs resulted in which
351               // retrieved element
352             } catch (Exception e)
353             {
354               System.err
355                       .println("Problem whilst retrieving cross references for Sequence : "
356                               + seq.getName());
357               e.printStackTrace();
358             }
359
360             if (retrieved != null)
361             {
362               updateDbrefMappings(dna, seq, xrfs, retrieved, cf);
363
364               SequenceIdMatcher matcher = new SequenceIdMatcher(
365                       dataset.getSequences());
366               matcher.addAll(addedPeers);
367               List<SequenceFeature> copiedFeatures = new ArrayList<SequenceFeature>();
368               CrossRef me = new CrossRef();
369               for (int rs = 0; rs < retrieved.length; rs++)
370               {
371                 // TODO: examine each sequence for 'redundancy'
372                 DBRefEntry[] dbr = retrieved[rs].getDBRefs();
373                 if (dbr != null && dbr.length > 0)
374                 {
375                   for (int di = 0; di < dbr.length; di++)
376                   {
377                     // find any entry where we should put in the sequence being
378                     // cross-referenced into the map
379                     Mapping map = dbr[di].getMap();
380                     if (map != null)
381                     {
382                       if (map.getTo() != null && map.getMap() != null)
383                       {
384                         SequenceI matched = matcher
385                                 .findIdMatch(map.getTo());
386                         if (matched != null)
387                         {
388                           /*
389                            * already got an xref to this sequence; update this
390                            * map to point to the same sequence, and add
391                            * any new dbrefs to it
392                            */
393                           for (DBRefEntry ref : map.getTo().getDBRefs())
394                           {
395                             matched.addDBRef(ref);
396                           }
397                           map.setTo(matched);
398                         }
399                         else
400                         {
401                           matcher.add(map.getTo());
402                         }
403                         try
404                         {
405                           // compare ms with dss and replace with dss in mapping
406                           // if map is congruent
407                           SequenceI ms = map.getTo();
408                           int sf = map.getMap().getToLowest();
409                           int st = map.getMap().getToHighest();
410                           SequenceI mappedrg = ms.getSubSequence(sf, st);
411                           // SequenceI loc = dss.getSubSequence(sf, st);
412                           if (mappedrg.getLength() > 0
413                                   && ms.getSequenceAsString().equals(
414                                           dss.getSequenceAsString()))
415                           // && mappedrg.getSequenceAsString().equals(
416                           // loc.getSequenceAsString()))
417                           {
418                             String msg = "Mapping updated from "
419                                     + ms.getName()
420                                     + " to retrieved crossreference "
421                                     + dss.getName();
422                             System.out.println(msg);
423                             // method to update all refs of existing To on
424                             // retrieved sequence with dss and merge any props
425                             // on To onto dss.
426                             map.setTo(dss);
427                             /*
428                              * copy sequence features as well, avoiding
429                              * duplication (e.g. from 2 transcripts)
430                              */
431                             SequenceFeature[] sfs = ms
432                                     .getSequenceFeatures();
433                             if (sfs != null)
434                             {
435                               for (SequenceFeature feat : sfs)
436                               {
437                                 /* 
438                                  * we override SequenceFeature.equals here (but
439                                  * not elsewhere) to ignore Parent attribute
440                                  * TODO not quite working yet!
441                                  */
442                                 if (!copiedFeatures
443                                         .contains(me.new MySequenceFeature(
444                                                 feat)))
445                                 {
446                                   dss.addSequenceFeature(feat);
447                                   copiedFeatures.add(feat);
448                                 }
449                               }
450                             }
451                             cf.addMap(retrieved[rs].getDatasetSequence(),
452                                     dss, map.getMap());
453                           }
454                           else
455                           {
456                             if (!addedPeers.contains(map.getTo()))
457                             {
458                               addedPeers.add(map.getTo());
459                             }
460                             cf.addMap(retrieved[rs].getDatasetSequence(),
461                                     map.getTo(), map.getMap());
462                           }
463                         } catch (Exception e)
464                         {
465                           System.err
466                                   .println("Exception when consolidating Mapped sequence set...");
467                           e.printStackTrace(System.err);
468                         }
469                       }
470                     }
471                   }
472                 }
473                 retrieved[rs].updatePDBIds();
474                 rseqs.add(retrieved[rs]);
475               }
476             }
477           }
478         }
479       }
480     }
481
482     Alignment ral = null;
483     if (rseqs.size() > 0)
484     {
485       ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
486       if (cf != null && !cf.isEmpty())
487       {
488         ral.addCodonFrame(cf);
489       }
490     }
491     return ral;
492   }
493
494   /**
495    * Updates any empty mappings in the cross-references with one to a compatible
496    * retrieved sequence if found, and adds any new mappings to the
497    * AlignedCodonFrame
498    * 
499    * @param dna
500    * @param mapFrom
501    * @param xrefs
502    * @param retrieved
503    * @param acf
504    */
505   static void updateDbrefMappings(boolean dna, SequenceI mapFrom,
506           DBRefEntry[] xrefs, SequenceI[] retrieved, AlignedCodonFrame acf)
507   {
508     SequenceIdMatcher matcher = new SequenceIdMatcher(retrieved);
509     for (DBRefEntry xref : xrefs)
510     {
511       if (!xref.hasMap())
512       {
513         String targetSeqName = xref.getSource() + "|"
514                 + xref.getAccessionId();
515         SequenceI[] matches = matcher.findAllIdMatches(targetSeqName);
516         if (matches == null)
517         {
518           return;
519         }
520         for (SequenceI seq : matches)
521         {
522           MapList mapping = null;
523           if (dna)
524           {
525             mapping = AlignmentUtils.mapCdnaToProtein(seq, mapFrom);
526           }
527           else
528           {
529             mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, seq);
530             if (mapping != null)
531             {
532               mapping = mapping.getInverse();
533             }
534           }
535           if (mapping != null)
536           {
537             xref.setMap(new Mapping(seq, mapping));
538             if (dna)
539             {
540               AlignmentUtils.computeProteinFeatures(mapFrom, seq, mapping);
541             }
542             if (dna)
543             {
544               acf.addMap(mapFrom, seq, mapping);
545             }
546             else
547             {
548               acf.addMap(seq, mapFrom, mapping.getInverse());
549             }
550             continue;
551           }
552         }
553       }
554     }
555   }
556
557   /**
558    * find references to lrfs in the cross-reference set of each sequence in
559    * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
560    * based on source and accession string only - Map and Version are nulled.
561    * 
562    * @param sequenceI
563    * @param lrfs
564    * @param dataset
565    * @param rseqs
566    * @return true if matches were found.
567    */
568   private static boolean searchDatasetXrefs(SequenceI sequenceI,
569           boolean dna, DBRefEntry[] lrfs, AlignmentI dataset,
570           List<SequenceI> rseqs, AlignedCodonFrame cf)
571   {
572     boolean found = false;
573     if (lrfs == null)
574     {
575       return false;
576     }
577     for (int i = 0; i < lrfs.length; i++)
578     {
579       DBRefEntry xref = new DBRefEntry(lrfs[i]);
580       // add in wildcards
581       xref.setVersion(null);
582       xref.setMap(null);
583       found = searchDataset(sequenceI, xref, dataset, rseqs, cf, false, dna);
584     }
585     return found;
586   }
587
588   /**
589    * search a given sequence dataset for references matching cross-references to
590    * the given sequence
591    * 
592    * @param sequenceI
593    * @param xrf
594    * @param dataset
595    * @param rseqs
596    *          set of unique sequences
597    * @param cf
598    * @return true if one or more unique sequences were found and added
599    */
600   public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
601           AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf)
602   {
603     return searchDataset(sequenceI, xrf, dataset, rseqs, cf, true, false);
604   }
605
606   /**
607    * TODO: generalise to different protein classifications Search dataset for
608    * DBRefEntrys matching the given one (xrf) and add the associated sequence to
609    * rseq.
610    * 
611    * @param sequenceI
612    * @param xrf
613    * @param dataset
614    * @param rseqs
615    * @param direct
616    *          - search all references or only subset
617    * @param dna
618    *          search dna or protein xrefs (if direct=false)
619    * @return true if relationship found and sequence added.
620    */
621   public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
622           AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf,
623           boolean direct, boolean dna)
624   {
625     boolean found = false;
626     SequenceI[] typer = new SequenceI[1];
627     if (dataset == null)
628     {
629       return false;
630     }
631     if (dataset.getSequences() == null)
632     {
633       System.err.println("Empty dataset sequence set - NO VECTOR");
634       return false;
635     }
636     List<SequenceI> ds;
637     synchronized (ds = dataset.getSequences())
638     {
639       for (SequenceI nxt : ds)
640       {
641         if (nxt != null)
642         {
643           if (nxt.getDatasetSequence() != null)
644           {
645             System.err
646                     .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
647           }
648           if (nxt != sequenceI && nxt != sequenceI.getDatasetSequence())
649           {
650             // check if this is the correct sequence type
651             {
652               typer[0] = nxt;
653               boolean isDna = jalview.util.Comparison.isNucleotide(typer);
654               if ((direct && isDna == dna) || (!direct && isDna != dna))
655               {
656                 // skip this sequence because it is same molecule type
657                 continue;
658               }
659             }
660
661             // look for direct or indirect references in common
662             DBRefEntry[] poss = nxt.getDBRefs(), cands = null;
663             if (direct)
664             {
665               cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
666             }
667             else
668             {
669               poss = CrossRef.findXDbRefs(dna, poss); //
670               cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
671             }
672             if (cands != null)
673             {
674               if (!rseqs.contains(nxt))
675               {
676                 rseqs.add(nxt);
677                 boolean foundmap = cf != null;
678                 // don't search if we aren't given a codon map object
679                 for (int r = 0; foundmap && r < cands.length; r++)
680                 {
681                   if (cands[r].hasMap())
682                   {
683                     if (cands[r].getMap().getTo() != null
684                             && cands[r].getMap().getMap().getFromRatio() != cands[r]
685                                     .getMap().getMap().getToRatio())
686                     {
687                       foundmap = true;
688                       // get sense of map correct for adding to product
689                       // alignment.
690                       if (dna)
691                       {
692                         // map is from dna seq to a protein product
693                         cf.addMap(sequenceI, nxt, cands[r].getMap()
694                                 .getMap());
695                       }
696                       else
697                       {
698                         // map should be from protein seq to its coding dna
699                         cf.addMap(nxt, sequenceI, cands[r].getMap()
700                                 .getMap().getInverse());
701                       }
702                     }
703                   }
704                 }
705                 // TODO: add mapping between sequences if necessary
706                 found = true;
707               }
708             }
709
710           }
711         }
712       }
713     }
714     return found;
715   }
716
717   /**
718    * precalculate different products that can be found for seqs in dataset and
719    * return them.
720    * 
721    * @param dna
722    * @param seqs
723    * @param dataset
724    * @param fake
725    *          - don't actually build lists - just get types
726    * @return public static Object[] buildXProductsList(boolean dna, SequenceI[]
727    *         seqs, AlignmentI dataset, boolean fake) { String types[] =
728    *         jalview.analysis.CrossRef.findSequenceXrefTypes( dna, seqs,
729    *         dataset); if (types != null) { System.out.println("Xref Types for:
730    *         "+(dna ? "dna" : "prot")); for (int t = 0; t < types.length; t++) {
731    *         System.out.println("Type: " + types[t]); SequenceI[] prod =
732    *         jalview.analysis.CrossRef.findXrefSequences(seqs, dna, types[t]);
733    *         System.out.println("Found " + ((prod == null) ? "no" : "" +
734    *         prod.length) + " products"); if (prod!=null) { for (int p=0;
735    *         p<prod.length; p++) { System.out.println("Prod "+p+":
736    *         "+prod[p].getDisplayId(true)); } } } } else {
737    *         System.out.println("Trying getProducts for
738    *         "+al.getSequenceAt(0).getDisplayId(true));
739    *         System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot"));
740    *         // have a bash at finding the products amongst all the retrieved
741    *         sequences. SequenceI[] prod =
742    *         jalview.analysis.CrossRef.findXrefSequences(al
743    *         .getSequencesArray(), dna, null, ds); System.out.println("Found " +
744    *         ((prod == null) ? "no" : "" + prod.length) + " products"); if
745    *         (prod!=null) { // select non-equivalent sequences from dataset list
746    *         for (int p=0; p<prod.length; p++) { System.out.println("Prod "+p+":
747    *         "+prod[p].getDisplayId(true)); } } } }
748    */
749 }