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.ws.SequenceFetcher;
34 import jalview.ws.seqfetcher.ASequenceFetcher;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Vector;
41 * Functions for cross-referencing sequence databases. user must first specify
42 * if cross-referencing from protein or dna (set dna==true)
50 * A sub-class that ignores Parent attribute when comparing sequence
51 * features. This avoids 'duplicate' CDS features that only
52 * differ in their parent Transcript ids.
54 class MySequenceFeature extends SequenceFeature
56 private SequenceFeature feat;
58 MySequenceFeature(SequenceFeature sf)
64 public boolean equals(Object o)
66 return feat.equals(o, true);
71 * Select just the DNA or protein references for a protein or dna sequence
74 * if true, select references from DNA (i.e. Protein databases), else
75 * DNA database references
77 * a set of references to select from
80 public static DBRefEntry[] findXDbRefs(boolean fromDna, DBRefEntry[] refs)
82 return DBRefUtils.selectRefs(refs, fromDna ? DBRefSource.PROTEINDBS
83 : DBRefSource.DNACODINGDBS);
84 // could attempt to find other cross
85 // refs here - ie PDB xrefs
86 // (not dna, not protein seq)
91 * true if seqs are DNA seqs
93 * @return a list of sequence database cross reference source types
95 public static String[] findSequenceXrefTypes(boolean dna, SequenceI[] seqs)
97 return findSequenceXrefTypes(dna, seqs, null);
101 * Indirect references are references from other sequences from the dataset to
102 * any of the direct DBRefEntrys on the given sequences.
105 * true if seqs are DNA seqs
107 * @return a list of sequence database cross reference source types
109 public static String[] findSequenceXrefTypes(boolean dna,
110 SequenceI[] seqs, AlignmentI dataset)
112 String[] dbrefs = null;
113 List<String> refs = new ArrayList<String>();
114 for (SequenceI seq : seqs)
119 while (dss.getDatasetSequence() != null)
121 dss = dss.getDatasetSequence();
123 DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRefs());
126 for (DBRefEntry ref : rfs)
128 if (!refs.contains(ref.getSource()))
130 refs.add(ref.getSource());
136 // search for references to this sequence's direct references.
137 DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRefs());
138 List<SequenceI> rseqs = new ArrayList<SequenceI>();
139 CrossRef.searchDatasetXrefs(seq, !dna, lrfs, dataset, rseqs,
140 null); // don't need to specify codon frame for mapping here
141 for (SequenceI rs : rseqs)
143 DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRefs());
146 for (DBRefEntry ref : xrs)
148 if (!refs.contains(ref.getSource()))
150 refs.add(ref.getSource());
154 // looks like copy and paste - change rfs to xrs?
155 // for (int r = 0; rfs != null && r < rfs.length; r++)
157 // if (!refs.contains(rfs[r].getSource()))
159 // refs.add(rfs[r].getSource());
168 dbrefs = new String[refs.size()];
169 refs.toArray(dbrefs);
174 public static boolean hasCdnaMap(SequenceI[] seqs)
176 // TODO unused - remove?
177 String[] reftypes = findSequenceXrefTypes(false, seqs);
178 for (int s = 0; s < reftypes.length; s++)
180 if (reftypes.equals(DBRefSource.EMBLCDS))
189 public static SequenceI[] getCdnaMap(SequenceI[] seqs)
191 // TODO unused - remove?
192 Vector cseqs = new Vector();
193 for (int s = 0; s < seqs.length; s++)
195 DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRefs());
196 for (int c = 0; c < cdna.length; c++)
198 if (cdna[c].getSource().equals(DBRefSource.EMBLCDS))
201 .println("TODO: unimplemented sequence retrieval for coding region sequence.");
202 // TODO: retrieve CDS dataset sequences
203 // need global dataset sequence retriever/resolver to reuse refs
204 // and construct Mapping entry.
205 // insert gaps in CDS according to peptide gaps.
206 // add gapped sequence to cseqs
210 if (cseqs.size() > 0)
212 SequenceI[] rsqs = new SequenceI[cseqs.size()];
213 cseqs.copyInto(rsqs);
223 * sequences whose xrefs are being retrieved
225 * true if sequences are nucleotide
228 * alignment to search for cross-referenced sequences (and possibly
231 * a list of sequences to add to if 'peers' to the original sequences
232 * are found e.g. alternative protein products for a protein's gene
233 * @return products (as dataset sequences)
235 public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,
236 String source, AlignmentI al, List<SequenceI> addedPeers)
238 AlignmentI dataset = al.getDataset() == null ? al : al.getDataset();
239 List<SequenceI> rseqs = new ArrayList<SequenceI>();
240 AlignedCodonFrame cf = new AlignedCodonFrame();
241 for (SequenceI seq : seqs)
244 while (dss.getDatasetSequence() != null)
246 dss = dss.getDatasetSequence();
248 boolean found = false;
249 DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRefs());
250 if ((xrfs == null || xrfs.length == 0) && dataset != null)
252 System.out.println("Attempting to find ds Xrefs refs.");
253 // FIXME should be dss not seq here?
254 DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seq.getDBRefs());
255 // less ambiguous would be a 'find primary dbRefEntry' method.
256 // filter for desired source xref here
257 found = CrossRef.searchDatasetXrefs(dss, !dna, lrfs, dataset,
260 for (int r = 0; xrfs != null && r < xrfs.length; r++)
262 DBRefEntry xref = xrfs[r];
263 if (source != null && !source.equals(xref.getSource()))
269 if (xref.getMap().getTo() != null)
271 SequenceI rsq = new Sequence(xref.getMap().getTo());
273 if (xref.getMap().getMap().getFromRatio() != xref
274 .getMap().getMap().getToRatio())
276 // get sense of map correct for adding to product alignment.
279 // map is from dna seq to a protein product
280 cf.addMap(dss, rsq, xref.getMap().getMap());
284 // map should be from protein seq to its coding dna
285 cf.addMap(rsq, dss, xref.getMap().getMap().getInverse());
293 // do a bit more work - search for sequences with references matching
294 // xrefs on this sequence.
297 found |= searchDataset(dss, xref, dataset, rseqs, cf); // ,false,!dna);
300 xrfs[r] = null; // we've recovered seqs for this one.
307 if (xrfs != null && xrfs.length > 0)
309 // Try and get the sequence reference...
311 * Ideal world - we ask for a sequence fetcher implementation here if
312 * (jalview.io.RunTimeEnvironment.getSequenceFetcher()) (
314 ASequenceFetcher sftch = new SequenceFetcher();
315 SequenceI[] retrieved = null;
317 for (int r = 0; r < xrfs.length; r++)
319 // filter out any irrelevant or irretrievable references
321 || ((source != null && !source.equals(xrfs[r]
322 .getSource())) || !sftch.isFetchable(xrfs[r]
332 .println("Attempting to retrieve cross referenced sequences.");
333 DBRefEntry[] t = new DBRefEntry[l];
335 for (int r = 0; r < xrfs.length; r++)
345 retrieved = sftch.getSequences(xrfs, !dna);
346 // problem here is we don't know which of xrfs resulted in which
348 } catch (Exception e)
351 .println("Problem whilst retrieving cross references for Sequence : "
356 if (retrieved != null)
358 List<SequenceFeature> copiedFeatures = new ArrayList<SequenceFeature>();
359 CrossRef me = new CrossRef();
360 for (int rs = 0; rs < retrieved.length; rs++)
362 // TODO: examine each sequence for 'redundancy'
363 DBRefEntry[] dbr = retrieved[rs].getDBRefs();
364 if (dbr != null && dbr.length > 0)
366 for (int di = 0; di < dbr.length; di++)
368 // find any entry where we should put in the sequence being
369 // cross-referenced into the map
370 Mapping map = dbr[di].getMap();
373 if (map.getTo() != null && map.getMap() != null)
375 // should search the local dataset to find any existing
376 // candidates for To !
379 // compare ms with dss and replace with dss in mapping
380 // if map is congruent
381 SequenceI ms = map.getTo();
382 int sf = map.getMap().getToLowest();
383 int st = map.getMap().getToHighest();
384 SequenceI mappedrg = ms.getSubSequence(sf, st);
385 // SequenceI loc = dss.getSubSequence(sf, st);
386 if (mappedrg.getLength() > 0
387 && ms.getSequenceAsString().equals(
388 dss.getSequenceAsString()))
389 // && mappedrg.getSequenceAsString().equals(
390 // loc.getSequenceAsString()))
392 String msg = "Mapping updated from "
394 + " to retrieved crossreference "
396 System.out.println(msg);
397 // method to update all refs of existing To on
398 // retrieved sequence with dss and merge any props
402 * copy sequence features as well, avoiding
403 * duplication (e.g. from 2 transcripts)
405 SequenceFeature[] sfs = ms
406 .getSequenceFeatures();
409 for (SequenceFeature feat : sfs)
412 * we override SequenceFeature.equals here (but
413 * not elsewhere) to ignore Parent attribute
414 * TODO not quite working yet!
417 .contains(me.new MySequenceFeature(
420 dss.addSequenceFeature(feat);
421 copiedFeatures.add(feat);
425 cf.addMap(retrieved[rs].getDatasetSequence(),
430 addedPeers.add(map.getTo());
431 cf.addMap(retrieved[rs].getDatasetSequence(),
432 map.getTo(), map.getMap());
434 } catch (Exception e)
437 .println("Exception when consolidating Mapped sequence set...");
438 e.printStackTrace(System.err);
444 retrieved[rs].updatePDBIds();
445 rseqs.add(retrieved[rs]);
453 Alignment ral = null;
454 if (rseqs.size() > 0)
456 ral = new Alignment(rseqs.toArray(new SequenceI[rseqs.size()]));
457 if (cf != null && !cf.isEmpty())
459 ral.addCodonFrame(cf);
466 * find references to lrfs in the cross-reference set of each sequence in
467 * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
468 * based on source and accession string only - Map and Version are nulled.
474 * @return true if matches were found.
476 private static boolean searchDatasetXrefs(SequenceI sequenceI,
477 boolean dna, DBRefEntry[] lrfs, AlignmentI dataset,
478 List<SequenceI> rseqs, AlignedCodonFrame cf)
480 boolean found = false;
485 for (int i = 0; i < lrfs.length; i++)
487 DBRefEntry xref = new DBRefEntry(lrfs[i]);
489 xref.setVersion(null);
491 found = searchDataset(sequenceI, xref, dataset, rseqs, cf, false, dna);
497 * search a given sequence dataset for references matching cross-references to
504 * set of unique sequences
506 * @return true if one or more unique sequences were found and added
508 public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
509 AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf)
511 return searchDataset(sequenceI, xrf, dataset, rseqs, cf, true, false);
515 * TODO: generalise to different protein classifications Search dataset for
516 * DBRefEntrys matching the given one (xrf) and add the associated sequence to
524 * - search all references or only subset
526 * search dna or protein xrefs (if direct=false)
527 * @return true if relationship found and sequence added.
529 public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
530 AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf,
531 boolean direct, boolean dna)
533 boolean found = false;
534 SequenceI[] typer = new SequenceI[1];
539 if (dataset.getSequences() == null)
541 System.err.println("Empty dataset sequence set - NO VECTOR");
545 synchronized (ds = dataset.getSequences())
547 for (SequenceI nxt : ds)
551 if (nxt.getDatasetSequence() != null)
554 .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
556 if (nxt != sequenceI && nxt != sequenceI.getDatasetSequence())
558 // check if this is the correct sequence type
561 boolean isDna = jalview.util.Comparison.isNucleotide(typer);
562 if ((direct && isDna == dna) || (!direct && isDna != dna))
564 // skip this sequence because it is same molecule type
569 // look for direct or indirect references in common
570 DBRefEntry[] poss = nxt.getDBRefs(), cands = null;
573 cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
577 poss = CrossRef.findXDbRefs(dna, poss); //
578 cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
582 if (!rseqs.contains(nxt))
585 boolean foundmap = cf != null;
586 // don't search if we aren't given a codon map object
587 for (int r = 0; foundmap && r < cands.length; r++)
589 if (cands[r].hasMap())
591 if (cands[r].getMap().getTo() != null
592 && cands[r].getMap().getMap().getFromRatio() != cands[r]
593 .getMap().getMap().getToRatio())
596 // get sense of map correct for adding to product
600 // map is from dna seq to a protein product
601 cf.addMap(sequenceI, nxt, cands[r].getMap()
606 // map should be from protein seq to its coding dna
607 cf.addMap(nxt, sequenceI, cands[r].getMap()
608 .getMap().getInverse());
613 // TODO: add mapping between sequences if necessary
626 * precalculate different products that can be found for seqs in dataset and
633 * - don't actually build lists - just get types
634 * @return public static Object[] buildXProductsList(boolean dna, SequenceI[]
635 * seqs, AlignmentI dataset, boolean fake) { String types[] =
636 * jalview.analysis.CrossRef.findSequenceXrefTypes( dna, seqs,
637 * dataset); if (types != null) { System.out.println("Xref Types for:
638 * "+(dna ? "dna" : "prot")); for (int t = 0; t < types.length; t++) {
639 * System.out.println("Type: " + types[t]); SequenceI[] prod =
640 * jalview.analysis.CrossRef.findXrefSequences(seqs, dna, types[t]);
641 * System.out.println("Found " + ((prod == null) ? "no" : "" +
642 * prod.length) + " products"); if (prod!=null) { for (int p=0;
643 * p<prod.length; p++) { System.out.println("Prod "+p+":
644 * "+prod[p].getDisplayId(true)); } } } } else {
645 * System.out.println("Trying getProducts for
646 * "+al.getSequenceAt(0).getDisplayId(true));
647 * System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot"));
648 * // have a bash at finding the products amongst all the retrieved
649 * sequences. SequenceI[] prod =
650 * jalview.analysis.CrossRef.findXrefSequences(al
651 * .getSequencesArray(), dna, null, ds); System.out.println("Found " +
652 * ((prod == null) ? "no" : "" + prod.length) + " products"); if
653 * (prod!=null) { // select non-equivalent sequences from dataset list
654 * for (int p=0; p<prod.length; p++) { System.out.println("Prod "+p+":
655 * "+prod[p].getDisplayId(true)); } } } }