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.
23 import jalview.analysis.AlignmentUtils;
24 import jalview.analysis.CrossRef;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.FeatureSettingsModelI;
27 import jalview.bin.Cache;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.DBRefEntry;
31 import jalview.datamodel.DBRefSource;
32 import jalview.datamodel.GeneLociI;
33 import jalview.datamodel.SequenceI;
34 import jalview.ext.ensembl.EnsemblInfo;
35 import jalview.ext.ensembl.EnsemblMap;
36 import jalview.io.gff.SequenceOntologyI;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.util.DBRefUtils;
39 import jalview.util.MapList;
40 import jalview.util.MappingUtils;
41 import jalview.util.MessageManager;
42 import jalview.ws.SequenceFetcher;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
51 * Factory constructor and runnable for discovering and displaying
52 * cross-references for a set of aligned sequences
57 public class CrossRefAction implements Runnable
59 private AlignFrame alignFrame;
61 private SequenceI[] sel;
63 private final boolean _odna;
65 private String source;
67 List<AlignmentViewPanel> xrefViews = new ArrayList<>();
69 List<AlignmentViewPanel> getXrefViews()
77 final long sttime = System.currentTimeMillis();
78 alignFrame.setProgressBar(MessageManager.formatMessage(
79 "status.searching_for_sequences_from", new Object[]
83 AlignmentI alignment = alignFrame.getViewport().getAlignment();
84 AlignmentI dataset = alignment.getDataset() == null ? alignment
85 : alignment.getDataset();
86 boolean dna = alignment.isNucleotide();
90 .println("Conflict: showProducts for alignment originally "
91 + "thought to be " + (_odna ? "DNA" : "Protein")
92 + " now searching for " + (dna ? "DNA" : "Protein")
95 AlignmentI xrefs = new CrossRef(sel, dataset)
96 .findXrefSequences(source, dna);
103 * try to look up chromosomal coordinates for nucleotide
104 * sequences (if not already retrieved)
106 findGeneLoci(xrefs.getSequences());
109 * get display scheme (if any) to apply to features
111 FeatureSettingsModelI featureColourScheme = new SequenceFetcher()
112 .getFeatureColourScheme(source);
114 if (dna && AlignmentUtils.looksLikeEnsembl(alignment))
116 // override default featureColourScheme so products have Ensembl variant colours
117 featureColourScheme = new SequenceFetcher()
118 .getFeatureColourScheme(DBRefSource.ENSEMBL);
121 AlignmentI xrefsAlignment = makeCrossReferencesAlignment(dataset,
125 xrefsAlignment = AlignmentUtils.makeCdsAlignment(
126 xrefsAlignment.getSequencesArray(), dataset, sel);
127 xrefsAlignment.alignAs(alignment);
131 * If we are opening a splitframe, make a copy of this alignment (sharing the same dataset
132 * sequences). If we are DNA, drop introns and update mappings
134 AlignmentI copyAlignment = null;
136 if (Cache.getDefault(Preferences.ENABLE_SPLIT_FRAME, true))
138 copyAlignment = copyAlignmentForSplitFrame(alignment, dataset, dna,
139 xrefs, xrefsAlignment);
140 if (copyAlignment == null)
147 * build AlignFrame(s) according to available alignment data
149 AlignFrame newFrame = new AlignFrame(xrefsAlignment,
150 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
151 if (Cache.getDefault("HIDE_INTRONS", true))
153 newFrame.hideFeatureColumns(SequenceOntologyI.EXON, false);
155 String newtitle = String.format("%s %s %s",
156 dna ? MessageManager.getString("label.proteins")
157 : MessageManager.getString("label.nucleotides"),
158 MessageManager.getString("label.for"), alignFrame.getTitle());
159 newFrame.setTitle(newtitle);
161 if (copyAlignment == null)
164 * split frame display is turned off in preferences file
166 Desktop.addInternalFrame(newFrame, newtitle,
167 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
168 xrefViews.add(newFrame.alignPanel);
169 return; // via finally clause
172 AlignFrame copyThis = new AlignFrame(copyAlignment,
173 AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
174 copyThis.setTitle(alignFrame.getTitle());
176 boolean showSequenceFeatures = alignFrame.getViewport()
177 .isShowSequenceFeatures();
178 newFrame.setShowSeqFeatures(showSequenceFeatures);
179 copyThis.setShowSeqFeatures(showSequenceFeatures);
180 FeatureRenderer myFeatureStyling = alignFrame.alignPanel
181 .getSeqPanel().seqCanvas.getFeatureRenderer();
184 * copy feature rendering settings to split frame
186 FeatureRenderer fr1 = newFrame.alignPanel.getSeqPanel().seqCanvas
187 .getFeatureRenderer();
188 fr1.transferSettings(myFeatureStyling);
189 fr1.findAllFeatures(true);
190 FeatureRenderer fr2 = copyThis.alignPanel.getSeqPanel().seqCanvas
191 .getFeatureRenderer();
192 fr2.transferSettings(myFeatureStyling);
193 fr2.findAllFeatures(true);
196 * apply 'database source' feature configuration
197 * if any - first to the new splitframe view about to be displayed
200 newFrame.getViewport().applyFeaturesStyle(featureColourScheme);
201 copyThis.getViewport().applyFeaturesStyle(featureColourScheme);
204 * and for JAL-3330 also to original alignFrame view(s)
205 * this currently trashes any original settings.
207 for (AlignmentViewPanel origpanel: alignFrame.getAlignPanels()) {
208 origpanel.getAlignViewport()
209 .mergeFeaturesStyle(featureColourScheme);
212 SplitFrame sf = new SplitFrame(dna ? copyThis : newFrame,
213 dna ? newFrame : copyThis);
215 newFrame.setVisible(true);
216 copyThis.setVisible(true);
217 String linkedTitle = MessageManager
218 .getString("label.linked_view_title");
219 Desktop.addInternalFrame(sf, linkedTitle, -1, -1);
220 sf.adjustInitialLayout();
222 // finally add the top, then bottom frame to the view list
223 xrefViews.add(dna ? copyThis.alignPanel : newFrame.alignPanel);
224 xrefViews.add(!dna ? copyThis.alignPanel : newFrame.alignPanel);
226 } catch (OutOfMemoryError e)
228 new OOMWarning("whilst fetching crossreferences", e);
229 } catch (Throwable e)
231 Cache.log.error("Error when finding crossreferences", e);
234 alignFrame.setProgressBar(MessageManager.formatMessage(
235 "status.finished_searching_for_sequences_from", new Object[]
236 { source }), sttime);
241 * Tries to add chromosomal coordinates to any nucleotide sequence which does
242 * not already have them. Coordinates are retrieved from Ensembl given an
243 * Ensembl identifier, either on the sequence itself or on a peptide sequence
244 * it has a reference to.
248 * - fetch EMBLCDS cross-references for Uniprot entry P30419
249 * - the EMBL sequences do not have xrefs to Ensembl
250 * - the Uniprot entry has xrefs to
251 * ENSP00000258960, ENSP00000468424, ENST00000258960, ENST00000592782
252 * - either of the transcript ids can be used to retrieve gene loci e.g.
253 * http://rest.ensembl.org/map/cds/ENST00000592782/1..100000
254 * Example (invertebrate):
255 * - fetch EMBLCDS cross-references for Uniprot entry Q43517 (FER1_SOLLC)
256 * - the Uniprot entry has an xref to ENSEMBLPLANTS Solyc10g044520.1.1
257 * - can retrieve gene loci with
258 * http://rest.ensemblgenomes.org/map/cds/Solyc10g044520.1.1/1..100000
263 public static void findGeneLoci(List<SequenceI> sequences)
265 Map<DBRefEntry, GeneLociI> retrievedLoci = new HashMap<>();
266 for (SequenceI seq : sequences)
268 findGeneLoci(seq, retrievedLoci);
273 * Tres to find chromosomal coordinates for the sequence, by searching its
274 * direct and indirect cross-references for Ensembl. If the loci have already
275 * been retrieved, just reads them out of the map of retrievedLoci; this is
276 * the case of an alternative transcript for the same protein. Otherwise calls
277 * a REST service to retrieve the loci, and if successful, adds them to the
278 * sequence and to the retrievedLoci.
281 * @param retrievedLoci
283 static void findGeneLoci(SequenceI seq,
284 Map<DBRefEntry, GeneLociI> retrievedLoci)
287 * don't replace any existing chromosomal coordinates
289 if (seq == null || seq.isProtein() || seq.getGeneLoci() != null
290 || seq.getDBRefs() == null)
295 Set<String> ensemblDivisions = new EnsemblInfo().getDivisions();
298 * first look for direct dbrefs from sequence to Ensembl
300 String[] divisionsArray = ensemblDivisions
301 .toArray(new String[ensemblDivisions.size()]);
302 DBRefEntry[] seqRefs = seq.getDBRefs();
303 DBRefEntry[] directEnsemblRefs = DBRefUtils.selectRefs(seqRefs,
305 if (directEnsemblRefs != null)
307 for (DBRefEntry ensemblRef : directEnsemblRefs)
309 if (fetchGeneLoci(seq, ensemblRef, retrievedLoci))
317 * else look for indirect dbrefs from sequence to Ensembl
319 for (DBRefEntry dbref : seq.getDBRefs())
321 if (dbref.getMap() != null && dbref.getMap().getTo() != null)
323 DBRefEntry[] dbrefs = dbref.getMap().getTo().getDBRefs();
324 DBRefEntry[] indirectEnsemblRefs = DBRefUtils.selectRefs(dbrefs,
326 if (indirectEnsemblRefs != null)
328 for (DBRefEntry ensemblRef : indirectEnsemblRefs)
330 if (fetchGeneLoci(seq, ensemblRef, retrievedLoci))
341 * Retrieves chromosomal coordinates for the Ensembl (or EnsemblGenomes)
342 * identifier in dbref. If successful, and the sequence length matches gene
343 * loci length, then add it to the sequence, and to the retrievedLoci map.
344 * Answers true if successful, else false.
348 * @param retrievedLoci
351 static boolean fetchGeneLoci(SequenceI seq, DBRefEntry dbref,
352 Map<DBRefEntry, GeneLociI> retrievedLoci)
354 String accession = dbref.getAccessionId();
355 String division = dbref.getSource();
358 * hack: ignore cross-references to Ensembl protein ids
359 * (or use map/translation perhaps?)
360 * todo: is there an equivalent in EnsemblGenomes?
362 if (accession.startsWith("ENSP"))
366 EnsemblMap mapper = new EnsemblMap();
369 * try CDS mapping first
371 GeneLociI geneLoci = mapper.getCdsMapping(division, accession, 1,
373 if (geneLoci != null)
375 MapList map = geneLoci.getMapping();
376 int mappedFromLength = MappingUtils.getLength(map.getFromRanges());
377 if (mappedFromLength == seq.getLength())
379 seq.setGeneLoci(geneLoci.getSpeciesId(), geneLoci.getAssemblyId(),
380 geneLoci.getChromosomeId(), map);
381 retrievedLoci.put(dbref, geneLoci);
387 * else try CDNA mapping
389 geneLoci = mapper.getCdnaMapping(division, accession, 1,
391 if (geneLoci != null)
393 MapList map = geneLoci.getMapping();
394 int mappedFromLength = MappingUtils.getLength(map.getFromRanges());
395 if (mappedFromLength == seq.getLength())
397 seq.setGeneLoci(geneLoci.getSpeciesId(), geneLoci.getAssemblyId(),
398 geneLoci.getChromosomeId(), map);
399 retrievedLoci.put(dbref, geneLoci);
412 * @param xrefsAlignment
415 protected AlignmentI copyAlignmentForSplitFrame(AlignmentI alignment,
416 AlignmentI dataset, boolean dna, AlignmentI xrefs,
417 AlignmentI xrefsAlignment)
419 AlignmentI copyAlignment;
420 boolean copyAlignmentIsAligned = false;
423 copyAlignment = AlignmentUtils.makeCdsAlignment(sel, dataset,
424 xrefsAlignment.getSequencesArray());
425 if (copyAlignment.getHeight() == 0)
427 JvOptionPane.showMessageDialog(alignFrame,
428 MessageManager.getString("label.cant_map_cds"),
429 MessageManager.getString("label.operation_failed"),
430 JvOptionPane.OK_OPTION);
431 System.err.println("Failed to make CDS alignment");
436 * pending getting Embl transcripts to 'align',
437 * we are only doing this for Ensembl
439 // TODO proper criteria for 'can align as cdna'
440 if (DBRefSource.ENSEMBL.equalsIgnoreCase(source)
441 || AlignmentUtils.looksLikeEnsembl(alignment))
443 copyAlignment.alignAs(alignment);
444 copyAlignmentIsAligned = true;
449 copyAlignment = AlignmentUtils.makeCopyAlignment(sel,
450 xrefs.getSequencesArray(), dataset);
453 .setGapCharacter(alignFrame.viewport.getGapCharacter());
455 StructureSelectionManager ssm = StructureSelectionManager
456 .getStructureSelectionManager(Desktop.instance);
459 * register any new mappings for sequence mouseover etc
460 * (will not duplicate any previously registered mappings)
462 ssm.registerMappings(dataset.getCodonFrames());
464 if (copyAlignment.getHeight() <= 0)
467 "No Sequences generated for xRef type " + source);
472 * align protein to dna
474 if (dna && copyAlignmentIsAligned)
476 xrefsAlignment.alignAs(copyAlignment);
481 * align cdna to protein - currently only if
482 * fetching and aligning Ensembl transcripts!
484 // TODO: generalise for other sources of locus/transcript/cds data
485 if (dna && DBRefSource.ENSEMBL.equalsIgnoreCase(source))
487 copyAlignment.alignAs(xrefsAlignment);
491 return copyAlignment;
495 * Makes an alignment containing the given sequences, and adds them to the
496 * given dataset, which is also set as the dataset for the new alignment
498 * TODO: refactor to DatasetI method
504 protected AlignmentI makeCrossReferencesAlignment(AlignmentI dataset,
507 SequenceI[] sprods = new SequenceI[seqs.getHeight()];
508 for (int s = 0; s < sprods.length; s++)
510 sprods[s] = (seqs.getSequenceAt(s)).deriveSequence();
511 if (dataset.getSequences() == null || !dataset.getSequences()
512 .contains(sprods[s].getDatasetSequence()))
514 dataset.addSequence(sprods[s].getDatasetSequence());
516 sprods[s].updatePDBIds();
518 Alignment al = new Alignment(sprods);
519 al.setDataset(dataset);
531 CrossRefAction(AlignFrame af, SequenceI[] seqs, boolean fromDna,
534 this.alignFrame = af;
536 this._odna = fromDna;
537 this.source = dbSource;
540 public static CrossRefAction getHandlerFor(final SequenceI[] sel,
541 final boolean fromDna, final String source,
542 final AlignFrame alignFrame)
544 return new CrossRefAction(alignFrame, sel, fromDna, source);