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<String>();
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 DBRefEntry[] rfs = DBRefUtils.selectDbRefs(!fromDna, seq.getDBRefs());
147 addXrefsToSources(rfs, sources);
151 * find sequence's direct (dna-to-dna, peptide-to-peptide) xrefs
153 DBRefEntry[] lrfs = DBRefUtils.selectDbRefs(fromDna, seq.getDBRefs());
154 List<SequenceI> foundSeqs = new ArrayList<SequenceI>();
157 * find sequences in the alignment which xref one of these DBRefs
158 * i.e. is xref-ed to a common sequence identifier
160 searchDatasetXrefs(fromDna, seq, lrfs, foundSeqs, null);
163 * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
165 for (SequenceI rs : foundSeqs)
167 DBRefEntry[] xrs = DBRefUtils
168 .selectDbRefs(!fromDna, rs.getDBRefs());
169 addXrefsToSources(xrs, sources);
175 * Helper method that adds the source identifiers of some cross-references to
176 * a (non-redundant) list of database sources
181 void addXrefsToSources(DBRefEntry[] xrefs, List<String> sources)
185 for (DBRefEntry ref : xrefs)
188 * avoid duplication e.g. ENSEMBL and Ensembl
190 String source = DBRefUtils.getCanonicalName(ref.getSource());
191 if (!sources.contains(source))
200 * Attempts to find cross-references from the sequences provided in the
201 * constructor to the given source database. Cross-references may be found
203 * <li>in dbrefs on the sequence which hold a mapping to a sequence
205 * <li>provided with a fetched sequence (e.g. ENA translation), or</li>
206 * <li>populated previously after getting cross-references</li>
208 * <li>as other sequences in the alignment which share a dbref identifier with
210 * <li>by fetching from the remote database</li>
212 * The cross-referenced sequences, and mappings to them, are added to the
216 * @return cross-referenced sequences (as dataset sequences)
218 public Alignment findXrefSequences(String source, boolean fromDna)
221 rseqs = new ArrayList<SequenceI>();
222 AlignedCodonFrame cf = new AlignedCodonFrame();
223 matcher = new SequenceIdMatcher(dataset.getSequences());
225 for (SequenceI seq : fromSeqs)
228 while (dss.getDatasetSequence() != null)
230 dss = dss.getDatasetSequence();
232 boolean found = false;
233 DBRefEntry[] xrfs = DBRefUtils
234 .selectDbRefs(!fromDna, dss.getDBRefs());
235 // ENST & ENSP comes in to both Protein and nucleotide, so we need to
238 if ((xrfs == null || xrfs.length == 0) && dataset != null)
241 * found no suitable dbrefs on sequence - look for sequences in the
242 * alignment which share a dbref with this one
244 DBRefEntry[] lrfs = DBRefUtils.selectDbRefs(fromDna,
248 * find sequences (except this one!), of complementary type,
249 * which have a dbref to an accession id for this sequence,
250 * and add them to the results
252 found = searchDatasetXrefs(fromDna, dss, lrfs, rseqs, cf);
254 if (xrfs == null && !found)
257 * no dbref to source on this sequence or matched
258 * complementary sequence in the dataset
262 List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(xrfs,
264 Iterator<DBRefEntry> refIterator = sourceRefs.iterator();
265 // At this point, if we are retrieving Ensembl, we still don't filter out
266 // ENST when looking for protein crossrefs.
267 while (refIterator.hasNext())
269 DBRefEntry xref = refIterator.next();
271 // we're only interested in coding cross-references, not
273 if (xref.hasMap() && xref.getMap().getMap().isTripletMap())
275 SequenceI mappedTo = xref.getMap().getTo();
276 if (mappedTo != null)
279 * dbref contains the sequence it maps to; add it to the
280 * results unless we have done so already (could happen if
281 * fetching xrefs for sequences which have xrefs in common)
282 * for example: UNIPROT {P0CE19, P0CE20} -> EMBL {J03321, X06707}
286 * problem: matcher.findIdMatch() is lenient - returns a sequence
287 * with a dbref to the search arg e.g. ENST for ENSP - wrong
288 * but findInDataset() matches ENSP when looking for Uniprot...
290 SequenceI matchInDataset = findInDataset(xref);
291 if (matchInDataset != null && xref.getMap().getTo() != null
292 && matchInDataset != xref.getMap().getTo())
295 .println("Implementation problem (reopen JAL-2154): CrossRef.findInDataset seems to have recovered a different sequence than the one explicitly mapped for xref."
299 + xref.getMap().getTo()
303 /*matcher.findIdMatch(mappedTo);*/
304 if (matchInDataset != null)
306 if (!rseqs.contains(matchInDataset))
308 rseqs.add(matchInDataset);
310 // even if rseqs contained matchInDataset - check mappings between
311 // these seqs are added
312 // need to try harder to only add unique mappings
313 if (xref.getMap().getMap().isTripletMap()
314 && dataset.getMapping(seq, matchInDataset) == null
315 && cf.getMappingBetween(seq, matchInDataset) == null)
317 // materialise a mapping for highlighting between these
321 cf.addMap(dss, matchInDataset, xref.getMap().getMap(),
322 xref.getMap().getMappedFromId());
326 cf.addMap(matchInDataset, dss, xref.getMap().getMap()
327 .getInverse(), 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(), xref.getMap()
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(xref.getSource() + "|"
359 + 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);
379 refIterator.remove();
384 * fetch from source database any dbrefs we haven't resolved up to here
386 if (!sourceRefs.isEmpty())
388 retrieveCrossRef(sourceRefs, seq, xrfs, fromDna, cf);
392 Alignment ral = null;
393 if (rseqs.size() > 0)
395 ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
398 dataset.addCodonFrame(cf);
404 private void retrieveCrossRef(List<DBRefEntry> sourceRefs, SequenceI seq,
405 DBRefEntry[] xrfs, boolean fromDna, AlignedCodonFrame cf)
407 ASequenceFetcher sftch = SequenceFetcherFactory.getSequenceFetcher();
408 SequenceI[] retrieved = null;
409 SequenceI dss = seq.getDatasetSequence() == null ? seq : seq
410 .getDatasetSequence();
411 // first filter in case we are retrieving crossrefs that have already been
412 // retrieved. this happens for cases where a database record doesn't yield
413 // protein products for CDS
414 removeAlreadyRetrievedSeqs(sourceRefs, fromDna);
415 if (sourceRefs.size() == 0)
417 // no more work to do! We already had all requested sequence records in
423 retrieved = sftch.getSequences(sourceRefs, !fromDna);
424 } catch (Exception e)
427 .println("Problem whilst retrieving cross references for Sequence : "
432 if (retrieved != null)
434 boolean addedXref = false;
435 List<SequenceI> newDsSeqs = new ArrayList<SequenceI>(), doNotAdd = new ArrayList<SequenceI>();
437 for (SequenceI retrievedSequence : retrieved)
439 // dataset gets contaminated ccwith non-ds sequences. why ??!
440 // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
441 SequenceI retrievedDss = retrievedSequence.getDatasetSequence() == null ? retrievedSequence
442 : retrievedSequence.getDatasetSequence();
443 addedXref |= importCrossRefSeq(cf, newDsSeqs, doNotAdd, dss,
448 // try again, after looking for matching IDs
449 // shouldn't need to do this unless the dbref mechanism has broken.
450 updateDbrefMappings(seq, xrfs, retrieved, cf, fromDna);
451 for (SequenceI retrievedSequence : retrieved)
453 // dataset gets contaminated ccwith non-ds sequences. why ??!
454 // try: Ensembl -> Nuc->Ensembl, Nuc->Uniprot-->Protein->EMBL->
455 SequenceI retrievedDss = retrievedSequence.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 DBRefEntry[] dbrSourceSet = sourceRefs.toArray(new DBRefEntry[0]);
486 for (SequenceI sq : dataset.getSequences())
488 boolean dupeFound = false;
489 // !fromDna means we are looking only for nucleotide sequences, not
491 if (sq.isProtein() == fromDna)
493 for (DBRefEntry dbr : sq.getPrimaryDBRefs())
495 for (DBRefEntry found : DBRefUtils.searchRefs(dbrSourceSet, dbr))
497 sourceRefs.remove(found);
504 // rebuild the search array from the filtered sourceRefs list
505 dbrSourceSet = sourceRefs.toArray(new DBRefEntry[0]);
511 * process sequence retrieved via a dbref on source sequence to resolve and
515 * @param sourceSequence
516 * @param retrievedSequence
517 * @return true if retrieveSequence was imported
519 private boolean importCrossRefSeq(AlignedCodonFrame cf,
520 List<SequenceI> newDsSeqs, List<SequenceI> doNotAdd,
521 SequenceI sourceSequence, SequenceI retrievedSequence)
524 * set when retrievedSequence has been verified as a crossreference for
527 boolean imported = false;
528 DBRefEntry[] dbr = retrievedSequence.getDBRefs();
531 for (DBRefEntry dbref : dbr)
533 SequenceI matched = findInDataset(dbref);
534 if (matched == sourceSequence)
536 // verified retrieved and source sequence cross-reference each other
539 // find any entry where we should put in the sequence being
540 // cross-referenced into the map
541 Mapping map = dbref.getMap();
544 if (map.getTo() != null && map.getMap() != null)
546 if (map.getTo() == sourceSequence)
548 // already called to import once, and most likely this sequence
549 // already imported !
555 * sequence is new to dataset, so save a reference so it can be added.
557 newDsSeqs.add(map.getTo());
562 * there was a matching sequence in dataset, so now, check to see if we can update the map.getTo() sequence to the existing one.
567 // compare ms with dss and replace with dss in mapping
568 // if map is congruent
569 SequenceI ms = map.getTo();
570 // TODO findInDataset requires exact sequence match but
571 // 'congruent' test is only for the mapped part
572 // maybe not a problem in practice since only ENA provide a
573 // mapping and it is to the full protein translation of CDS
574 // matcher.findIdMatch(map.getTo());
575 // TODO addendum: if matched is shorter than getTo, this will fail
576 // - when it should really succeed.
577 int sf = map.getMap().getToLowest();
578 int st = map.getMap().getToHighest();
579 SequenceI mappedrg = ms.getSubSequence(sf, st);
580 if (mappedrg.getLength() > 0
581 && ms.getSequenceAsString().equals(
582 matched.getSequenceAsString()))
585 * sequences were a match,
587 String msg = "Mapping updated from " + ms.getName()
588 + " to retrieved crossreference "
590 System.out.println(msg);
592 DBRefEntry[] toRefs = map.getTo().getDBRefs();
596 * transfer database refs
598 for (DBRefEntry ref : toRefs)
600 if (dbref.getSrcAccString().equals(
601 ref.getSrcAccString()))
603 continue; // avoid overwriting the ref on source sequence
605 matched.addDBRef(ref); // add or update mapping
608 doNotAdd.add(map.getTo());
612 * give the reverse reference the inverse mapping
613 * (if it doesn't have one already)
615 setReverseMapping(matched, dbref, cf);
618 * copy sequence features as well, avoiding
619 * duplication (e.g. same variation from two
622 List<SequenceFeature> sfs = ms.getFeatures()
624 for (SequenceFeature feat : sfs)
627 * make a flyweight feature object which ignores Parent
628 * attribute in equality test; this avoids creating many
629 * otherwise duplicate exon features on genomic sequence
631 SequenceFeature newFeature = new SequenceFeature(feat)
634 public boolean equals(Object o)
636 return super.equals(o, true);
639 matched.addSequenceFeature(newFeature);
642 cf.addMap(retrievedSequence, map.getTo(), map.getMap());
643 } catch (Exception e)
646 .println("Exception when consolidating Mapped sequence set...");
647 e.printStackTrace(System.err);
655 retrievedSequence.updatePDBIds();
656 rseqs.add(retrievedSequence);
657 if (dataset.findIndex(retrievedSequence) == -1)
659 dataset.addSequence(retrievedSequence);
660 matcher.add(retrievedSequence);
667 * Sets the inverse sequence mapping in the corresponding dbref of the mapped
668 * to sequence (if any). This is used after fetching a cross-referenced
669 * sequence, if the fetched sequence has a mapping to the original sequence,
670 * to set the mapping in the original sequence's dbref.
673 * the sequence mapped from
677 void setReverseMapping(SequenceI mapFrom, DBRefEntry dbref,
678 AlignedCodonFrame mappings)
680 SequenceI mapTo = dbref.getMap().getTo();
685 DBRefEntry[] dbrefs = mapTo.getDBRefs();
690 for (DBRefEntry toRef : dbrefs)
692 if (toRef.hasMap() && mapFrom == toRef.getMap().getTo())
695 * found the reverse dbref; update its mapping if null
697 if (toRef.getMap().getMap() == null)
699 MapList inverse = dbref.getMap().getMap().getInverse();
700 toRef.getMap().setMap(inverse);
701 mappings.addMap(mapTo, mapFrom, inverse);
708 * Returns null or the first sequence in the dataset which is identical to
709 * xref.mapTo, and has a) a primary dbref matching xref, or if none found, the
710 * first one with an ID source|xrefacc
713 * with map and mapped-to sequence
716 SequenceI findInDataset(DBRefEntry xref)
718 if (xref == null || !xref.hasMap() || xref.getMap().getTo() == null)
722 SequenceI mapsTo = xref.getMap().getTo();
723 String name = xref.getAccessionId();
724 String name2 = xref.getSource() + "|" + name;
725 SequenceI dss = mapsTo.getDatasetSequence() == null ? mapsTo : mapsTo
726 .getDatasetSequence();
727 // first check ds if ds is directly referenced
728 if (dataset.findIndex(dss) > -1)
732 DBRefEntry template = new DBRefEntry(xref.getSource(), null,
733 xref.getAccessionId());
735 * remember the first ID match - in case we don't find a match to template
737 SequenceI firstIdMatch = null;
738 for (SequenceI seq : dataset.getSequences())
740 // first check primary refs.
741 List<DBRefEntry> match = DBRefUtils.searchRefs(seq.getPrimaryDBRefs()
742 .toArray(new DBRefEntry[0]), template);
743 if (match != null && match.size() == 1 && sameSequence(seq, dss))
748 * clumsy alternative to using SequenceIdMatcher which currently
749 * returns sequences with a dbref to the matched accession id
750 * which we don't want
752 if (firstIdMatch == null
753 && (name.equals(seq.getName()) || seq.getName().startsWith(
756 if (sameSequence(seq, dss))
766 * Answers true if seq1 and seq2 contain exactly the same characters (ignoring
767 * case), else false. This method compares the lengths, then each character in
768 * turn, in order to 'fail fast'. For case-sensitive comparison, it would be
769 * possible to use Arrays.equals(seq1.getSequence(), seq2.getSequence()).
775 // TODO move to Sequence / SequenceI
776 static boolean sameSequence(SequenceI seq1, SequenceI seq2)
782 if (seq1 == null || seq2 == null)
787 if (seq1.getLength() != seq2.getLength())
791 int length = seq1.getLength();
792 for (int i = 0; i < length; i++)
794 int diff = seq1.getCharAt(i) - seq2.getCharAt(i);
796 * same char or differ in case only ('a'-'A' == 32)
798 if (diff != 0 && diff != 32 && diff != -32)
807 * Updates any empty mappings in the cross-references with one to a compatible
808 * retrieved sequence if found, and adds any new mappings to the
816 void updateDbrefMappings(SequenceI mapFrom, DBRefEntry[] xrefs,
817 SequenceI[] retrieved, AlignedCodonFrame acf, boolean fromDna)
819 SequenceIdMatcher idMatcher = new SequenceIdMatcher(retrieved);
820 for (DBRefEntry xref : xrefs)
824 String targetSeqName = xref.getSource() + "|"
825 + xref.getAccessionId();
826 SequenceI[] matches = idMatcher.findAllIdMatches(targetSeqName);
831 for (SequenceI seq : matches)
833 constructMapping(mapFrom, seq, xref, acf, fromDna);
840 * Tries to make a mapping between sequences. If successful, adds the mapping
841 * to the dbref and the mappings collection and answers true, otherwise
842 * answers false. The following methods of making are mapping are tried in
845 * <li>if 'mapTo' holds a mapping to 'mapFrom', take the inverse; this is, for
846 * example, the case after fetching EMBL cross-references for a Uniprot
848 * <li>else check if the dna translates exactly to the protein (give or take
849 * start and stop codons></li>
850 * <li>else try to map based on CDS features on the dna sequence</li>
859 boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
860 DBRefEntry xref, AlignedCodonFrame mappings, boolean fromDna)
862 MapList mapping = null;
863 SequenceI dsmapFrom = mapFrom.getDatasetSequence() == null ? mapFrom
864 : mapFrom.getDatasetSequence();
865 SequenceI dsmapTo = mapTo.getDatasetSequence() == null ? mapTo : mapTo
866 .getDatasetSequence();
868 * look for a reverse mapping, if found make its inverse.
869 * Note - we do this on dataset sequences only.
871 if (dsmapTo.getDBRefs() != null)
873 for (DBRefEntry dbref : dsmapTo.getDBRefs())
875 String name = dbref.getSource() + "|" + dbref.getAccessionId();
876 if (dbref.hasMap() && dsmapFrom.getName().startsWith(name))
879 * looks like we've found a map from 'mapTo' to 'mapFrom'
880 * - invert it to make the mapping the other way
882 MapList reverse = dbref.getMap().getMap().getInverse();
883 xref.setMap(new Mapping(dsmapTo, reverse));
884 mappings.addMap(mapFrom, dsmapTo, reverse);
892 mapping = AlignmentUtils.mapCdnaToProtein(mapTo, mapFrom);
896 mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, mapTo);
899 mapping = mapping.getInverse();
906 xref.setMap(new Mapping(mapTo, mapping));
909 * and add a reverse DbRef with the inverse mapping
911 if (mapFrom.getDatasetSequence() != null && false)
912 // && mapFrom.getDatasetSequence().getSourceDBRef() != null)
914 // possible need to search primary references... except, why doesn't xref
915 // == getSourceDBRef ??
916 // DBRefEntry dbref = new DBRefEntry(mapFrom.getDatasetSequence()
917 // .getSourceDBRef());
918 // dbref.setMap(new Mapping(mapFrom.getDatasetSequence(), mapping
920 // mapTo.addDBRef(dbref);
925 AlignmentUtils.computeProteinFeatures(mapFrom, mapTo, mapping);
926 mappings.addMap(mapFrom, mapTo, mapping);
930 mappings.addMap(mapTo, mapFrom, mapping.getInverse());
937 * find references to lrfs in the cross-reference set of each sequence in
938 * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
939 * based on source and accession string only - Map and Version are nulled.
942 * - true if context was searching from Dna sequences, false if
943 * context was searching from Protein sequences
947 * @return true if matches were found.
949 private boolean searchDatasetXrefs(boolean fromDna, SequenceI sequenceI,
950 DBRefEntry[] lrfs, List<SequenceI> foundSeqs, AlignedCodonFrame cf)
952 boolean found = false;
957 for (int i = 0; i < lrfs.length; i++)
959 DBRefEntry xref = new DBRefEntry(lrfs[i]);
961 xref.setVersion(null);
963 found |= searchDataset(fromDna, sequenceI, xref, foundSeqs, cf, false);
969 * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
970 * associated sequence to rseqs
973 * true if context was searching for refs *from* dna sequence, false
974 * if context was searching for refs *from* protein sequence
976 * a sequence to ignore (start point of search)
978 * a cross-reference to try to match
980 * result list to add to
982 * a set of sequence mappings to add to
984 * - indicates the type of relationship between returned sequences,
985 * xrf, and sequenceI that is required.
987 * <li>direct implies xrf is a primary reference for sequenceI AND
988 * the sequences to be located (eg a uniprot ID for a protein
989 * sequence, and a uniprot ref on a transcript sequence).</li>
990 * <li>indirect means xrf is a cross reference with respect to
991 * sequenceI or all the returned sequences (eg a genomic reference
992 * associated with a locus and one or more transcripts)</li>
994 * @return true if relationship found and sequence added.
996 boolean searchDataset(boolean fromDna, SequenceI fromSeq, DBRefEntry xrf,
997 List<SequenceI> foundSeqs, AlignedCodonFrame mappings,
1000 boolean found = false;
1001 if (dataset == null)
1005 if (dataset.getSequences() == null)
1007 System.err.println("Empty dataset sequence set - NO VECTOR");
1011 synchronized (ds = dataset.getSequences())
1013 for (SequenceI nxt : ds)
1017 if (nxt.getDatasetSequence() != null)
1020 .println("Implementation warning: CrossRef initialised with a dataset alignment with non-dataset sequences in it! ("
1021 + nxt.getDisplayId(true)
1022 + " has ds reference "
1023 + nxt.getDatasetSequence().getDisplayId(true)
1026 if (nxt == fromSeq || nxt == fromSeq.getDatasetSequence())
1031 * only look at same molecule type if 'direct', or
1032 * complementary type if !direct
1035 boolean isDna = !nxt.isProtein();
1036 if (direct ? (isDna != fromDna) : (isDna == fromDna))
1038 // skip this sequence because it is wrong molecule type
1043 // look for direct or indirect references in common
1044 DBRefEntry[] poss = nxt.getDBRefs();
1045 List<DBRefEntry> cands = null;
1047 // todo: indirect specifies we select either direct references to nxt
1048 // that match xrf which is indirect to sequenceI, or indirect
1049 // references to nxt that match xrf which is direct to sequenceI
1050 cands = DBRefUtils.searchRefs(poss, xrf);
1053 // poss = DBRefUtils.selectDbRefs(nxt.isProtein()!fromDna, poss);
1054 // cands = DBRefUtils.searchRefs(poss, xrf);
1056 if (!cands.isEmpty())
1058 if (foundSeqs.contains(nxt))
1064 if (mappings != null && !direct)
1067 * if the matched sequence has mapped dbrefs to
1068 * protein product / cdna, add equivalent mappings to
1069 * our source sequence
1071 for (DBRefEntry candidate : cands)
1073 Mapping mapping = candidate.getMap();
1074 if (mapping != null)
1076 MapList map = mapping.getMap();
1077 if (mapping.getTo() != null
1078 && map.getFromRatio() != map.getToRatio())
1081 * add a mapping, as from dna to peptide sequence
1083 if (map.getFromRatio() == 3)
1085 mappings.addMap(nxt, fromSeq, map);
1089 mappings.addMap(nxt, fromSeq, map.getInverse());