--- /dev/null
+package jalview.viewmodel.seqfeatures;
+
+import java.awt.Color;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Vector;
+
+import jalview.api.DBRefEntryI;
+import jalview.datamodel.AlignmentI;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.datamodel.features.SequenceFeaturesI;
+
+public class IdColumns
+{
+
+ AlignmentI alignment = null;
+
+ public IdColumns(AlignmentI al)
+ {
+ alignment = al;
+ columns.put(STRUCTURES_NUM.getLabel(), STRUCTURES_NUM);
+ updateTypeList();
+ }
+
+ public final static IdColumn STRUCTURES_NUM;
+ static
+ {
+ STRUCTURES_NUM = new IdColumn("3D Structures",
+ "Number of associated structure files", false);
+ }
+
+ LinkedHashMap<String, IdColumn> columns = new LinkedHashMap<String, IdColumn>();
+
+ /**
+ * register a feature on the columnset
+ *
+ * @param sf
+ * @return true if feature was not seen before
+ */
+ boolean updateListForFeature(SequenceFeature sf)
+ {
+ String colname = sf.getType();
+ if (columns.get(colname) != null)
+ {
+ return false;
+ }
+
+ IdColumn col = new IdColumn(colname,
+ "Nonpositional feature: " + colname, false);
+ col.featureTypeName = sf.getType();
+ // col.featureGroupName = sf.getFeatureGroup();
+ columns.put(colname, col);
+ return true;
+ }
+
+ boolean updateListForDbxref(DBRefEntryI dbref)
+ {
+ String colname = dbref.getSource();
+ if (columns.get(colname) != null)
+ {
+ return false;
+ }
+
+ IdColumn col = new IdColumn(colname,
+ "Database CrossReference: " + colname, false);
+ col.DbRefName = colname;
+ // col.featureGroupName = sf.getFeatureGroup();
+ columns.put(colname, col);
+ return true;
+ }
+
+ public void updateTypeList()
+ {
+ for (SequenceI sq : alignment.getSequences())
+ {
+ SequenceI seq = sq;
+ while (seq.getDatasetSequence() != null)
+ {
+ seq = seq.getDatasetSequence();
+ }
+
+ SequenceFeaturesI sqf = seq.getFeatures();
+ List<SequenceFeature> nonpos = sqf.getNonPositionalFeatures();
+ if (nonpos != null)
+ {
+ for (SequenceFeature sf : nonpos)
+ {
+ updateListForFeature(sf);
+ }
+ }
+ for (DBRefEntryI dbr : seq.getDBRefs())
+ {
+ updateListForDbxref(dbr);
+ }
+ }
+ }
+
+ public void toggleVisible(String column)
+ {
+ IdColumn col = columns.get(column);
+ if (col != null)
+ {
+ col.visible = !col.visible;
+ }
+ }
+
+ public List<IdColumn> getVisible()
+ {
+ // probably want to cache this
+ ArrayList<IdColumn> vis = new ArrayList();
+ for (IdColumn col : columns.values())
+ {
+ if (col.visible)
+ {
+ vis.add(col);
+ }
+ }
+ return vis;
+ }
+
+ public final class ColumnCell
+ {
+ public final String label;
+
+ public final Color bg;
+
+ public final Color fg;
+
+ public ColumnCell(final String label, final Color bg, final Color fg)
+ {
+ this.label = label;
+ this.bg = bg;
+ this.fg = fg;
+ }
+ }
+
+ /**
+ * render the column value for this sequence
+ *
+ * @param seq
+ * @param col
+ * @return
+ */
+ public ColumnCell getCellFor(SequenceI seq, IdColumn col)
+ {
+ ColumnCell cell = null;
+ if (col != null)
+ {
+ if (col == STRUCTURES_NUM)
+ {
+ while (seq.getDatasetSequence() != null)
+ {
+ seq = seq.getDatasetSequence();
+ }
+ Vector pdbE = seq.getAllPDBEntries();
+ if (pdbE == null)
+ {
+ return null;
+ }
+ return new ColumnCell("" + pdbE.size(), Color.red, Color.white);
+ }
+ if (col.featureTypeName != null)
+ {
+ List<SequenceFeature> np = seq.getFeatures()
+ .getNonPositionalFeatures(col.featureTypeName);
+ if (np != null)
+ {
+ for (SequenceFeature npfeat : np)
+ {
+ // nb deal with multiplicities!
+ if (col.featureGroupName != null && npfeat.featureGroup != null
+ && npfeat.featureGroup.equals(col.featureGroupName))
+ {
+ Color fg = Color.black;
+ Color bg = Color.white;
+
+ return new ColumnCell(npfeat.description, fg, bg);
+ }
+ }
+ }
+ }
+ if (col.DbRefName != null)
+ {
+ for (DBRefEntryI dbr : seq.getDBRefs())
+ {
+ if (dbr.getSource().equals(col.DbRefName))
+ {
+ return new ColumnCell(dbr.getAccessionId(), Color.black,
+ Color.white);
+ }
+ }
+ }
+ }
+ // no value for this sequence in given column
+ return null;
+ }
+
+ public IdColumn[] getIdColumns()
+ {
+ return columns.values().toArray(new IdColumn[0]);
+ }
+}