1 package jalview.analysis;
3 import jalview.datamodel.AlignedCodonFrame;
4 import jalview.datamodel.Alignment;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.DBRefEntry;
7 import jalview.datamodel.Mapping;
8 import jalview.datamodel.Sequence;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.datamodel.SequenceI;
11 import jalview.util.Comparison;
12 import jalview.util.DBRefUtils;
13 import jalview.util.MapList;
14 import jalview.ws.SequenceFetcherFactory;
15 import jalview.ws.seqfetcher.ASequenceFetcher;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
21 public class CrossRefs
24 * Finds cross-references for sequences from a specified source database.
25 * These may be found in four ways:
27 * <li>as a DBRefEntry on the known sequence, which has a mapped-to sequence</li>
28 * <li>a sequence of complementary type in the alignment dataset, which has a
29 * DBRefEntry to one of the known sequence's 'direct' DBRefs</li>
30 * <li>a sequence of complementary type in the alignment, which has a
31 * DBRefEntry to one of the known sequence's 'cross-ref' DBRefs</li>
32 * <li>by fetching the accession from the remote database</li>
36 * the sequences whose cross-references we are searching for
38 * true if the sequences are from a nucleotide alignment, else false
40 * the database source we want cross-references to
42 * the alignment dataset the sequences belong to
43 * @return an alignment containing cross-reference sequences, or null if none
46 public static AlignmentI findXrefSequences(SequenceI[] seqs, boolean dna,
47 String source, AlignmentI dataset)
49 List<SequenceI> foundSeqs = new ArrayList<SequenceI>();
50 AlignedCodonFrame mappings = new AlignedCodonFrame();
52 for (SequenceI seq : seqs)
54 if (dna != Comparison.isNucleotide(seq))
57 * mixed alignment, and this sequence is of the wrong type
63 * get this sequence's dbrefs to source database (if any)
65 List<DBRefEntry> sourceRefs = DBRefUtils.searchRefsForSource(
66 seq.getDBRefs(), source);
69 * first extract any mapped sequences from sourceRefs
71 findMappedDbrefs(seq, sourceRefs, foundSeqs, mappings);
74 * for remaining sourceRefs, try to match a
75 * complementary sequence in the dataset
77 findIndirectCrossReferences(seq, source, sourceRefs, dataset,
81 * fetch any remaining sourceRefs from the source database
83 fetchCrossReferences(seq, sourceRefs, foundSeqs, mappings, dna,
87 if (foundSeqs.isEmpty())
91 AlignmentI crossRefs = new Alignment(
92 foundSeqs.toArray(new SequenceI[foundSeqs.size()]));
93 crossRefs.addCodonFrame(mappings);
98 * Looks for DBRefEntrys to 'source' which have a mapping to a sequence. If
99 * found, adds the sequence to foundSeqs and removes the dbref from the list.
102 * the dataset sequence we are searching from
104 * the sequence's dbrefs to 'source'
106 * a list of cross-references to add to
108 * a set of sequence mappings to add to
111 static void findMappedDbrefs(SequenceI seq, List<DBRefEntry> sourceRefs,
112 List<SequenceI> foundSeqs, AlignedCodonFrame mappings)
114 Iterator<DBRefEntry> refs = sourceRefs.iterator();
115 while (refs.hasNext())
117 DBRefEntry dbref = refs.next();
118 Mapping map = dbref.getMap();
121 SequenceI mappedTo = map.getTo();
122 if (mappedTo != null)
124 foundSeqs.add(new Sequence(mappedTo));
128 * check mapping is not 'direct' (it shouldn't be if we reach here)
129 * and add mapping (dna-to-peptide or vice versa) to the set
131 MapList mapList = map.getMap();
132 int fromRatio = mapList.getFromRatio();
133 int toRatio = mapList.getToRatio();
134 if (fromRatio != toRatio)
138 mappings.addMap(seq, mappedTo, mapList);
142 mappings.addMap(mappedTo, seq, mapList.getInverse());
151 * Tries to fetch seq's database references to 'source' database, and add them
152 * to the foundSeqs list. If found, tries to make a mapping between seq and
153 * the retrieved sequence and insert it into the database reference.
161 static void fetchCrossReferences(SequenceI seq,
162 List<DBRefEntry> sourceRefs, List<SequenceI> foundSeqs,
163 AlignedCodonFrame mappings, boolean dna, AlignmentI dataset)
165 ASequenceFetcher sftch = SequenceFetcherFactory.getSequenceFetcher();
166 SequenceI[] retrieved;
169 retrieved = sftch.getSequences(sourceRefs, !dna);
170 } catch (Exception e)
173 .println("Problem whilst retrieving cross references for Sequence : "
179 if (retrieved != null)
181 updateDbrefMappings(dna, seq, sourceRefs, retrieved, mappings);
183 SequenceIdMatcher matcher = new SequenceIdMatcher(
184 dataset.getSequences());
185 List<SequenceFeature> copiedFeatures = new ArrayList<SequenceFeature>();
186 CrossRef me = new CrossRef();
187 for (int rs = 0; rs < retrieved.length; rs++)
189 // TODO: examine each sequence for 'redundancy'
190 DBRefEntry[] dbr = retrieved[rs].getDBRefs();
191 if (dbr != null && dbr.length > 0)
193 for (int di = 0; di < dbr.length; di++)
195 // find any entry where we should put in the sequence being
196 // cross-referenced into the map
197 Mapping map = dbr[di].getMap();
200 if (map.getTo() != null && map.getMap() != null)
202 SequenceI matched = matcher.findIdMatch(map.getTo());
206 * already got an xref to this sequence; update this
207 * map to point to the same sequence, and add
208 * any new dbrefs to it
210 for (DBRefEntry ref : map.getTo().getDBRefs())
212 matched.addDBRef(ref); // add or update mapping
218 matcher.add(map.getTo());
222 // compare ms with dss and replace with dss in mapping
223 // if map is congruent
224 SequenceI ms = map.getTo();
225 int sf = map.getMap().getToLowest();
226 int st = map.getMap().getToHighest();
227 SequenceI mappedrg = ms.getSubSequence(sf, st);
228 // SequenceI loc = dss.getSubSequence(sf, st);
229 if (mappedrg.getLength() > 0
230 && ms.getSequenceAsString().equals(
231 seq.getSequenceAsString()))
232 // && mappedrg.getSequenceAsString().equals(
233 // loc.getSequenceAsString()))
235 String msg = "Mapping updated from " + ms.getName()
236 + " to retrieved crossreference "
238 System.out.println(msg);
239 // method to update all refs of existing To on
240 // retrieved sequence with dss and merge any props
244 * copy sequence features as well, avoiding
245 * duplication (e.g. same variation from 2
248 SequenceFeature[] sfs = ms.getSequenceFeatures();
251 for (SequenceFeature feat : sfs)
254 * we override SequenceFeature.equals here (but
255 * not elsewhere) to ignore Parent attribute
256 * TODO not quite working yet!
259 .contains(me.new MySequenceFeature(feat)))
261 seq.addSequenceFeature(feat);
262 copiedFeatures.add(feat);
267 mappings.addMap(retrieved[rs].getDatasetSequence(),
268 map.getTo(), map.getMap());
269 } catch (Exception e)
272 .println("Exception when consolidating Mapped sequence set...");
273 e.printStackTrace(System.err);
279 retrieved[rs].updatePDBIds();
280 foundSeqs.add(retrieved[rs]);
286 * Searches the alignment for a sequence of complementary type to 'seq' which
287 * shares a DBRefEntry with it. If found, adds the sequence to foundSeqs and
288 * removes the resolved sourceRef from the search list.
298 static void findIndirectCrossReferences(SequenceI seq, String source,
299 List<DBRefEntry> sourceRefs, AlignmentI dataset,
300 List<SequenceI> foundSeqs, AlignedCodonFrame mappings)
302 Iterator<DBRefEntry> refs = sourceRefs.iterator();
303 while (refs.hasNext())
305 DBRefEntry dbref = refs.next();
306 boolean found = searchDatasetForCrossReference(seq, dbref, dataset,
307 foundSeqs, mappings);
316 * Searches the dataset for a sequence of opposite type to 'excluding', which
317 * has a cross-reference matching dbref. If found, adds the sequence to
318 * foundSeqs and removes dbref from the search list.
321 * a sequence to ignore (start point of search)
323 * a cross-reference to try to match
325 * sequences to search in
327 * result list to add to
329 * a set of sequence mappings to add to
330 * @return true if relationship found and sequence added
332 static boolean searchDatasetForCrossReference(SequenceI excluding,
333 DBRefEntry dbref, AlignmentI dataset, List<SequenceI> foundSeqs,
334 AlignedCodonFrame mappings)
336 boolean fromNucleotide = Comparison.isNucleotide(excluding);
337 boolean found = false;
342 if (dataset.getSequences() == null)
347 synchronized (ds = dataset.getSequences())
349 for (SequenceI nxt : ds)
353 if (nxt.getDatasetSequence() != null)
356 .println("Implementation warning: getProducts passed a dataset alignment without dataset sequences in it!");
358 if (nxt == excluding || nxt == excluding.getDatasetSequence())
362 if (foundSeqs.contains(nxt))
365 * already added this sequence to cross-refs
369 boolean isDna = Comparison.isNucleotide(nxt);
370 if (isDna == fromNucleotide)
373 * skip this sequence - wrong molecule type
379 * check if this sequence has any dbref matching source and accession
380 * (version and mapping may differ)
382 List<DBRefEntry> candidates = DBRefUtils.searchRefs(
383 nxt.getDBRefs(), dbref);
385 if (candidates.isEmpty())
391 if (mappings != null)
393 // don't search if we aren't given a codon map object
394 for (DBRefEntry candidate : candidates)
396 if (candidate.hasMap())
398 Mapping mapping = candidate.getMap();
399 MapList map = mapping.getMap();
400 if (mapping.getTo() != null
401 && map.getFromRatio() != map.getToRatio())
405 // map is from dna seq to a protein product
406 mappings.addMap(excluding, nxt, map);
410 // map is from protein seq to its coding dna
411 mappings.addMap(nxt, excluding, map.getInverse());
424 * Updates any empty mappings in the cross-references with one to a compatible
425 * retrieved sequence if found, and adds any new mappings to the
434 static void updateDbrefMappings(boolean dna, SequenceI mapFrom,
435 List<DBRefEntry> xrefs, SequenceI[] retrieved,
436 AlignedCodonFrame mappings)
438 SequenceIdMatcher matcher = new SequenceIdMatcher(retrieved);
439 for (DBRefEntry xref : xrefs)
443 String targetSeqName = xref.getSource() + "|"
444 + xref.getAccessionId();
445 SequenceI[] matches = matcher.findAllIdMatches(targetSeqName);
450 for (SequenceI seq : matches)
452 MapList mapping = null;
455 mapping = AlignmentUtils.mapCdnaToProtein(seq, mapFrom);
459 mapping = AlignmentUtils.mapCdnaToProtein(mapFrom, seq);
462 mapping = mapping.getInverse();
467 xref.setMap(new Mapping(seq, mapping));
470 AlignmentUtils.computeProteinFeatures(mapFrom, seq, mapping);
474 mappings.addMap(mapFrom, seq, mapping);
478 mappings.addMap(seq, mapFrom, mapping.getInverse());