+ /*
+ * restore feature colour scheme
+ */
+ Color maxColour = new Color(setting.getColour());
+ if (setting.hasMincolour())
+ {
+ /*
+ * minColour is always set unless a simple colour
+ * (including for colour by label though it doesn't use it)
+ */
+ Color minColour = new Color(setting.getMincolour());
+ Color noValueColour = minColour;
+ NoValueColour noColour = setting.getNoValueColour();
+ if (noColour == NoValueColour.NONE)
+ {
+ noValueColour = null;
+ }
+ else if (noColour == NoValueColour.MAX)
+ {
+ noValueColour = maxColour;
+ }
+ float min = setting.hasMin() ? setting.getMin() : 0f;
+ float max = setting.hasMin() ? setting.getMax() : 1f;
+ FeatureColourI gc = new FeatureColour(minColour, maxColour,
+ noValueColour, min, max);
+ if (setting.getAttributeNameCount() > 0)
+ {
+ gc.setAttributeName(setting.getAttributeName());
+ }
+ if (setting.hasThreshold())
+ {
+ gc.setThreshold(setting.getThreshold());
+ int threshstate = setting.getThreshstate();
+ // -1 = None, 0 = Below, 1 = Above threshold
+ if (threshstate == 0)
+ {
+ gc.setBelowThreshold(true);
+ }
+ else if (threshstate == 1)
+ {
+ gc.setAboveThreshold(true);
+ }
+ }
+ gc.setAutoScaled(true); // default
+ if (setting.hasAutoScale())
+ {
+ gc.setAutoScaled(setting.getAutoScale());
+ }
+ if (setting.hasColourByLabel())
+ {
+ gc.setColourByLabel(setting.getColourByLabel());
+ }
+ // and put in the feature colour table.
+ featureColours.put(featureType, gc);
+ }
+ else
+ {
+ featureColours.put(featureType,
+ new FeatureColour(maxColour));
+ }
+ renderOrder[fs] = featureType;
+ if (setting.hasOrder())
+ {
+ featureOrder.put(featureType, setting.getOrder());
+ }
+ else
+ {
+ featureOrder.put(featureType, new Float(
+ fs / jms.getFeatureSettings().getSettingCount()));
+ }
+ if (setting.getDisplay())
+ {
+ fdi.setVisible(featureType);
+ }
+ }
+ Map<String, Boolean> fgtable = new Hashtable<>();
+ for (int gs = 0; gs < jms.getFeatureSettings().getGroupCount(); gs++)
+ {
+ Group grp = jms.getFeatureSettings().getGroup(gs);
+ fgtable.put(grp.getName(), new Boolean(grp.getDisplay()));
+ }
+ // FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
+ // fgtable, featureColours, jms.getFeatureSettings().hasTransparency() ?
+ // jms.getFeatureSettings().getTransparency() : 0.0, featureOrder);
+ FeatureRendererSettings frs = new FeatureRendererSettings(renderOrder,
+ fgtable, featureColours, 1.0f, featureOrder);
+ fr.transferSettings(frs);
+ }
+
+ if (view.getHiddenColumnsCount() > 0)
+ {
+ for (int c = 0; c < view.getHiddenColumnsCount(); c++)
+ {
+ af.viewport.hideColumns(view.getHiddenColumns(c).getStart(),
+ view.getHiddenColumns(c).getEnd() // +1
+ );
+ }
+ }
+ if (view.getCalcIdParam() != null)
+ {
+ for (CalcIdParam calcIdParam : view.getCalcIdParam())
+ {
+ if (calcIdParam != null)
+ {
+ if (recoverCalcIdParam(calcIdParam, af.viewport))
+ {
+ }
+ else
+ {
+ warn("Couldn't recover parameters for "
+ + calcIdParam.getCalcId());
+ }
+ }
+ }
+ }
+ af.setMenusFromViewport(af.viewport);
+ af.setTitle(view.getTitle());
+ // TODO: we don't need to do this if the viewport is aready visible.
+ /*
+ * Add the AlignFrame to the desktop (it may be 'gathered' later), unless it
+ * has a 'cdna/protein complement' view, in which case save it in order to
+ * populate a SplitFrame once all views have been read in.
+ */
+ String complementaryViewId = view.getComplementId();
+ if (complementaryViewId == null)
+ {
+ Desktop.addInternalFrame(af, view.getTitle(), view.getWidth(),
+ view.getHeight());
+ // recompute any autoannotation
+ af.alignPanel.updateAnnotation(false, true);
+ reorderAutoannotation(af, al, autoAlan);
+ af.alignPanel.alignmentChanged();
+ }
+ else
+ {
+ splitFrameCandidates.put(view, af);
+ }
+ return af;
+ }
+
+ /**
+ * Reads saved data to restore Colour by Annotation settings
+ *
+ * @param viewAnnColour
+ * @param af
+ * @param al
+ * @param jms
+ * @param checkGroupAnnColour
+ * @return
+ */
+ private ColourSchemeI constructAnnotationColour(
+ AnnotationColours viewAnnColour, AlignFrame af, AlignmentI al,
+ JalviewModelSequence jms, boolean checkGroupAnnColour)
+ {
+ boolean propagateAnnColour = false;
+ AlignmentI annAlignment = af != null ? af.viewport.getAlignment() : al;
+ if (checkGroupAnnColour && al.getGroups() != null
+ && al.getGroups().size() > 0)
+ {
+ // pre 2.8.1 behaviour
+ // check to see if we should transfer annotation colours
+ propagateAnnColour = true;
+ for (SequenceGroup sg : al.getGroups())
+ {
+ if (sg.getColourScheme() instanceof AnnotationColourGradient)
+ {
+ propagateAnnColour = false;
+ }
+ }
+ }
+
+ /*
+ * 2.10.2- : saved annotationId is AlignmentAnnotation.annotationId
+ */
+ String annotationId = viewAnnColour.getAnnotation();
+ AlignmentAnnotation matchedAnnotation = annotationIds.get(annotationId);
+
+ /*
+ * pre 2.10.2: saved annotationId is AlignmentAnnotation.label
+ */
+ if (matchedAnnotation == null
+ && annAlignment.getAlignmentAnnotation() != null)
+ {
+ for (int i = 0; i < annAlignment.getAlignmentAnnotation().length; i++)
+ {
+ if (annotationId
+ .equals(annAlignment.getAlignmentAnnotation()[i].label))
+ {
+ matchedAnnotation = annAlignment.getAlignmentAnnotation()[i];
+ break;
+ }
+ }
+ }
+ if (matchedAnnotation == null)
+ {
+ System.err.println("Failed to match annotation colour scheme for "
+ + annotationId);
+ return null;
+ }
+ if (matchedAnnotation.getThreshold() == null)
+ {
+ matchedAnnotation.setThreshold(new GraphLine(
+ viewAnnColour.getThreshold(), "Threshold", Color.black));
+ }
+
+ AnnotationColourGradient cs = null;
+ if (viewAnnColour.getColourScheme().equals("None"))
+ {
+ cs = new AnnotationColourGradient(matchedAnnotation,
+ new Color(viewAnnColour.getMinColour()),
+ new Color(viewAnnColour.getMaxColour()),
+ viewAnnColour.getAboveThreshold());
+ }
+ else if (viewAnnColour.getColourScheme().startsWith("ucs"))
+ {
+ cs = new AnnotationColourGradient(matchedAnnotation,
+ getUserColourScheme(jms, viewAnnColour.getColourScheme()),
+ viewAnnColour.getAboveThreshold());
+ }
+ else
+ {
+ cs = new AnnotationColourGradient(matchedAnnotation,
+ ColourSchemeProperty.getColourScheme(al,
+ viewAnnColour.getColourScheme()),
+ viewAnnColour.getAboveThreshold());
+ }
+
+ boolean perSequenceOnly = viewAnnColour.isPerSequence();
+ boolean useOriginalColours = viewAnnColour.isPredefinedColours();
+ cs.setSeqAssociated(perSequenceOnly);
+ cs.setPredefinedColours(useOriginalColours);
+
+ if (propagateAnnColour && al.getGroups() != null)
+ {
+ // Also use these settings for all the groups
+ for (int g = 0; g < al.getGroups().size(); g++)
+ {
+ SequenceGroup sg = al.getGroups().get(g);
+ if (sg.getGroupColourScheme() == null)
+ {
+ continue;
+ }
+
+ AnnotationColourGradient groupScheme = new AnnotationColourGradient(
+ matchedAnnotation, sg.getColourScheme(),
+ viewAnnColour.getAboveThreshold());
+ sg.setColourScheme(groupScheme);
+ groupScheme.setSeqAssociated(perSequenceOnly);
+ groupScheme.setPredefinedColours(useOriginalColours);
+ }
+ }
+ return cs;
+ }
+
+ private void reorderAutoannotation(AlignFrame af, AlignmentI al,
+ List<JvAnnotRow> autoAlan)
+ {
+ // copy over visualization settings for autocalculated annotation in the
+ // view
+ if (al.getAlignmentAnnotation() != null)
+ {
+ /**
+ * Kludge for magic autoannotation names (see JAL-811)
+ */
+ String[] magicNames = new String[] { "Consensus", "Quality",
+ "Conservation" };
+ JvAnnotRow nullAnnot = new JvAnnotRow(-1, null);
+ Hashtable<String, JvAnnotRow> visan = new Hashtable<>();
+ for (String nm : magicNames)
+ {
+ visan.put(nm, nullAnnot);
+ }
+ for (JvAnnotRow auan : autoAlan)
+ {
+ visan.put(auan.template.label
+ + (auan.template.getCalcId() == null ? ""
+ : "\t" + auan.template.getCalcId()),
+ auan);
+ }
+ int hSize = al.getAlignmentAnnotation().length;
+ List<JvAnnotRow> reorder = new ArrayList<>();
+ // work through any autoCalculated annotation already on the view
+ // removing it if it should be placed in a different location on the
+ // annotation panel.
+ List<String> remains = new ArrayList<>(visan.keySet());
+ for (int h = 0; h < hSize; h++)
+ {
+ jalview.datamodel.AlignmentAnnotation jalan = al
+ .getAlignmentAnnotation()[h];
+ if (jalan.autoCalculated)
+ {
+ String k;
+ JvAnnotRow valan = visan.get(k = jalan.label);
+ if (jalan.getCalcId() != null)
+ {
+ valan = visan.get(k = jalan.label + "\t" + jalan.getCalcId());
+ }
+
+ if (valan != null)
+ {
+ // delete the auto calculated row from the alignment
+ al.deleteAnnotation(jalan, false);
+ remains.remove(k);
+ hSize--;
+ h--;
+ if (valan != nullAnnot)
+ {
+ if (jalan != valan.template)
+ {
+ // newly created autoannotation row instance
+ // so keep a reference to the visible annotation row
+ // and copy over all relevant attributes
+ if (valan.template.graphHeight >= 0)
+
+ {
+ jalan.graphHeight = valan.template.graphHeight;
+ }
+ jalan.visible = valan.template.visible;
+ }
+ reorder.add(new JvAnnotRow(valan.order, jalan));
+ }
+ }
+ }
+ }
+ // Add any (possibly stale) autocalculated rows that were not appended to
+ // the view during construction
+ for (String other : remains)
+ {
+ JvAnnotRow othera = visan.get(other);
+ if (othera != nullAnnot && othera.template.getCalcId() != null
+ && othera.template.getCalcId().length() > 0)
+ {
+ reorder.add(othera);
+ }
+ }
+ // now put the automatic annotation in its correct place
+ int s = 0, srt[] = new int[reorder.size()];
+ JvAnnotRow[] rws = new JvAnnotRow[reorder.size()];
+ for (JvAnnotRow jvar : reorder)
+ {
+ rws[s] = jvar;
+ srt[s++] = jvar.order;
+ }
+ reorder.clear();
+ jalview.util.QuickSort.sort(srt, rws);
+ // and re-insert the annotation at its correct position
+ for (JvAnnotRow jvar : rws)
+ {
+ al.addAnnotation(jvar.template, jvar.order);
+ }
+ af.alignPanel.adjustAnnotationHeight();
+ }
+ }
+
+ Hashtable skipList = null;
+
+ /**
+ * TODO remove this method
+ *
+ * @param view
+ * @return AlignFrame bound to sequenceSetId from view, if one exists. private
+ * AlignFrame getSkippedFrame(Viewport view) { if (skipList==null) {
+ * throw new Error("Implementation Error. No skipList defined for this
+ * Jalview2XML instance."); } return (AlignFrame)
+ * skipList.get(view.getSequenceSetId()); }
+ */
+
+ /**
+ * Check if the Jalview view contained in object should be skipped or not.
+ *
+ * @param object
+ * @return true if view's sequenceSetId is a key in skipList
+ */
+ private boolean skipViewport(JalviewModel object)
+ {
+ if (skipList == null)
+ {
+ return false;
+ }
+ String id;
+ if (skipList.containsKey(
+ id = object.getJalviewModelSequence().getViewport()[0]
+ .getSequenceSetId()))
+ {
+ if (Cache.log != null && Cache.log.isDebugEnabled())
+ {
+ Cache.log.debug("Skipping seuqence set id " + id);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public void addToSkipList(AlignFrame af)
+ {
+ if (skipList == null)
+ {
+ skipList = new Hashtable();
+ }
+ skipList.put(af.getViewport().getSequenceSetId(), af);
+ }
+
+ public void clearSkipList()
+ {
+ if (skipList != null)
+ {
+ skipList.clear();
+ skipList = null;
+ }
+ }
+
+ private void recoverDatasetFor(SequenceSet vamsasSet, AlignmentI al,
+ boolean ignoreUnrefed)
+ {
+ jalview.datamodel.AlignmentI ds = getDatasetFor(
+ vamsasSet.getDatasetId());
+ Vector dseqs = null;
+ if (ds == null)
+ {
+ // create a list of new dataset sequences
+ dseqs = new Vector();
+ }
+ for (int i = 0, iSize = vamsasSet.getSequenceCount(); i < iSize; i++)
+ {
+ Sequence vamsasSeq = vamsasSet.getSequence(i);
+ ensureJalviewDatasetSequence(vamsasSeq, ds, dseqs, ignoreUnrefed, i);
+ }
+ // create a new dataset
+ if (ds == null)
+ {
+ SequenceI[] dsseqs = new SequenceI[dseqs.size()];
+ dseqs.copyInto(dsseqs);
+ ds = new jalview.datamodel.Alignment(dsseqs);
+ debug("Created new dataset " + vamsasSet.getDatasetId()
+ + " for alignment " + System.identityHashCode(al));
+ addDatasetRef(vamsasSet.getDatasetId(), ds);
+ }
+ // set the dataset for the newly imported alignment.
+ if (al.getDataset() == null && !ignoreUnrefed)
+ {
+ al.setDataset(ds);
+ }
+ }
+
+ /**
+ *
+ * @param vamsasSeq
+ * sequence definition to create/merge dataset sequence for
+ * @param ds
+ * dataset alignment
+ * @param dseqs
+ * vector to add new dataset sequence to
+ * @param ignoreUnrefed
+ * - when true, don't create new sequences from vamsasSeq if it's id
+ * doesn't already have an asssociated Jalview sequence.
+ * @param vseqpos
+ * - used to reorder the sequence in the alignment according to the
+ * vamsasSeq array ordering, to preserve ordering of dataset
+ */
+ private void ensureJalviewDatasetSequence(Sequence vamsasSeq,
+ AlignmentI ds, Vector dseqs, boolean ignoreUnrefed, int vseqpos)
+ {
+ // JBP TODO: Check this is called for AlCodonFrames to support recovery of
+ // xRef Codon Maps
+ SequenceI sq = seqRefIds.get(vamsasSeq.getId());
+ boolean reorder = false;
+ SequenceI dsq = null;
+ if (sq != null && sq.getDatasetSequence() != null)
+ {
+ dsq = sq.getDatasetSequence();
+ }
+ else
+ {
+ reorder = true;
+ }
+ if (sq == null && ignoreUnrefed)
+ {
+ return;
+ }
+ String sqid = vamsasSeq.getDsseqid();
+ if (dsq == null)
+ {
+ // need to create or add a new dataset sequence reference to this sequence
+ if (sqid != null)
+ {
+ dsq = seqRefIds.get(sqid);
+ }
+ // check again
+ if (dsq == null)
+ {
+ // make a new dataset sequence
+ dsq = sq.createDatasetSequence();
+ if (sqid == null)
+ {
+ // make up a new dataset reference for this sequence
+ sqid = seqHash(dsq);
+ }
+ dsq.setVamsasId(uniqueSetSuffix + sqid);
+ seqRefIds.put(sqid, dsq);
+ if (ds == null)
+ {
+ if (dseqs != null)
+ {
+ dseqs.addElement(dsq);
+ }
+ }
+ else
+ {
+ ds.addSequence(dsq);
+ }
+ }
+ else
+ {
+ if (sq != dsq)
+ { // make this dataset sequence sq's dataset sequence
+ sq.setDatasetSequence(dsq);
+ // and update the current dataset alignment
+ if (ds == null)
+ {
+ if (dseqs != null)
+ {
+ if (!dseqs.contains(dsq))
+ {
+ dseqs.add(dsq);
+ }
+ }
+ else
+ {
+ if (ds.findIndex(dsq) < 0)
+ {
+ ds.addSequence(dsq);
+ }
+ }
+ }
+ }
+ }
+ }
+ // TODO: refactor this as a merge dataset sequence function
+ // now check that sq (the dataset sequence) sequence really is the union of
+ // all references to it
+ // boolean pre = sq.getStart() < dsq.getStart();
+ // boolean post = sq.getEnd() > dsq.getEnd();
+ // if (pre || post)
+ if (sq != dsq)
+ {
+ // StringBuffer sb = new StringBuffer();
+ String newres = jalview.analysis.AlignSeq.extractGaps(
+ jalview.util.Comparison.GapChars, sq.getSequenceAsString());
+ if (!newres.equalsIgnoreCase(dsq.getSequenceAsString())
+ && newres.length() > dsq.getLength())
+ {
+ // Update with the longer sequence.
+ synchronized (dsq)
+ {
+ /*
+ * if (pre) { sb.insert(0, newres .substring(0, dsq.getStart() -
+ * sq.getStart())); dsq.setStart(sq.getStart()); } if (post) {
+ * sb.append(newres.substring(newres.length() - sq.getEnd() -
+ * dsq.getEnd())); dsq.setEnd(sq.getEnd()); }
+ */
+ dsq.setSequence(newres);
+ }
+ // TODO: merges will never happen if we 'know' we have the real dataset
+ // sequence - this should be detected when id==dssid
+ System.err.println(
+ "DEBUG Notice: Merged dataset sequence (if you see this often, post at http://issues.jalview.org/browse/JAL-1474)"); // ("
+ // + (pre ? "prepended" : "") + " "
+ // + (post ? "appended" : ""));
+ }
+ }
+ else
+ {
+ // sequence refs are identical. We may need to update the existing dataset
+ // alignment with this one, though.
+ if (ds != null && dseqs == null)
+ {
+ int opos = ds.findIndex(dsq);
+ SequenceI tseq = null;
+ if (opos != -1 && vseqpos != opos)
+ {
+ // remove from old position
+ ds.deleteSequence(dsq);
+ }
+ if (vseqpos < ds.getHeight())
+ {
+ if (vseqpos != opos)
+ {
+ // save sequence at destination position
+ tseq = ds.getSequenceAt(vseqpos);
+ ds.replaceSequenceAt(vseqpos, dsq);
+ ds.addSequence(tseq);
+ }
+ }
+ else
+ {
+ ds.addSequence(dsq);
+ }
+ }
+ }
+ }
+
+ /*
+ * TODO use AlignmentI here and in related methods - needs
+ * AlignmentI.getDataset() changed to return AlignmentI instead of Alignment
+ */
+ Hashtable<String, AlignmentI> datasetIds = null;
+
+ IdentityHashMap<AlignmentI, String> dataset2Ids = null;
+
+ private AlignmentI getDatasetFor(String datasetId)
+ {
+ if (datasetIds == null)
+ {
+ datasetIds = new Hashtable<>();
+ return null;
+ }
+ if (datasetIds.containsKey(datasetId))
+ {
+ return datasetIds.get(datasetId);
+ }
+ return null;
+ }
+
+ private void addDatasetRef(String datasetId, AlignmentI dataset)
+ {
+ if (datasetIds == null)
+ {
+ datasetIds = new Hashtable<>();
+ }
+ datasetIds.put(datasetId, dataset);
+ }
+
+ /**
+ * make a new dataset ID for this jalview dataset alignment
+ *
+ * @param dataset
+ * @return
+ */
+ private String getDatasetIdRef(AlignmentI dataset)
+ {
+ if (dataset.getDataset() != null)
+ {
+ warn("Serious issue! Dataset Object passed to getDatasetIdRef is not a Jalview DATASET alignment...");
+ }
+ String datasetId = makeHashCode(dataset, null);
+ if (datasetId == null)
+ {
+ // make a new datasetId and record it
+ if (dataset2Ids == null)
+ {
+ dataset2Ids = new IdentityHashMap<>();
+ }
+ else
+ {
+ datasetId = dataset2Ids.get(dataset);
+ }
+ if (datasetId == null)
+ {
+ datasetId = "ds" + dataset2Ids.size() + 1;
+ dataset2Ids.put(dataset, datasetId);
+ }
+ }
+ return datasetId;
+ }
+
+ private void addDBRefs(SequenceI datasetSequence, Sequence sequence)
+ {
+ for (int d = 0; d < sequence.getDBRefCount(); d++)
+ {
+ DBRef dr = sequence.getDBRef(d);
+ jalview.datamodel.DBRefEntry entry = new jalview.datamodel.DBRefEntry(
+ sequence.getDBRef(d).getSource(),
+ sequence.getDBRef(d).getVersion(),
+ sequence.getDBRef(d).getAccessionId());
+ if (dr.getMapping() != null)
+ {
+ entry.setMap(addMapping(dr.getMapping()));
+ }
+ datasetSequence.addDBRef(entry);
+ }
+ }
+
+ private jalview.datamodel.Mapping addMapping(Mapping m)
+ {
+ SequenceI dsto = null;
+ // Mapping m = dr.getMapping();
+ int fr[] = new int[m.getMapListFromCount() * 2];
+ Enumeration f = m.enumerateMapListFrom();
+ for (int _i = 0; f.hasMoreElements(); _i += 2)
+ {
+ MapListFrom mf = (MapListFrom) f.nextElement();
+ fr[_i] = mf.getStart();
+ fr[_i + 1] = mf.getEnd();
+ }
+ int fto[] = new int[m.getMapListToCount() * 2];
+ f = m.enumerateMapListTo();
+ for (int _i = 0; f.hasMoreElements(); _i += 2)
+ {
+ MapListTo mf = (MapListTo) f.nextElement();
+ fto[_i] = mf.getStart();
+ fto[_i + 1] = mf.getEnd();
+ }
+ jalview.datamodel.Mapping jmap = new jalview.datamodel.Mapping(dsto, fr,
+ fto, (int) m.getMapFromUnit(), (int) m.getMapToUnit());
+ if (m.getMappingChoice() != null)
+ {
+ MappingChoice mc = m.getMappingChoice();
+ if (mc.getDseqFor() != null)
+ {
+ String dsfor = "" + mc.getDseqFor();
+ if (seqRefIds.containsKey(dsfor))
+ {
+ /**
+ * recover from hash
+ */
+ jmap.setTo(seqRefIds.get(dsfor));
+ }
+ else
+ {
+ frefedSequence.add(newMappingRef(dsfor, jmap));
+ }
+ }
+ else
+ {
+ /**
+ * local sequence definition
+ */
+ Sequence ms = mc.getSequence();
+ SequenceI djs = null;
+ String sqid = ms.getDsseqid();
+ if (sqid != null && sqid.length() > 0)
+ {
+ /*
+ * recover dataset sequence
+ */
+ djs = seqRefIds.get(sqid);
+ }
+ else
+ {
+ System.err.println(
+ "Warning - making up dataset sequence id for DbRef sequence map reference");
+ sqid = ((Object) ms).toString(); // make up a new hascode for
+ // undefined dataset sequence hash
+ // (unlikely to happen)
+ }
+
+ if (djs == null)
+ {
+ /**
+ * make a new dataset sequence and add it to refIds hash
+ */
+ djs = new jalview.datamodel.Sequence(ms.getName(),
+ ms.getSequence());
+ djs.setStart(jmap.getMap().getToLowest());
+ djs.setEnd(jmap.getMap().getToHighest());
+ djs.setVamsasId(uniqueSetSuffix + sqid);
+ jmap.setTo(djs);
+ incompleteSeqs.put(sqid, djs);
+ seqRefIds.put(sqid, djs);
+
+ }
+ jalview.bin.Cache.log.debug("about to recurse on addDBRefs.");
+ addDBRefs(djs, ms);
+
+ }
+ }
+ return (jmap);
+
+ }
+
+ /**
+ * Provides a 'copy' of an alignment view (on action New View) by 'saving' the
+ * view as XML (but not to file), and then reloading it
+ *
+ * @param ap
+ * @return
+ */
+ public AlignmentPanel copyAlignPanel(AlignmentPanel ap)
+ {
+ initSeqRefs();
+ JalviewModel jm = saveState(ap, null, null, null);
+
+ uniqueSetSuffix = "";
+ jm.getJalviewModelSequence().getViewport(0).setId(null);
+ // we don't overwrite the view we just copied
+
+ if (this.frefedSequence == null)
+ {
+ frefedSequence = new Vector<>();
+ }
+
+ viewportsAdded.clear();
+
+ AlignFrame af = loadFromObject(jm, null, false, null);
+ af.alignPanels.clear();
+ af.closeMenuItem_actionPerformed(true);
+
+ /*
+ * if(ap.av.getAlignment().getAlignmentAnnotation()!=null) { for(int i=0;
+ * i<ap.av.getAlignment().getAlignmentAnnotation().length; i++) {
+ * if(!ap.av.getAlignment().getAlignmentAnnotation()[i].autoCalculated) {
+ * af.alignPanel.av.getAlignment().getAlignmentAnnotation()[i] =
+ * ap.av.getAlignment().getAlignmentAnnotation()[i]; } } }
+ */
+
+ return af.alignPanel;
+ }
+
+ private Hashtable jvids2vobj;
+
+ private void warn(String msg)
+ {
+ warn(msg, null);
+ }
+
+ private void warn(String msg, Exception e)
+ {
+ if (Cache.log != null)
+ {
+ if (e != null)
+ {
+ Cache.log.warn(msg, e);
+ }
+ else
+ {
+ Cache.log.warn(msg);
+ }
+ }
+ else
+ {
+ System.err.println("Warning: " + msg);
+ if (e != null)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private void debug(String string)
+ {
+ debug(string, null);
+ }
+
+ private void debug(String msg, Exception e)
+ {
+ if (Cache.log != null)
+ {
+ if (e != null)
+ {
+ Cache.log.debug(msg, e);
+ }
+ else
+ {
+ Cache.log.debug(msg);
+ }
+ }
+ else
+ {
+ System.err.println("Warning: " + msg);
+ if (e != null)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * set the object to ID mapping tables used to write/recover objects and XML
+ * ID strings for the jalview project. If external tables are provided then
+ * finalize and clearSeqRefs will not clear the tables when the Jalview2XML
+ * object goes out of scope. - also populates the datasetIds hashtable with
+ * alignment objects containing dataset sequences
+ *
+ * @param vobj2jv
+ * Map from ID strings to jalview datamodel
+ * @param jv2vobj
+ * Map from jalview datamodel to ID strings
+ *
+ *
+ */
+ public void setObjectMappingTables(Hashtable vobj2jv,
+ IdentityHashMap jv2vobj)
+ {
+ this.jv2vobj = jv2vobj;
+ this.vobj2jv = vobj2jv;
+ Iterator ds = jv2vobj.keySet().iterator();
+ String id;
+ while (ds.hasNext())
+ {
+ Object jvobj = ds.next();
+ id = jv2vobj.get(jvobj).toString();
+ if (jvobj instanceof jalview.datamodel.Alignment)
+ {
+ if (((jalview.datamodel.Alignment) jvobj).getDataset() == null)
+ {
+ addDatasetRef(id, (jalview.datamodel.Alignment) jvobj);
+ }
+ }
+ else if (jvobj instanceof jalview.datamodel.Sequence)
+ {
+ // register sequence object so the XML parser can recover it.
+ if (seqRefIds == null)
+ {
+ seqRefIds = new HashMap<>();
+ }
+ if (seqsToIds == null)
+ {
+ seqsToIds = new IdentityHashMap<>();
+ }
+ seqRefIds.put(jv2vobj.get(jvobj).toString(), (SequenceI) jvobj);
+ seqsToIds.put((SequenceI) jvobj, id);
+ }
+ else if (jvobj instanceof jalview.datamodel.AlignmentAnnotation)
+ {
+ String anid;
+ AlignmentAnnotation jvann = (AlignmentAnnotation) jvobj;
+ annotationIds.put(anid = jv2vobj.get(jvobj).toString(), jvann);
+ if (jvann.annotationId == null)
+ {
+ jvann.annotationId = anid;
+ }
+ if (!jvann.annotationId.equals(anid))
+ {
+ // TODO verify that this is the correct behaviour
+ this.warn("Overriding Annotation ID for " + anid
+ + " from different id : " + jvann.annotationId);
+ jvann.annotationId = anid;
+ }
+ }
+ else if (jvobj instanceof String)
+ {
+ if (jvids2vobj == null)
+ {
+ jvids2vobj = new Hashtable();
+ jvids2vobj.put(jvobj, jv2vobj.get(jvobj).toString());
+ }
+ }
+ else
+ {
+ Cache.log.debug("Ignoring " + jvobj.getClass() + " (ID = " + id);
+ }
+ }
+ }
+
+ /**
+ * set the uniqueSetSuffix used to prefix/suffix object IDs for jalview
+ * objects created from the project archive. If string is null (default for
+ * construction) then suffix will be set automatically.
+ *
+ * @param string
+ */
+ public void setUniqueSetSuffix(String string)
+ {
+ uniqueSetSuffix = string;
+
+ }
+
+ /**
+ * uses skipList2 as the skipList for skipping views on sequence sets
+ * associated with keys in the skipList
+ *
+ * @param skipList2
+ */
+ public void setSkipList(Hashtable skipList2)
+ {
+ skipList = skipList2;
+ }
+
+ /**
+ * Reads the jar entry of given name and returns its contents, or null if the
+ * entry is not found.
+ *
+ * @param jprovider
+ * @param jarEntryName
+ * @return
+ */
+ protected String readJarEntry(jarInputStreamProvider jprovider,
+ String jarEntryName)
+ {
+ String result = null;
+ BufferedReader in = null;
+
+ try
+ {
+ /*
+ * Reopen the jar input stream and traverse its entries to find a matching
+ * name
+ */
+ JarInputStream jin = jprovider.getJarInputStream();
+ JarEntry entry = null;
+ do
+ {
+ entry = jin.getNextJarEntry();
+ } while (entry != null && !entry.getName().equals(jarEntryName));
+
+ if (entry != null)
+ {
+ StringBuilder out = new StringBuilder(256);
+ in = new BufferedReader(new InputStreamReader(jin, UTF_8));
+ String data;
+
+ while ((data = in.readLine()) != null)
+ {
+ out.append(data);
+ }
+ result = out.toString();
+ }
+ else
+ {
+ warn("Couldn't find entry in Jalview Jar for " + jarEntryName);
+ }
+ } catch (Exception ex)
+ {
+ ex.printStackTrace();
+ } finally
+ {
+ if (in != null)
+ {
+ try
+ {
+ in.close();
+ } catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ * Returns an incrementing counter (0, 1, 2...)
+ *
+ * @return
+ */
+ private synchronized int nextCounter()
+ {
+ return counter++;
+ }
+
+ /**
+ * Populates an XML model of the feature colour scheme for one feature type
+ *
+ * @param featureType
+ * @param fcol
+ * @return
+ */
+ protected static jalview.schemabinding.version2.Colour marshalColour(
+ String featureType, FeatureColourI fcol)
+ {
+ jalview.schemabinding.version2.Colour col = new jalview.schemabinding.version2.Colour();
+ if (fcol.isSimpleColour())
+ {
+ col.setRGB(Format.getHexString(fcol.getColour()));
+ }
+ else
+ {
+ col.setRGB(Format.getHexString(fcol.getMaxColour()));
+ col.setMin(fcol.getMin());
+ col.setMax(fcol.getMax());
+ col.setMinRGB(jalview.util.Format.getHexString(fcol.getMinColour()));
+ col.setAutoScale(fcol.isAutoScaled());
+ col.setThreshold(fcol.getThreshold());
+ col.setColourByLabel(fcol.isColourByLabel());
+ col.setThreshType(fcol.isAboveThreshold() ? ColourThreshTypeType.ABOVE
+ : (fcol.isBelowThreshold() ? ColourThreshTypeType.BELOW
+ : ColourThreshTypeType.NONE));
+ if (fcol.isColourByAttribute())
+ {
+ col.setAttributeName(fcol.getAttributeName());
+ }
+ Color noColour = fcol.getNoColour();
+ if (noColour == null)
+ {
+ col.setNoValueColour(NoValueColour.NONE);
+ }
+ else if (noColour == fcol.getMaxColour())
+ {
+ col.setNoValueColour(NoValueColour.MAX);
+ }
+ else
+ {
+ col.setNoValueColour(NoValueColour.MIN);
+ }
+ }
+ col.setName(featureType);
+ return col;
+ }
+
+ /**
+ * Populates an XML model of the feature filter(s) for one feature type
+ *
+ * @param firstMatcher
+ * the first (or only) match condition)
+ * @param filter
+ * remaining match conditions (if any)
+ * @param and
+ * if true, conditions are and-ed, else or-ed
+ */
+ protected static MatcherSet marshalFilter(FeatureMatcherI firstMatcher,
+ Iterator<FeatureMatcherI> filters, boolean and)
+ {
+ MatcherSet result = new MatcherSet();
+
+ if (filters.hasNext())
+ {
+ /*
+ * compound matcher
+ */
+ CompoundMatcher compound = new CompoundMatcher();
+ compound.setAnd(and);
+ MatcherSet matcher1 = marshalFilter(firstMatcher,
+ Collections.emptyIterator(), and);
+ compound.addMatcherSet(matcher1);
+ FeatureMatcherI nextMatcher = filters.next();
+ MatcherSet matcher2 = marshalFilter(nextMatcher, filters, and);
+ compound.addMatcherSet(matcher2);
+ result.setCompoundMatcher(compound);
+ }
+ else
+ {
+ /*
+ * single condition matcher
+ */
+ MatchCondition matcherModel = new MatchCondition();
+ matcherModel.setCondition(
+ firstMatcher.getMatcher().getCondition().getStableName());
+ matcherModel.setValue(firstMatcher.getMatcher().getPattern());
+ if (firstMatcher.isByAttribute())
+ {
+ matcherModel.setBy(FeatureMatcherByType.BYATTRIBUTE);
+ matcherModel.setAttributeName(firstMatcher.getAttribute());
+ }
+ else if (firstMatcher.isByLabel())
+ {
+ matcherModel.setBy(FeatureMatcherByType.BYLABEL);
+ }
+ else if (firstMatcher.isByScore())
+ {
+ matcherModel.setBy(FeatureMatcherByType.BYSCORE);
+ }
+ result.setMatchCondition(matcherModel);
+ }
+
+ return result;
+ }
+
+ /**
+ * Loads one XML model of a feature filter to a Jalview object
+ *
+ * @param featureType
+ * @param matcherSetModel
+ * @return
+ */
+ protected static FeatureMatcherSetI unmarshalFilter(
+ String featureType, MatcherSet matcherSetModel)
+ {
+ FeatureMatcherSetI result = new FeatureMatcherSet();
+ try
+ {
+ unmarshalFilterConditions(result, matcherSetModel, true);
+ } catch (IllegalStateException e)
+ {
+ // mixing AND and OR conditions perhaps
+ System.err.println(
+ String.format("Error reading filter conditions for '%s': %s",
+ featureType, e.getMessage()));
+ // return as much as was parsed up to the error
+ }
+
+ return result;
+ }
+
+ /**
+ * Adds feature match conditions to matcherSet as unmarshalled from XML
+ * (possibly recursively for compound conditions)
+ *
+ * @param matcherSet
+ * @param matcherSetModel
+ * @param and
+ * if true, multiple conditions are AND-ed, else they are OR-ed
+ * @throws IllegalStateException
+ * if AND and OR conditions are mixed
+ */
+ protected static void unmarshalFilterConditions(
+ FeatureMatcherSetI matcherSet, MatcherSet matcherSetModel,
+ boolean and)
+ {
+ MatchCondition mc = matcherSetModel.getMatchCondition();
+ if (mc != null)
+ {
+ /*
+ * single condition
+ */
+ FeatureMatcherByType filterBy = mc.getBy();
+ Condition cond = Condition.fromString(mc.getCondition());
+ String pattern = mc.getValue();
+ FeatureMatcherI matchCondition = null;
+ if (filterBy == FeatureMatcherByType.BYLABEL)
+ {
+ matchCondition = FeatureMatcher.byLabel(cond, pattern);
+ }
+ else if (filterBy == FeatureMatcherByType.BYSCORE)
+ {
+ matchCondition = FeatureMatcher.byScore(cond, pattern);
+
+ }
+ else if (filterBy == FeatureMatcherByType.BYATTRIBUTE)
+ {
+ String[] attNames = mc.getAttributeName();
+ matchCondition = FeatureMatcher.byAttribute(cond, pattern,
+ attNames);
+ }
+
+ /*
+ * note this throws IllegalStateException if AND-ing to a
+ * previously OR-ed compound condition, or vice versa
+ */
+ if (and)
+ {
+ matcherSet.and(matchCondition);
+ }
+ else
+ {
+ matcherSet.or(matchCondition);
+ }
+ }
+ else
+ {
+ /*
+ * compound condition
+ */
+ MatcherSet[] matchers = matcherSetModel.getCompoundMatcher()
+ .getMatcherSet();
+ boolean anded = matcherSetModel.getCompoundMatcher().getAnd();
+ if (matchers.length == 2)
+ {
+ unmarshalFilterConditions(matcherSet, matchers[0], anded);
+ unmarshalFilterConditions(matcherSet, matchers[1], anded);
+ }
+ else
+ {
+ System.err.println("Malformed compound filter condition");
+ }
+ }
+ }
+
+ /**
+ * Loads one XML model of a feature colour to a Jalview object
+ *
+ * @param colourModel
+ * @return
+ */
+ protected static FeatureColourI unmarshalColour(
+ jalview.schemabinding.version2.Colour colourModel)
+ {
+ FeatureColourI colour = null;
+
+ if (colourModel.hasMax())
+ {
+ Color mincol = null;
+ Color maxcol = null;
+ Color noValueColour = null;
+
+ try
+ {
+ mincol = new Color(Integer.parseInt(colourModel.getMinRGB(), 16));
+ maxcol = new Color(Integer.parseInt(colourModel.getRGB(), 16));
+ } catch (Exception e)
+ {
+ Cache.log.warn("Couldn't parse out graduated feature color.", e);
+ }
+
+ NoValueColour noCol = colourModel.getNoValueColour();
+ if (noCol == NoValueColour.MIN)
+ {
+ noValueColour = mincol;
+ }
+ else if (noCol == NoValueColour.MAX)
+ {
+ noValueColour = maxcol;
+ }
+
+ colour = new FeatureColour(mincol, maxcol, noValueColour,
+ colourModel.getMin(),
+ colourModel.getMax());
+ String[] attributes = colourModel.getAttributeName();
+ if (attributes != null && attributes.length > 0)
+ {
+ colour.setAttributeName(attributes);
+ }
+ if (colourModel.hasAutoScale())
+ {
+ colour.setAutoScaled(colourModel.getAutoScale());
+ }
+ if (colourModel.hasColourByLabel())
+ {
+ colour.setColourByLabel(colourModel.getColourByLabel());
+ }
+ if (colourModel.hasThreshold())
+ {
+ colour.setThreshold(colourModel.getThreshold());
+ }
+ ColourThreshTypeType ttyp = colourModel.getThreshType();
+ if (ttyp != null)
+ {
+ if (ttyp == ColourThreshTypeType.ABOVE)
+ {
+ colour.setAboveThreshold(true);
+ }
+ else if (ttyp == ColourThreshTypeType.BELOW)
+ {
+ colour.setBelowThreshold(true);
+ }
+ }
+ }
+ else
+ {
+ Color color = new Color(Integer.parseInt(colourModel.getRGB(), 16));
+ colour = new FeatureColour(color);
+ }
+
+ return colour;