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.SequenceFetcherFactory;
35 import jalview.ws.seqfetcher.ASequenceFetcher;
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
42 * Functions for cross-referencing sequence databases.
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
54 private AlignmentI dataset;
57 * the sequences for which we are seeking cross-references
59 private SequenceI[] fromSeqs;
62 * matcher built from dataset
64 SequenceIdMatcher matcher;
67 * sequences found by cross-ref searches to fromSeqs
69 List<SequenceI> rseqs;
75 * the sequences for which we are seeking cross-references
77 * the containing alignment dataset (may be searched to resolve
80 public CrossRef(SequenceI[] seqs, AlignmentI ds)
83 dataset = ds.getDataset() == null ? ds : ds.getDataset();
87 * Returns a list of distinct database sources for which sequences have either
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>
96 * - when true, cross-references *from* dna returned. When false,
97 * cross-references *from* protein are returned
100 public List<String> findXrefSourcesForSequences(boolean dna)
102 List<String> sources = new ArrayList<>();
103 for (SequenceI seq : fromSeqs)
107 findXrefSourcesForSequence(seq, dna, sources);
110 sources.remove(DBRefSource.EMBL); // hack to prevent EMBL xrefs resulting in
111 // redundant datasets
114 sources.remove(DBRefSource.ENSEMBL); // hack to prevent Ensembl and
115 // EnsemblGenomes xref option shown
117 sources.remove(DBRefSource.ENSEMBLGENOMES);
119 // redundant datasets
124 * Returns a list of distinct database sources for which a sequence has either
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>
133 * the sequence whose dbrefs we are searching against
135 * when true, context is DNA - so sources identifying protein
136 * products will be returned.
138 * a list of sources to add matches to
140 void findXrefSourcesForSequence(SequenceI seq, boolean fromDna,
141 List<String> sources)
144 * first find seq's xrefs (dna-to-peptide or peptide-to-dna)
146 List<DBRefEntry> rfs = DBRefUtils.selectDbRefs(!fromDna,
148 addXrefsToSources(rfs, sources);
152 * find sequence's direct (dna-to-dna, peptide-to-peptide) xrefs
154 List<DBRefEntry> lrfs = DBRefUtils.selectDbRefs(fromDna,
156 List<SequenceI> foundSeqs = new ArrayList<>();
159 * find sequences in the alignment which xref one of these DBRefs
160 * i.e. is xref-ed to a common sequence identifier
162 searchDatasetXrefs(fromDna, seq, lrfs, foundSeqs, null);
165 * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
167 for (SequenceI rs : foundSeqs)
169 List<DBRefEntry> xrs = DBRefUtils.selectDbRefs(!fromDna,
171 addXrefsToSources(xrs, sources);
177 * Helper method that adds the source identifiers of some cross-references to
178 * a (non-redundant) list of database sources
183 void addXrefsToSources(List<DBRefEntry> xrefs, List<String> sources)
187 for (DBRefEntry ref : xrefs)
190 * avoid duplication e.g. ENSEMBL and Ensembl
192 String source = DBRefUtils.getCanonicalName(ref.getSource());
193 if (!sources.contains(source))
202 * Attempts to find cross-references from the sequences provided in the
203 * constructor to the given source database. Cross-references may be found
205 * <li>in dbrefs on the sequence which hold a mapping to a sequence
207 * <li>provided with a fetched sequence (e.g. ENA translation), or</li>
208 * <li>populated previously after getting cross-references</li>
210 * <li>as other sequences in the alignment which share a dbref identifier with
212 * <li>by fetching from the remote database</li>
214 * The cross-referenced sequences, and mappings to them, are added to the
218 * @return cross-referenced sequences (as dataset sequences)
220 public Alignment findXrefSequences(String source, boolean fromDna)
223 rseqs = new ArrayList<>();
224 AlignedCodonFrame cf = new AlignedCodonFrame();
225 matcher = new SequenceIdMatcher(dataset.getSequences());
227 for (SequenceI seq : fromSeqs)
230 while (dss.getDatasetSequence() != null)
232 dss = dss.getDatasetSequence();
234 boolean found = false;
235 List<DBRefEntry> xrfs = DBRefUtils.selectDbRefs(!fromDna,
237 // ENST & ENSP comes in to both Protein and nucleotide, so we need to
240 if ((xrfs == null || xrfs.size() == 0) && dataset != null)
243 * found no suitable dbrefs on sequence - look for sequences in the
244 * alignment which share a dbref with this one
246 List<DBRefEntry> lrfs = DBRefUtils.selectDbRefs(fromDna,
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
254 found = searchDatasetXrefs(fromDna, dss, lrfs, rseqs, cf);
256 if (xrfs == null && !found)
259 * no dbref to source on this sequence or matched
260 * complementary sequence in the dataset
264 List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(xrfs,
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())
271 DBRefEntry xref = refIterator.next();
273 // we're only interested in coding cross-references, not
275 if (xref.hasMap() && xref.getMap().getMap().isTripletMap())
277 SequenceI mappedTo = xref.getMap().getTo();
278 if (mappedTo != null)
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}
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...
292 SequenceI matchInDataset = findInDataset(xref);
293 if (matchInDataset != null && xref.getMap().getTo() != null
294 && matchInDataset != xref.getMap().getTo())
296 jalview.bin.Console.errPrintln(
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:"
302 /*matcher.findIdMatch(mappedTo);*/
303 if (matchInDataset != null)
305 if (!rseqs.contains(matchInDataset))
307 rseqs.add(matchInDataset);
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)
316 // materialise a mapping for highlighting between these
320 cf.addMap(dss, matchInDataset, xref.getMap().getMap(),
321 xref.getMap().getMappedFromId());
325 cf.addMap(matchInDataset, dss,
326 xref.getMap().getMap().getInverse(),
327 xref.getMap().getMappedFromId());
331 refIterator.remove();
334 // TODO: need to determine if this should be a deriveSequence
335 SequenceI rsq = new Sequence(mappedTo);
337 if (xref.getMap().getMap().isTripletMap())
339 // get sense of map correct for adding to product alignment.
342 // map is from dna seq to a protein product
343 cf.addMap(dss, rsq, xref.getMap().getMap(),
344 xref.getMap().getMappedFromId());
348 // map should be from protein seq to its coding dna
349 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse(),
350 xref.getMap().getMappedFromId());
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
362 if (matchedSeq != null && matchedSeq.isProtein() == fromDna)
364 if (constructMapping(seq, matchedSeq, xref, cf, fromDna))
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);
380 refIterator.remove();
385 * fetch from source database any dbrefs we haven't resolved up to here
387 if (!sourceRefs.isEmpty())
389 retrieveCrossRef(sourceRefs, seq, xrfs, fromDna, cf);
393 Alignment ral = null;
394 if (rseqs.size() > 0)
396 ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
399 dataset.addCodonFrame(cf);
405 private void retrieveCrossRef(List<DBRefEntry> sourceRefs, SequenceI seq,
406 List<DBRefEntry> xrfs, boolean fromDna, AlignedCodonFrame cf)
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)
418 // no more work to do! We already had all requested sequence records in
424 retrieved = sftch.getSequences(sourceRefs, !fromDna);
425 } catch (Exception e)
427 jalview.bin.Console.errPrintln(
428 "Problem whilst retrieving cross references for Sequence : "
433 if (retrieved != null)
435 boolean addedXref = false;
436 List<SequenceI> newDsSeqs = new ArrayList<>(),
437 doNotAdd = new ArrayList<>();
439 for (SequenceI retrievedSequence : retrieved)
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,
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
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)
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,
470 for (SequenceI newToSeq : newDsSeqs)
472 if (!doNotAdd.contains(newToSeq)
473 && dataset.findIndex(newToSeq) == -1)
475 dataset.addSequence(newToSeq);
476 matcher.add(newToSeq);
483 * Search dataset for sequences with a primary reference contained in
487 * - list of references to filter.
489 * - type of sequence to search for matching primary reference.
491 private void removeAlreadyRetrievedSeqs(List<DBRefEntry> sourceRefs,
494 List<DBRefEntry> dbrSourceSet = new ArrayList<>(sourceRefs);
495 List<SequenceI> dsSeqs = dataset.getSequences();
496 for (int ids = 0, nds = dsSeqs.size(); ids < nds; ids++)
498 SequenceI sq = dsSeqs.get(ids);
499 boolean dupeFound = false;
500 // !fromDna means we are looking only for nucleotide sequences, not
502 if (sq.isProtein() == fromDna)
504 List<DBRefEntry> sqdbrefs = sq.getPrimaryDBRefs();
505 for (int idb = 0, ndb = sqdbrefs.size(); idb < ndb; idb++)
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++)
512 sourceRefs.remove(searchrefs.get(isr));
519 // rebuild the search array from the filtered sourceRefs list
520 dbrSourceSet.clear();
521 dbrSourceSet.addAll(sourceRefs);
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
533 * @param sourceSequence
534 * @param retrievedSequence
535 * @return true if retrieveSequence was imported
537 private boolean importCrossRefSeq(AlignedCodonFrame cf,
538 List<SequenceI> newDsSeqs, List<SequenceI> doNotAdd,
539 SequenceI sourceSequence, SequenceI retrievedSequence)
542 * set when retrievedSequence has been verified as a crossreference for
545 boolean imported = false;
546 List<DBRefEntry> dbr = retrievedSequence.getDBRefs();
549 for (int ib = 0, nb = dbr.size(); ib < nb; ib++)
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)
557 // verified retrieved and source sequence cross-reference each other
560 // find any entry where we should put in the sequence being
561 // cross-referenced into the map
562 Mapping map = dbref.getMap();
565 SequenceI ms = map.getTo();
566 if (ms != null && map.getMap() != null)
568 if (ms == sourceSequence)
570 // already called to import once, and most likely this sequence
571 // already imported !
577 * sequence is new to dataset, so save a reference so it can be added.
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.
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()))
605 * sequences were a match,
607 String msg = "Mapping updated from " + ms.getName()
608 + " to retrieved crossreference "
610 jalview.bin.Console.outPrintln(msg);
612 List<DBRefEntry> toRefs = map.getTo().getDBRefs();
616 * transfer database refs
618 for (DBRefEntry ref : toRefs)
620 if (dbref.getSrcAccString()
621 .equals(ref.getSrcAccString()))
623 continue; // avoid overwriting the ref on source sequence
625 matched.addDBRef(ref); // add or update mapping
628 doNotAdd.add(map.getTo());
632 * give the reverse reference the inverse mapping
633 * (if it doesn't have one already)
635 setReverseMapping(matched, dbref, cf);
638 * copy sequence features as well, avoiding
639 * duplication (e.g. same variation from two
642 List<SequenceFeature> sfs = ms.getFeatures()
644 for (SequenceFeature feat : sfs)
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
651 SequenceFeature newFeature = new SequenceFeature(feat)
654 public boolean equals(Object o)
656 return super.equals(o, true);
659 matched.addSequenceFeature(newFeature);
662 cf.addMap(retrievedSequence, map.getTo(), map.getMap());
663 } catch (Exception e)
665 jalview.bin.Console.errPrintln(
666 "Exception when consolidating Mapped sequence set...");
667 e.printStackTrace(System.err);
675 retrievedSequence.updatePDBIds();
676 rseqs.add(retrievedSequence);
677 if (dataset.findIndex(retrievedSequence) == -1)
679 dataset.addSequence(retrievedSequence);
680 matcher.add(retrievedSequence);
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.
693 * the sequence mapped from
697 void setReverseMapping(SequenceI mapFrom, DBRefEntry dbref,
698 AlignedCodonFrame mappings)
700 SequenceI mapTo = dbref.getMap().getTo();
705 List<DBRefEntry> dbrefs = mapTo.getDBRefs();
710 for (DBRefEntry toRef : dbrefs)
712 if (toRef.hasMap() && mapFrom == toRef.getMap().getTo())
715 * found the reverse dbref; update its mapping if null
717 if (toRef.getMap().getMap() == null)
719 MapList inverse = dbref.getMap().getMap().getInverse();
720 toRef.getMap().setMap(inverse);
721 mappings.addMap(mapTo, mapFrom, inverse);
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
734 * with map and mapped-to sequence
737 SequenceI findInDataset(DBRefEntry xref)
739 if (xref == null || !xref.hasMap() || xref.getMap().getTo() == null)
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)
753 DBRefEntry template = new DBRefEntry(xref.getSource(), null,
754 xref.getAccessionId());
756 * remember the first ID match - in case we don't find a match to template
758 SequenceI firstIdMatch = null;
759 for (SequenceI seq : dataset.getSequences())
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))
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
773 if (firstIdMatch == null && (name.equals(seq.getName())
774 || seq.getName().startsWith(name2)))
776 if (sameSequence(seq, dss))
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()).
795 // TODO move to Sequence / SequenceI
796 static boolean sameSequence(SequenceI seq1, SequenceI seq2)
802 if (seq1 == null || seq2 == null)
807 if (seq1.getLength() != seq2.getLength())
811 int length = seq1.getLength();
812 for (int i = 0; i < length; i++)
814 int diff = seq1.getCharAt(i) - seq2.getCharAt(i);
816 * same char or differ in case only ('a'-'A' == 32)
818 if (diff != 0 && diff != 32 && diff != -32)
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.
837 void updateDbrefMappings(SequenceI mapFrom, List<DBRefEntry> xrefs,
838 SequenceI[] retrieved, AlignedCodonFrame acf, boolean fromDna)
840 SequenceIdMatcher idMatcher = new SequenceIdMatcher(retrieved);
841 for (DBRefEntry xref : xrefs)
845 String targetSeqName = xref.getSource() + "|"
846 + xref.getAccessionId();
847 SequenceI[] matches = idMatcher.findAllIdMatches(targetSeqName);
852 for (SequenceI seq : matches)
854 constructMapping(mapFrom, seq, xref, acf, fromDna);
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
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
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>
880 boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
881 DBRefEntry xref, AlignedCodonFrame mappings, boolean fromDna)
883 MapList mapping = null;
884 SequenceI dsmapFrom = mapFrom.getDatasetSequence() == null ? mapFrom
885 : mapFrom.getDatasetSequence();
886 SequenceI dsmapTo = mapTo.getDatasetSequence() == null ? mapTo
887 : mapTo.getDatasetSequence();
889 * look for a reverse mapping, if found make its inverse.
890 * Note - we do this on dataset sequences only.
892 if (dsmapTo.getDBRefs() != null)
894 for (DBRefEntry dbref : dsmapTo.getDBRefs())
896 String name = dbref.getSource() + "|" + dbref.getAccessionId();
897 if (dbref.hasMap() && dsmapFrom.getName().startsWith(name))
900 * looks like we've found a map from 'mapTo' to 'mapFrom'
901 * - invert it to make the mapping the other way
903 MapList reverse = dbref.getMap().getMap().getInverse();
904 xref.setMap(new Mapping(dsmapTo, reverse));
905 mappings.addMap(mapFrom, dsmapTo, reverse);
913 mapping = AlignmentUtils.mapCdnaToProtein(mapTo, mapFrom);
917 mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, mapTo);
920 mapping = mapping.getInverse();
927 xref.setMap(new Mapping(mapTo, mapping));
930 * and add a reverse DbRef with the inverse mapping
932 if (mapFrom.getDatasetSequence() != null && false)
933 // && mapFrom.getDatasetSequence().getSourceDBRef() != null)
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
941 // mapTo.addDBRef(dbref);
946 // AlignmentUtils.computeProteinFeatures(mapFrom, mapTo, mapping);
947 mappings.addMap(mapFrom, mapTo, mapping);
951 mappings.addMap(mapTo, mapFrom, mapping.getInverse());
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.
963 * - true if context was searching from Dna sequences, false if
964 * context was searching from Protein sequences
968 * @return true if matches were found.
970 private boolean searchDatasetXrefs(boolean fromDna, SequenceI sequenceI,
971 List<DBRefEntry> lrfs, List<SequenceI> foundSeqs,
972 AlignedCodonFrame cf)
974 boolean found = false;
979 for (int i = 0, n = lrfs.size(); i < n; i++)
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);
992 * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
993 * associated sequence to rseqs
996 * true if context was searching for refs *from* dna sequence, false
997 * if context was searching for refs *from* protein sequence
999 * a sequence to ignore (start point of search)
1001 * a cross-reference to try to match
1003 * result list to add to
1005 * a set of sequence mappings to add to
1007 * - indicates the type of relationship between returned sequences,
1008 * xrf, and sequenceI that is required.
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>
1018 * SEARCH_MODE_FULL for all; SEARCH_MODE_NO_MAP_NO_VERSION optional
1019 * @return true if relationship found and sequence added.
1021 boolean searchDataset(boolean fromDna, SequenceI fromSeq, DBRefEntry xrf,
1022 List<SequenceI> foundSeqs, AlignedCodonFrame mappings,
1023 boolean direct, int mode)
1025 boolean found = false;
1026 if (dataset == null)
1030 if (dataset.getSequences() == null)
1033 .errPrintln("Empty dataset sequence set - NO VECTOR");
1036 List<SequenceI> ds = dataset.getSequences();
1039 for (SequenceI nxt : ds)
1043 if (nxt.getDatasetSequence() != null)
1045 jalview.bin.Console.errPrintln(
1046 "Implementation warning: CrossRef initialised with a dataset alignment with non-dataset sequences in it! ("
1047 + nxt.getDisplayId(true) + " has ds reference "
1048 + nxt.getDatasetSequence().getDisplayId(true)
1051 if (nxt == fromSeq || nxt == fromSeq.getDatasetSequence())
1056 * only look at same molecule type if 'direct', or
1057 * complementary type if !direct
1060 boolean isDna = !nxt.isProtein();
1061 if (direct ? (isDna != fromDna) : (isDna == fromDna))
1063 // skip this sequence because it is wrong molecule type
1068 // look for direct or indirect references in common
1069 List<DBRefEntry> poss = nxt.getDBRefs();
1070 List<DBRefEntry> cands = null;
1072 // todo: indirect specifies we select either direct references to nxt
1073 // that match xrf which is indirect to sequenceI, or indirect
1074 // references to nxt that match xrf which is direct to sequenceI
1075 cands = DBRefUtils.searchRefs(poss, xrf, mode);
1078 // poss = DBRefUtils.selectDbRefs(nxt.isProtein()!fromDna, poss);
1079 // cands = DBRefUtils.searchRefs(poss, xrf);
1081 if (!cands.isEmpty())
1083 if (foundSeqs.contains(nxt))
1089 if (mappings != null && !direct)
1092 * if the matched sequence has mapped dbrefs to
1093 * protein product / cdna, add equivalent mappings to
1094 * our source sequence
1096 for (DBRefEntry candidate : cands)
1098 Mapping mapping = candidate.getMap();
1099 if (mapping != null)
1101 MapList map = mapping.getMap();
1102 if (mapping.getTo() != null
1103 && map.getFromRatio() != map.getToRatio())
1106 * add a mapping, as from dna to peptide sequence
1108 if (map.getFromRatio() == 3)
1110 mappings.addMap(nxt, fromSeq, map);
1114 mappings.addMap(nxt, fromSeq, map.getInverse());