JAL-3438 spotless for 2.11.2.0
[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.SequenceFetcherFactory;
35 import jalview.ws.seqfetcher.ASequenceFetcher;
36
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40
41 /**
42  * Functions for cross-referencing sequence databases.
43  * 
44  * @author JimP
45  * 
46  */
47 public class CrossRef
48 {
49   /*
50    * the dataset of the alignment for which we are searching for 
51    * cross-references; in some cases we may resolve xrefs by 
52    * searching in the dataset
53    */
54   private AlignmentI dataset;
55
56   /*
57    * the sequences for which we are seeking cross-references
58    */
59   private SequenceI[] fromSeqs;
60
61   /**
62    * matcher built from dataset
63    */
64   SequenceIdMatcher matcher;
65
66   /**
67    * sequences found by cross-ref searches to fromSeqs
68    */
69   List<SequenceI> rseqs;
70
71   /**
72    * Constructor
73    * 
74    * @param seqs
75    *          the sequences for which we are seeking cross-references
76    * @param ds
77    *          the containing alignment dataset (may be searched to resolve
78    *          cross-references)
79    */
80   public CrossRef(SequenceI[] seqs, AlignmentI ds)
81   {
82     fromSeqs = seqs;
83     dataset = ds.getDataset() == null ? ds : ds.getDataset();
84   }
85
86   /**
87    * Returns a list of distinct database sources for which sequences have either
88    * <ul>
89    * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
90    * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
91    * reference from another sequence in the dataset which has a cross-reference
92    * to a direct DBRefEntry on the given sequence</li>
93    * </ul>
94    * 
95    * @param dna
96    *          - when true, cross-references *from* dna returned. When false,
97    *          cross-references *from* protein are returned
98    * @return
99    */
100   public List<String> findXrefSourcesForSequences(boolean dna)
101   {
102     List<String> sources = new ArrayList<>();
103     for (SequenceI seq : fromSeqs)
104     {
105       if (seq != null)
106       {
107         findXrefSourcesForSequence(seq, dna, sources);
108       }
109     }
110     sources.remove(DBRefSource.EMBL); // hack to prevent EMBL xrefs resulting in
111                                       // redundant datasets
112     if (dna)
113     {
114       sources.remove(DBRefSource.ENSEMBL); // hack to prevent Ensembl and
115                                            // EnsemblGenomes xref option shown
116                                            // from cdna panel
117       sources.remove(DBRefSource.ENSEMBLGENOMES);
118     }
119     // redundant datasets
120     return sources;
121   }
122
123   /**
124    * Returns a list of distinct database sources for which a sequence has either
125    * <ul>
126    * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
127    * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
128    * reference from another sequence in the dataset which has a cross-reference
129    * to a direct DBRefEntry on the given sequence</li>
130    * </ul>
131    * 
132    * @param seq
133    *          the sequence whose dbrefs we are searching against
134    * @param fromDna
135    *          when true, context is DNA - so sources identifying protein
136    *          products will be returned.
137    * @param sources
138    *          a list of sources to add matches to
139    */
140   void findXrefSourcesForSequence(SequenceI seq, boolean fromDna,
141           List<String> sources)
142   {
143     /*
144      * first find seq's xrefs (dna-to-peptide or peptide-to-dna)
145      */
146     List<DBRefEntry> rfs = DBRefUtils.selectDbRefs(!fromDna,
147             seq.getDBRefs());
148     addXrefsToSources(rfs, sources);
149     if (dataset != null)
150     {
151       /*
152        * find sequence's direct (dna-to-dna, peptide-to-peptide) xrefs
153        */
154       List<DBRefEntry> lrfs = DBRefUtils.selectDbRefs(fromDna,
155               seq.getDBRefs());
156       List<SequenceI> foundSeqs = new ArrayList<>();
157
158       /*
159        * find sequences in the alignment which xref one of these DBRefs
160        * i.e. is xref-ed to a common sequence identifier
161        */
162       searchDatasetXrefs(fromDna, seq, lrfs, foundSeqs, null);
163
164       /*
165        * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
166        */
167       for (SequenceI rs : foundSeqs)
168       {
169         List<DBRefEntry> xrs = DBRefUtils.selectDbRefs(!fromDna,
170                 rs.getDBRefs());
171         addXrefsToSources(xrs, sources);
172       }
173     }
174   }
175
176   /**
177    * Helper method that adds the source identifiers of some cross-references to
178    * a (non-redundant) list of database sources
179    * 
180    * @param xrefs
181    * @param sources
182    */
183   void addXrefsToSources(List<DBRefEntry> xrefs, List<String> sources)
184   {
185     if (xrefs != null)
186     {
187       for (DBRefEntry ref : xrefs)
188       {
189         /*
190          * avoid duplication e.g. ENSEMBL and Ensembl
191          */
192         String source = DBRefUtils.getCanonicalName(ref.getSource());
193         if (!sources.contains(source))
194         {
195           sources.add(source);
196         }
197       }
198     }
199   }
200
201   /**
202    * Attempts to find cross-references from the sequences provided in the
203    * constructor to the given source database. Cross-references may be found
204    * <ul>
205    * <li>in dbrefs on the sequence which hold a mapping to a sequence
206    * <ul>
207    * <li>provided with a fetched sequence (e.g. ENA translation), or</li>
208    * <li>populated previously after getting cross-references</li>
209    * </ul>
210    * <li>as other sequences in the alignment which share a dbref identifier with
211    * the sequence</li>
212    * <li>by fetching from the remote database</li>
213    * </ul>
214    * The cross-referenced sequences, and mappings to them, are added to the
215    * alignment dataset.
216    * 
217    * @param source
218    * @return cross-referenced sequences (as dataset sequences)
219    */
220   public Alignment findXrefSequences(String source, boolean fromDna)
221   {
222
223     rseqs = new ArrayList<>();
224     AlignedCodonFrame cf = new AlignedCodonFrame();
225     matcher = new SequenceIdMatcher(dataset.getSequences());
226
227     for (SequenceI seq : fromSeqs)
228     {
229       SequenceI dss = seq;
230       while (dss.getDatasetSequence() != null)
231       {
232         dss = dss.getDatasetSequence();
233       }
234       boolean found = false;
235       List<DBRefEntry> xrfs = DBRefUtils.selectDbRefs(!fromDna,
236               dss.getDBRefs());
237       // ENST & ENSP comes in to both Protein and nucleotide, so we need to
238       // filter them
239       // out later.
240       if ((xrfs == null || xrfs.size() == 0) && dataset != null)
241       {
242         /*
243          * found no suitable dbrefs on sequence - look for sequences in the
244          * alignment which share a dbref with this one
245          */
246         List<DBRefEntry> lrfs = DBRefUtils.selectDbRefs(fromDna,
247                 seq.getDBRefs());
248
249         /*
250          * find sequences (except this one!), of complementary type,
251          *  which have a dbref to an accession id for this sequence,
252          *  and add them to the results
253          */
254         found = searchDatasetXrefs(fromDna, dss, lrfs, rseqs, cf);
255       }
256       if (xrfs == null && !found)
257       {
258         /*
259          * no dbref to source on this sequence or matched
260          * complementary sequence in the dataset 
261          */
262         continue;
263       }
264       List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(xrfs,
265               source);
266       Iterator<DBRefEntry> refIterator = sourceRefs.iterator();
267       // At this point, if we are retrieving Ensembl, we still don't filter out
268       // ENST when looking for protein crossrefs.
269       while (refIterator.hasNext())
270       {
271         DBRefEntry xref = refIterator.next();
272         found = false;
273         // we're only interested in coding cross-references, not
274         // locus->transcript
275         if (xref.hasMap() && xref.getMap().getMap().isTripletMap())
276         {
277           SequenceI mappedTo = xref.getMap().getTo();
278           if (mappedTo != null)
279           {
280             /*
281              * dbref contains the sequence it maps to; add it to the
282              * results unless we have done so already (could happen if 
283              * fetching xrefs for sequences which have xrefs in common)
284              * for example: UNIPROT {P0CE19, P0CE20} -> EMBL {J03321, X06707}
285              */
286             found = true;
287             /*
288              * problem: matcher.findIdMatch() is lenient - returns a sequence
289              * with a dbref to the search arg e.g. ENST for ENSP - wrong
290              * but findInDataset() matches ENSP when looking for Uniprot...
291              */
292             SequenceI matchInDataset = findInDataset(xref);
293             if (matchInDataset != null && xref.getMap().getTo() != null
294                     && matchInDataset != xref.getMap().getTo())
295             {
296               System.err.println(
297                       "Implementation problem (reopen JAL-2154): CrossRef.findInDataset seems to have recovered a different sequence than the one explicitly mapped for xref."
298                               + "Found:" + matchInDataset + "\nExpected:"
299                               + xref.getMap().getTo() + "\nFor xref:"
300                               + xref);
301             }
302             /*matcher.findIdMatch(mappedTo);*/
303             if (matchInDataset != null)
304             {
305               if (!rseqs.contains(matchInDataset))
306               {
307                 rseqs.add(matchInDataset);
308               }
309               // even if rseqs contained matchInDataset - check mappings between
310               // these seqs are added
311               // need to try harder to only add unique mappings
312               if (xref.getMap().getMap().isTripletMap()
313                       && dataset.getMapping(seq, matchInDataset) == null
314                       && cf.getMappingBetween(seq, matchInDataset) == null)
315               {
316                 // materialise a mapping for highlighting between these
317                 // sequences
318                 if (fromDna)
319                 {
320                   cf.addMap(dss, matchInDataset, xref.getMap().getMap(),
321                           xref.getMap().getMappedFromId());
322                 }
323                 else
324                 {
325                   cf.addMap(matchInDataset, dss,
326                           xref.getMap().getMap().getInverse(),
327                           xref.getMap().getMappedFromId());
328                 }
329               }
330
331               refIterator.remove();
332               continue;
333             }
334             // TODO: need to determine if this should be a deriveSequence
335             SequenceI rsq = new Sequence(mappedTo);
336             rseqs.add(rsq);
337             if (xref.getMap().getMap().isTripletMap())
338             {
339               // get sense of map correct for adding to product alignment.
340               if (fromDna)
341               {
342                 // map is from dna seq to a protein product
343                 cf.addMap(dss, rsq, xref.getMap().getMap(),
344                         xref.getMap().getMappedFromId());
345               }
346               else
347               {
348                 // map should be from protein seq to its coding dna
349                 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse(),
350                         xref.getMap().getMappedFromId());
351               }
352             }
353           }
354         }
355
356         if (!found)
357         {
358           SequenceI matchedSeq = matcher.findIdMatch(
359                   xref.getSource() + "|" + xref.getAccessionId());
360           // if there was a match, check it's at least the right type of
361           // molecule!
362           if (matchedSeq != null && matchedSeq.isProtein() == fromDna)
363           {
364             if (constructMapping(seq, matchedSeq, xref, cf, fromDna))
365             {
366               found = true;
367             }
368           }
369         }
370
371         if (!found)
372         {
373           // do a bit more work - search for sequences with references matching
374           // xrefs on this sequence.
375           found = searchDataset(fromDna, dss, xref, rseqs, cf, false,
376                   DBRefUtils.SEARCH_MODE_FULL);
377         }
378         if (found)
379         {
380           refIterator.remove();
381         }
382       }
383
384       /*
385        * fetch from source database any dbrefs we haven't resolved up to here
386        */
387       if (!sourceRefs.isEmpty())
388       {
389         retrieveCrossRef(sourceRefs, seq, xrfs, fromDna, cf);
390       }
391     }
392
393     Alignment ral = null;
394     if (rseqs.size() > 0)
395     {
396       ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
397       if (!cf.isEmpty())
398       {
399         dataset.addCodonFrame(cf);
400       }
401     }
402     return ral;
403   }
404
405   private void retrieveCrossRef(List<DBRefEntry> sourceRefs, SequenceI seq,
406           List<DBRefEntry> xrfs, boolean fromDna, AlignedCodonFrame cf)
407   {
408     ASequenceFetcher sftch = SequenceFetcherFactory.getSequenceFetcher();
409     SequenceI[] retrieved = null;
410     SequenceI dss = seq.getDatasetSequence() == null ? seq
411             : seq.getDatasetSequence();
412     // first filter in case we are retrieving crossrefs that have already been
413     // retrieved. this happens for cases where a database record doesn't yield
414     // protein products for CDS
415     removeAlreadyRetrievedSeqs(sourceRefs, fromDna);
416     if (sourceRefs.size() == 0)
417     {
418       // no more work to do! We already had all requested sequence records in
419       // the dataset.
420       return;
421     }
422     try
423     {
424       retrieved = sftch.getSequences(sourceRefs, !fromDna);
425     } catch (Exception e)
426     {
427       System.err.println(
428               "Problem whilst retrieving cross references for Sequence : "
429                       + seq.getName());
430       e.printStackTrace();
431     }
432
433     if (retrieved != null)
434     {
435       boolean addedXref = false;
436       List<SequenceI> newDsSeqs = new ArrayList<>(),
437               doNotAdd = new ArrayList<>();
438
439       for (SequenceI retrievedSequence : retrieved)
440       {
441         // dataset gets contaminated ccwith non-ds sequences. why ??!
442         // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
443         SequenceI retrievedDss = retrievedSequence
444                 .getDatasetSequence() == null ? retrievedSequence
445                         : retrievedSequence.getDatasetSequence();
446         addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
447                 retrievedDss);
448       }
449       // JBPNote: What assumptions are made for dbref structures on
450       // retrieved sequences ?
451       // addedXref will be true means importCrossRefSeq found
452       // sequences with dbrefs with mappings to sequences congruent with dss
453
454       if (!addedXref)
455       {
456         // try again, after looking for matching IDs
457         // shouldn't need to do this unless the dbref mechanism has broken.
458         updateDbrefMappings(seq, xrfs, retrieved, cf, fromDna);
459         for (SequenceI retrievedSequence : retrieved)
460         {
461           // dataset gets contaminated ccwith non-ds sequences. why ??!
462           // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
463           SequenceI retrievedDss = retrievedSequence
464                   .getDatasetSequence() == null ? retrievedSequence
465                           : retrievedSequence.getDatasetSequence();
466           addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
467                   retrievedDss);
468         }
469       }
470       for (SequenceI newToSeq : newDsSeqs)
471       {
472         if (!doNotAdd.contains(newToSeq)
473                 && dataset.findIndex(newToSeq) == -1)
474         {
475           dataset.addSequence(newToSeq);
476           matcher.add(newToSeq);
477         }
478       }
479     }
480   }
481
482   /**
483    * Search dataset for sequences with a primary reference contained in
484    * sourceRefs.
485    * 
486    * @param sourceRefs
487    *          - list of references to filter.
488    * @param fromDna
489    *          - type of sequence to search for matching primary reference.
490    */
491   private void removeAlreadyRetrievedSeqs(List<DBRefEntry> sourceRefs,
492           boolean fromDna)
493   {
494     List<DBRefEntry> dbrSourceSet = new ArrayList<>(sourceRefs);
495     List<SequenceI> dsSeqs = dataset.getSequences();
496     for (int ids = 0, nds = dsSeqs.size(); ids < nds; ids++)
497     {
498       SequenceI sq = dsSeqs.get(ids);
499       boolean dupeFound = false;
500       // !fromDna means we are looking only for nucleotide sequences, not
501       // protein
502       if (sq.isProtein() == fromDna)
503       {
504         List<DBRefEntry> sqdbrefs = sq.getPrimaryDBRefs();
505         for (int idb = 0, ndb = sqdbrefs.size(); idb < ndb; idb++)
506         {
507           DBRefEntry dbr = sqdbrefs.get(idb);
508           List<DBRefEntry> searchrefs = DBRefUtils.searchRefs(dbrSourceSet,
509                   dbr, DBRefUtils.SEARCH_MODE_FULL);
510           for (int isr = 0, nsr = searchrefs.size(); isr < nsr; isr++)
511           {
512             sourceRefs.remove(searchrefs.get(isr));
513             dupeFound = true;
514           }
515         }
516       }
517       if (dupeFound)
518       {
519         // rebuild the search array from the filtered sourceRefs list
520         dbrSourceSet.clear();
521         dbrSourceSet.addAll(sourceRefs);
522       }
523     }
524   }
525
526   /**
527    * process sequence retrieved via a dbref on source sequence to resolve and
528    * transfer data JBPNote: as of 2022-02-03 - this assumes retrievedSequence
529    * has dbRefs with Mapping references to a sequence congruent with
530    * sourceSequence
531    * 
532    * @param cf
533    * @param sourceSequence
534    * @param retrievedSequence
535    * @return true if retrieveSequence was imported
536    */
537   private boolean importCrossRefSeq(AlignedCodonFrame cf,
538           List<SequenceI> newDsSeqs, List<SequenceI> doNotAdd,
539           SequenceI sourceSequence, SequenceI retrievedSequence)
540   {
541     /**
542      * set when retrievedSequence has been verified as a crossreference for
543      * sourceSequence
544      */
545     boolean imported = false;
546     List<DBRefEntry> dbr = retrievedSequence.getDBRefs();
547     if (dbr != null)
548     {
549       for (int ib = 0, nb = dbr.size(); ib < nb; ib++)
550       {
551
552         DBRefEntry dbref = dbr.get(ib);
553         // matched will return null if the dbref has no map
554         SequenceI matched = findInDataset(dbref);
555         if (matched == sourceSequence)
556         {
557           // verified retrieved and source sequence cross-reference each other
558           imported = true;
559         }
560         // find any entry where we should put in the sequence being
561         // cross-referenced into the map
562         Mapping map = dbref.getMap();
563         if (map != null)
564         {
565           SequenceI ms = map.getTo();
566           if (ms != null && map.getMap() != null)
567           {
568             if (ms == sourceSequence)
569             {
570               // already called to import once, and most likely this sequence
571               // already imported !
572               continue;
573             }
574             if (matched == null)
575             {
576               /*
577                * sequence is new to dataset, so save a reference so it can be added. 
578                */
579               newDsSeqs.add(ms);
580               continue;
581             }
582
583             /*
584              * there was a matching sequence in dataset, so now, check to see if we can update the map.getTo() sequence to the existing one.
585              */
586
587             try
588             {
589               // compare ms with dss and replace with dss in mapping
590               // if map is congruent
591               // TODO findInDataset requires exact sequence match but
592               // 'congruent' test is only for the mapped part
593               // maybe not a problem in practice since only ENA provide a
594               // mapping and it is to the full protein translation of CDS
595               // matcher.findIdMatch(map.getTo());
596               // TODO addendum: if matched is shorter than getTo, this will fail
597               // - when it should really succeed.
598               int sf = map.getMap().getToLowest();
599               int st = map.getMap().getToHighest();
600               SequenceI mappedrg = ms.getSubSequence(sf, st);
601               if (mappedrg.getLength() > 0 && ms.getSequenceAsString()
602                       .equals(matched.getSequenceAsString()))
603               {
604                 /*
605                  * sequences were a match, 
606                  */
607                 String msg = "Mapping updated from " + ms.getName()
608                         + " to retrieved crossreference "
609                         + matched.getName();
610                 System.out.println(msg);
611
612                 List<DBRefEntry> toRefs = map.getTo().getDBRefs();
613                 if (toRefs != null)
614                 {
615                   /*
616                    * transfer database refs
617                    */
618                   for (DBRefEntry ref : toRefs)
619                   {
620                     if (dbref.getSrcAccString()
621                             .equals(ref.getSrcAccString()))
622                     {
623                       continue; // avoid overwriting the ref on source sequence
624                     }
625                     matched.addDBRef(ref); // add or update mapping
626                   }
627                 }
628                 doNotAdd.add(map.getTo());
629                 map.setTo(matched);
630
631                 /*
632                  * give the reverse reference the inverse mapping 
633                  * (if it doesn't have one already)
634                  */
635                 setReverseMapping(matched, dbref, cf);
636
637                 /*
638                  * copy sequence features as well, avoiding
639                  * duplication (e.g. same variation from two 
640                  * transcripts)
641                  */
642                 List<SequenceFeature> sfs = ms.getFeatures()
643                         .getAllFeatures();
644                 for (SequenceFeature feat : sfs)
645                 {
646                   /*
647                    * make a flyweight feature object which ignores Parent
648                    * attribute in equality test; this avoids creating many
649                    * otherwise duplicate exon features on genomic sequence
650                    */
651                   SequenceFeature newFeature = new SequenceFeature(feat)
652                   {
653                     @Override
654                     public boolean equals(Object o)
655                     {
656                       return super.equals(o, true);
657                     }
658                   };
659                   matched.addSequenceFeature(newFeature);
660                 }
661               }
662               cf.addMap(retrievedSequence, map.getTo(), map.getMap());
663             } catch (Exception e)
664             {
665               System.err.println(
666                       "Exception when consolidating Mapped sequence set...");
667               e.printStackTrace(System.err);
668             }
669           }
670         }
671       }
672     }
673     if (imported)
674     {
675       retrievedSequence.updatePDBIds();
676       rseqs.add(retrievedSequence);
677       if (dataset.findIndex(retrievedSequence) == -1)
678       {
679         dataset.addSequence(retrievedSequence);
680         matcher.add(retrievedSequence);
681       }
682     }
683     return imported;
684   }
685
686   /**
687    * Sets the inverse sequence mapping in the corresponding dbref of the mapped
688    * to sequence (if any). This is used after fetching a cross-referenced
689    * sequence, if the fetched sequence has a mapping to the original sequence,
690    * to set the mapping in the original sequence's dbref.
691    * 
692    * @param mapFrom
693    *          the sequence mapped from
694    * @param dbref
695    * @param mappings
696    */
697   void setReverseMapping(SequenceI mapFrom, DBRefEntry dbref,
698           AlignedCodonFrame mappings)
699   {
700     SequenceI mapTo = dbref.getMap().getTo();
701     if (mapTo == null)
702     {
703       return;
704     }
705     List<DBRefEntry> dbrefs = mapTo.getDBRefs();
706     if (dbrefs == null)
707     {
708       return;
709     }
710     for (DBRefEntry toRef : dbrefs)
711     {
712       if (toRef.hasMap() && mapFrom == toRef.getMap().getTo())
713       {
714         /*
715          * found the reverse dbref; update its mapping if null
716          */
717         if (toRef.getMap().getMap() == null)
718         {
719           MapList inverse = dbref.getMap().getMap().getInverse();
720           toRef.getMap().setMap(inverse);
721           mappings.addMap(mapTo, mapFrom, inverse);
722         }
723       }
724     }
725   }
726
727   /**
728    * Returns null or the first sequence in the dataset which is identical to
729    * xref.mapTo, and has a) a primary dbref matching xref, or if none found, the
730    * first one with an ID source|xrefacc JBPNote: Could refactor this to
731    * AlignmentI/DatasetI
732    * 
733    * @param xref
734    *          with map and mapped-to sequence
735    * @return
736    */
737   SequenceI findInDataset(DBRefEntry xref)
738   {
739     if (xref == null || !xref.hasMap() || xref.getMap().getTo() == null)
740     {
741       return null;
742     }
743     SequenceI mapsTo = xref.getMap().getTo();
744     String name = xref.getAccessionId();
745     String name2 = xref.getSource() + "|" + name;
746     SequenceI dss = mapsTo.getDatasetSequence() == null ? mapsTo
747             : mapsTo.getDatasetSequence();
748     // first check ds if ds is directly referenced
749     if (dataset.findIndex(dss) > -1)
750     {
751       return dss;
752     }
753     DBRefEntry template = new DBRefEntry(xref.getSource(), null,
754             xref.getAccessionId());
755     /**
756      * remember the first ID match - in case we don't find a match to template
757      */
758     SequenceI firstIdMatch = null;
759     for (SequenceI seq : dataset.getSequences())
760     {
761       // first check primary refs.
762       List<DBRefEntry> match = DBRefUtils.searchRefs(seq.getPrimaryDBRefs(),
763               template, DBRefUtils.SEARCH_MODE_FULL);
764       if (match != null && match.size() == 1 && sameSequence(seq, dss))
765       {
766         return seq;
767       }
768       /*
769        * clumsy alternative to using SequenceIdMatcher which currently
770        * returns sequences with a dbref to the matched accession id 
771        * which we don't want
772        */
773       if (firstIdMatch == null && (name.equals(seq.getName())
774               || seq.getName().startsWith(name2)))
775       {
776         if (sameSequence(seq, dss))
777         {
778           firstIdMatch = seq;
779         }
780       }
781     }
782     return firstIdMatch;
783   }
784
785   /**
786    * Answers true if seq1 and seq2 contain exactly the same characters (ignoring
787    * case), else false. This method compares the lengths, then each character in
788    * turn, in order to 'fail fast'. For case-sensitive comparison, it would be
789    * possible to use Arrays.equals(seq1.getSequence(), seq2.getSequence()).
790    * 
791    * @param seq1
792    * @param seq2
793    * @return
794    */
795   // TODO move to Sequence / SequenceI
796   static boolean sameSequence(SequenceI seq1, SequenceI seq2)
797   {
798     if (seq1 == seq2)
799     {
800       return true;
801     }
802     if (seq1 == null || seq2 == null)
803     {
804       return false;
805     }
806
807     if (seq1.getLength() != seq2.getLength())
808     {
809       return false;
810     }
811     int length = seq1.getLength();
812     for (int i = 0; i < length; i++)
813     {
814       int diff = seq1.getCharAt(i) - seq2.getCharAt(i);
815       /*
816        * same char or differ in case only ('a'-'A' == 32)
817        */
818       if (diff != 0 && diff != 32 && diff != -32)
819       {
820         return false;
821       }
822     }
823     return true;
824   }
825
826   /**
827    * Updates any empty mappings in the cross-references with one to a compatible
828    * retrieved sequence if found, and adds any new mappings to the
829    * AlignedCodonFrame JBPNote: TODO: this relies on sequence IDs like
830    * UNIPROT|ACCESSION - which do not always happen.
831    * 
832    * @param mapFrom
833    * @param xrefs
834    * @param retrieved
835    * @param acf
836    */
837   void updateDbrefMappings(SequenceI mapFrom, List<DBRefEntry> xrefs,
838           SequenceI[] retrieved, AlignedCodonFrame acf, boolean fromDna)
839   {
840     SequenceIdMatcher idMatcher = new SequenceIdMatcher(retrieved);
841     for (DBRefEntry xref : xrefs)
842     {
843       if (!xref.hasMap())
844       {
845         String targetSeqName = xref.getSource() + "|"
846                 + xref.getAccessionId();
847         SequenceI[] matches = idMatcher.findAllIdMatches(targetSeqName);
848         if (matches == null)
849         {
850           return;
851         }
852         for (SequenceI seq : matches)
853         {
854           constructMapping(mapFrom, seq, xref, acf, fromDna);
855         }
856       }
857     }
858   }
859
860   /**
861    * Tries to make a mapping between sequences. If successful, adds the mapping
862    * to the dbref and the mappings collection and answers true, otherwise
863    * answers false. The following methods of making are mapping are tried in
864    * turn:
865    * <ul>
866    * <li>if 'mapTo' holds a mapping to 'mapFrom', take the inverse; this is, for
867    * example, the case after fetching EMBL cross-references for a Uniprot
868    * sequence</li>
869    * <li>else check if the dna translates exactly to the protein (give or take
870    * start and stop codons></li>
871    * <li>else try to map based on CDS features on the dna sequence</li>
872    * </ul>
873    * 
874    * @param mapFrom
875    * @param mapTo
876    * @param xref
877    * @param mappings
878    * @return
879    */
880   boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
881           DBRefEntry xref, AlignedCodonFrame mappings, boolean fromDna)
882   {
883     MapList mapping = null;
884     SequenceI dsmapFrom = mapFrom.getDatasetSequence() == null ? mapFrom
885             : mapFrom.getDatasetSequence();
886     SequenceI dsmapTo = mapTo.getDatasetSequence() == null ? mapTo
887             : mapTo.getDatasetSequence();
888     /*
889      * look for a reverse mapping, if found make its inverse. 
890      * Note - we do this on dataset sequences only.
891      */
892     if (dsmapTo.getDBRefs() != null)
893     {
894       for (DBRefEntry dbref : dsmapTo.getDBRefs())
895       {
896         String name = dbref.getSource() + "|" + dbref.getAccessionId();
897         if (dbref.hasMap() && dsmapFrom.getName().startsWith(name))
898         {
899           /*
900            * looks like we've found a map from 'mapTo' to 'mapFrom'
901            * - invert it to make the mapping the other way 
902            */
903           MapList reverse = dbref.getMap().getMap().getInverse();
904           xref.setMap(new Mapping(dsmapTo, reverse));
905           mappings.addMap(mapFrom, dsmapTo, reverse);
906           return true;
907         }
908       }
909     }
910
911     if (fromDna)
912     {
913       mapping = AlignmentUtils.mapCdnaToProtein(mapTo, mapFrom);
914     }
915     else
916     {
917       mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, mapTo);
918       if (mapping != null)
919       {
920         mapping = mapping.getInverse();
921       }
922     }
923     if (mapping == null)
924     {
925       return false;
926     }
927     xref.setMap(new Mapping(mapTo, mapping));
928
929     /*
930      * and add a reverse DbRef with the inverse mapping
931      */
932     if (mapFrom.getDatasetSequence() != null && false)
933     // && mapFrom.getDatasetSequence().getSourceDBRef() != null)
934     {
935       // possible need to search primary references... except, why doesn't xref
936       // == getSourceDBRef ??
937       // DBRefEntry dbref = new DBRefEntry(mapFrom.getDatasetSequence()
938       // .getSourceDBRef());
939       // dbref.setMap(new Mapping(mapFrom.getDatasetSequence(), mapping
940       // .getInverse()));
941       // mapTo.addDBRef(dbref);
942     }
943
944     if (fromDna)
945     {
946       // AlignmentUtils.computeProteinFeatures(mapFrom, mapTo, mapping);
947       mappings.addMap(mapFrom, mapTo, mapping);
948     }
949     else
950     {
951       mappings.addMap(mapTo, mapFrom, mapping.getInverse());
952     }
953
954     return true;
955   }
956
957   /**
958    * find references to lrfs in the cross-reference set of each sequence in
959    * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
960    * based on source and accession string only - Map and Version are nulled.
961    * 
962    * @param fromDna
963    *          - true if context was searching from Dna sequences, false if
964    *          context was searching from Protein sequences
965    * @param sequenceI
966    * @param lrfs
967    * @param foundSeqs
968    * @return true if matches were found.
969    */
970   private boolean searchDatasetXrefs(boolean fromDna, SequenceI sequenceI,
971           List<DBRefEntry> lrfs, List<SequenceI> foundSeqs,
972           AlignedCodonFrame cf)
973   {
974     boolean found = false;
975     if (lrfs == null)
976     {
977       return false;
978     }
979     for (int i = 0, n = lrfs.size(); i < n; i++)
980     {
981       // DBRefEntry xref = new DBRefEntry(lrfs.get(i));
982       // // add in wildcards
983       // xref.setVersion(null);
984       // xref.setMap(null);
985       found |= searchDataset(fromDna, sequenceI, lrfs.get(i), foundSeqs, cf,
986               false, DBRefUtils.SEARCH_MODE_NO_MAP_NO_VERSION);
987     }
988     return found;
989   }
990
991   /**
992    * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
993    * associated sequence to rseqs
994    * 
995    * @param fromDna
996    *          true if context was searching for refs *from* dna sequence, false
997    *          if context was searching for refs *from* protein sequence
998    * @param fromSeq
999    *          a sequence to ignore (start point of search)
1000    * @param xrf
1001    *          a cross-reference to try to match
1002    * @param foundSeqs
1003    *          result list to add to
1004    * @param mappings
1005    *          a set of sequence mappings to add to
1006    * @param direct
1007    *          - indicates the type of relationship between returned sequences,
1008    *          xrf, and sequenceI that is required.
1009    *          <ul>
1010    *          <li>direct implies xrf is a primary reference for sequenceI AND
1011    *          the sequences to be located (eg a uniprot ID for a protein
1012    *          sequence, and a uniprot ref on a transcript sequence).</li>
1013    *          <li>indirect means xrf is a cross reference with respect to
1014    *          sequenceI or all the returned sequences (eg a genomic reference
1015    *          associated with a locus and one or more transcripts)</li>
1016    *          </ul>
1017    * @param mode
1018    *          SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
1019    * @return true if relationship found and sequence added.
1020    */
1021   boolean searchDataset(boolean fromDna, SequenceI fromSeq, DBRefEntry xrf,
1022           List<SequenceI> foundSeqs, AlignedCodonFrame mappings,
1023           boolean direct, int mode)
1024   {
1025     boolean found = false;
1026     if (dataset == null)
1027     {
1028       return false;
1029     }
1030     if (dataset.getSequences() == null)
1031     {
1032       System.err.println("Empty dataset sequence set - NO VECTOR");
1033       return false;
1034     }
1035     List<SequenceI> ds = dataset.getSequences();
1036     synchronized (ds)
1037     {
1038       for (SequenceI nxt : ds)
1039       {
1040         if (nxt != null)
1041         {
1042           if (nxt.getDatasetSequence() != null)
1043           {
1044             System.err.println(
1045                     "Implementation warning: CrossRef initialised with a dataset alignment with non-dataset sequences in it! ("
1046                             + nxt.getDisplayId(true) + " has ds reference "
1047                             + nxt.getDatasetSequence().getDisplayId(true)
1048                             + ")");
1049           }
1050           if (nxt == fromSeq || nxt == fromSeq.getDatasetSequence())
1051           {
1052             continue;
1053           }
1054           /*
1055            * only look at same molecule type if 'direct', or
1056            * complementary type if !direct
1057            */
1058           {
1059             boolean isDna = !nxt.isProtein();
1060             if (direct ? (isDna != fromDna) : (isDna == fromDna))
1061             {
1062               // skip this sequence because it is wrong molecule type
1063               continue;
1064             }
1065           }
1066
1067           // look for direct or indirect references in common
1068           List<DBRefEntry> poss = nxt.getDBRefs();
1069           List<DBRefEntry> cands = null;
1070
1071           // todo: indirect specifies we select either direct references to nxt
1072           // that match xrf which is indirect to sequenceI, or indirect
1073           // references to nxt that match xrf which is direct to sequenceI
1074           cands = DBRefUtils.searchRefs(poss, xrf, mode);
1075           // else
1076           // {
1077           // poss = DBRefUtils.selectDbRefs(nxt.isProtein()!fromDna, poss);
1078           // cands = DBRefUtils.searchRefs(poss, xrf);
1079           // }
1080           if (!cands.isEmpty())
1081           {
1082             if (foundSeqs.contains(nxt))
1083             {
1084               continue;
1085             }
1086             found = true;
1087             foundSeqs.add(nxt);
1088             if (mappings != null && !direct)
1089             {
1090               /*
1091                * if the matched sequence has mapped dbrefs to
1092                * protein product / cdna, add equivalent mappings to
1093                * our source sequence
1094                */
1095               for (DBRefEntry candidate : cands)
1096               {
1097                 Mapping mapping = candidate.getMap();
1098                 if (mapping != null)
1099                 {
1100                   MapList map = mapping.getMap();
1101                   if (mapping.getTo() != null
1102                           && map.getFromRatio() != map.getToRatio())
1103                   {
1104                     /*
1105                      * add a mapping, as from dna to peptide sequence
1106                      */
1107                     if (map.getFromRatio() == 3)
1108                     {
1109                       mappings.addMap(nxt, fromSeq, map);
1110                     }
1111                     else
1112                     {
1113                       mappings.addMap(nxt, fromSeq, map.getInverse());
1114                     }
1115                   }
1116                 }
1117               }
1118             }
1119           }
1120         }
1121       }
1122     }
1123     return found;
1124   }
1125 }