2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import java.awt.Rectangle;
25 import java.util.jar.*;
29 import org.exolab.castor.xml.*;
31 import uk.ac.vamsas.objects.utils.MapList;
32 import jalview.bin.Cache;
33 import jalview.datamodel.Alignment;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.SequenceI;
36 import jalview.schemabinding.version2.*;
37 import jalview.schemes.*;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.util.jarInputStreamProvider;
42 * Write out the current jalview desktop state as a Jalview XML stream.
44 * Note: the vamsas objects referred to here are primitive versions of the
45 * VAMSAS project schema elements - they are not the same and most likely never
51 public class Jalview2XML
54 * create/return unique hash string for sq
57 * @return new or existing unique string for sq
59 String seqHash(SequenceI sq)
61 if (seqsToIds == null)
65 if (seqsToIds.containsKey(sq))
67 return (String) seqsToIds.get(sq);
71 // create sequential key
72 String key = "sq" + (seqsToIds.size() + 1);
73 key = makeHashCode(sq, key); // check we don't have an external reference
75 seqsToIds.put(sq, key);
84 if (seqRefIds != null)
88 if (seqsToIds != null)
98 warn("clearSeqRefs called when _cleartables was not set. Doing nothing.");
99 // seqRefIds = new Hashtable();
100 // seqsToIds = new IdentityHashMap();
106 if (seqsToIds == null)
108 seqsToIds = new IdentityHashMap();
110 if (seqRefIds == null)
112 seqRefIds = new Hashtable();
117 * SequenceI reference -> XML ID string in jalview XML. Populated as XML reps
118 * of sequence objects are created.
120 java.util.IdentityHashMap seqsToIds = null;
123 * jalview XML Sequence ID to jalview sequence object reference (both dataset
124 * and alignment sequences. Populated as XML reps of sequence objects are
127 java.util.Hashtable seqRefIds = null; // key->SequenceI resolution
129 Vector frefedSequence = null;
131 boolean raiseGUI = true; // whether errors are raised in dialog boxes or not
137 public Jalview2XML(boolean raiseGUI)
139 this.raiseGUI = raiseGUI;
142 public void resolveFrefedSequences()
144 if (frefedSequence.size() > 0)
146 int r = 0, rSize = frefedSequence.size();
149 Object[] ref = (Object[]) frefedSequence.elementAt(r);
152 String sref = (String) ref[0];
153 if (seqRefIds.containsKey(sref))
155 if (ref[1] instanceof jalview.datamodel.Mapping)
157 SequenceI seq = (SequenceI) seqRefIds.get(sref);
158 while (seq.getDatasetSequence() != null)
160 seq = seq.getDatasetSequence();
162 ((jalview.datamodel.Mapping) ref[1]).setTo(seq);
166 if (ref[1] instanceof jalview.datamodel.AlignedCodonFrame)
168 SequenceI seq = (SequenceI) seqRefIds.get(sref);
169 while (seq.getDatasetSequence() != null)
171 seq = seq.getDatasetSequence();
174 && ref[2] instanceof jalview.datamodel.Mapping)
176 jalview.datamodel.Mapping mp = (jalview.datamodel.Mapping) ref[2];
177 ((jalview.datamodel.AlignedCodonFrame) ref[1]).addMap(
178 seq, mp.getTo(), mp.getMap());
183 .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for AlcodonFrames involving "
184 + ref[2].getClass() + " type objects.");
190 .println("IMPLEMENTATION ERROR: Unimplemented forward sequence references for "
191 + ref[1].getClass() + " type objects.");
194 frefedSequence.remove(r);
200 .println("IMPLEMENTATION WARNING: Unresolved forward reference for hash string "
202 + " with objecttype "
203 + ref[1].getClass());
210 frefedSequence.remove(r);
218 * This maintains a list of viewports, the key being the seqSetId. Important
219 * to set historyItem and redoList for multiple views
221 Hashtable viewportsAdded;
223 Hashtable annotationIds = new Hashtable();
225 String uniqueSetSuffix = "";
228 * List of pdbfiles added to Jar
230 Vector pdbfiles = null;
232 // SAVES SEVERAL ALIGNMENT WINDOWS TO SAME JARFILE
233 public void SaveState(File statefile)
237 FileOutputStream fos = new FileOutputStream(statefile);
238 JarOutputStream jout = new JarOutputStream(fos);
241 } catch (Exception e)
243 // TODO: inform user of the problem - they need to know if their data was
245 if (errorMessage == null)
247 errorMessage = "Couldn't write Jalview Archive to output file '"
248 + statefile + "' - See console error log for details";
252 errorMessage += "(output file was '" + statefile + "')";
260 * Writes a jalview project archive to the given Jar output stream.
264 public void SaveState(JarOutputStream jout)
266 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
276 // NOTE UTF-8 MUST BE USED FOR WRITING UNICODE CHARS
277 // //////////////////////////////////////////////////
278 // NOTE ALSO new PrintWriter must be used for each new JarEntry
279 PrintWriter out = null;
281 Vector shortNames = new Vector();
284 for (int i = frames.length - 1; i > -1; i--)
286 if (frames[i] instanceof AlignFrame)
288 AlignFrame af = (AlignFrame) frames[i];
291 && skipList.containsKey(af.getViewport()
292 .getSequenceSetId()))
297 String shortName = af.getTitle();
299 if (shortName.indexOf(File.separatorChar) > -1)
301 shortName = shortName.substring(shortName
302 .lastIndexOf(File.separatorChar) + 1);
307 while (shortNames.contains(shortName))
309 if (shortName.endsWith("_" + (count - 1)))
311 shortName = shortName
312 .substring(0, shortName.lastIndexOf("_"));
315 shortName = shortName.concat("_" + count);
319 shortNames.addElement(shortName);
321 if (!shortName.endsWith(".xml"))
323 shortName = shortName + ".xml";
326 int ap, apSize = af.alignPanels.size();
327 for (ap = 0; ap < apSize; ap++)
329 AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
331 String fileName = apSize == 1 ? shortName : ap + shortName;
332 if (!fileName.endsWith(".xml"))
334 fileName = fileName + ".xml";
337 SaveState(apanel, fileName, jout);
344 } catch (Exception foo)
349 } catch (Exception ex)
351 // TODO: inform user of the problem - they need to know if their data was
353 if (errorMessage == null)
355 errorMessage = "Couldn't write Jalview Archive - see error output for details";
357 ex.printStackTrace();
361 // USE THIS METHOD TO SAVE A SINGLE ALIGNMENT WINDOW
362 public boolean SaveAlignment(AlignFrame af, String jarFile,
367 int ap, apSize = af.alignPanels.size();
368 FileOutputStream fos = new FileOutputStream(jarFile);
369 JarOutputStream jout = new JarOutputStream(fos);
370 for (ap = 0; ap < apSize; ap++)
372 AlignmentPanel apanel = (AlignmentPanel) af.alignPanels
374 String jfileName = apSize == 1 ? fileName : fileName + ap;
375 if (!jfileName.endsWith(".xml"))
377 jfileName = jfileName + ".xml";
379 SaveState(apanel, jfileName, jout);
385 } catch (Exception foo)
391 } catch (Exception ex)
393 errorMessage = "Couldn't Write alignment view to Jalview Archive - see error output for details";
394 ex.printStackTrace();
400 * create a JalviewModel from an algnment view and marshall it to a
404 * panel to create jalview model for
406 * name of alignment panel written to output stream
412 public JalviewModel SaveState(AlignmentPanel ap, String fileName,
413 JarOutputStream jout)
417 Vector userColours = new Vector();
419 AlignViewport av = ap.av;
421 JalviewModel object = new JalviewModel();
422 object.setVamsasModel(new jalview.schemabinding.version2.VamsasModel());
424 object.setCreationDate(new java.util.Date(System.currentTimeMillis()));
425 object.setVersion(jalview.bin.Cache.getProperty("VERSION"));
427 jalview.datamodel.AlignmentI jal = av.alignment;
429 if (av.hasHiddenRows)
431 jal = jal.getHiddenSequences().getFullAlignment();
434 SequenceSet vamsasSet = new SequenceSet();
436 JalviewModelSequence jms = new JalviewModelSequence();
438 vamsasSet.setGapChar(jal.getGapCharacter() + "");
440 if (jal.getDataset() != null)
442 // dataset id is the dataset's hashcode
443 vamsasSet.setDatasetId(getDatasetIdRef(jal.getDataset()));
445 if (jal.getProperties() != null)
447 Enumeration en = jal.getProperties().keys();
448 while (en.hasMoreElements())
450 String key = en.nextElement().toString();
451 SequenceSetProperties ssp = new SequenceSetProperties();
453 ssp.setValue(jal.getProperties().get(key).toString());
454 vamsasSet.addSequenceSetProperties(ssp);
462 jalview.datamodel.SequenceI jds;
463 for (int i = 0; i < jal.getHeight(); i++)
465 jds = jal.getSequenceAt(i);
468 if (seqRefIds.get(id) != null)
470 // This happens for two reasons: 1. multiple views are being serialised.
471 // 2. the hashCode has collided with another sequence's code. This DOES
472 // HAPPEN! (PF00072.15.stk does this)
473 // JBPNote: Uncomment to debug writing out of files that do not read
474 // back in due to ArrayOutOfBoundExceptions.
475 // System.err.println("vamsasSeq backref: "+id+"");
476 // System.err.println(jds.getName()+"
477 // "+jds.getStart()+"-"+jds.getEnd()+" "+jds.getSequenceAsString());
478 // System.err.println("Hashcode: "+seqHash(jds));
479 // SequenceI rsq = (SequenceI) seqRefIds.get(id + "");
480 // System.err.println(rsq.getName()+"
481 // "+rsq.getStart()+"-"+rsq.getEnd()+" "+rsq.getSequenceAsString());
482 // System.err.println("Hashcode: "+seqHash(rsq));
486 vamsasSeq = createVamsasSequence(id, jds);
487 vamsasSet.addSequence(vamsasSeq);
488 seqRefIds.put(id, jds);
492 jseq.setStart(jds.getStart());
493 jseq.setEnd(jds.getEnd());
494 jseq.setColour(av.getSequenceColour(jds).getRGB());
496 jseq.setId(id); // jseq id should be a string not a number
498 if (av.hasHiddenRows)
500 jseq.setHidden(av.alignment.getHiddenSequences().isHidden(jds));
502 if (av.hiddenRepSequences != null
503 && av.hiddenRepSequences.containsKey(jal.getSequenceAt(i)))
505 jalview.datamodel.SequenceI[] reps = ((jalview.datamodel.SequenceGroup) av.hiddenRepSequences
506 .get(jal.getSequenceAt(i))).getSequencesInOrder(jal);
508 for (int h = 0; h < reps.length; h++)
510 if (reps[h] != jal.getSequenceAt(i))
512 jseq.addHiddenSequences(jal.findIndex(reps[h]));
518 if (jds.getDatasetSequence().getSequenceFeatures() != null)
520 jalview.datamodel.SequenceFeature[] sf = jds.getDatasetSequence()
521 .getSequenceFeatures();
523 while (index < sf.length)
525 Features features = new Features();
527 features.setBegin(sf[index].getBegin());
528 features.setEnd(sf[index].getEnd());
529 features.setDescription(sf[index].getDescription());
530 features.setType(sf[index].getType());
531 features.setFeatureGroup(sf[index].getFeatureGroup());
532 features.setScore(sf[index].getScore());
533 if (sf[index].links != null)
535 for (int l = 0; l < sf[index].links.size(); l++)
537 OtherData keyValue = new OtherData();
538 keyValue.setKey("LINK_" + l);
539 keyValue.setValue(sf[index].links.elementAt(l).toString());
540 features.addOtherData(keyValue);
543 if (sf[index].otherDetails != null)
546 Enumeration keys = sf[index].otherDetails.keys();
547 while (keys.hasMoreElements())
549 key = keys.nextElement().toString();
550 OtherData keyValue = new OtherData();
551 keyValue.setKey(key);
552 keyValue.setValue(sf[index].otherDetails.get(key).toString());
553 features.addOtherData(keyValue);
557 jseq.addFeatures(features);
562 if (jds.getDatasetSequence().getPDBId() != null)
564 Enumeration en = jds.getDatasetSequence().getPDBId().elements();
565 while (en.hasMoreElements())
567 Pdbids pdb = new Pdbids();
568 jalview.datamodel.PDBEntry entry = (jalview.datamodel.PDBEntry) en
571 pdb.setId(entry.getId());
572 pdb.setType(entry.getType());
575 // This must have been loaded, is it still visible?
576 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
577 String matchedFile=null;
578 for (int f = frames.length - 1; f > -1; f--)
580 if (frames[f] instanceof AppJmol)
582 jmol = (AppJmol) frames[f];
583 if (!jmol.pdbentry.getId().equals(entry.getId())
584 && !(entry.getId().length()>4
585 && entry.getId().toLowerCase().startsWith(jmol.pdbentry.getId().toLowerCase())))
587 matchedFile = jmol.pdbentry.getFile(); // record the file so we can get at it if the ID match is ambiguous (e.g. 1QIP==1qipA)
588 StructureState state = new StructureState();
589 state.setVisible(true);
590 state.setXpos(jmol.getX());
591 state.setYpos(jmol.getY());
592 state.setWidth(jmol.getWidth());
593 state.setHeight(jmol.getHeight());
594 state.setViewId(jmol.getViewId());
595 String statestring = jmol.viewer.getStateInfo();
598 state.setContent(statestring.replaceAll("\n", ""));
600 for (int s = 0; s < jmol.sequence.length; s++)
602 if (jal.findIndex(jmol.sequence[s]) > -1)
604 pdb.addStructureState(state);
610 if (matchedFile!=null || entry.getFile() != null )
612 if (entry.getFile()!=null)
615 matchedFile = entry.getFile();
617 pdb.setFile(matchedFile); // entry.getFile());
618 if (pdbfiles == null)
620 pdbfiles = new Vector();
623 if (!pdbfiles.contains(entry.getId()))
625 pdbfiles.addElement(entry.getId());
628 File file = new File(matchedFile);
629 if (file.exists() && jout != null)
631 byte[] data = new byte[(int) file.length()];
632 jout.putNextEntry(new JarEntry(entry.getId()));
633 DataInputStream dis = new DataInputStream(
634 new FileInputStream(file));
637 DataOutputStream dout = new DataOutputStream(jout);
638 dout.write(data, 0, data.length);
642 } catch (Exception ex)
644 ex.printStackTrace();
650 if (entry.getProperty() != null)
652 PdbentryItem item = new PdbentryItem();
653 Hashtable properties = entry.getProperty();
654 Enumeration en2 = properties.keys();
655 while (en2.hasMoreElements())
657 Property prop = new Property();
658 String key = en2.nextElement().toString();
660 prop.setValue(properties.get(key).toString());
661 item.addProperty(prop);
663 pdb.addPdbentryItem(item);
673 if (av.hasHiddenRows)
678 if (jal.getCodonFrames() != null && jal.getCodonFrames().length > 0)
680 jalview.datamodel.AlignedCodonFrame[] jac = jal.getCodonFrames();
681 for (int i = 0; i < jac.length; i++)
683 AlcodonFrame alc = new AlcodonFrame();
684 vamsasSet.addAlcodonFrame(alc);
685 for (int p = 0; p < jac[i].aaWidth; p++)
687 Alcodon cmap = new Alcodon();
688 if (jac[i].codons[p]!=null)
690 // Null codons indicate a gapped column in the translated peptide alignment.
691 cmap.setPos1(jac[i].codons[p][0]);
692 cmap.setPos2(jac[i].codons[p][1]);
693 cmap.setPos3(jac[i].codons[p][2]);
695 alc.addAlcodon(cmap);
697 if (jac[i].getProtMappings() != null
698 && jac[i].getProtMappings().length > 0)
700 SequenceI[] dnas = jac[i].getdnaSeqs();
701 jalview.datamodel.Mapping[] pmaps = jac[i].getProtMappings();
702 for (int m = 0; m < pmaps.length; m++)
704 AlcodMap alcmap = new AlcodMap();
705 alcmap.setDnasq(seqHash(dnas[m]));
706 alcmap.setMapping(createVamsasMapping(pmaps[m], dnas[m], null,
708 alc.addAlcodMap(alcmap);
715 // /////////////////////////////////
716 if (av.currentTree != null)
718 // FIND ANY ASSOCIATED TREES
719 // NOT IMPLEMENTED FOR HEADLESS STATE AT PRESENT
720 if (Desktop.desktop != null)
722 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
724 for (int t = 0; t < frames.length; t++)
726 if (frames[t] instanceof TreePanel)
728 TreePanel tp = (TreePanel) frames[t];
730 if (tp.treeCanvas.av.alignment == jal)
732 Tree tree = new Tree();
733 tree.setTitle(tp.getTitle());
734 tree.setCurrentTree((av.currentTree == tp.getTree()));
735 tree.setNewick(tp.getTree().toString());
736 tree.setThreshold(tp.treeCanvas.threshold);
738 tree.setFitToWindow(tp.fitToWindow.getState());
739 tree.setFontName(tp.getTreeFont().getName());
740 tree.setFontSize(tp.getTreeFont().getSize());
741 tree.setFontStyle(tp.getTreeFont().getStyle());
742 tree.setMarkUnlinked(tp.placeholdersMenu.getState());
744 tree.setShowBootstrap(tp.bootstrapMenu.getState());
745 tree.setShowDistances(tp.distanceMenu.getState());
747 tree.setHeight(tp.getHeight());
748 tree.setWidth(tp.getWidth());
749 tree.setXpos(tp.getX());
750 tree.setYpos(tp.getY());
751 tree.setId(makeHashCode(tp, null));
760 if (jal.getAlignmentAnnotation() != null)
762 jalview.datamodel.AlignmentAnnotation[] aa = jal
763 .getAlignmentAnnotation();
765 for (int i = 0; i < aa.length; i++)
767 Annotation an = new Annotation();
769 if (aa[i].annotationId != null)
771 annotationIds.put(aa[i].annotationId, aa[i]);
774 an.setId(aa[i].annotationId);
776 if (aa[i] == av.quality || aa[i] == av.conservation
777 || aa[i] == av.consensus)
779 an.setLabel(aa[i].label);
781 vamsasSet.addAnnotation(an);
785 an.setVisible(aa[i].visible);
787 an.setDescription(aa[i].description);
789 if (aa[i].sequenceRef != null)
791 // TODO later annotation sequenceRef should be the XML ID of the
792 // sequence rather than its display name
793 an.setSequenceRef(aa[i].sequenceRef.getName());
799 an.setGraphType(aa[i].graph);
800 an.setGraphGroup(aa[i].graphGroup);
801 if (aa[i].getThreshold() != null)
803 ThresholdLine line = new ThresholdLine();
804 line.setLabel(aa[i].getThreshold().label);
805 line.setValue(aa[i].getThreshold().value);
806 line.setColour(aa[i].getThreshold().colour.getRGB());
807 an.setThresholdLine(line);
815 an.setLabel(aa[i].label);
816 if (aa[i].hasScore())
818 an.setScore(aa[i].getScore());
820 AnnotationElement ae;
821 if (aa[i].annotations != null)
823 an.setScoreOnly(false);
824 for (int a = 0; a < aa[i].annotations.length; a++)
826 if ((aa[i] == null) || (aa[i].annotations[a] == null))
831 ae = new AnnotationElement();
832 if (aa[i].annotations[a].description != null)
833 ae.setDescription(aa[i].annotations[a].description);
834 if (aa[i].annotations[a].displayCharacter != null)
835 ae.setDisplayCharacter(aa[i].annotations[a].displayCharacter);
837 if (!Float.isNaN(aa[i].annotations[a].value))
838 ae.setValue(aa[i].annotations[a].value);
841 if (aa[i].annotations[a].secondaryStructure != ' '
842 && aa[i].annotations[a].secondaryStructure != '\0')
844 .setSecondaryStructure(aa[i].annotations[a].secondaryStructure
847 if (aa[i].annotations[a].colour != null
848 && aa[i].annotations[a].colour != java.awt.Color.black)
850 ae.setColour(aa[i].annotations[a].colour.getRGB());
853 an.addAnnotationElement(ae);
858 an.setScoreOnly(true);
860 vamsasSet.addAnnotation(an);
865 if (jal.getGroups() != null)
867 JGroup[] groups = new JGroup[jal.getGroups().size()];
869 for (int i = 0; i < groups.length; i++)
871 groups[i] = new JGroup();
873 jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) jal
874 .getGroups().elementAt(i);
875 groups[i].setStart(sg.getStartRes());
876 groups[i].setEnd(sg.getEndRes());
877 groups[i].setName(sg.getName()); // TODO later sequence group should
878 // specify IDs of sequences, not just
882 if (sg.cs.conservationApplied())
884 groups[i].setConsThreshold(sg.cs.getConservationInc());
886 if (sg.cs instanceof jalview.schemes.UserColourScheme)
888 groups[i].setColour(SetUserColourScheme(sg.cs, userColours,
894 .setColour(ColourSchemeProperty.getColourName(sg.cs));
897 else if (sg.cs instanceof jalview.schemes.AnnotationColourGradient)
900 .setColour(ColourSchemeProperty
901 .getColourName(((jalview.schemes.AnnotationColourGradient) sg.cs)
904 else if (sg.cs instanceof jalview.schemes.UserColourScheme)
907 .setColour(SetUserColourScheme(sg.cs, userColours, jms));
911 groups[i].setColour(ColourSchemeProperty.getColourName(sg.cs));
914 groups[i].setPidThreshold(sg.cs.getThreshold());
917 groups[i].setOutlineColour(sg.getOutlineColour().getRGB());
918 groups[i].setDisplayBoxes(sg.getDisplayBoxes());
919 groups[i].setDisplayText(sg.getDisplayText());
920 groups[i].setColourText(sg.getColourText());
921 groups[i].setTextCol1(sg.textColour.getRGB());
922 groups[i].setTextCol2(sg.textColour2.getRGB());
923 groups[i].setTextColThreshold(sg.thresholdTextColour);
924 groups[i].setShowUnconserved(sg.getShowunconserved());
925 for (int s = 0; s < sg.getSize(); s++)
927 jalview.datamodel.Sequence seq = (jalview.datamodel.Sequence) sg
929 groups[i].addSeq(seqHash(seq));
933 jms.setJGroup(groups);
936 // /////////SAVE VIEWPORT
937 Viewport view = new Viewport();
938 view.setTitle(ap.alignFrame.getTitle());
939 view.setSequenceSetId(makeHashCode(av.getSequenceSetId(), av
940 .getSequenceSetId()));
941 view.setId(av.getViewId());
942 view.setViewName(av.viewName);
943 view.setGatheredViews(av.gatherViewsHere);
945 if (ap.av.explodedPosition != null)
947 view.setXpos(av.explodedPosition.x);
948 view.setYpos(av.explodedPosition.y);
949 view.setWidth(av.explodedPosition.width);
950 view.setHeight(av.explodedPosition.height);
954 view.setXpos(ap.alignFrame.getBounds().x);
955 view.setYpos(ap.alignFrame.getBounds().y);
956 view.setWidth(ap.alignFrame.getBounds().width);
957 view.setHeight(ap.alignFrame.getBounds().height);
960 view.setStartRes(av.startRes);
961 view.setStartSeq(av.startSeq);
963 if (av.getGlobalColourScheme() instanceof jalview.schemes.UserColourScheme)
965 view.setBgColour(SetUserColourScheme(av.getGlobalColourScheme(),
968 else if (av.getGlobalColourScheme() instanceof jalview.schemes.AnnotationColourGradient)
970 jalview.schemes.AnnotationColourGradient acg = (jalview.schemes.AnnotationColourGradient) av
971 .getGlobalColourScheme();
973 AnnotationColours ac = new AnnotationColours();
974 ac.setAboveThreshold(acg.getAboveThreshold());
975 ac.setThreshold(acg.getAnnotationThreshold());
976 ac.setAnnotation(acg.getAnnotation());
977 if (acg.getBaseColour() instanceof jalview.schemes.UserColourScheme)
979 ac.setColourScheme(SetUserColourScheme(acg.getBaseColour(),
984 ac.setColourScheme(ColourSchemeProperty.getColourName(acg
988 ac.setMaxColour(acg.getMaxColour().getRGB());
989 ac.setMinColour(acg.getMinColour().getRGB());
990 view.setAnnotationColours(ac);
991 view.setBgColour("AnnotationColourGradient");
995 view.setBgColour(ColourSchemeProperty.getColourName(av
996 .getGlobalColourScheme()));
999 ColourSchemeI cs = av.getGlobalColourScheme();
1003 if (cs.conservationApplied())
1005 view.setConsThreshold(cs.getConservationInc());
1006 if (cs instanceof jalview.schemes.UserColourScheme)
1008 view.setBgColour(SetUserColourScheme(cs, userColours, jms));
1012 if (cs instanceof ResidueColourScheme)
1014 view.setPidThreshold(cs.getThreshold());
1018 view.setConservationSelected(av.getConservationSelected());
1019 view.setPidSelected(av.getAbovePIDThreshold());
1020 view.setFontName(av.font.getName());
1021 view.setFontSize(av.font.getSize());
1022 view.setFontStyle(av.font.getStyle());
1023 view.setRenderGaps(av.renderGaps);
1024 view.setShowAnnotation(av.getShowAnnotation());
1025 view.setShowBoxes(av.getShowBoxes());
1026 view.setShowColourText(av.getColourText());
1027 view.setShowFullId(av.getShowJVSuffix());
1028 view.setRightAlignIds(av.rightAlignIds);
1029 view.setShowSequenceFeatures(av.showSequenceFeatures);
1030 view.setShowText(av.getShowText());
1031 view.setShowUnconserved(av.getShowUnconserved());
1032 view.setWrapAlignment(av.getWrapAlignment());
1033 view.setTextCol1(av.textColour.getRGB());
1034 view.setTextCol2(av.textColour2.getRGB());
1035 view.setTextColThreshold(av.thresholdTextColour);
1037 if (av.featuresDisplayed != null)
1039 jalview.schemabinding.version2.FeatureSettings fs = new jalview.schemabinding.version2.FeatureSettings();
1041 String[] renderOrder = ap.seqPanel.seqCanvas.getFeatureRenderer().renderOrder;
1043 Vector settingsAdded = new Vector();
1044 for (int ro = 0; ro < renderOrder.length; ro++)
1046 Setting setting = new Setting();
1047 setting.setType(renderOrder[ro]);
1048 setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
1049 .getColour(renderOrder[ro]).getRGB());
1051 setting.setDisplay(av.featuresDisplayed
1052 .containsKey(renderOrder[ro]));
1053 float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(
1057 setting.setOrder(rorder);
1059 fs.addSetting(setting);
1060 settingsAdded.addElement(renderOrder[ro]);
1063 // Make sure we save none displayed feature settings
1064 Enumeration en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureColours
1066 while (en.hasMoreElements())
1068 String key = en.nextElement().toString();
1069 if (settingsAdded.contains(key))
1074 Setting setting = new Setting();
1075 setting.setType(key);
1076 setting.setColour(ap.seqPanel.seqCanvas.getFeatureRenderer()
1077 .getColour(key).getRGB());
1079 setting.setDisplay(false);
1080 float rorder = ap.seqPanel.seqCanvas.getFeatureRenderer().getOrder(
1084 setting.setOrder(rorder);
1086 fs.addSetting(setting);
1087 settingsAdded.addElement(key);
1089 en = ap.seqPanel.seqCanvas.getFeatureRenderer().featureGroups.keys();
1090 Vector groupsAdded = new Vector();
1091 while (en.hasMoreElements())
1093 String grp = en.nextElement().toString();
1094 if (groupsAdded.contains(grp))
1098 Group g = new Group();
1101 .setDisplay(((Boolean) ap.seqPanel.seqCanvas
1102 .getFeatureRenderer().featureGroups.get(grp))
1105 groupsAdded.addElement(grp);
1107 jms.setFeatureSettings(fs);
1111 if (av.hasHiddenColumns)
1113 for (int c = 0; c < av.getColumnSelection().getHiddenColumns().size(); c++)
1115 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
1117 HiddenColumns hc = new HiddenColumns();
1118 hc.setStart(region[0]);
1119 hc.setEnd(region[1]);
1120 view.addHiddenColumns(hc);
1124 jms.addViewport(view);
1126 object.setJalviewModelSequence(jms);
1127 object.getVamsasModel().addSequenceSet(vamsasSet);
1129 if (jout != null && fileName != null)
1131 // We may not want to write the object to disk,
1132 // eg we can copy the alignViewport to a new view object
1133 // using save and then load
1136 JarEntry entry = new JarEntry(fileName);
1137 jout.putNextEntry(entry);
1138 PrintWriter pout = new PrintWriter(new OutputStreamWriter(jout,
1140 org.exolab.castor.xml.Marshaller marshaller = new org.exolab.castor.xml.Marshaller(
1142 marshaller.marshal(object);
1145 } catch (Exception ex)
1147 // TODO: raise error in GUI if marshalling failed.
1148 ex.printStackTrace();
1155 * External mapping between jalview objects and objects yielding a valid and
1156 * unique object ID string. This is null for normal Jalview project IO, but
1157 * non-null when a jalview project is being read or written as part of a
1160 IdentityHashMap jv2vobj = null;
1163 * Construct a unique ID for jvobj using either existing bindings or if none
1164 * exist, the result of the hashcode call for the object.
1167 * jalview data object
1168 * @return unique ID for referring to jvobj
1170 private String makeHashCode(Object jvobj, String altCode)
1172 if (jv2vobj != null)
1174 Object id = jv2vobj.get(jvobj);
1177 return id.toString();
1179 // check string ID mappings
1180 if (jvids2vobj != null && jvobj instanceof String)
1182 id = jvids2vobj.get(jvobj);
1186 return id.toString();
1188 // give up and warn that something has gone wrong
1189 warn("Cannot find ID for object in external mapping : " + jvobj);
1195 * return local jalview object mapped to ID, if it exists
1199 * @return null or object bound to idcode
1201 private Object retrieveExistingObj(String idcode)
1203 if (idcode != null && vobj2jv != null)
1205 return vobj2jv.get(idcode);
1211 * binding from ID strings from external mapping table to jalview data model
1214 private Hashtable vobj2jv;
1216 private Sequence createVamsasSequence(String id, SequenceI jds)
1218 return createVamsasSequence(true, id, jds, null);
1221 private Sequence createVamsasSequence(boolean recurse, String id,
1222 SequenceI jds, SequenceI parentseq)
1224 Sequence vamsasSeq = new Sequence();
1225 vamsasSeq.setId(id);
1226 vamsasSeq.setName(jds.getName());
1227 vamsasSeq.setSequence(jds.getSequenceAsString());
1228 vamsasSeq.setDescription(jds.getDescription());
1229 jalview.datamodel.DBRefEntry[] dbrefs = null;
1230 if (jds.getDatasetSequence() != null)
1232 vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
1233 if (jds.getDatasetSequence().getDBRef() != null)
1235 dbrefs = jds.getDatasetSequence().getDBRef();
1240 vamsasSeq.setDsseqid(id); // so we can tell which sequences really are
1241 // dataset sequences only
1242 dbrefs = jds.getDBRef();
1246 for (int d = 0; d < dbrefs.length; d++)
1248 DBRef dbref = new DBRef();
1249 dbref.setSource(dbrefs[d].getSource());
1250 dbref.setVersion(dbrefs[d].getVersion());
1251 dbref.setAccessionId(dbrefs[d].getAccessionId());
1252 if (dbrefs[d].hasMap())
1254 Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
1256 dbref.setMapping(mp);
1258 vamsasSeq.addDBRef(dbref);
1264 private Mapping createVamsasMapping(jalview.datamodel.Mapping jmp,
1265 SequenceI parentseq, SequenceI jds, boolean recurse)
1268 if (jmp.getMap() != null)
1272 jalview.util.MapList mlst = jmp.getMap();
1273 int r[] = mlst.getFromRanges();
1274 for (int s = 0; s < r.length; s += 2)
1276 MapListFrom mfrom = new MapListFrom();
1277 mfrom.setStart(r[s]);
1278 mfrom.setEnd(r[s + 1]);
1279 mp.addMapListFrom(mfrom);
1281 r = mlst.getToRanges();
1282 for (int s = 0; s < r.length; s += 2)
1284 MapListTo mto = new MapListTo();
1286 mto.setEnd(r[s + 1]);
1287 mp.addMapListTo(mto);
1289 mp.setMapFromUnit(mlst.getFromRatio());
1290 mp.setMapToUnit(mlst.getToRatio());
1291 if (jmp.getTo() != null)
1293 MappingChoice mpc = new MappingChoice();
1295 && (parentseq != jmp.getTo() || parentseq
1296 .getDatasetSequence() != jmp.getTo()))
1298 mpc.setSequence(createVamsasSequence(false, seqHash(jmp.getTo()),
1304 SequenceI ps = null;
1305 if (parentseq != jmp.getTo()
1306 && parentseq.getDatasetSequence() != jmp.getTo())
1308 // chaining dbref rather than a handshaking one
1309 jmpid = seqHash(ps = jmp.getTo());
1313 jmpid = seqHash(ps = parentseq);
1315 mpc.setDseqFor(jmpid);
1316 if (!seqRefIds.containsKey(mpc.getDseqFor()))
1318 jalview.bin.Cache.log.debug("creatign new DseqFor ID");
1319 seqRefIds.put(mpc.getDseqFor(), ps);
1323 jalview.bin.Cache.log.debug("reusing DseqFor ID");
1326 mp.setMappingChoice(mpc);
1332 String SetUserColourScheme(jalview.schemes.ColourSchemeI cs,
1333 Vector userColours, JalviewModelSequence jms)
1336 jalview.schemes.UserColourScheme ucs = (jalview.schemes.UserColourScheme) cs;
1338 if (!userColours.contains(ucs))
1340 userColours.add(ucs);
1342 java.awt.Color[] colours = ucs.getColours();
1343 jalview.schemabinding.version2.UserColours uc = new jalview.schemabinding.version2.UserColours();
1344 jalview.schemabinding.version2.UserColourScheme jbucs = new jalview.schemabinding.version2.UserColourScheme();
1346 for (int i = 0; i < colours.length; i++)
1348 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
1349 col.setName(ResidueProperties.aa[i]);
1350 col.setRGB(jalview.util.Format.getHexString(colours[i]));
1351 jbucs.addColour(col);
1353 if (ucs.getLowerCaseColours() != null)
1355 colours = ucs.getLowerCaseColours();
1356 for (int i = 0; i < colours.length; i++)
1358 jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
1359 col.setName(ResidueProperties.aa[i].toLowerCase());
1360 col.setRGB(jalview.util.Format.getHexString(colours[i]));
1361 jbucs.addColour(col);
1365 id = "ucs" + userColours.indexOf(ucs);
1367 uc.setUserColourScheme(jbucs);
1368 jms.addUserColours(uc);
1374 jalview.schemes.UserColourScheme GetUserColourScheme(
1375 JalviewModelSequence jms, String id)
1377 UserColours[] uc = jms.getUserColours();
1378 UserColours colours = null;
1380 for (int i = 0; i < uc.length; i++)
1382 if (uc[i].getId().equals(id))
1390 java.awt.Color[] newColours = new java.awt.Color[24];
1392 for (int i = 0; i < 24; i++)
1394 newColours[i] = new java.awt.Color(Integer.parseInt(colours
1395 .getUserColourScheme().getColour(i).getRGB(), 16));
1398 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme(
1401 if (colours.getUserColourScheme().getColourCount() > 24)
1403 newColours = new java.awt.Color[23];
1404 for (int i = 0; i < 23; i++)
1406 newColours[i] = new java.awt.Color(Integer.parseInt(colours
1407 .getUserColourScheme().getColour(i + 24).getRGB(), 16));
1409 ucs.setLowerCaseColours(newColours);
1416 * contains last error message (if any) encountered by XML loader.
1418 String errorMessage = null;
1421 * flag to control whether the Jalview2XML_V1 parser should be deferred to if
1422 * exceptions are raised during project XML parsing
1424 public boolean attemptversion1parse = true;
1427 * Load a jalview project archive from a jar file
1430 * HTTP URL or filename
1432 public AlignFrame LoadJalviewAlign(final String file)
1435 jalview.gui.AlignFrame af = null;
1439 // UNMARSHALLER SEEMS TO CLOSE JARINPUTSTREAM, MOST ANNOYING
1440 // Workaround is to make sure caller implements the JarInputStreamProvider
1442 // so we can re-open the jar input stream for each entry.
1444 jarInputStreamProvider jprovider = createjarInputStreamProvider(file);
1445 af = LoadJalviewAlign(jprovider);
1446 } catch (MalformedURLException e)
1448 errorMessage = "Invalid URL format for '" + file + "'";
1454 private jarInputStreamProvider createjarInputStreamProvider(
1455 final String file) throws MalformedURLException
1458 errorMessage = null;
1459 uniqueSetSuffix = null;
1461 viewportsAdded = null;
1462 frefedSequence = null;
1464 if (file.startsWith("http://"))
1466 url = new URL(file);
1468 final URL _url = url;
1469 return new jarInputStreamProvider()
1472 public JarInputStream getJarInputStream() throws IOException
1476 return new JarInputStream(_url.openStream());
1480 return new JarInputStream(new FileInputStream(file));
1484 public String getFilename()
1492 * Recover jalview session from a jalview project archive. Caller may
1493 * initialise uniqueSetSuffix, seqRefIds, viewportsAdded and frefedSequence
1494 * themselves. Any null fields will be initialised with default values,
1495 * non-null fields are left alone.
1500 public AlignFrame LoadJalviewAlign(final jarInputStreamProvider jprovider)
1502 errorMessage = null;
1503 if (uniqueSetSuffix == null)
1505 uniqueSetSuffix = System.currentTimeMillis() % 100000 + "";
1507 if (seqRefIds == null)
1509 seqRefIds = new Hashtable();
1511 if (viewportsAdded == null)
1513 viewportsAdded = new Hashtable();
1515 if (frefedSequence == null)
1517 frefedSequence = new Vector();
1520 jalview.gui.AlignFrame af = null;
1521 Hashtable gatherToThisFrame = new Hashtable();
1522 final String file = jprovider.getFilename();
1525 JarInputStream jin = null;
1526 JarEntry jarentry = null;
1531 jin = jprovider.getJarInputStream();
1532 for (int i = 0; i < entryCount; i++)
1534 jarentry = jin.getNextJarEntry();
1537 if (jarentry != null && jarentry.getName().endsWith(".xml"))
1539 InputStreamReader in = new InputStreamReader(jin, "UTF-8");
1540 JalviewModel object = new JalviewModel();
1542 Unmarshaller unmar = new Unmarshaller(object);
1543 unmar.setValidation(false);
1544 object = (JalviewModel) unmar.unmarshal(in);
1545 if (true) // !skipViewport(object))
1547 af = LoadFromObject(object, file, true, jprovider);
1548 if (af.viewport.gatherViewsHere)
1550 gatherToThisFrame.put(af.viewport.getSequenceSetId(), af);
1555 else if (jarentry != null)
1557 // Some other file here.
1560 } while (jarentry != null);
1561 resolveFrefedSequences();
1562 } catch (java.io.FileNotFoundException ex)
1564 ex.printStackTrace();
1565 errorMessage = "Couldn't locate Jalview XML file : " + file;
1566 System.err.println("Exception whilst loading jalview XML file : "
1568 } catch (java.net.UnknownHostException ex)
1570 ex.printStackTrace();
1571 errorMessage = "Couldn't locate Jalview XML file : " + file;
1572 System.err.println("Exception whilst loading jalview XML file : "
1574 } catch (Exception ex)
1576 System.err.println("Parsing as Jalview Version 2 file failed.");
1577 ex.printStackTrace(System.err);
1578 if (attemptversion1parse)
1580 // Is Version 1 Jar file?
1583 af = new Jalview2XML_V1(raiseGUI).LoadJalviewAlign(jprovider);
1584 } catch (Exception ex2)
1586 System.err.println("Exception whilst loading as jalviewXMLV1:");
1587 ex2.printStackTrace();
1591 if (Desktop.instance != null)
1593 Desktop.instance.stopLoading();
1597 System.out.println("Successfully loaded archive file");
1600 ex.printStackTrace();
1602 System.err.println("Exception whilst loading jalview XML file : "
1604 } catch (OutOfMemoryError e)
1606 new jalview.gui.OOMWarning("loading jalview XML file", e,
1610 if (Desktop.instance != null)
1612 Desktop.instance.stopLoading();
1615 Enumeration en = gatherToThisFrame.elements();
1616 while (en.hasMoreElements())
1618 Desktop.instance.gatherViews((AlignFrame) en.nextElement());
1620 if (errorMessage != null)
1628 * check errorMessage for a valid error message and raise an error box in the
1629 * GUI or write the current errorMessage to stderr and then clear the error
1632 protected void reportErrors()
1634 reportErrors(false);
1637 protected void reportErrors(final boolean saving)
1639 if (errorMessage != null)
1641 final String finalErrorMessage = errorMessage;
1644 javax.swing.SwingUtilities.invokeLater(new Runnable()
1648 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1649 finalErrorMessage, "Error "
1650 + (saving ? "saving" : "loading")
1651 + " Jalview file", JOptionPane.WARNING_MESSAGE);
1657 System.err.println("Problem loading Jalview file: " + errorMessage);
1660 errorMessage = null;
1663 Hashtable alreadyLoadedPDB;
1666 * when set, local views will be updated from view stored in JalviewXML
1667 * Currently (28th Sep 2008) things will go horribly wrong in vamsas document
1668 * sync if this is set to true.
1670 private boolean updateLocalViews = false;
1672 String loadPDBFile(jarInputStreamProvider jprovider, String pdbId)
1674 if (alreadyLoadedPDB == null)
1675 alreadyLoadedPDB = new Hashtable();
1677 if (alreadyLoadedPDB.containsKey(pdbId))
1678 return alreadyLoadedPDB.get(pdbId).toString();
1682 JarInputStream jin = jprovider.getJarInputStream();
1684 * if (jprovider.startsWith("http://")) { jin = new JarInputStream(new
1685 * URL(jprovider).openStream()); } else { jin = new JarInputStream(new
1686 * FileInputStream(jprovider)); }
1689 JarEntry entry = null;
1692 entry = jin.getNextJarEntry();
1693 } while (entry != null && !entry.getName().equals(pdbId));
1696 BufferedReader in = new BufferedReader(new InputStreamReader(jin));
1697 File outFile = File.createTempFile("jalview_pdb", ".txt");
1698 outFile.deleteOnExit();
1699 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1702 while ((data = in.readLine()) != null)
1709 } catch (Exception foo)
1715 alreadyLoadedPDB.put(pdbId, outFile.getAbsolutePath());
1716 return outFile.getAbsolutePath();
1720 warn("Couldn't find PDB file entry in Jalview Jar for " + pdbId);
1722 } catch (Exception ex)
1724 ex.printStackTrace();
1731 * Load alignment frame from jalview XML DOM object
1736 * filename source string
1737 * @param loadTreesAndStructures
1738 * when false only create Viewport
1740 * data source provider
1741 * @return alignment frame created from view stored in DOM
1743 AlignFrame LoadFromObject(JalviewModel object, String file,
1744 boolean loadTreesAndStructures, jarInputStreamProvider jprovider)
1746 SequenceSet vamsasSet = object.getVamsasModel().getSequenceSet(0);
1747 Sequence[] vamsasSeq = vamsasSet.getSequence();
1749 JalviewModelSequence jms = object.getJalviewModelSequence();
1751 Viewport view = jms.getViewport(0);
1752 // ////////////////////////////////
1755 Vector hiddenSeqs = null;
1756 jalview.datamodel.Sequence jseq;
1758 ArrayList tmpseqs = new ArrayList();
1760 boolean multipleView = false;
1762 JSeq[] JSEQ = object.getJalviewModelSequence().getJSeq();
1763 int vi = 0; // counter in vamsasSeq array
1764 for (int i = 0; i < JSEQ.length; i++)
1766 String seqId = JSEQ[i].getId();
1768 if (seqRefIds.get(seqId) != null)
1770 tmpseqs.add((jalview.datamodel.Sequence) seqRefIds.get(seqId));
1771 multipleView = true;
1775 jseq = new jalview.datamodel.Sequence(vamsasSeq[vi].getName(),
1776 vamsasSeq[vi].getSequence());
1777 jseq.setDescription(vamsasSeq[vi].getDescription());
1778 jseq.setStart(JSEQ[i].getStart());
1779 jseq.setEnd(JSEQ[i].getEnd());
1780 jseq.setVamsasId(uniqueSetSuffix + seqId);
1781 seqRefIds.put(vamsasSeq[vi].getId(), jseq);
1786 if (JSEQ[i].getHidden())
1788 if (hiddenSeqs == null)
1790 hiddenSeqs = new Vector();
1793 hiddenSeqs.addElement((jalview.datamodel.Sequence) seqRefIds
1800 // Create the alignment object from the sequence set
1801 // ///////////////////////////////
1802 jalview.datamodel.Sequence[] orderedSeqs = new jalview.datamodel.Sequence[tmpseqs
1805 tmpseqs.toArray(orderedSeqs);
1807 jalview.datamodel.Alignment al = new jalview.datamodel.Alignment(
1810 // / Add the alignment properties
1811 for (int i = 0; i < vamsasSet.getSequenceSetPropertiesCount(); i++)
1813 SequenceSetProperties ssp = vamsasSet.getSequenceSetProperties(i);
1814 al.setProperty(ssp.getKey(), ssp.getValue());
1818 // SequenceFeatures are added to the DatasetSequence,
1819 // so we must create or recover the dataset before loading features
1820 // ///////////////////////////////
1821 if (vamsasSet.getDatasetId() == null || vamsasSet.getDatasetId() == "")
1823 // older jalview projects do not have a dataset id.
1824 al.setDataset(null);
1828 recoverDatasetFor(vamsasSet, al);
1830 // ///////////////////////////////
1832 Hashtable pdbloaded = new Hashtable();
1835 // load sequence features, database references and any associated PDB
1836 // structures for the alignment
1837 for (int i = 0; i < vamsasSeq.length; i++)
1839 if (JSEQ[i].getFeaturesCount() > 0)
1841 Features[] features = JSEQ[i].getFeatures();
1842 for (int f = 0; f < features.length; f++)
1844 jalview.datamodel.SequenceFeature sf = new jalview.datamodel.SequenceFeature(
1845 features[f].getType(), features[f].getDescription(),
1846 features[f].getStatus(), features[f].getBegin(),
1847 features[f].getEnd(), features[f].getFeatureGroup());
1849 sf.setScore(features[f].getScore());
1850 for (int od = 0; od < features[f].getOtherDataCount(); od++)
1852 OtherData keyValue = features[f].getOtherData(od);
1853 if (keyValue.getKey().startsWith("LINK"))
1855 sf.addLink(keyValue.getValue());
1859 sf.setValue(keyValue.getKey(), keyValue.getValue());
1864 al.getSequenceAt(i).getDatasetSequence().addSequenceFeature(sf);
1867 if (vamsasSeq[i].getDBRefCount() > 0)
1869 addDBRefs(al.getSequenceAt(i).getDatasetSequence(), vamsasSeq[i]);
1871 if (JSEQ[i].getPdbidsCount() > 0)
1873 Pdbids[] ids = JSEQ[i].getPdbids();
1874 for (int p = 0; p < ids.length; p++)
1876 jalview.datamodel.PDBEntry entry = new jalview.datamodel.PDBEntry();
1877 entry.setId(ids[p].getId());
1878 entry.setType(ids[p].getType());
1879 if (ids[p].getFile() != null)
1881 if (!pdbloaded.containsKey(ids[p].getFile()))
1883 entry.setFile(loadPDBFile(jprovider, ids[p].getId()));
1887 entry.setFile(pdbloaded.get(ids[p].getId()).toString());
1891 al.getSequenceAt(i).getDatasetSequence().addPDBId(entry);
1895 } // end !multipleview
1897 // ///////////////////////////////
1898 // LOAD SEQUENCE MAPPINGS
1900 if (vamsasSet.getAlcodonFrameCount() > 0)
1902 // TODO Potentially this should only be done once for all views of an
1904 AlcodonFrame[] alc = vamsasSet.getAlcodonFrame();
1905 for (int i = 0; i < alc.length; i++)
1907 jalview.datamodel.AlignedCodonFrame cf = new jalview.datamodel.AlignedCodonFrame(
1908 alc[i].getAlcodonCount());
1909 if (alc[i].getAlcodonCount() > 0)
1911 Alcodon[] alcods = alc[i].getAlcodon();
1912 for (int p = 0; p < cf.codons.length; p++)
1914 if (alcods[p].hasPos1() && alcods[p].hasPos2() && alcods[p].hasPos3())
1916 // translated codons require three valid positions
1917 cf.codons[p] = new int[3];
1918 cf.codons[p][0] = (int) alcods[p].getPos1();
1919 cf.codons[p][1] = (int) alcods[p].getPos2();
1920 cf.codons[p][2] = (int) alcods[p].getPos3();
1922 cf.codons[p] = null;
1926 if (alc[i].getAlcodMapCount() > 0)
1928 AlcodMap[] maps = alc[i].getAlcodMap();
1929 for (int m = 0; m < maps.length; m++)
1931 SequenceI dnaseq = (SequenceI) seqRefIds
1932 .get(maps[m].getDnasq());
1934 jalview.datamodel.Mapping mapping = null;
1935 // attach to dna sequence reference.
1936 if (maps[m].getMapping() != null)
1938 mapping = addMapping(maps[m].getMapping());
1942 cf.addMap(dnaseq, mapping.getTo(), mapping.getMap());
1947 frefedSequence.add(new Object[]
1948 { maps[m].getDnasq(), cf, mapping });
1952 al.addCodonFrame(cf);
1957 // ////////////////////////////////
1959 boolean hideQuality = true, hideConservation = true, hideConsensus = true;
1961 if (vamsasSet.getAnnotationCount() > 0)
1963 Annotation[] an = vamsasSet.getAnnotation();
1965 for (int i = 0; i < an.length; i++)
1967 // set visibility for automatic annotation for this view
1968 if (an[i].getLabel().equals("Quality"))
1970 hideQuality = false;
1973 else if (an[i].getLabel().equals("Conservation"))
1975 hideConservation = false;
1978 else if (an[i].getLabel().equals("Consensus"))
1980 hideConsensus = false;
1983 // set visiblity for other annotation in this view
1984 if (an[i].getId() != null
1985 && annotationIds.containsKey(an[i].getId()))
1987 jalview.datamodel.AlignmentAnnotation jda = (jalview.datamodel.AlignmentAnnotation) annotationIds
1988 .get(an[i].getId());
1989 // in principle Visible should always be true for annotation displayed
1990 // in multiple views
1991 if (an[i].hasVisible())
1992 jda.visible = an[i].getVisible();
1994 al.addAnnotation(jda);
1998 // Construct new annotation from model.
1999 AnnotationElement[] ae = an[i].getAnnotationElement();
2000 jalview.datamodel.Annotation[] anot = null;
2002 if (!an[i].getScoreOnly())
2004 anot = new jalview.datamodel.Annotation[al.getWidth()];
2006 for (int aa = 0; aa < ae.length && aa < anot.length; aa++)
2008 if (ae[aa].getPosition() >= anot.length)
2011 anot[ae[aa].getPosition()] = new jalview.datamodel.Annotation(
2013 ae[aa].getDisplayCharacter(), ae[aa].getDescription(), (ae[aa]
2014 .getSecondaryStructure() == null || ae[aa]
2015 .getSecondaryStructure().length() == 0) ? ' ' : ae[aa]
2016 .getSecondaryStructure().charAt(0), ae[aa].getValue()
2019 // JBPNote: Consider verifying dataflow for IO of secondary
2020 // structure annotation read from Stockholm files
2021 // this was added to try to ensure that
2022 // if (anot[ae[aa].getPosition()].secondaryStructure>' ')
2024 // anot[ae[aa].getPosition()].displayCharacter = "";
2026 anot[ae[aa].getPosition()].colour = new java.awt.Color(ae[aa]
2030 jalview.datamodel.AlignmentAnnotation jaa = null;
2032 if (an[i].getGraph())
2034 jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
2035 an[i].getDescription(), anot, 0, 0, an[i].getGraphType());
2037 jaa.graphGroup = an[i].getGraphGroup();
2039 if (an[i].getThresholdLine() != null)
2041 jaa.setThreshold(new jalview.datamodel.GraphLine(an[i]
2042 .getThresholdLine().getValue(), an[i]
2043 .getThresholdLine().getLabel(), new java.awt.Color(
2044 an[i].getThresholdLine().getColour())));
2051 jaa = new jalview.datamodel.AlignmentAnnotation(an[i].getLabel(),
2052 an[i].getDescription(), anot);
2054 // register new annotation
2055 if (an[i].getId() != null)
2057 annotationIds.put(an[i].getId(), jaa);
2058 jaa.annotationId = an[i].getId();
2060 // recover sequence association
2061 if (an[i].getSequenceRef() != null)
2063 if (al.findName(an[i].getSequenceRef()) != null)
2065 jaa.createSequenceMapping(al.findName(an[i].getSequenceRef()),
2067 al.findName(an[i].getSequenceRef()).addAlignmentAnnotation(jaa);
2070 if (an[i].hasScore())
2072 jaa.setScore(an[i].getScore());
2075 if (an[i].hasVisible())
2076 jaa.visible = an[i].getVisible();
2078 al.addAnnotation(jaa);
2082 // ///////////////////////
2084 // Create alignment markup and styles for this view
2085 if (jms.getJGroupCount() > 0)
2087 JGroup[] groups = jms.getJGroup();
2089 for (int i = 0; i < groups.length; i++)
2091 ColourSchemeI cs = null;
2093 if (groups[i].getColour() != null)
2095 if (groups[i].getColour().startsWith("ucs"))
2097 cs = GetUserColourScheme(jms, groups[i].getColour());
2101 cs = ColourSchemeProperty.getColour(al, groups[i].getColour());
2106 cs.setThreshold(groups[i].getPidThreshold(), true);
2110 Vector seqs = new Vector();
2112 for (int s = 0; s < groups[i].getSeqCount(); s++)
2114 String seqId = groups[i].getSeq(s) + "";
2115 jalview.datamodel.SequenceI ts = (jalview.datamodel.SequenceI) seqRefIds
2120 seqs.addElement(ts);
2124 if (seqs.size() < 1)
2129 jalview.datamodel.SequenceGroup sg = new jalview.datamodel.SequenceGroup(
2130 seqs, groups[i].getName(), cs, groups[i].getDisplayBoxes(),
2131 groups[i].getDisplayText(), groups[i].getColourText(),
2132 groups[i].getStart(), groups[i].getEnd());
2135 .setOutlineColour(new java.awt.Color(groups[i]
2136 .getOutlineColour()));
2138 sg.textColour = new java.awt.Color(groups[i].getTextCol1());
2139 sg.textColour2 = new java.awt.Color(groups[i].getTextCol2());
2140 sg.setShowunconserved(groups[i].hasShowUnconserved() ? groups[i].isShowUnconserved() : false);
2141 sg.thresholdTextColour = groups[i].getTextColThreshold();
2143 if (groups[i].getConsThreshold() != 0)
2145 jalview.analysis.Conservation c = new jalview.analysis.Conservation(
2146 "All", ResidueProperties.propHash, 3, sg
2147 .getSequences(null), 0, sg.getWidth() - 1);
2149 c.verdict(false, 25);
2150 sg.cs.setConservation(c);
2157 // ///////////////////////////////
2160 // If we just load in the same jar file again, the sequenceSetId
2161 // will be the same, and we end up with multiple references
2162 // to the same sequenceSet. We must modify this id on load
2163 // so that each load of the file gives a unique id
2164 String uniqueSeqSetId = view.getSequenceSetId() + uniqueSetSuffix;
2165 String viewId = (view.getId() == null ? null : view.getId()
2167 AlignFrame af = null;
2168 AlignViewport av = null;
2169 // now check to see if we really need to create a new viewport.
2170 if (multipleView && viewportsAdded.size() == 0)
2172 // We recovered an alignment for which a viewport already exists.
2173 // TODO: fix up any settings necessary for overlaying stored state onto
2174 // state recovered from another document. (may not be necessary).
2175 // we may need a binding from a viewport in memory to one recovered from
2177 // and then recover its containing af to allow the settings to be applied.
2178 // TODO: fix for vamsas demo
2180 .println("About to recover a viewport for existing alignment: Sequence set ID is "
2182 Object seqsetobj = retrieveExistingObj(uniqueSeqSetId);
2183 if (seqsetobj != null)
2185 if (seqsetobj instanceof String)
2187 uniqueSeqSetId = (String) seqsetobj;
2189 .println("Recovered extant sequence set ID mapping for ID : New Sequence set ID is "
2195 .println("Warning : Collision between sequence set ID string and existing jalview object mapping.");
2200 AlignmentPanel ap = null;
2201 boolean isnewview = true;
2204 // Check to see if this alignment already has a view id == viewId
2205 jalview.gui.AlignmentPanel views[] = Desktop
2206 .getAlignmentPanels(uniqueSeqSetId);
2207 if (views != null && views.length > 0)
2209 for (int v = 0; v < views.length; v++)
2211 if (views[v].av.getViewId().equalsIgnoreCase(viewId))
2213 // recover the existing alignpanel, alignframe, viewport
2214 af = views[v].alignFrame;
2217 // TODO: could even skip resetting view settings if we don't want to
2218 // change the local settings from other jalview processes
2227 af = loadViewport(file, JSEQ, hiddenSeqs, al, hideConsensus,
2228 hideQuality, hideConservation, jms, view, uniqueSeqSetId,
2234 // /////////////////////////////////////
2235 if (loadTreesAndStructures && jms.getTreeCount() > 0)
2239 for (int t = 0; t < jms.getTreeCount(); t++)
2242 Tree tree = jms.getTree(t);
2244 TreePanel tp = (TreePanel) retrieveExistingObj(tree.getId());
2247 tp = af.ShowNewickTree(new jalview.io.NewickFile(tree
2248 .getNewick()), tree.getTitle(), tree.getWidth(), tree
2249 .getHeight(), tree.getXpos(), tree.getYpos());
2250 if (tree.getId() != null)
2252 // perhaps bind the tree id to something ?
2257 // update local tree attributes ?
2258 // TODO: should check if tp has been manipulated by user - if so its settings shouldn't be modified
2259 tp.setTitle(tree.getTitle());
2260 tp.setBounds(new Rectangle(tree.getXpos(), tree.getYpos(), tree
2261 .getWidth(), tree.getHeight()));
2262 tp.av = av; // af.viewport; // TODO: verify 'associate with all
2265 tp.treeCanvas.av = av; // af.viewport;
2266 tp.treeCanvas.ap = ap; // af.alignPanel;
2271 warn("There was a problem recovering stored Newick tree: \n"+tree.getNewick());
2275 tp.fitToWindow.setState(tree.getFitToWindow());
2276 tp.fitToWindow_actionPerformed(null);
2278 if (tree.getFontName() != null)
2280 tp.setTreeFont(new java.awt.Font(tree.getFontName(), tree
2281 .getFontStyle(), tree.getFontSize()));
2285 tp.setTreeFont(new java.awt.Font(view.getFontName(), view
2286 .getFontStyle(), tree.getFontSize()));
2289 tp.showPlaceholders(tree.getMarkUnlinked());
2290 tp.showBootstrap(tree.getShowBootstrap());
2291 tp.showDistances(tree.getShowDistances());
2293 tp.treeCanvas.threshold = tree.getThreshold();
2295 if (tree.getCurrentTree())
2297 af.viewport.setCurrentTree(tp.getTree());
2301 } catch (Exception ex)
2303 ex.printStackTrace();
2307 // //LOAD STRUCTURES
2308 if (loadTreesAndStructures)
2310 for (int i = 0; i < JSEQ.length; i++)
2312 if (JSEQ[i].getPdbidsCount() > 0)
2314 Pdbids[] ids = JSEQ[i].getPdbids();
2315 for (int p = 0; p < ids.length; p++)
2317 for (int s = 0; s < ids[p].getStructureStateCount(); s++)
2319 // check to see if we haven't already created this structure view
2320 String sviewid = (ids[p].getStructureState(s).getViewId() == null) ? null
2321 : ids[p].getStructureState(s).getViewId()
2323 jalview.datamodel.PDBEntry jpdb = new jalview.datamodel.PDBEntry();
2324 // Originally : ids[p].getFile()
2325 // : TODO: verify external PDB file recovery still works in normal
2326 // jalview project load
2327 jpdb.setFile(loadPDBFile(jprovider, ids[p].getId()));
2328 jpdb.setId(ids[p].getId());
2330 int x = ids[p].getStructureState(s).getXpos();
2331 int y = ids[p].getStructureState(s).getYpos();
2332 int width = ids[p].getStructureState(s).getWidth();
2333 int height = ids[p].getStructureState(s).getHeight();
2334 AppJmol comp = null;
2335 JInternalFrame[] frames = null;
2340 frames = Desktop.desktop.getAllFrames();
2341 } catch (ArrayIndexOutOfBoundsException e)
2343 // occasional No such child exceptions are thrown here...
2348 } catch (Exception f)
2353 } while (frames == null);
2354 // search for any Jmol windows already open from other
2355 // alignment views that exactly match the stored structure state
2356 for (int f = 0; comp == null && f < frames.length; f++)
2358 if (frames[f] instanceof AppJmol)
2361 && ((AppJmol) frames[f]).getViewId().equals(
2364 // post jalview 2.4 schema includes structure view id
2365 comp = (AppJmol) frames[f];
2367 else if (frames[f].getX() == x && frames[f].getY() == y
2368 && frames[f].getHeight() == height
2369 && frames[f].getWidth() == width)
2371 comp = (AppJmol) frames[f];
2375 // Probably don't need to do this anymore...
2376 // Desktop.desktop.getComponentAt(x, y);
2377 // TODO: NOW: check that this recovers the PDB file correctly.
2378 String pdbFile = loadPDBFile(jprovider, ids[p].getId());
2380 jalview.datamodel.SequenceI[] seq = new jalview.datamodel.SequenceI[]
2381 { (jalview.datamodel.SequenceI) seqRefIds.get(JSEQ[i].getId()
2386 // create a new Jmol window
2387 String state = ids[p].getStructureState(s).getContent();
2388 StringBuffer newFileLoc=null;
2389 if (state.indexOf("load")>-1) {
2390 newFileLoc = new StringBuffer(state.substring(
2391 0, state.indexOf("\"", state.indexOf("load")) + 1));
2393 newFileLoc.append(jpdb.getFile());
2394 newFileLoc.append(state.substring(state.indexOf("\"", state
2395 .indexOf("load \"") + 6)));
2397 System.err.println("Ignoring incomplete Jmol state for PDB "+ids[p].getId());
2399 newFileLoc = new StringBuffer(state);
2400 newFileLoc.append("; load \"");
2401 newFileLoc.append(jpdb.getFile());
2402 newFileLoc.append("\";");
2405 if (newFileLoc!=null) {
2406 new AppJmol(pdbFile, ids[p].getId(), seq, af.alignPanel,
2407 newFileLoc.toString(), new java.awt.Rectangle(x, y,
2408 width, height), sviewid);
2413 // if (comp != null)
2415 // NOTE: if the jalview project is part of a shared session then
2416 // view synchronization should/could be done here.
2418 // add mapping for this sequence to the already open Jmol
2419 // instance (if it doesn't already exist)
2421 StructureSelectionManager.getStructureSelectionManager()
2422 .setMapping(seq, null, pdbFile,
2423 jalview.io.AppletFormatAdapter.FILE);
2425 ((AppJmol) comp).addSequence(seq);
2436 AlignFrame loadViewport(String file, JSeq[] JSEQ, Vector hiddenSeqs,
2437 Alignment al, boolean hideConsensus, boolean hideQuality,
2438 boolean hideConservation, JalviewModelSequence jms,
2439 Viewport view, String uniqueSeqSetId, String viewId)
2441 AlignFrame af = null;
2442 af = new AlignFrame(al, view.getWidth(), view.getHeight(),
2443 uniqueSeqSetId, viewId);
2445 af.setFileName(file, "Jalview");
2447 for (int i = 0; i < JSEQ.length; i++)
2449 af.viewport.setSequenceColour(af.viewport.alignment.getSequenceAt(i),
2450 new java.awt.Color(JSEQ[i].getColour()));
2453 af.viewport.gatherViewsHere = view.getGatheredViews();
2455 if (view.getSequenceSetId() != null)
2457 jalview.gui.AlignViewport av = (jalview.gui.AlignViewport) viewportsAdded
2458 .get(uniqueSeqSetId);
2460 af.viewport.sequenceSetID = uniqueSeqSetId;
2463 // propagate shared settings to this new view
2464 af.viewport.historyList = av.historyList;
2465 af.viewport.redoList = av.redoList;
2469 viewportsAdded.put(uniqueSeqSetId, af.viewport);
2471 // TODO: check if this method can be called repeatedly without
2472 // side-effects if alignpanel already registered.
2473 PaintRefresher.Register(af.alignPanel, uniqueSeqSetId);
2475 // apply Hidden regions to view.
2476 if (hiddenSeqs != null)
2478 for (int s = 0; s < JSEQ.length; s++)
2480 jalview.datamodel.SequenceGroup hidden = new jalview.datamodel.SequenceGroup();
2482 for (int r = 0; r < JSEQ[s].getHiddenSequencesCount(); r++)
2484 hidden.addSequence(al
2485 .getSequenceAt(JSEQ[s].getHiddenSequences(r)), false);
2487 af.viewport.hideRepSequences(al.getSequenceAt(s), hidden);
2490 jalview.datamodel.SequenceI[] hseqs = new jalview.datamodel.SequenceI[hiddenSeqs
2493 for (int s = 0; s < hiddenSeqs.size(); s++)
2495 hseqs[s] = (jalview.datamodel.SequenceI) hiddenSeqs.elementAt(s);
2498 af.viewport.hideSequence(hseqs);
2501 // set visibility of annotation in view
2502 if ((hideConsensus || hideQuality || hideConservation)
2503 && al.getAlignmentAnnotation() != null)
2505 int hSize = al.getAlignmentAnnotation().length;
2506 for (int h = 0; h < hSize; h++)
2508 if ((hideConsensus && al.getAlignmentAnnotation()[h].label
2509 .equals("Consensus"))
2510 || (hideQuality && al.getAlignmentAnnotation()[h].label
2512 || (hideConservation && al.getAlignmentAnnotation()[h].label
2513 .equals("Conservation")))
2515 al.deleteAnnotation(al.getAlignmentAnnotation()[h]);
2520 af.alignPanel.adjustAnnotationHeight();
2522 // recover view properties and display parameters
2523 if (view.getViewName() != null)
2525 af.viewport.viewName = view.getViewName();
2526 af.setInitialTabVisible();
2528 af.setBounds(view.getXpos(), view.getYpos(), view.getWidth(), view
2531 af.viewport.setShowAnnotation(view.getShowAnnotation());
2532 af.viewport.setAbovePIDThreshold(view.getPidSelected());
2534 af.viewport.setColourText(view.getShowColourText());
2536 af.viewport.setConservationSelected(view.getConservationSelected());
2537 af.viewport.setShowJVSuffix(view.getShowFullId());
2538 af.viewport.rightAlignIds = view.getRightAlignIds();
2539 af.viewport.setFont(new java.awt.Font(view.getFontName(), view
2540 .getFontStyle(), view.getFontSize()));
2541 af.alignPanel.fontChanged();
2542 af.viewport.setRenderGaps(view.getRenderGaps());
2543 af.viewport.setWrapAlignment(view.getWrapAlignment());
2544 af.alignPanel.setWrapAlignment(view.getWrapAlignment());
2545 af.viewport.setShowAnnotation(view.getShowAnnotation());
2546 af.alignPanel.setAnnotationVisible(view.getShowAnnotation());
2548 af.viewport.setShowBoxes(view.getShowBoxes());
2550 af.viewport.setShowText(view.getShowText());
2552 af.viewport.textColour = new java.awt.Color(view.getTextCol1());
2553 af.viewport.textColour2 = new java.awt.Color(view.getTextCol2());
2554 af.viewport.thresholdTextColour = view.getTextColThreshold();
2555 af.viewport.setShowUnconserved(view.hasShowUnconserved() ? view.isShowUnconserved() : false);
2556 af.viewport.setStartRes(view.getStartRes());
2557 af.viewport.setStartSeq(view.getStartSeq());
2559 ColourSchemeI cs = null;
2560 // apply colourschemes
2561 if (view.getBgColour() != null)
2563 if (view.getBgColour().startsWith("ucs"))
2565 cs = GetUserColourScheme(jms, view.getBgColour());
2567 else if (view.getBgColour().startsWith("Annotation"))
2569 // int find annotation
2570 for (int i = 0; i < af.viewport.alignment.getAlignmentAnnotation().length; i++)
2572 if (af.viewport.alignment.getAlignmentAnnotation()[i].label
2573 .equals(view.getAnnotationColours().getAnnotation()))
2575 if (af.viewport.alignment.getAlignmentAnnotation()[i]
2576 .getThreshold() == null)
2578 af.viewport.alignment.getAlignmentAnnotation()[i]
2579 .setThreshold(new jalview.datamodel.GraphLine(view
2580 .getAnnotationColours().getThreshold(),
2581 "Threshold", java.awt.Color.black)
2586 if (view.getAnnotationColours().getColourScheme()
2589 cs = new AnnotationColourGradient(af.viewport.alignment
2590 .getAlignmentAnnotation()[i], new java.awt.Color(view
2591 .getAnnotationColours().getMinColour()),
2592 new java.awt.Color(view.getAnnotationColours()
2593 .getMaxColour()), view.getAnnotationColours()
2594 .getAboveThreshold());
2596 else if (view.getAnnotationColours().getColourScheme()
2599 cs = new AnnotationColourGradient(af.viewport.alignment
2600 .getAlignmentAnnotation()[i], GetUserColourScheme(
2601 jms, view.getAnnotationColours().getColourScheme()),
2602 view.getAnnotationColours().getAboveThreshold());
2606 cs = new AnnotationColourGradient(af.viewport.alignment
2607 .getAlignmentAnnotation()[i], ColourSchemeProperty
2608 .getColour(al, view.getAnnotationColours()
2609 .getColourScheme()), view
2610 .getAnnotationColours().getAboveThreshold());
2613 // Also use these settings for all the groups
2614 if (al.getGroups() != null)
2616 for (int g = 0; g < al.getGroups().size(); g++)
2618 jalview.datamodel.SequenceGroup sg = (jalview.datamodel.SequenceGroup) al
2619 .getGroups().elementAt(g);
2628 * (view.getAnnotationColours().getColourScheme().equals("None")) {
2629 * sg.cs = new AnnotationColourGradient(
2630 * af.viewport.alignment.getAlignmentAnnotation()[i], new
2631 * java.awt.Color(view.getAnnotationColours(). getMinColour()),
2632 * new java.awt.Color(view.getAnnotationColours().
2634 * view.getAnnotationColours().getAboveThreshold()); } else
2637 sg.cs = new AnnotationColourGradient(
2638 af.viewport.alignment.getAlignmentAnnotation()[i],
2639 sg.cs, view.getAnnotationColours()
2640 .getAboveThreshold());
2653 cs = ColourSchemeProperty.getColour(al, view.getBgColour());
2658 cs.setThreshold(view.getPidThreshold(), true);
2659 cs.setConsensus(af.viewport.hconsensus);
2663 af.viewport.setGlobalColourScheme(cs);
2664 af.viewport.setColourAppliesToAllGroups(false);
2666 if (view.getConservationSelected() && cs != null)
2668 cs.setConservationInc(view.getConsThreshold());
2671 af.changeColour(cs);
2673 af.viewport.setColourAppliesToAllGroups(true);
2675 if (view.getShowSequenceFeatures())
2677 af.viewport.showSequenceFeatures = true;
2679 // recover featre settings
2680 if (jms.getFeatureSettings() != null)
2682 af.viewport.featuresDisplayed = new Hashtable();
2683 String[] renderOrder = new String[jms.getFeatureSettings()
2684 .getSettingCount()];
2685 for (int fs = 0; fs < jms.getFeatureSettings().getSettingCount(); fs++)
2687 Setting setting = jms.getFeatureSettings().getSetting(fs);
2688 if (setting.hasMincolour())
2690 // TODO: determine how to set data independent bounds for a graduated colour scheme's range.
2691 GraduatedColor gc = new GraduatedColor(new java.awt.Color(setting.getMincolour()), new java.awt.Color(setting.getColour()),
2693 if (setting.hasThreshold()) {
2694 gc.setThresh(setting.getThreshold());
2695 gc.setThreshType(setting.getThreshstate());
2698 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setColour(
2699 setting.getType(), new java.awt.Color(setting.getColour()));
2701 renderOrder[fs] = setting.getType();
2702 if (setting.hasOrder())
2703 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(
2704 setting.getType(), setting.getOrder());
2706 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().setOrder(
2708 fs / jms.getFeatureSettings().getSettingCount());
2709 if (setting.getDisplay())
2711 af.viewport.featuresDisplayed.put(setting.getType(), new Integer(
2712 setting.getColour()));
2715 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().renderOrder = renderOrder;
2717 af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureGroups = fgtable = new Hashtable();
2718 for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
2720 Group grp = jms.getFeatureSettings().getGroup(gs);
2721 fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
2725 if (view.getHiddenColumnsCount() > 0)
2727 for (int c = 0; c < view.getHiddenColumnsCount(); c++)
2729 af.viewport.hideColumns(view.getHiddenColumns(c).getStart(), view
2730 .getHiddenColumns(c).getEnd() // +1
2735 af.setMenusFromViewport(af.viewport);
2736 // TODO: we don't need to do this if the viewport is aready visible.
2737 Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(), view
2742 Hashtable skipList = null;
2745 * TODO remove this method
2748 * @return AlignFrame bound to sequenceSetId from view, if one exists. private
2749 * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
2750 * throw new Error("Implementation Error. No skipList defined for this
2751 * Jalview2XML instance."); } return (AlignFrame)
2752 * skipList.get(view.getSequenceSetId()); }
2756 * Check if the Jalview view contained in object should be skipped or not.
2759 * @return true if view's sequenceSetId is a key in skipList
2761 private boolean skipViewport(JalviewModel object)
2763 if (skipList == null)
2768 if (skipList.containsKey(id = object.getJalviewModelSequence()
2769 .getViewport()[0].getSequenceSetId()))
2771 if (Cache.log != null && Cache.log.isDebugEnabled())
2773 Cache.log.debug("Skipping seuqence set id " + id);
2780 public void AddToSkipList(AlignFrame af)
2782 if (skipList == null)
2784 skipList = new Hashtable();
2786 skipList.put(af.getViewport().getSequenceSetId(), af);
2789 public void clearSkipList()
2791 if (skipList != null)
2798 private void recoverDatasetFor(SequenceSet vamsasSet, Alignment al)
2800 jalview.datamodel.Alignment ds = getDatasetFor(vamsasSet.getDatasetId());
2801 Vector dseqs = null;
2804 // create a list of new dataset sequences
2805 dseqs = new Vector();
2807 for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
2809 Sequence vamsasSeq = vamsasSet.getSequence(i);
2810 ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs);
2812 // create a new dataset
2815 SequenceI[] dsseqs = new SequenceI[dseqs.size()];
2816 dseqs.copyInto(dsseqs);
2817 ds = new jalview.datamodel.Alignment(dsseqs);
2818 debug("Created new dataset "+vamsasSet.getDatasetId()+" for alignment "+System.identityHashCode(al));
2819 addDatasetRef(vamsasSet.getDatasetId(), ds);
2821 // set the dataset for the newly imported alignment.
2822 if (al.getDataset() == null)
2831 * sequence definition to create/merge dataset sequence for
2835 * vector to add new dataset sequence to
2837 private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
2838 AlignmentI ds, Vector dseqs)
2840 // JBP TODO: Check this is called for AlCodonFrames to support recovery of
2842 jalview.datamodel.Sequence sq = (jalview.datamodel.Sequence) seqRefIds
2843 .get(vamsasSeq.getId());
2844 jalview.datamodel.SequenceI dsq = null;
2845 if (sq != null && sq.getDatasetSequence() != null)
2847 dsq = (jalview.datamodel.SequenceI) sq.getDatasetSequence();
2850 String sqid = vamsasSeq.getDsseqid();
2853 // need to create or add a new dataset sequence reference to this sequence
2856 dsq = (jalview.datamodel.SequenceI) seqRefIds.get(sqid);
2861 // make a new dataset sequence
2862 dsq = sq.createDatasetSequence();
2865 // make up a new dataset reference for this sequence
2866 sqid = seqHash(dsq);
2868 dsq.setVamsasId(uniqueSetSuffix + sqid);
2869 seqRefIds.put(sqid, dsq);
2874 dseqs.addElement(dsq);
2879 ds.addSequence(dsq);
2885 { // make this dataset sequence sq's dataset sequence
2886 sq.setDatasetSequence(dsq);
2890 // TODO: refactor this as a merge dataset sequence function
2891 // now check that sq (the dataset sequence) sequence really is the union of
2892 // all references to it
2893 // boolean pre = sq.getStart() < dsq.getStart();
2894 // boolean post = sq.getEnd() > dsq.getEnd();
2898 StringBuffer sb = new StringBuffer();
2899 String newres = jalview.analysis.AlignSeq.extractGaps(
2900 jalview.util.Comparison.GapChars, sq.getSequenceAsString());
2901 if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
2902 && newres.length() > dsq.getLength())
2904 // Update with the longer sequence.
2908 * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
2909 * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
2910 * sb.append(newres.substring(newres.length() - sq.getEnd() -
2911 * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
2913 dsq.setSequence(sb.toString());
2915 // TODO: merges will never happen if we 'know' we have the real dataset
2916 // sequence - this should be detected when id==dssid
2917 System.err.println("DEBUG Notice: Merged dataset sequence"); // ("
2918 // + (pre ? "prepended" : "") + " "
2919 // + (post ? "appended" : ""));
2924 java.util.Hashtable datasetIds = null;
2925 java.util.IdentityHashMap dataset2Ids = null;
2926 private Alignment getDatasetFor(String datasetId)
2928 if (datasetIds == null)
2930 datasetIds = new Hashtable();
2933 if (datasetIds.containsKey(datasetId))
2935 return (Alignment) datasetIds.get(datasetId);
2940 private void addDatasetRef(String datasetId, Alignment dataset)
2942 if (datasetIds == null)
2944 datasetIds = new Hashtable();
2946 datasetIds.put(datasetId, dataset);
2949 * make a new dataset ID for this jalview dataset alignment
2953 private String getDatasetIdRef(jalview.datamodel.Alignment dataset)
2955 if (dataset.getDataset()!=null)
2957 warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
2959 String datasetId=makeHashCode(dataset, null);
2960 if (datasetId==null)
2962 // make a new datasetId and record it
2963 if (dataset2Ids == null)
2965 dataset2Ids = new IdentityHashMap();
2967 datasetId = (String) dataset2Ids.get(dataset);
2969 if (datasetId==null)
2971 datasetId = "ds"+dataset2Ids.size()+1;
2972 dataset2Ids.put(dataset,datasetId);
2977 private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
2979 for (int d = 0; d < sequence.getDBRefCount(); d++)
2981 DBRef dr = sequence.getDBRef(d);
2982 jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
2983 sequence.getDBRef(d).getSource(), sequence.getDBRef(d)
2984 .getVersion(), sequence.getDBRef(d).getAccessionId());
2985 if (dr.getMapping() != null)
2987 entry.setMap(addMapping(dr.getMapping()));
2989 datasetSequence.addDBRef(entry);
2993 private jalview.datamodel.Mapping addMapping(Mapping m)
2995 SequenceI dsto = null;
2996 // Mapping m = dr.getMapping();
2997 int fr[] = new int[m.getMapListFromCount() * 2];
2998 Enumeration f = m.enumerateMapListFrom();
2999 for (int _i = 0; f.hasMoreElements(); _i += 2)
3001 MapListFrom mf = (MapListFrom) f.nextElement();
3002 fr[_i] = mf.getStart();
3003 fr[_i + 1] = mf.getEnd();
3005 int fto[] = new int[m.getMapListToCount() * 2];
3006 f = m.enumerateMapListTo();
3007 for (int _i = 0; f.hasMoreElements(); _i += 2)
3009 MapListTo mf = (MapListTo) f.nextElement();
3010 fto[_i] = mf.getStart();
3011 fto[_i + 1] = mf.getEnd();
3013 jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto,
3014 fr, fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
3015 if (m.getMappingChoice() != null)
3017 MappingChoice mc = m.getMappingChoice();
3018 if (mc.getDseqFor() != null)
3020 String dsfor = ""+mc.getDseqFor();
3021 if (seqRefIds.containsKey(dsfor))
3026 jmap.setTo((SequenceI) seqRefIds.get(dsfor));
3030 frefedSequence.add(new Object[]
3037 * local sequence definition
3039 Sequence ms = mc.getSequence();
3040 jalview.datamodel.Sequence djs = null;
3041 String sqid = ms.getDsseqid();
3042 if (sqid != null && sqid.length() > 0)
3045 * recover dataset sequence
3047 djs = (jalview.datamodel.Sequence) seqRefIds.get(sqid);
3052 .println("Warning - making up dataset sequence id for DbRef sequence map reference");
3053 sqid = ((Object) ms).toString(); // make up a new hascode for
3054 // undefined dataset sequence hash
3055 // (unlikely to happen)
3061 * make a new dataset sequence and add it to refIds hash
3063 djs = new jalview.datamodel.Sequence(ms.getName(), ms
3065 djs.setStart(jmap.getMap().getToLowest());
3066 djs.setEnd(jmap.getMap().getToHighest());
3067 djs.setVamsasId(uniqueSetSuffix + sqid);
3069 seqRefIds.put(sqid, djs);
3072 jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
3081 public jalview.gui.AlignmentPanel copyAlignPanel(AlignmentPanel ap,
3082 boolean keepSeqRefs)
3085 jalview.schemabinding.version2.JalviewModel jm = SaveState(ap, null,
3091 jm.getJalviewModelSequence().getViewport(0).setSequenceSetId(null);
3095 uniqueSetSuffix = "";
3096 jm.getJalviewModelSequence().getViewport(0).setId(null); // we don't overwrite the view we just copied
3098 if (this.frefedSequence==null)
3100 frefedSequence = new Vector();
3103 viewportsAdded = new Hashtable();
3105 AlignFrame af = LoadFromObject(jm, null, false, null);
3106 af.alignPanels.clear();
3107 af.closeMenuItem_actionPerformed(true);
3110 * if(ap.av.alignment.getAlignmentAnnotation()!=null) { for(int i=0; i<ap.av.alignment.getAlignmentAnnotation().length;
3111 * i++) { if(!ap.av.alignment.getAlignmentAnnotation()[i].autoCalculated) {
3112 * af.alignPanel.av.alignment.getAlignmentAnnotation()[i] =
3113 * ap.av.alignment.getAlignmentAnnotation()[i]; } } }
3116 return af.alignPanel;
3120 * flag indicating if hashtables should be cleared on finalization TODO this
3121 * flag may not be necessary
3123 private boolean _cleartables = true;
3125 private Hashtable jvids2vobj;
3130 * @see java.lang.Object#finalize()
3132 protected void finalize() throws Throwable
3134 // really make sure we have no buried refs left.
3139 this.seqRefIds = null;
3140 this.seqsToIds = null;
3144 private void warn(String msg)
3149 private void warn(String msg, Exception e)
3151 if (Cache.log != null)
3155 Cache.log.warn(msg, e);
3159 Cache.log.warn(msg);
3164 System.err.println("Warning: " + msg);
3167 e.printStackTrace();
3172 private void debug(String string)
3176 private void debug(String msg, Exception e)
3178 if (Cache.log != null)
3182 Cache.log.debug(msg, e);
3186 Cache.log.debug(msg);
3191 System.err.println("Warning: " + msg);
3194 e.printStackTrace();
3200 * set the object to ID mapping tables used to write/recover objects and XML
3201 * ID strings for the jalview project. If external tables are provided then
3202 * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
3203 * object goes out of scope. - also populates the datasetIds hashtable with
3204 * alignment objects containing dataset sequences
3207 * Map from ID strings to jalview datamodel
3209 * Map from jalview datamodel to ID strings
3213 public void setObjectMappingTables(Hashtable vobj2jv,
3214 IdentityHashMap jv2vobj)
3216 this.jv2vobj = jv2vobj;
3217 this.vobj2jv = vobj2jv;
3218 Iterator ds = jv2vobj.keySet().iterator();
3220 while (ds.hasNext())
3222 Object jvobj = ds.next();
3223 id = jv2vobj.get(jvobj).toString();
3224 if (jvobj instanceof jalview.datamodel.Alignment)
3226 if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
3228 addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
3231 else if (jvobj instanceof jalview.datamodel.Sequence)
3233 // register sequence object so the XML parser can recover it.
3234 if (seqRefIds == null)
3236 seqRefIds = new Hashtable();
3238 if (seqsToIds == null)
3240 seqsToIds = new IdentityHashMap();
3242 seqRefIds.put(jv2vobj.get(jvobj).toString(), jvobj);
3243 seqsToIds.put(jvobj, id);
3245 else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
3247 if (annotationIds == null)
3249 annotationIds = new Hashtable();
3252 annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvobj);
3253 jalview.datamodel.AlignmentAnnotation jvann = (jalview.datamodel.AlignmentAnnotation) jvobj;
3254 if (jvann.annotationId == null)
3256 jvann.annotationId = anid;
3258 if (!jvann.annotationId.equals(anid))
3260 // TODO verify that this is the correct behaviour
3261 this.warn("Overriding Annotation ID for " + anid
3262 + " from different id : " + jvann.annotationId);
3263 jvann.annotationId = anid;
3266 else if (jvobj instanceof String)
3268 if (jvids2vobj == null)
3270 jvids2vobj = new Hashtable();
3271 jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
3275 Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
3280 * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
3281 * objects created from the project archive. If string is null (default for
3282 * construction) then suffix will be set automatically.
3286 public void setUniqueSetSuffix(String string)
3288 uniqueSetSuffix = string;
3293 * uses skipList2 as the skipList for skipping views on sequence sets
3294 * associated with keys in the skipList
3298 public void setSkipList(Hashtable skipList2)
3300 skipList = skipList2;