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 java.util.ArrayList;
24 import java.util.List;
25 import java.util.Vector;
27 import jalview.datamodel.AlignedCodonFrame;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.DBRefEntry;
31 import jalview.datamodel.DBRefSource;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.util.DBRefUtils;
35 import jalview.ws.SequenceFetcher;
36 import jalview.ws.seqfetcher.ASequenceFetcher;
39 * Functions for cross-referencing sequence databases. user must first specify
40 * if cross-referencing from protein or dna (set dna==true)
48 * Select just the DNA or protein references for a protein or dna sequence
51 * if true, select references from DNA (i.e. Protein databases), else
52 * DNA database references
54 * a set of references to select from
57 public static DBRefEntry[] findXDbRefs(boolean fromDna, DBRefEntry[] refs)
59 return DBRefUtils.selectRefs(refs, fromDna ? DBRefSource.PROTEINDBS
60 : DBRefSource.DNACODINGDBS);
61 // could attempt to find other cross
62 // refs here - ie PDB xrefs
63 // (not dna, not protein seq)
68 * true if seqs are DNA seqs
70 * @return a list of sequence database cross reference source types
72 public static String[] findSequenceXrefTypes(boolean dna, SequenceI[] seqs)
74 return findSequenceXrefTypes(dna, seqs, null);
78 * Indirect references are references from other sequences from the dataset to
79 * any of the direct DBRefEntrys on the given sequences.
82 * true if seqs are DNA seqs
84 * @return a list of sequence database cross reference source types
86 public static String[] findSequenceXrefTypes(boolean dna,
87 SequenceI[] seqs, AlignmentI dataset)
89 String[] dbrefs = null;
90 List<String> refs = new ArrayList<String>();
91 for (int s = 0; s < seqs.length; s++)
95 SequenceI dss = seqs[s];
96 while (dss.getDatasetSequence() != null)
98 dss = dss.getDatasetSequence();
100 DBRefEntry[] rfs = findXDbRefs(dna, dss.getDBRef());
101 for (int r = 0; rfs != null && r < rfs.length; r++)
103 if (!refs.contains(rfs[r].getSource()))
105 refs.add(rfs[r].getSource());
110 // search for references to this sequence's direct references.
111 DBRefEntry[] lrfs = CrossRef
112 .findXDbRefs(!dna, seqs[s].getDBRef());
113 List<SequenceI> rseqs = new ArrayList<SequenceI>();
114 CrossRef.searchDatasetXrefs(seqs[s], !dna, lrfs, dataset, rseqs,
115 null); // don't need to specify codon frame for mapping here
116 for (SequenceI rs : rseqs)
118 DBRefEntry[] xrs = findXDbRefs(dna, rs.getDBRef()); // not used??
119 for (int r = 0; rfs != null && r < rfs.length; r++)
121 if (!refs.contains(rfs[r].getSource()))
123 refs.add(rfs[r].getSource());
132 dbrefs = new String[refs.size()];
133 refs.toArray(dbrefs);
139 * if (dna) { if (rfs[r].hasMap()) { // most likely this is a protein cross
140 * reference if (!refs.contains(rfs[r].getSource())) {
141 * refs.addElement(rfs[r].getSource()); } } }
143 public static boolean hasCdnaMap(SequenceI[] seqs)
145 String[] reftypes = findSequenceXrefTypes(false, seqs);
146 for (int s = 0; s < reftypes.length; s++)
148 if (reftypes.equals(DBRefSource.EMBLCDS))
157 public static SequenceI[] getCdnaMap(SequenceI[] seqs)
159 Vector cseqs = new Vector();
160 for (int s = 0; s < seqs.length; s++)
162 DBRefEntry[] cdna = findXDbRefs(true, seqs[s].getDBRef());
163 for (int c = 0; c < cdna.length; c++)
165 if (cdna[c].getSource().equals(DBRefSource.EMBLCDS))
168 .println("TODO: unimplemented sequence retrieval for coding region sequence.");
169 // TODO: retrieve CDS dataset sequences
170 // need global dataset sequence retriever/resolver to reuse refs
171 // and construct Mapping entry.
172 // insert gaps in CDS according to peptide gaps.
173 // add gapped sequence to cseqs
177 if (cseqs.size() > 0)
179 SequenceI[] rsqs = new SequenceI[cseqs.size()];
180 cseqs.copyInto(rsqs);
193 public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,
196 return findXrefSequences(seqs, dna, source, null);
205 * alignment to search for product sequences.
206 * @return products (as dataset sequences)
208 public static Alignment findXrefSequences(SequenceI[] seqs, boolean dna,
209 String source, AlignmentI dataset)
211 List<SequenceI> rseqs = new ArrayList<SequenceI>();
212 Alignment ral = null;
213 AlignedCodonFrame cf = new AlignedCodonFrame(); // nominal width
214 for (int s = 0; s < seqs.length; s++)
216 SequenceI dss = seqs[s];
217 while (dss.getDatasetSequence() != null)
219 dss = dss.getDatasetSequence();
221 boolean found = false;
222 DBRefEntry[] xrfs = CrossRef.findXDbRefs(dna, dss.getDBRef());
223 if ((xrfs == null || xrfs.length == 0) && dataset != null)
225 System.out.println("Attempting to find ds Xrefs refs.");
226 DBRefEntry[] lrfs = CrossRef.findXDbRefs(!dna, seqs[s].getDBRef());
227 // less ambiguous would be a 'find primary dbRefEntry' method.
228 // filter for desired source xref here
229 found = CrossRef.searchDatasetXrefs(dss, !dna, lrfs, dataset,
232 for (int r = 0; xrfs != null && r < xrfs.length; r++)
234 if (source != null && !source.equals(xrfs[r].getSource()))
238 if (xrfs[r].hasMap())
240 if (xrfs[r].getMap().getTo() != null)
242 SequenceI rsq = new Sequence(xrfs[r].getMap().getTo());
244 if (xrfs[r].getMap().getMap().getFromRatio() != xrfs[r]
245 .getMap().getMap().getToRatio())
247 // get sense of map correct for adding to product alignment.
250 // map is from dna seq to a protein product
251 cf.addMap(dss, rsq, xrfs[r].getMap().getMap());
255 // map should be from protein seq to its coding dna
256 cf.addMap(rsq, dss, xrfs[r].getMap().getMap().getInverse());
264 // do a bit more work - search for sequences with references matching
265 // xrefs on this sequence.
268 found |= searchDataset(dss, xrfs[r], dataset, rseqs, cf); // ,false,!dna);
271 xrfs[r] = null; // we've recovered seqs for this one.
278 if (xrfs != null && xrfs.length > 0)
280 // Try and get the sequence reference...
282 * Ideal world - we ask for a sequence fetcher implementation here if
283 * (jalview.io.RunTimeEnvironment.getSequenceFetcher()) (
285 ASequenceFetcher sftch = new SequenceFetcher();
286 SequenceI[] retrieved = null;
288 for (int r = 0; r < xrfs.length; r++)
290 // filter out any irrelevant or irretrievable references
292 || ((source != null && !source.equals(xrfs[r]
293 .getSource())) || !sftch.isFetchable(xrfs[r]
303 .println("Attempting to retrieve cross referenced sequences.");
304 DBRefEntry[] t = new DBRefEntry[l];
306 for (int r = 0; r < xrfs.length; r++)
316 retrieved = sftch.getSequences(xrfs); // problem here is we don't
317 // know which of xrfs
320 } catch (Exception e)
323 .println("Problem whilst retrieving cross references for Sequence : "
324 + seqs[s].getName());
327 if (retrieved != null)
329 for (int rs = 0; rs < retrieved.length; rs++)
331 // TODO: examine each sequence for 'redundancy'
332 jalview.datamodel.DBRefEntry[] dbr = retrieved[rs]
334 if (dbr != null && dbr.length > 0)
336 for (int di = 0; di < dbr.length; di++)
338 // find any entry where we should put in the sequence being
339 // cross-referenced into the map
340 jalview.datamodel.Mapping map = dbr[di].getMap();
343 if (map.getTo() != null && map.getMap() != null)
345 // should search the local dataset to find any existing
346 // candidates for To !
349 // compare ms with dss and replace with dss in mapping
350 // if map is congruent
351 SequenceI ms = map.getTo();
352 int sf = map.getMap().getToLowest();
353 int st = map.getMap().getToHighest();
354 SequenceI mappedrg = ms.getSubSequence(sf, st);
355 SequenceI loc = dss.getSubSequence(sf, st);
356 if (mappedrg.getLength() > 0
357 && mappedrg.getSequenceAsString().equals(
358 loc.getSequenceAsString()))
361 .println("Mapping updated for retrieved crossreference");
362 // method to update all refs of existing To on
363 // retrieved sequence with dss and merge any props
367 } catch (Exception e)
370 .println("Exception when consolidating Mapped sequence set...");
371 e.printStackTrace(System.err);
377 retrieved[rs].updatePDBIds();
378 rseqs.add(retrieved[rs]);
385 if (rseqs.size() > 0)
387 SequenceI[] rsqs = new SequenceI[rseqs.size()];
389 ral = new Alignment(rsqs);
390 if (cf != null && cf.getProtMappings() != null)
392 ral.addCodonFrame(cf);
399 * find references to lrfs in the cross-reference set of each sequence in
400 * dataset (that is not equal to sequenceI) Identifies matching DBRefEntry
401 * based on source and accession string only - Map and Version are nulled.
407 * @return true if matches were found.
409 private static boolean searchDatasetXrefs(SequenceI sequenceI,
410 boolean dna, DBRefEntry[] lrfs, AlignmentI dataset,
411 List<SequenceI> rseqs,
412 AlignedCodonFrame cf)
414 boolean found = false;
419 for (int i = 0; i < lrfs.length; i++)
421 DBRefEntry xref = new DBRefEntry(lrfs[i]);
423 xref.setVersion(null);
425 found = searchDataset(sequenceI, xref, dataset, rseqs, cf, false, dna);
431 * search a given sequence dataset for references matching cross-references to
438 * set of unique sequences
440 * @return true if one or more unique sequences were found and added
442 public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
443 AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf)
445 return searchDataset(sequenceI, xrf, dataset, rseqs, cf, true, false);
449 * TODO: generalise to different protein classifications Search dataset for
450 * DBRefEntrys matching the given one (xrf) and add the associated sequence to
458 * - search all references or only subset
460 * search dna or protein xrefs (if direct=false)
461 * @return true if relationship found and sequence added.
463 public static boolean searchDataset(SequenceI sequenceI, DBRefEntry xrf,
464 AlignmentI dataset, List<SequenceI> rseqs, AlignedCodonFrame cf,
465 boolean direct, boolean dna)
467 boolean found = false;
468 SequenceI[] typer = new SequenceI[1];
473 if (dataset.getSequences() == null)
475 System.err.println("Empty dataset sequence set - NO VECTOR");
479 synchronized (ds = dataset.getSequences())
481 for (SequenceI nxt : ds)
485 if (nxt.getDatasetSequence() != null)
488 .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
490 if (nxt != sequenceI && nxt != sequenceI.getDatasetSequence())
492 // check if this is the correct sequence type
495 boolean isDna = jalview.util.Comparison.isNucleotide(typer);
496 if ((direct && isDna == dna) || (!direct && isDna != dna))
498 // skip this sequence because it is same molecule type
503 // look for direct or indirect references in common
504 DBRefEntry[] poss = nxt.getDBRef(), cands = null;
507 cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
511 poss = CrossRef.findXDbRefs(dna, poss); //
512 cands = jalview.util.DBRefUtils.searchRefs(poss, xrf);
516 if (!rseqs.contains(nxt))
519 boolean foundmap = cf != null;
520 // don't search if we aren't given a codon map object
521 for (int r = 0; foundmap && r < cands.length; r++)
523 if (cands[r].hasMap())
525 if (cands[r].getMap().getTo() != null
526 && cands[r].getMap().getMap().getFromRatio() != cands[r]
527 .getMap().getMap().getToRatio())
530 // get sense of map correct for adding to product
534 // map is from dna seq to a protein product
535 cf.addMap(sequenceI, nxt, cands[r].getMap()
540 // map should be from protein seq to its coding dna
541 cf.addMap(nxt, sequenceI, cands[r].getMap()
542 .getMap().getInverse());
547 // TODO: add mapping between sequences if necessary
560 * precalculate different products that can be found for seqs in dataset and
567 * - don't actually build lists - just get types
568 * @return public static Object[] buildXProductsList(boolean dna, SequenceI[]
569 * seqs, AlignmentI dataset, boolean fake) { String types[] =
570 * jalview.analysis.CrossRef.findSequenceXrefTypes( dna, seqs,
571 * dataset); if (types != null) { System.out.println("Xref Types for:
572 * "+(dna ? "dna" : "prot")); for (int t = 0; t < types.length; t++) {
573 * System.out.println("Type: " + types[t]); SequenceI[] prod =
574 * jalview.analysis.CrossRef.findXrefSequences(seqs, dna, types[t]);
575 * System.out.println("Found " + ((prod == null) ? "no" : "" +
576 * prod.length) + " products"); if (prod!=null) { for (int p=0;
577 * p<prod.length; p++) { System.out.println("Prod "+p+":
578 * "+prod[p].getDisplayId(true)); } } } } else {
579 * System.out.println("Trying getProducts for
580 * "+al.getSequenceAt(0).getDisplayId(true));
581 * System.out.println("Search DS Xref for: "+(dna ? "dna" : "prot"));
582 * // have a bash at finding the products amongst all the retrieved
583 * sequences. SequenceI[] prod =
584 * jalview.analysis.CrossRef.findXrefSequences(al
585 * .getSequencesArray(), dna, null, ds); System.out.println("Found " +
586 * ((prod == null) ? "no" : "" + prod.length) + " products"); if
587 * (prod!=null) { // select non-equivalent sequences from dataset list
588 * for (int p=0; p<prod.length; p++) { System.out.println("Prod "+p+":
589 * "+prod[p].getDisplayId(true)); } } } }