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.Mapping;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceI;
31 import jalview.util.Comparison;
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;
65 * the sequences for which we are seeking cross-references
67 * the containing alignment dataset (may be searched to resolve
70 public CrossRef(SequenceI[] seqs, AlignmentI ds)
73 dataset = ds.getDataset() == null ? ds : ds.getDataset();
77 * Returns a list of distinct database sources for which sequences have either
79 * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
80 * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
81 * reference from another sequence in the dataset which has a cross-reference
82 * to a direct DBRefEntry on the given sequence</li>
86 * - when true, cross-references *from* dna returned. When false,
87 * cross-references *from* protein are returned
90 public List<String> findXrefSourcesForSequences(boolean dna)
92 List<String> sources = new ArrayList<String>();
93 for (SequenceI seq : fromSeqs)
97 findXrefSourcesForSequence(seq, dna, sources);
104 * Returns a list of distinct database sources for which a sequence has either
106 * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
107 * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
108 * reference from another sequence in the dataset which has a cross-reference
109 * to a direct DBRefEntry on the given sequence</li>
113 * the sequence whose dbrefs we are searching against
115 * a list of sources to add matches to
117 void findXrefSourcesForSequence(SequenceI seq, boolean fromDna,
118 List<String> sources)
121 * first find seq's xrefs (dna-to-peptide or peptide-to-dna)
123 DBRefEntry[] rfs = DBRefUtils.selectDbRefs(!fromDna, seq.getDBRefs());
124 addXrefsToSources(rfs, sources);
128 * find sequence's direct (dna-to-dna, peptide-to-peptide) xrefs
130 DBRefEntry[] lrfs = DBRefUtils.selectDbRefs(fromDna, seq.getDBRefs());
131 List<SequenceI> rseqs = new ArrayList<SequenceI>();
134 * find sequences in the alignment which xref one of these DBRefs
135 * i.e. is xref-ed to a common sequence identifier
137 searchDatasetXrefs(fromDna, seq, lrfs, rseqs, null);
140 * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
142 for (SequenceI rs : rseqs)
144 DBRefEntry[] xrs = DBRefUtils
145 .selectDbRefs(!fromDna, rs.getDBRefs());
146 addXrefsToSources(xrs, sources);
152 * Helper method that adds the source identifiers of some cross-references to
153 * a (non-redundant) list of database sources
158 void addXrefsToSources(DBRefEntry[] xrefs, List<String> sources)
162 for (DBRefEntry ref : xrefs)
165 * avoid duplication e.g. ENSEMBL and Ensembl
167 String source = DBRefUtils.getCanonicalName(ref.getSource());
168 if (!sources.contains(source))
177 * Attempts to find cross-references from the sequences provided in the
178 * constructor to the given source database. Cross-references may be found
180 * <li>in dbrefs on the sequence which hold a mapping to a sequence
182 * <li>provided with a fetched sequence (e.g. ENA translation), or</li>
183 * <li>populated previously after getting cross-references</li>
185 * <li>as other sequences in the alignment which share a dbref identifier with
187 * <li>by fetching from the remote database</li>
189 * The cross-referenced sequences, and mappings to them, are added to the
193 * @return cross-referenced sequences (as dataset sequences)
195 public Alignment findXrefSequences(String source, boolean fromDna)
198 List<SequenceI> rseqs = new ArrayList<SequenceI>();
199 AlignedCodonFrame cf = new AlignedCodonFrame();
200 SequenceIdMatcher matcher = new SequenceIdMatcher(
201 dataset.getSequences());
203 for (SequenceI seq : fromSeqs)
206 while (dss.getDatasetSequence() != null)
208 dss = dss.getDatasetSequence();
210 boolean found = false;
211 DBRefEntry[] xrfs = DBRefUtils
212 .selectDbRefs(!fromDna, dss.getDBRefs());
213 if ((xrfs == null || xrfs.length == 0) && dataset != null)
216 * found no suitable dbrefs on sequence - look for sequences in the
217 * alignment which share a dbref with this one
219 DBRefEntry[] lrfs = DBRefUtils.selectDbRefs(fromDna,
223 * find sequences (except this one!), of complementary type,
224 * which have a dbref to an accession id for this sequence,
225 * and add them to the results
227 found = searchDatasetXrefs(fromDna, dss, lrfs, rseqs, cf);
229 if (xrfs == null && !found)
232 * no dbref to source on this sequence or matched
233 * complementary sequence in the dataset
237 List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(xrfs,
239 Iterator<DBRefEntry> refIterator = sourceRefs.iterator();
240 while (refIterator.hasNext())
242 DBRefEntry xref = refIterator.next();
246 SequenceI mappedTo = xref.getMap().getTo();
247 if (mappedTo != null)
250 * dbref contains the sequence it maps to; add it to the
251 * results unless we have done so already (could happen if
252 * fetching xrefs for sequences which have xrefs in common)
253 * for example: UNIPROT {P0CE19, P0CE20} -> EMBL {J03321, X06707}
257 * problem: matcher.findIdMatch() is lenient - returns a sequence
258 * with a dbref to the search arg e.g. ENST for ENSP - wrong
259 * but findInDataset() matches ENSP when looking for Uniprot...
261 SequenceI matchInDataset = findInDataset(xref);
262 /*matcher.findIdMatch(mappedTo);*/
263 if (matchInDataset != null)
265 if (!rseqs.contains(matchInDataset))
267 rseqs.add(matchInDataset);
269 refIterator.remove();
272 SequenceI rsq = new Sequence(mappedTo);
274 if (xref.getMap().getMap().getFromRatio() != xref.getMap()
275 .getMap().getToRatio())
277 // get sense of map correct for adding to product alignment.
280 // map is from dna seq to a protein product
281 cf.addMap(dss, rsq, xref.getMap().getMap());
285 // map should be from protein seq to its coding dna
286 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse());
294 SequenceI matchedSeq = matcher.findIdMatch(xref.getSource() + "|"
295 + xref.getAccessionId());
296 if (matchedSeq != null)
298 if (constructMapping(seq, matchedSeq, xref, cf, fromDna))
307 // do a bit more work - search for sequences with references matching
308 // xrefs on this sequence.
309 found = searchDataset(fromDna, dss, xref, rseqs, cf, false);
313 refIterator.remove();
318 * fetch from source database any dbrefs we haven't resolved up to here
320 if (!sourceRefs.isEmpty())
322 ASequenceFetcher sftch = SequenceFetcherFactory
323 .getSequenceFetcher();
324 SequenceI[] retrieved = null;
327 retrieved = sftch.getSequences(sourceRefs, !fromDna);
328 } catch (Exception e)
331 .println("Problem whilst retrieving cross references for Sequence : "
336 if (retrieved != null)
338 updateDbrefMappings(seq, xrfs, retrieved, cf, fromDna);
339 for (SequenceI retrievedSequence : retrieved)
341 SequenceI retrievedDss = retrievedSequence.getDatasetSequence() == null ? retrievedSequence
342 : retrievedSequence.getDatasetSequence();
343 DBRefEntry[] dbr = retrievedSequence.getDBRefs();
346 for (DBRefEntry dbref : dbr)
348 // find any entry where we should put in the sequence being
349 // cross-referenced into the map
350 Mapping map = dbref.getMap();
353 if (map.getTo() != null && map.getMap() != null)
355 // TODO findInDataset requires exact sequence match but
356 // 'congruent' test is only for the mapped part
357 // maybe not a problem in practice since only ENA provide a
358 // mapping and it is to the full protein translation of CDS
359 SequenceI matched = findInDataset(dbref);
360 // matcher.findIdMatch(map.getTo());
364 * already got an xref to this sequence; update this
365 * map to point to the same sequence, and add
366 * any new dbrefs to it
368 DBRefEntry[] toRefs = map.getTo().getDBRefs();
371 for (DBRefEntry ref : toRefs)
373 matched.addDBRef(ref); // add or update mapping
380 matcher.add(map.getTo());
384 // compare ms with dss and replace with dss in mapping
385 // if map is congruent
386 SequenceI ms = map.getTo();
387 int sf = map.getMap().getToLowest();
388 int st = map.getMap().getToHighest();
389 SequenceI mappedrg = ms.getSubSequence(sf, st);
390 // SequenceI loc = dss.getSubSequence(sf, st);
391 if (mappedrg.getLength() > 0
392 && ms.getSequenceAsString().equals(
393 dss.getSequenceAsString()))
394 // && mappedrg.getSequenceAsString().equals(
395 // loc.getSequenceAsString()))
397 String msg = "Mapping updated from " + ms.getName()
398 + " to retrieved crossreference "
400 System.out.println(msg);
404 * give the reverse reference the inverse mapping
405 * (if it doesn't have one already)
407 setReverseMapping(dss, dbref, cf);
410 * copy sequence features as well, avoiding
411 * duplication (e.g. same variation from two
414 SequenceFeature[] sfs = ms.getSequenceFeatures();
417 for (SequenceFeature feat : sfs)
420 * make a flyweight feature object which ignores Parent
421 * attribute in equality test; this avoids creating many
422 * otherwise duplicate exon features on genomic sequence
424 SequenceFeature newFeature = new SequenceFeature(
428 public boolean equals(Object o)
430 return super.equals(o, true);
433 dss.addSequenceFeature(newFeature);
437 cf.addMap(retrievedDss, map.getTo(), map.getMap());
438 } catch (Exception e)
441 .println("Exception when consolidating Mapped sequence set...");
442 e.printStackTrace(System.err);
448 retrievedSequence.updatePDBIds();
449 rseqs.add(retrievedDss);
450 dataset.addSequence(retrievedDss);
451 matcher.add(retrievedDss);
457 Alignment ral = null;
458 if (rseqs.size() > 0)
460 ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
463 dataset.addCodonFrame(cf);
470 * Sets the inverse sequence mapping in the corresponding dbref of the mapped
471 * to sequence (if any). This is used after fetching a cross-referenced
472 * sequence, if the fetched sequence has a mapping to the original sequence,
473 * to set the mapping in the original sequence's dbref.
476 * the sequence mapped from
480 void setReverseMapping(SequenceI mapFrom, DBRefEntry dbref,
481 AlignedCodonFrame mappings)
483 SequenceI mapTo = dbref.getMap().getTo();
488 DBRefEntry[] dbrefs = mapTo.getDBRefs();
493 for (DBRefEntry toRef : dbrefs)
495 if (toRef.hasMap() && mapFrom == toRef.getMap().getTo())
498 * found the reverse dbref; update its mapping if null
500 if (toRef.getMap().getMap() == null)
502 MapList inverse = dbref.getMap().getMap().getInverse();
503 toRef.getMap().setMap(inverse);
504 mappings.addMap(mapTo, mapFrom, inverse);
511 * Returns the first identical sequence in the dataset if any, else null
516 SequenceI findInDataset(DBRefEntry xref)
518 if (xref == null || !xref.hasMap() || xref.getMap().getTo() == null)
522 SequenceI mapsTo = xref.getMap().getTo();
523 String name = xref.getAccessionId();
524 String name2 = xref.getSource() + "|" + name;
525 SequenceI dss = mapsTo.getDatasetSequence() == null ? mapsTo : mapsTo
526 .getDatasetSequence();
527 for (SequenceI seq : dataset.getSequences())
530 * clumsy alternative to using SequenceIdMatcher which currently
531 * returns sequences with a dbref to the matched accession id
532 * which we don't want
534 if (name.equals(seq.getName()) || seq.getName().startsWith(name2))
536 if (sameSequence(seq, dss))
546 * Answers true if seq1 and seq2 contain exactly the same characters (ignoring
547 * case), else false. This method compares the lengths, then each character in
548 * turn, in order to 'fail fast'. For case-sensitive comparison, it would be
549 * possible to use Arrays.equals(seq1.getSequence(), seq2.getSequence()).
555 // TODO move to Sequence / SequenceI
556 static boolean sameSequence(SequenceI seq1, SequenceI seq2)
562 if (seq1 == null || seq2 == null)
566 char[] c1 = seq1.getSequence();
567 char[] c2 = seq2.getSequence();
568 if (c1.length != c2.length)
572 for (int i = 0; i < c1.length; i++)
574 int diff = c1[i] - c2[i];
576 * same char or differ in case only ('a'-'A' == 32)
578 if (diff != 0 && diff != 32 && diff != -32)
587 * Updates any empty mappings in the cross-references with one to a compatible
588 * retrieved sequence if found, and adds any new mappings to the
596 void updateDbrefMappings(SequenceI mapFrom, DBRefEntry[] xrefs,
597 SequenceI[] retrieved, AlignedCodonFrame acf, boolean fromDna)
599 SequenceIdMatcher matcher = new SequenceIdMatcher(retrieved);
600 for (DBRefEntry xref : xrefs)
604 String targetSeqName = xref.getSource() + "|"
605 + xref.getAccessionId();
606 SequenceI[] matches = matcher.findAllIdMatches(targetSeqName);
611 for (SequenceI seq : matches)
613 constructMapping(mapFrom, seq, xref, acf, fromDna);
620 * Tries to make a mapping between sequences. If successful, adds the mapping
621 * to the dbref and the mappings collection and answers true, otherwise
622 * answers false. The following methods of making are mapping are tried in
625 * <li>if 'mapTo' holds a mapping to 'mapFrom', take the inverse; this is, for
626 * example, the case after fetching EMBL cross-references for a Uniprot
628 * <li>else check if the dna translates exactly to the protein (give or take
629 * start and stop codons></li>
630 * <li>else try to map based on CDS features on the dna sequence</li>
639 boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
640 DBRefEntry xref, AlignedCodonFrame mappings, boolean fromDna)
642 MapList mapping = null;
645 * look for a reverse mapping, if found make its inverse
647 if (mapTo.getDBRefs() != null)
649 for (DBRefEntry dbref : mapTo.getDBRefs())
651 String name = dbref.getSource() + "|" + dbref.getAccessionId();
652 if (dbref.hasMap() && mapFrom.getName().startsWith(name))
655 * looks like we've found a map from 'mapTo' to 'mapFrom'
656 * - invert it to make the mapping the other way
658 MapList reverse = dbref.getMap().getMap().getInverse();
659 xref.setMap(new Mapping(mapTo, reverse));
660 mappings.addMap(mapFrom, mapTo, reverse);
668 mapping = AlignmentUtils.mapCdnaToProtein(mapTo, mapFrom);
672 mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, mapTo);
675 mapping = mapping.getInverse();
682 xref.setMap(new Mapping(mapTo, mapping));
685 AlignmentUtils.computeProteinFeatures(mapFrom, mapTo, mapping);
686 mappings.addMap(mapFrom, mapTo, mapping);
690 mappings.addMap(mapTo, mapFrom, mapping.getInverse());
697 * find references to lrfs in the cross-reference set of each sequence in
698 * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
699 * based on source and accession string only - Map and Version are nulled.
702 * - true if context was searching from Dna sequences, false if
703 * context was searching from Protein sequences
707 * @return true if matches were found.
709 private boolean searchDatasetXrefs(boolean fromDna, SequenceI sequenceI,
710 DBRefEntry[] lrfs, List<SequenceI> rseqs, AlignedCodonFrame cf)
712 boolean found = false;
717 for (int i = 0; i < lrfs.length; i++)
719 DBRefEntry xref = new DBRefEntry(lrfs[i]);
721 xref.setVersion(null);
723 found |= searchDataset(fromDna, sequenceI, xref, rseqs, cf, false);
729 * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
730 * associated sequence to rseqs
733 * true if context was searching for refs *from* dna sequence, false
734 * if context was searching for refs *from* protein sequence
736 * a sequence to ignore (start point of search)
738 * a cross-reference to try to match
740 * result list to add to
742 * a set of sequence mappings to add to
744 * - search all references or only subset
745 * @return true if relationship found and sequence added.
747 boolean searchDataset(boolean fromDna, SequenceI sequenceI,
748 DBRefEntry xrf, List<SequenceI> rseqs, AlignedCodonFrame cf,
751 boolean found = false;
756 if (dataset.getSequences() == null)
758 System.err.println("Empty dataset sequence set - NO VECTOR");
762 synchronized (ds = dataset.getSequences())
764 for (SequenceI nxt : ds)
768 if (nxt.getDatasetSequence() != null)
771 .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
773 if (nxt == sequenceI || nxt == sequenceI.getDatasetSequence())
778 * only look at same molecule type if 'direct', or
779 * complementary type if !direct
782 boolean isDna = !nxt.isProtein();
783 if (direct ? (isDna != fromDna) : (isDna == fromDna))
785 // skip this sequence because it is wrong molecule type
790 // look for direct or indirect references in common
791 DBRefEntry[] poss = nxt.getDBRefs();
792 List<DBRefEntry> cands = null;
794 * TODO does this make any sense?
795 * if 'direct', search the dbrefs for xrf
796 * else, filter the dbrefs by type and then search for xrf
797 * - the result is the same isn't it?
801 cands = DBRefUtils.searchRefs(poss, xrf);
805 poss = DBRefUtils.selectDbRefs(!fromDna, poss);
806 cands = DBRefUtils.searchRefs(poss, xrf);
808 if (!cands.isEmpty())
810 if (!rseqs.contains(nxt))
816 // don't search if we aren't given a codon map object
817 for (DBRefEntry candidate : cands)
819 Mapping mapping = candidate.getMap();
822 MapList map = mapping.getMap();
823 if (mapping.getTo() != null
824 && map.getFromRatio() != map.getToRatio())
826 // get sense of map correct for adding to product
830 // map is from dna seq to a protein product
831 cf.addMap(sequenceI, nxt, map);
835 // map should be from protein seq to its coding dna
836 cf.addMap(nxt, sequenceI, map.getInverse());
842 // TODO: add mapping between sequences if necessary