2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis;
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;
36 import java.util.ArrayList;
37 import java.util.Iterator;
38 import java.util.List;
41 * Functions for cross-referencing sequence databases.
49 * the dataset of the alignment for which we are searching for
50 * cross-references; in some cases we may resolve xrefs by
51 * searching in the dataset
53 private AlignmentI dataset;
56 * the sequences for which we are seeking cross-references
58 private SequenceI[] fromSeqs;
61 * matcher built from dataset
63 SequenceIdMatcher matcher;
66 * sequences found by cross-ref searches to fromSeqs
68 List<SequenceI> rseqs;
74 * the sequences for which we are seeking cross-references
76 * the containing alignment dataset (may be searched to resolve
79 public CrossRef(SequenceI[] seqs, AlignmentI ds)
82 dataset = ds.getDataset() == null ? ds : ds.getDataset();
86 * Returns a list of distinct database sources for which sequences have either
88 * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
89 * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
90 * reference from another sequence in the dataset which has a cross-reference
91 * to a direct DBRefEntry on the given sequence</li>
95 * - when true, cross-references *from* dna returned. When false,
96 * cross-references *from* protein are returned
99 public List<String> findXrefSourcesForSequences(boolean dna)
101 List<String> sources = new ArrayList<>();
102 for (SequenceI seq : fromSeqs)
106 findXrefSourcesForSequence(seq, dna, sources);
109 sources.remove(DBRefSource.EMBL); // hack to prevent EMBL xrefs resulting in
110 // redundant datasets
113 sources.remove(DBRefSource.ENSEMBL); // hack to prevent Ensembl and
114 // EnsemblGenomes xref option shown
116 sources.remove(DBRefSource.ENSEMBLGENOMES);
118 // redundant datasets
123 * Returns a list of distinct database sources for which a sequence has either
125 * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
126 * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
127 * reference from another sequence in the dataset which has a cross-reference
128 * to a direct DBRefEntry on the given sequence</li>
132 * the sequence whose dbrefs we are searching against
134 * when true, context is DNA - so sources identifying protein
135 * products will be returned.
137 * a list of sources to add matches to
139 void findXrefSourcesForSequence(SequenceI seq, boolean fromDna,
140 List<String> sources)
143 * first find seq's xrefs (dna-to-peptide or peptide-to-dna)
145 List<DBRefEntry> rfs = DBRefUtils.selectDbRefs(!fromDna, seq.getDBRefs());
146 addXrefsToSources(rfs, sources);
150 * find sequence's direct (dna-to-dna, peptide-to-peptide) xrefs
152 List<DBRefEntry> lrfs = DBRefUtils.selectDbRefs(fromDna, seq.getDBRefs());
153 List<SequenceI> foundSeqs = new ArrayList<>();
156 * find sequences in the alignment which xref one of these DBRefs
157 * i.e. is xref-ed to a common sequence identifier
159 searchDatasetXrefs(fromDna, seq, lrfs, foundSeqs, null);
162 * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
164 for (SequenceI rs : foundSeqs)
166 List<DBRefEntry> xrs = DBRefUtils.selectDbRefs(!fromDna,
168 addXrefsToSources(xrs, sources);
174 * Helper method that adds the source identifiers of some cross-references to
175 * a (non-redundant) list of database sources
180 void addXrefsToSources(List<DBRefEntry> xrefs, List<String> sources)
184 for (DBRefEntry ref : xrefs)
187 * avoid duplication e.g. ENSEMBL and Ensembl
189 String source = DBRefUtils.getCanonicalName(ref.getSource());
190 if (!sources.contains(source))
199 * Attempts to find cross-references from the sequences provided in the
200 * constructor to the given source database. Cross-references may be found
202 * <li>in dbrefs on the sequence which hold a mapping to a sequence
204 * <li>provided with a fetched sequence (e.g. ENA translation), or</li>
205 * <li>populated previously after getting cross-references</li>
207 * <li>as other sequences in the alignment which share a dbref identifier with
209 * <li>by fetching from the remote database</li>
211 * The cross-referenced sequences, and mappings to them, are added to the
215 * @return cross-referenced sequences (as dataset sequences)
217 public Alignment findXrefSequences(String source, boolean fromDna)
220 rseqs = new ArrayList<>();
221 AlignedCodonFrame cf = new AlignedCodonFrame();
222 matcher = new SequenceIdMatcher(dataset.getSequences());
224 for (SequenceI seq : fromSeqs)
227 while (dss.getDatasetSequence() != null)
229 dss = dss.getDatasetSequence();
231 boolean found = false;
232 List<DBRefEntry> xrfs = DBRefUtils.selectDbRefs(!fromDna,
234 // ENST & ENSP comes in to both Protein and nucleotide, so we need to
237 if ((xrfs == null || xrfs.size() == 0) && dataset != null)
240 * found no suitable dbrefs on sequence - look for sequences in the
241 * alignment which share a dbref with this one
243 List<DBRefEntry> lrfs = DBRefUtils.selectDbRefs(fromDna,
247 * find sequences (except this one!), of complementary type,
248 * which have a dbref to an accession id for this sequence,
249 * and add them to the results
251 found = searchDatasetXrefs(fromDna, dss, lrfs, rseqs, cf);
253 if (xrfs == null && !found)
256 * no dbref to source on this sequence or matched
257 * complementary sequence in the dataset
261 List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(xrfs,
263 Iterator<DBRefEntry> refIterator = sourceRefs.iterator();
264 // At this point, if we are retrieving Ensembl, we still don't filter out
265 // ENST when looking for protein crossrefs.
266 while (refIterator.hasNext())
268 DBRefEntry xref = refIterator.next();
270 // we're only interested in coding cross-references, not
272 if (xref.hasMap() && xref.getMap().getMap().isTripletMap())
274 SequenceI mappedTo = xref.getMap().getTo();
275 if (mappedTo != null)
278 * dbref contains the sequence it maps to; add it to the
279 * results unless we have done so already (could happen if
280 * fetching xrefs for sequences which have xrefs in common)
281 * for example: UNIPROT {P0CE19, P0CE20} -> EMBL {J03321, X06707}
285 * problem: matcher.findIdMatch() is lenient - returns a sequence
286 * with a dbref to the search arg e.g. ENST for ENSP - wrong
287 * but findInDataset() matches ENSP when looking for Uniprot...
289 SequenceI matchInDataset = findInDataset(xref);
290 if (matchInDataset != null && xref.getMap().getTo() != null
291 && matchInDataset != xref.getMap().getTo())
294 "Implementation problem (reopen JAL-2154): CrossRef.findInDataset seems to have recovered a different sequence than the one explicitly mapped for xref."
295 + "Found:" + matchInDataset + "\nExpected:"
296 + xref.getMap().getTo() + "\nFor xref:"
299 /*matcher.findIdMatch(mappedTo);*/
300 if (matchInDataset != null)
302 if (!rseqs.contains(matchInDataset))
304 rseqs.add(matchInDataset);
306 // even if rseqs contained matchInDataset - check mappings between
307 // these seqs are added
308 // need to try harder to only add unique mappings
309 if (xref.getMap().getMap().isTripletMap()
310 && dataset.getMapping(seq, matchInDataset) == null
311 && cf.getMappingBetween(seq, matchInDataset) == null)
313 // materialise a mapping for highlighting between these
317 cf.addMap(dss, matchInDataset, xref.getMap().getMap(),
318 xref.getMap().getMappedFromId());
322 cf.addMap(matchInDataset, dss,
323 xref.getMap().getMap().getInverse(),
324 xref.getMap().getMappedFromId());
328 refIterator.remove();
331 // TODO: need to determine if this should be a deriveSequence
332 SequenceI rsq = new Sequence(mappedTo);
334 if (xref.getMap().getMap().isTripletMap())
336 // get sense of map correct for adding to product alignment.
339 // map is from dna seq to a protein product
340 cf.addMap(dss, rsq, xref.getMap().getMap(),
341 xref.getMap().getMappedFromId());
345 // map should be from protein seq to its coding dna
346 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse(),
347 xref.getMap().getMappedFromId());
355 SequenceI matchedSeq = matcher.findIdMatch(
356 xref.getSource() + "|" + xref.getAccessionId());
357 // if there was a match, check it's at least the right type of
359 if (matchedSeq != null && matchedSeq.isProtein() == fromDna)
361 if (constructMapping(seq, matchedSeq, xref, cf, fromDna))
370 // do a bit more work - search for sequences with references matching
371 // xrefs on this sequence.
372 found = searchDataset(fromDna, dss, xref, rseqs, cf, false, DBRefUtils.SEARCH_MODE_FULL);
376 refIterator.remove();
381 * fetch from source database any dbrefs we haven't resolved up to here
383 if (!sourceRefs.isEmpty())
385 retrieveCrossRef(sourceRefs, seq, xrfs, fromDna, cf);
389 Alignment ral = null;
390 if (rseqs.size() > 0)
392 ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
395 dataset.addCodonFrame(cf);
401 private void retrieveCrossRef(List<DBRefEntry> sourceRefs, SequenceI seq,
402 List<DBRefEntry> xrfs, boolean fromDna, AlignedCodonFrame cf)
404 SequenceI[] retrieved = null;
405 SequenceI dss = seq.getDatasetSequence() == null ? seq
406 : seq.getDatasetSequence();
407 // first filter in case we are retrieving crossrefs that have already been
408 // retrieved. this happens for cases where a database record doesn't yield
409 // protein products for CDS
410 removeAlreadyRetrievedSeqs(sourceRefs, fromDna);
411 if (sourceRefs.size() == 0)
413 // no more work to do! We already had all requested sequence records in
419 retrieved = SequenceFetcher.getInstance()
420 .getSequences(sourceRefs, !fromDna);
421 } catch (Exception e)
424 "Problem whilst retrieving cross references for Sequence : "
429 if (retrieved != null)
431 boolean addedXref = false;
432 List<SequenceI> newDsSeqs = new ArrayList<>(),
433 doNotAdd = new ArrayList<>();
435 for (SequenceI retrievedSequence : retrieved)
437 // dataset gets contaminated ccwith non-ds sequences. why ??!
438 // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
439 SequenceI retrievedDss = retrievedSequence
440 .getDatasetSequence() == null ? retrievedSequence
441 : retrievedSequence.getDatasetSequence();
442 addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
447 // try again, after looking for matching IDs
448 // shouldn't need to do this unless the dbref mechanism has broken.
449 updateDbrefMappings(seq, xrfs, retrieved, cf, fromDna);
450 for (SequenceI retrievedSequence : retrieved)
452 // dataset gets contaminated ccwith non-ds sequences. why ??!
453 // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
454 SequenceI retrievedDss = retrievedSequence
455 .getDatasetSequence() == null ? retrievedSequence
456 : retrievedSequence.getDatasetSequence();
457 addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
461 for (SequenceI newToSeq : newDsSeqs)
463 if (!doNotAdd.contains(newToSeq)
464 && dataset.findIndex(newToSeq) == -1)
466 dataset.addSequence(newToSeq);
467 matcher.add(newToSeq);
474 * Search dataset for sequences with a primary reference contained in
478 * - list of references to filter.
480 * - type of sequence to search for matching primary reference.
482 private void removeAlreadyRetrievedSeqs(List<DBRefEntry> sourceRefs,
485 List<DBRefEntry> dbrSourceSet = new ArrayList<>(sourceRefs);
486 List<SequenceI> dsSeqs = dataset.getSequences();
487 for (int ids = 0, nds = dsSeqs.size(); ids < nds; ids++)
489 SequenceI sq = dsSeqs.get(ids);
490 boolean dupeFound = false;
491 // !fromDna means we are looking only for nucleotide sequences, not
493 if (sq.isProtein() == fromDna)
495 List<DBRefEntry> sqdbrefs = sq.getPrimaryDBRefs();
496 for (int idb = 0, ndb = sqdbrefs.size(); idb < ndb; idb++)
498 DBRefEntry dbr = sqdbrefs.get(idb);
499 List<DBRefEntry> searchrefs = DBRefUtils.searchRefs(dbrSourceSet, dbr, DBRefUtils.SEARCH_MODE_FULL);
500 for (int isr = 0, nsr = searchrefs.size(); isr < nsr; isr++)
502 sourceRefs.remove(searchrefs.get(isr));
509 // rebuild the search array from the filtered sourceRefs list
510 dbrSourceSet.clear();
511 dbrSourceSet.addAll(sourceRefs);
517 * process sequence retrieved via a dbref on source sequence to resolve and
521 * @param sourceSequence
522 * @param retrievedSequence
523 * @return true if retrieveSequence was imported
525 private boolean importCrossRefSeq(AlignedCodonFrame cf,
526 List<SequenceI> newDsSeqs, List<SequenceI> doNotAdd,
527 SequenceI sourceSequence, SequenceI retrievedSequence)
530 * set when retrievedSequence has been verified as a crossreference for
533 boolean imported = false;
534 List<DBRefEntry> dbr = retrievedSequence.getDBRefs();
537 for (int ib = 0, nb = dbr.size(); ib < nb; ib++)
540 DBRefEntry dbref = dbr.get(ib);
541 SequenceI matched = findInDataset(dbref);
542 if (matched == sourceSequence)
544 // verified retrieved and source sequence cross-reference each other
547 // find any entry where we should put in the sequence being
548 // cross-referenced into the map
549 Mapping map = dbref.getMap();
552 SequenceI ms = map.getTo();
553 if (ms != null && map.getMap() != null)
555 if (ms == sourceSequence)
557 // already called to import once, and most likely this sequence
558 // already imported !
564 * sequence is new to dataset, so save a reference so it can be added.
571 * there was a matching sequence in dataset, so now, check to see if we can update the map.getTo() sequence to the existing one.
576 // compare ms with dss and replace with dss in mapping
577 // if map is congruent
578 // TODO findInDataset requires exact sequence match but
579 // 'congruent' test is only for the mapped part
580 // maybe not a problem in practice since only ENA provide a
581 // mapping and it is to the full protein translation of CDS
582 // matcher.findIdMatch(map.getTo());
583 // TODO addendum: if matched is shorter than getTo, this will fail
584 // - when it should really succeed.
585 int sf = map.getMap().getToLowest();
586 int st = map.getMap().getToHighest();
587 SequenceI mappedrg = ms.getSubSequence(sf, st);
588 if (mappedrg.getLength() > 0 && ms.getSequenceAsString()
589 .equals(matched.getSequenceAsString()))
592 * sequences were a match,
594 String msg = "Mapping updated from " + ms.getName()
595 + " to retrieved crossreference "
597 System.out.println(msg);
599 List<DBRefEntry> toRefs = map.getTo().getDBRefs();
603 * transfer database refs
605 for (DBRefEntry ref : toRefs)
607 if (dbref.getSrcAccString()
608 .equals(ref.getSrcAccString()))
610 continue; // avoid overwriting the ref on source sequence
612 matched.addDBRef(ref); // add or update mapping
615 doNotAdd.add(map.getTo());
619 * give the reverse reference the inverse mapping
620 * (if it doesn't have one already)
622 setReverseMapping(matched, dbref, cf);
625 * copy sequence features as well, avoiding
626 * duplication (e.g. same variation from two
629 List<SequenceFeature> sfs = ms.getFeatures()
631 for (SequenceFeature feat : sfs)
634 * make a flyweight feature object which ignores Parent
635 * attribute in equality test; this avoids creating many
636 * otherwise duplicate exon features on genomic sequence
638 SequenceFeature newFeature = new SequenceFeature(feat)
640 // BH 2019.08.15 We must override equalsInterval, not
641 // equals, because that is part of the IntervalI interface,
642 // and IntervalStore may need that for proper, faster
644 // But SequenceFeature changes were reverted...
646 public boolean equals(Object o)
648 return o instanceof SequenceFeature
649 && equalsWithParent((SequenceFeature) o);
653 public boolean equalsWithParent(SequenceFeature sf)
655 return sf != null && equals(sf, true);
658 matched.addSequenceFeature(newFeature);
661 cf.addMap(retrievedSequence, map.getTo(), map.getMap());
662 } catch (Exception e)
665 "Exception when consolidating Mapped sequence set...");
666 e.printStackTrace(System.err);
674 retrievedSequence.updatePDBIds();
675 rseqs.add(retrievedSequence);
676 if (dataset.findIndex(retrievedSequence) == -1)
678 dataset.addSequence(retrievedSequence);
679 matcher.add(retrievedSequence);
686 * Sets the inverse sequence mapping in the corresponding dbref of the mapped
687 * to sequence (if any). This is used after fetching a cross-referenced
688 * sequence, if the fetched sequence has a mapping to the original sequence,
689 * to set the mapping in the original sequence's dbref.
692 * the sequence mapped from
696 void setReverseMapping(SequenceI mapFrom, DBRefEntry dbref,
697 AlignedCodonFrame mappings)
699 SequenceI mapTo = dbref.getMap().getTo();
704 List<DBRefEntry> dbrefs = mapTo.getDBRefs();
709 for (DBRefEntry toRef : dbrefs)
711 if (toRef.hasMap() && mapFrom == toRef.getMap().getTo())
714 * found the reverse dbref; update its mapping if null
716 if (toRef.getMap().getMap() == null)
718 MapList inverse = dbref.getMap().getMap().getInverse();
719 toRef.getMap().setMap(inverse);
720 mappings.addMap(mapTo, mapFrom, inverse);
727 * Returns null or the first sequence in the dataset which is identical to
728 * xref.mapTo, and has a) a primary dbref matching xref, or if none found, the
729 * first one with an ID source|xrefacc
732 * with map and mapped-to sequence
735 SequenceI findInDataset(DBRefEntry xref)
737 if (xref == null || !xref.hasMap() || xref.getMap().getTo() == null)
741 SequenceI mapsTo = xref.getMap().getTo();
742 String name = xref.getAccessionId();
743 String name2 = xref.getSource() + "|" + name;
744 SequenceI dss = mapsTo.getDatasetSequence() == null ? mapsTo
745 : mapsTo.getDatasetSequence();
746 // first check ds if ds is directly referenced
747 if (dataset.findIndex(dss) > -1)
751 DBRefEntry template = new DBRefEntry(xref.getSource(), null,
752 xref.getAccessionId());
754 * remember the first ID match - in case we don't find a match to template
756 SequenceI firstIdMatch = null;
757 for (SequenceI seq : dataset.getSequences())
759 // first check primary refs.
760 List<DBRefEntry> match = DBRefUtils.searchRefs(
761 seq.getPrimaryDBRefs(), template, DBRefUtils.SEARCH_MODE_FULL);
762 if (match != null && match.size() == 1 && sameSequence(seq, dss))
767 * clumsy alternative to using SequenceIdMatcher which currently
768 * returns sequences with a dbref to the matched accession id
769 * which we don't want
771 if (firstIdMatch == null && (name.equals(seq.getName())
772 || seq.getName().startsWith(name2)))
774 if (sameSequence(seq, dss))
784 * Answers true if seq1 and seq2 contain exactly the same characters (ignoring
785 * case), else false. This method compares the lengths, then each character in
786 * turn, in order to 'fail fast'. For case-sensitive comparison, it would be
787 * possible to use Arrays.equals(seq1.getSequence(), seq2.getSequence()).
793 // TODO move to Sequence / SequenceI
794 static boolean sameSequence(SequenceI seq1, SequenceI seq2)
800 if (seq1 == null || seq2 == null)
805 if (seq1.getLength() != seq2.getLength())
809 int length = seq1.getLength();
810 for (int i = 0; i < length; i++)
812 int diff = seq1.getCharAt(i) - seq2.getCharAt(i);
814 * same char or differ in case only ('a'-'A' == 32)
816 if (diff != 0 && diff != 32 && diff != -32)
825 * Updates any empty mappings in the cross-references with one to a compatible
826 * retrieved sequence if found, and adds any new mappings to the
834 void updateDbrefMappings(SequenceI mapFrom, List<DBRefEntry> xrefs,
835 SequenceI[] retrieved, AlignedCodonFrame acf, boolean fromDna)
837 SequenceIdMatcher idMatcher = new SequenceIdMatcher(retrieved);
838 for (DBRefEntry xref : xrefs)
842 String targetSeqName = xref.getSource() + "|"
843 + xref.getAccessionId();
844 SequenceI[] matches = idMatcher.findAllIdMatches(targetSeqName);
849 for (SequenceI seq : matches)
851 constructMapping(mapFrom, seq, xref, acf, fromDna);
858 * Tries to make a mapping between sequences. If successful, adds the mapping
859 * to the dbref and the mappings collection and answers true, otherwise
860 * answers false. The following methods of making are mapping are tried in
863 * <li>if 'mapTo' holds a mapping to 'mapFrom', take the inverse; this is, for
864 * example, the case after fetching EMBL cross-references for a Uniprot
866 * <li>else check if the dna translates exactly to the protein (give or take
867 * start and stop codons></li>
868 * <li>else try to map based on CDS features on the dna sequence</li>
877 boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
878 DBRefEntry xref, AlignedCodonFrame mappings, boolean fromDna)
880 MapList mapping = null;
881 SequenceI dsmapFrom = mapFrom.getDatasetSequence() == null ? mapFrom
882 : mapFrom.getDatasetSequence();
883 SequenceI dsmapTo = mapTo.getDatasetSequence() == null ? mapTo
884 : mapTo.getDatasetSequence();
886 * look for a reverse mapping, if found make its inverse.
887 * Note - we do this on dataset sequences only.
889 if (dsmapTo.getDBRefs() != null)
891 for (DBRefEntry dbref : dsmapTo.getDBRefs())
893 String name = dbref.getSource() + "|" + dbref.getAccessionId();
894 if (dbref.hasMap() && dsmapFrom.getName().startsWith(name))
897 * looks like we've found a map from 'mapTo' to 'mapFrom'
898 * - invert it to make the mapping the other way
900 MapList reverse = dbref.getMap().getMap().getInverse();
901 xref.setMap(new Mapping(dsmapTo, reverse));
902 mappings.addMap(mapFrom, dsmapTo, reverse);
910 mapping = AlignmentUtils.mapCdnaToProtein(mapTo, mapFrom);
914 mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, mapTo);
917 mapping = mapping.getInverse();
924 xref.setMap(new Mapping(mapTo, mapping));
927 * and add a reverse DbRef with the inverse mapping
929 if (mapFrom.getDatasetSequence() != null && false)
930 // && mapFrom.getDatasetSequence().getSourceDBRef() != null)
932 // possible need to search primary references... except, why doesn't xref
933 // == getSourceDBRef ??
934 // DBRefEntry dbref = new DBRefEntry(mapFrom.getDatasetSequence()
935 // .getSourceDBRef());
936 // dbref.setMap(new Mapping(mapFrom.getDatasetSequence(), mapping
938 // mapTo.addDBRef(dbref);
943 AlignmentUtils.computeProteinFeatures(mapFrom, mapTo, mapping);
944 mappings.addMap(mapFrom, mapTo, mapping);
948 mappings.addMap(mapTo, mapFrom, mapping.getInverse());
955 * find references to lrfs in the cross-reference set of each sequence in
956 * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
957 * based on source and accession string only - Map and Version are nulled.
960 * - true if context was searching from Dna sequences, false if
961 * context was searching from Protein sequences
965 * @return true if matches were found.
967 private boolean searchDatasetXrefs(boolean fromDna, SequenceI sequenceI,
968 List<DBRefEntry> lrfs, List<SequenceI> foundSeqs,
969 AlignedCodonFrame cf)
971 boolean found = false;
976 for (int i = 0, n = lrfs.size(); i < n; i++)
978 // DBRefEntry xref = new DBRefEntry(lrfs.get(i));
979 // // add in wildcards
980 // xref.setVersion(null);
981 // xref.setMap(null);
982 found |= searchDataset(fromDna, sequenceI, lrfs.get(i), foundSeqs, cf,
983 false, DBRefUtils.SEARCH_MODE_NO_MAP_NO_VERSION);
989 * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
990 * associated sequence to rseqs
993 * true if context was searching for refs *from* dna sequence, false
994 * if context was searching for refs *from* protein sequence
996 * a sequence to ignore (start point of search)
998 * a cross-reference to try to match
1000 * result list to add to
1002 * a set of sequence mappings to add to
1004 * - indicates the type of relationship between returned sequences,
1005 * xrf, and sequenceI that is required.
1007 * <li>direct implies xrf is a primary reference for sequenceI AND
1008 * the sequences to be located (eg a uniprot ID for a protein
1009 * sequence, and a uniprot ref on a transcript sequence).</li>
1010 * <li>indirect means xrf is a cross reference with respect to
1011 * sequenceI or all the returned sequences (eg a genomic reference
1012 * associated with a locus and one or more transcripts)</li>
1014 * @param mode SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
1015 * @return true if relationship found and sequence added.
1017 boolean searchDataset(boolean fromDna, SequenceI fromSeq, DBRefEntry xrf,
1018 List<SequenceI> foundSeqs, AlignedCodonFrame mappings,
1019 boolean direct, int mode)
1021 boolean found = false;
1022 if (dataset == null)
1026 if (dataset.getSequences() == null)
1028 System.err.println("Empty dataset sequence set - NO VECTOR");
1031 List<SequenceI> ds = dataset.getSequences();
1034 for (SequenceI nxt : ds)
1038 if (nxt.getDatasetSequence() != null)
1041 "Implementation warning: CrossRef initialised with a dataset alignment with non-dataset sequences in it! ("
1042 + nxt.getDisplayId(true) + " has ds reference "
1043 + nxt.getDatasetSequence().getDisplayId(true)
1046 if (nxt == fromSeq || nxt == fromSeq.getDatasetSequence())
1051 * only look at same molecule type if 'direct', or
1052 * complementary type if !direct
1055 boolean isDna = !nxt.isProtein();
1056 if (direct ? (isDna != fromDna) : (isDna == fromDna))
1058 // skip this sequence because it is wrong molecule type
1063 // look for direct or indirect references in common
1064 List<DBRefEntry> poss = nxt.getDBRefs();
1065 List<DBRefEntry> cands = null;
1067 // todo: indirect specifies we select either direct references to nxt
1068 // that match xrf which is indirect to sequenceI, or indirect
1069 // references to nxt that match xrf which is direct to sequenceI
1070 cands = DBRefUtils.searchRefs(poss, xrf, mode);
1073 // poss = DBRefUtils.selectDbRefs(nxt.isProtein()!fromDna, poss);
1074 // cands = DBRefUtils.searchRefs(poss, xrf);
1076 if (!cands.isEmpty())
1078 if (foundSeqs.contains(nxt))
1084 if (mappings != null && !direct)
1087 * if the matched sequence has mapped dbrefs to
1088 * protein product / cdna, add equivalent mappings to
1089 * our source sequence
1091 for (DBRefEntry candidate : cands)
1093 Mapping mapping = candidate.getMap();
1094 if (mapping != null)
1096 MapList map = mapping.getMap();
1097 if (mapping.getTo() != null
1098 && map.getFromRatio() != map.getToRatio())
1101 * add a mapping, as from dna to peptide sequence
1103 if (map.getFromRatio() == 3)
1105 mappings.addMap(nxt, fromSeq, map);
1109 mappings.addMap(nxt, fromSeq, map.getInverse());