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 * true if we are searching for cross-references from nucleotide,
58 * i.e. for protein sequences, false if the reverse
60 private boolean fromDna;
63 * the sequences for which we are seeking cross-references
65 private SequenceI[] fromSeqs;
71 * the sequences for which we are seeking cross-references
73 * the containing alignment dataset (may be searched to resolve
76 public CrossRef(SequenceI[] seqs, AlignmentI ds)
79 fromDna = ds.isNucleotide();
80 dataset = ds.getDataset() == null ? ds : ds.getDataset();
84 * Returns a list of distinct database sources for which sequences have either
86 * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
87 * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
88 * reference from another sequence in the dataset which has a cross-reference
89 * to a direct DBRefEntry on the given sequence</li>
93 public List<String> findXrefSourcesForSequences()
95 List<String> sources = new ArrayList<String>();
96 for (SequenceI seq : fromSeqs)
100 findXrefSourcesForSequence(seq, sources);
107 * Returns a list of distinct database sources for which a sequence has either
109 * <li>a (dna-to-protein or protein-to-dna) cross-reference</li>
110 * <li>an indirect cross-reference - a (dna-to-protein or protein-to-dna)
111 * reference from another sequence in the dataset which has a cross-reference
112 * to a direct DBRefEntry on the given sequence</li>
116 * the sequence whose dbrefs we are searching against
118 * a list of sources to add matches to
120 void findXrefSourcesForSequence(SequenceI seq, List<String> sources)
123 * first find seq's xrefs (dna-to-peptide or peptide-to-dna)
125 DBRefEntry[] rfs = DBRefUtils.selectDbRefs(!fromDna, seq.getDBRefs());
126 addXrefsToSources(rfs, sources);
130 * find sequence's direct (dna-to-dna, peptide-to-peptide) xrefs
132 DBRefEntry[] lrfs = DBRefUtils.selectDbRefs(fromDna, seq.getDBRefs());
133 List<SequenceI> rseqs = new ArrayList<SequenceI>();
136 * find sequences in the alignment which xref one of these DBRefs
137 * i.e. is xref-ed to a common sequence identifier
139 searchDatasetXrefs(seq, lrfs, rseqs, null);
142 * add those sequences' (dna-to-peptide or peptide-to-dna) dbref sources
144 for (SequenceI rs : rseqs)
146 DBRefEntry[] xrs = DBRefUtils
147 .selectDbRefs(!fromDna, rs.getDBRefs());
148 addXrefsToSources(xrs, sources);
154 * Helper method that adds the source identifiers of some cross-references to
155 * a (non-redundant) list of database sources
160 void addXrefsToSources(DBRefEntry[] xrefs, List<String> sources)
164 for (DBRefEntry ref : xrefs)
167 * avoid duplication e.g. ENSEMBL and Ensembl
169 String source = DBRefUtils.getCanonicalName(ref.getSource());
170 if (!sources.contains(source))
179 * Attempts to find cross-references from the sequences provided in the
180 * constructor to the given source database. Cross-references may be found
182 * <li>in dbrefs on the sequence which hold a mapping to a sequence
184 * <li>provided with a fetched sequence (e.g. ENA translation), or</li>
185 * <li>populated previously after getting cross-references</li>
187 * <li>as other sequences in the alignment which share a dbref identifier with
189 * <li>by fetching from the remote database</li>
191 * The cross-referenced sequences, and mappings to them, are added to the
195 * @return cross-referenced sequences (as dataset sequences)
197 public Alignment findXrefSequences(String source)
200 List<SequenceI> rseqs = new ArrayList<SequenceI>();
201 AlignedCodonFrame cf = new AlignedCodonFrame();
202 SequenceIdMatcher matcher = new SequenceIdMatcher(
203 dataset.getSequences());
205 for (SequenceI seq : fromSeqs)
208 while (dss.getDatasetSequence() != null)
210 dss = dss.getDatasetSequence();
212 boolean found = false;
213 DBRefEntry[] xrfs = DBRefUtils
214 .selectDbRefs(!fromDna, dss.getDBRefs());
215 if ((xrfs == null || xrfs.length == 0) && dataset != null)
218 * found no suitable dbrefs on sequence - look for sequences in the
219 * alignment which share a dbref with this one
221 DBRefEntry[] lrfs = DBRefUtils.selectDbRefs(fromDna,
225 * find sequences (except this one!), of complementary type,
226 * which have a dbref to an accession id for this sequence,
227 * and add them to the results
229 found = searchDatasetXrefs(dss, lrfs, rseqs, cf);
231 if (xrfs == null && !found)
234 * no dbref to source on this sequence or matched
235 * complementary sequence in the dataset
239 List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(xrfs,
241 Iterator<DBRefEntry> refIterator = sourceRefs.iterator();
242 while (refIterator.hasNext())
244 DBRefEntry xref = refIterator.next();
248 SequenceI mappedTo = xref.getMap().getTo();
249 if (mappedTo != null)
252 * dbref contains the sequence it maps to; add it to the
253 * results unless we have done so already (could happen if
254 * fetching xrefs for sequences which have xrefs in common)
255 * for example: UNIPROT {P0CE19, P0CE20} -> EMBL {J03321, X06707}
259 * problem: matcher.findIdMatch() is lenient - returns a sequence
260 * with a dbref to the search arg e.g. ENST for ENSP - wrong
261 * but findInDataset() matches ENSP when looking for Uniprot...
263 SequenceI matchInDataset = findInDataset(xref);
264 /*matcher.findIdMatch(mappedTo);*/
265 if (matchInDataset != null)
267 if (!rseqs.contains(matchInDataset))
269 rseqs.add(matchInDataset);
271 refIterator.remove();
274 SequenceI rsq = new Sequence(mappedTo);
276 if (xref.getMap().getMap().getFromRatio() != xref.getMap()
277 .getMap().getToRatio())
279 // get sense of map correct for adding to product alignment.
282 // map is from dna seq to a protein product
283 cf.addMap(dss, rsq, xref.getMap().getMap());
287 // map should be from protein seq to its coding dna
288 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse());
296 SequenceI matchedSeq = matcher.findIdMatch(xref.getSource() + "|"
297 + xref.getAccessionId());
298 if (matchedSeq != null)
300 if (constructMapping(seq, matchedSeq, xref, cf))
309 // do a bit more work - search for sequences with references matching
310 // xrefs on this sequence.
311 found = searchDataset(dss, xref, rseqs, cf, false);
315 refIterator.remove();
320 * fetch from source database any dbrefs we haven't resolved up to here
322 if (!sourceRefs.isEmpty())
324 ASequenceFetcher sftch = SequenceFetcherFactory
325 .getSequenceFetcher();
326 SequenceI[] retrieved = null;
329 retrieved = sftch.getSequences(sourceRefs, !fromDna);
330 } catch (Exception e)
333 .println("Problem whilst retrieving cross references for Sequence : "
338 if (retrieved != null)
340 updateDbrefMappings(seq, xrfs, retrieved, cf);
341 for (SequenceI retrievedSequence : retrieved)
343 SequenceI retrievedDss = retrievedSequence.getDatasetSequence() == null ? retrievedSequence
344 : retrievedSequence.getDatasetSequence();
345 DBRefEntry[] dbr = retrievedSequence.getDBRefs();
348 for (DBRefEntry dbref : dbr)
350 // find any entry where we should put in the sequence being
351 // cross-referenced into the map
352 Mapping map = dbref.getMap();
355 if (map.getTo() != null && map.getMap() != null)
357 // TODO findInDataset requires exact sequence match but
358 // 'congruent' test is only for the mapped part
359 // maybe not a problem in practice since only ENA provide a
360 // mapping and it is to the full protein translation of CDS
361 SequenceI matched = findInDataset(dbref);
362 // matcher.findIdMatch(map.getTo());
366 * already got an xref to this sequence; update this
367 * map to point to the same sequence, and add
368 * any new dbrefs to it
370 DBRefEntry[] toRefs = map.getTo().getDBRefs();
373 for (DBRefEntry ref : toRefs)
375 matched.addDBRef(ref); // add or update mapping
382 matcher.add(map.getTo());
386 // compare ms with dss and replace with dss in mapping
387 // if map is congruent
388 SequenceI ms = map.getTo();
389 int sf = map.getMap().getToLowest();
390 int st = map.getMap().getToHighest();
391 SequenceI mappedrg = ms.getSubSequence(sf, st);
392 // SequenceI loc = dss.getSubSequence(sf, st);
393 if (mappedrg.getLength() > 0
394 && ms.getSequenceAsString().equals(
395 dss.getSequenceAsString()))
396 // && mappedrg.getSequenceAsString().equals(
397 // loc.getSequenceAsString()))
399 String msg = "Mapping updated from " + ms.getName()
400 + " to retrieved crossreference "
402 System.out.println(msg);
406 * give the reverse reference the inverse mapping
407 * (if it doesn't have one already)
409 setReverseMapping(dss, dbref, cf);
412 * copy sequence features as well, avoiding
413 * duplication (e.g. same variation from two
416 SequenceFeature[] sfs = ms.getSequenceFeatures();
419 for (SequenceFeature feat : sfs)
422 * make a flyweight feature object which ignores Parent
423 * attribute in equality test; this avoids creating many
424 * otherwise duplicate exon features on genomic sequence
426 SequenceFeature newFeature = new SequenceFeature(
430 public boolean equals(Object o)
432 return super.equals(o, true);
435 dss.addSequenceFeature(newFeature);
439 cf.addMap(retrievedDss, map.getTo(), map.getMap());
440 } catch (Exception e)
443 .println("Exception when consolidating Mapped sequence set...");
444 e.printStackTrace(System.err);
450 retrievedSequence.updatePDBIds();
451 rseqs.add(retrievedDss);
452 dataset.addSequence(retrievedDss);
453 matcher.add(retrievedDss);
459 Alignment ral = null;
460 if (rseqs.size() > 0)
462 ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
465 dataset.addCodonFrame(cf);
472 * Sets the inverse sequence mapping in the corresponding dbref of the mapped
473 * to sequence (if any). This is used after fetching a cross-referenced
474 * sequence, if the fetched sequence has a mapping to the original sequence,
475 * to set the mapping in the original sequence's dbref.
478 * the sequence mapped from
482 void setReverseMapping(SequenceI mapFrom, DBRefEntry dbref,
483 AlignedCodonFrame mappings)
485 SequenceI mapTo = dbref.getMap().getTo();
490 DBRefEntry[] dbrefs = mapTo.getDBRefs();
495 for (DBRefEntry toRef : dbrefs)
497 if (toRef.hasMap() && mapFrom == toRef.getMap().getTo())
500 * found the reverse dbref; update its mapping if null
502 if (toRef.getMap().getMap() == null)
504 MapList inverse = dbref.getMap().getMap().getInverse();
505 toRef.getMap().setMap(inverse);
506 mappings.addMap(mapTo, mapFrom, inverse);
513 * Returns the first identical sequence in the dataset if any, else null
518 SequenceI findInDataset(DBRefEntry xref)
520 if (xref == null || !xref.hasMap() || xref.getMap().getTo() == null)
524 SequenceI mapsTo = xref.getMap().getTo();
525 String name = xref.getAccessionId();
526 String name2 = xref.getSource() + "|" + name;
527 SequenceI dss = mapsTo.getDatasetSequence() == null ? mapsTo : mapsTo
528 .getDatasetSequence();
529 for (SequenceI seq : dataset.getSequences())
532 * clumsy alternative to using SequenceIdMatcher which currently
533 * returns sequences with a dbref to the matched accession id
534 * which we don't want
536 if (name.equals(seq.getName()) || seq.getName().startsWith(name2))
538 if (sameSequence(seq, dss))
548 * Answers true if seq1 and seq2 contain exactly the same characters (ignoring
549 * case), else false. This method compares the lengths, then each character in
550 * turn, in order to 'fail fast'. For case-sensitive comparison, it would be
551 * possible to use Arrays.equals(seq1.getSequence(), seq2.getSequence()).
557 // TODO move to Sequence / SequenceI
558 static boolean sameSequence(SequenceI seq1, SequenceI seq2)
564 if (seq1 == null || seq2 == null)
568 char[] c1 = seq1.getSequence();
569 char[] c2 = seq2.getSequence();
570 if (c1.length != c2.length)
574 for (int i = 0; i < c1.length; i++)
576 int diff = c1[i] - c2[i];
578 * same char or differ in case only ('a'-'A' == 32)
580 if (diff != 0 && diff != 32 && diff != -32)
589 * Updates any empty mappings in the cross-references with one to a compatible
590 * retrieved sequence if found, and adds any new mappings to the
598 void updateDbrefMappings(SequenceI mapFrom,
599 DBRefEntry[] xrefs, SequenceI[] retrieved, AlignedCodonFrame acf)
601 SequenceIdMatcher matcher = new SequenceIdMatcher(retrieved);
602 for (DBRefEntry xref : xrefs)
606 String targetSeqName = xref.getSource() + "|"
607 + xref.getAccessionId();
608 SequenceI[] matches = matcher.findAllIdMatches(targetSeqName);
613 for (SequenceI seq : matches)
615 constructMapping(mapFrom, seq, xref, acf);
622 * Tries to make a mapping between sequences. If successful, adds the mapping
623 * to the dbref and the mappings collection and answers true, otherwise
624 * answers false. The following methods of making are mapping are tried in
627 * <li>if 'mapTo' holds a mapping to 'mapFrom', take the inverse; this is, for
628 * example, the case after fetching EMBL cross-references for a Uniprot
630 * <li>else check if the dna translates exactly to the protein (give or take
631 * start and stop codons></li>
632 * <li>else try to map based on CDS features on the dna sequence</li>
641 boolean constructMapping(SequenceI mapFrom, SequenceI mapTo,
642 DBRefEntry xref, AlignedCodonFrame mappings)
644 MapList mapping = null;
647 * look for a reverse mapping, if found make its inverse
649 if (mapTo.getDBRefs() != null)
651 for (DBRefEntry dbref : mapTo.getDBRefs())
653 String name = dbref.getSource() + "|" + dbref.getAccessionId();
654 if (dbref.hasMap() && mapFrom.getName().startsWith(name))
657 * looks like we've found a map from 'mapTo' to 'mapFrom'
658 * - invert it to make the mapping the other way
660 MapList reverse = dbref.getMap().getMap().getInverse();
661 xref.setMap(new Mapping(mapTo, reverse));
662 mappings.addMap(mapFrom, mapTo, reverse);
670 mapping = AlignmentUtils.mapCdnaToProtein(mapTo, mapFrom);
674 mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, mapTo);
677 mapping = mapping.getInverse();
684 xref.setMap(new Mapping(mapTo, mapping));
687 AlignmentUtils.computeProteinFeatures(mapFrom, mapTo, mapping);
688 mappings.addMap(mapFrom, mapTo, mapping);
692 mappings.addMap(mapTo, mapFrom, mapping.getInverse());
699 * find references to lrfs in the cross-reference set of each sequence in
700 * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
701 * based on source and accession string only - Map and Version are nulled.
706 * @return true if matches were found.
708 private boolean searchDatasetXrefs(SequenceI sequenceI,
709 DBRefEntry[] lrfs, List<SequenceI> rseqs, AlignedCodonFrame cf)
711 boolean found = false;
716 for (int i = 0; i < lrfs.length; i++)
718 DBRefEntry xref = new DBRefEntry(lrfs[i]);
720 xref.setVersion(null);
722 found |= searchDataset(sequenceI, xref, rseqs, cf, false);
728 * Searches dataset for DBRefEntrys matching the given one (xrf) and adds the
729 * associated sequence to rseqs
732 * a sequence to ignore (start point of search)
734 * a cross-reference to try to match
736 * result list to add to
738 * a set of sequence mappings to add to
740 * - search all references or only subset
741 * @return true if relationship found and sequence added.
743 boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
744 List<SequenceI> rseqs, AlignedCodonFrame cf, boolean direct)
746 boolean found = false;
751 if (dataset.getSequences() == null)
753 System.err.println("Empty dataset sequence set - NO VECTOR");
757 synchronized (ds = dataset.getSequences())
759 for (SequenceI nxt : ds)
763 if (nxt.getDatasetSequence() != null)
766 .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
768 if (nxt == sequenceI || nxt == sequenceI.getDatasetSequence())
773 * only look at same molecule type if 'direct', or
774 * complementary type if !direct
777 boolean isDna = Comparison
778 .isNucleotide(new SequenceI[] { nxt });
779 if (direct ? (isDna != fromDna) : (isDna == fromDna))
781 // skip this sequence because it is wrong molecule type
786 // look for direct or indirect references in common
787 DBRefEntry[] poss = nxt.getDBRefs();
788 List<DBRefEntry> cands = null;
790 * TODO does this make any sense?
791 * if 'direct', search the dbrefs for xrf
792 * else, filter the dbrefs by type and then search for xrf
793 * - the result is the same isn't it?
797 cands = DBRefUtils.searchRefs(poss, xrf);
801 poss = DBRefUtils.selectDbRefs(!fromDna, poss);
802 cands = DBRefUtils.searchRefs(poss, xrf);
804 if (!cands.isEmpty())
806 if (!rseqs.contains(nxt))
812 // don't search if we aren't given a codon map object
813 for (DBRefEntry candidate : cands)
815 Mapping mapping = candidate.getMap();
818 MapList map = mapping.getMap();
819 if (mapping.getTo() != null
820 && map.getFromRatio() != map.getToRatio())
822 // get sense of map correct for adding to product
826 // map is from dna seq to a protein product
827 cf.addMap(sequenceI, nxt, map);
831 // map should be from protein seq to its coding dna
832 cf.addMap(nxt, sequenceI, map.getInverse());
838 // TODO: add mapping between sequences if necessary