1 package ext.edu.ucsf.rbvi.strucviz2;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.HashSet;
9 import java.util.TreeMap;
11 import ext.edu.ucsf.rbvi.strucviz2.StructureManager.ModelType;
14 * This class provides the implementation for the ChimeraModel, ChimeraChain,
15 * and ChimeraResidue objects
20 public class ChimeraModel implements ChimeraStructuralObject
23 private String name; // The name of this model
25 private ModelType type; // The type of the model
27 private int modelNumber; // The model number
29 private int subModelNumber; // The sub-model number
31 private Color modelColor = null; // The color of this model (from Chimera)
33 private Object userData = null; // User data associated with this model
35 private boolean selected = false; // The selected state of this model
37 private TreeMap<String, ChimeraChain> chainMap; // The list of chains
39 // private TreeMap<String, ChimeraResidue> residueMap; // The list of residues
40 private HashSet<ChimeraResidue> funcResidues; // List of functional residues
43 * Constructor to create a model
46 * the name of this model
51 * @param subModelNumber
52 * the sub-model number
54 public ChimeraModel(String name, ModelType type, int modelNumber,
59 this.modelNumber = modelNumber;
60 this.subModelNumber = subModelNumber;
62 this.chainMap = new TreeMap<String, ChimeraChain>();
63 this.funcResidues = new HashSet<ChimeraResidue>();
67 * Constructor to create a model from the Chimera input line
70 * Chimera input line from which to construct this model
72 // TODO: [Optional] How to distinguish between PDB and MODBASE?
73 // invoked when listing models: listm type molecule; lists level molecule
74 // line = model id #0 type Molecule name 1ert
75 public ChimeraModel(String inputLine)
77 this.name = ChimUtils.parseModelName(inputLine);
78 // TODO: [Optional] Write a separate method to get model type
79 if (name.startsWith("smiles"))
81 this.type = ModelType.SMILES;
85 this.type = ModelType.PDB_MODEL;
87 this.modelNumber = ChimUtils.parseModelNumber(inputLine)[0];
88 this.subModelNumber = ChimUtils.parseModelNumber(inputLine)[1];
90 this.chainMap = new TreeMap<String, ChimeraChain>();
91 this.funcResidues = new HashSet<ChimeraResidue>();
95 * Add a residue to this model
100 public void addResidue(ChimeraResidue residue)
102 residue.setChimeraModel(this);
103 // residueMap.put(residue.getIndex(), residue);
104 String chainId = residue.getChainId();
107 addResidue(chainId, residue);
111 addResidue("_", residue);
113 // Put it in our map so that we can return it in order
114 // residueMap.put(residue.getIndex(), residue);
118 * Add a residue to a chain in this model. If the chain associated with
119 * chainId doesn't exist, it will be created.
122 * to add the residue to
124 * to add to the chain
126 public void addResidue(String chainId, ChimeraResidue residue)
128 ChimeraChain chain = null;
129 if (!chainMap.containsKey(chainId))
131 chain = new ChimeraChain(this.modelNumber, this.subModelNumber,
133 chain.setChimeraModel(this);
134 chainMap.put(chainId, chain);
138 chain = chainMap.get(chainId);
140 chain.addResidue(residue);
144 * Get the ChimeraModel (required for ChimeraStructuralObject interface)
146 * @return ChimeraModel
148 public ChimeraModel getChimeraModel()
154 * Get the model color of this model
156 * @return model color of this model
158 public Color getModelColor()
160 return this.modelColor;
164 * Set the color of this model
167 * Color of this model
169 public void setModelColor(Color color)
171 this.modelColor = color;
175 * Return the name of this model
179 public String getModelName()
185 * Set the name of this model
190 public void setModelName(String name)
196 * Get the model number of this model
198 * @return integer model number
200 public int getModelNumber()
206 * Set the model number of this model
209 * integer model number
211 public void setModelNumber(int modelNumber)
213 this.modelNumber = modelNumber;
217 * Get the sub-model number of this model
219 * @return integer sub-model number
221 public int getSubModelNumber()
223 return subModelNumber;
227 * Set the sub-model number of this model
229 * @param subModelNumber
230 * integer model number
232 public void setSubModelNumber(int subModelNumber)
234 this.subModelNumber = subModelNumber;
237 public ModelType getModelType()
242 public void setModelType(ModelType type)
247 public HashSet<ChimeraResidue> getFuncResidues()
252 public void setFuncResidues(List<String> residues)
254 for (String residue : residues)
256 for (ChimeraChain chain : getChains())
258 if (residue.indexOf("-") > 0)
260 funcResidues.addAll(chain.getResidueRange(residue));
264 funcResidues.add(chain.getResidue(residue));
271 * Get the user data for this model
275 public Object getUserData()
281 * Set the user data for this model
284 * user data to associate with this model
286 public void setUserData(Object data)
288 this.userData = data;
292 * Return the selected state of this model
294 * @return the selected state
296 public boolean isSelected()
302 * Set the selected state of this model
305 * a boolean to set the selected state to
307 public void setSelected(boolean selected)
309 this.selected = selected;
313 * Return the chains in this model as a List
315 * @return the chains in this model as a list
317 public List<ChimeraStructuralObject> getChildren()
319 return new ArrayList<ChimeraStructuralObject>(chainMap.values());
323 * Return the chains in this model as a colleciton
325 * @return the chains in this model
327 public Collection<ChimeraChain> getChains()
329 return chainMap.values();
333 * Get the number of chains in this model
335 * @return integer chain count
337 public int getChainCount()
339 return chainMap.size();
343 * Get the list of chain names associated with this model
345 * @return return the list of chain names for this model
347 public Collection<String> getChainNames()
349 return chainMap.keySet();
353 * Get the residues associated with this model
355 * @return the list of residues in this model
357 public Collection<ChimeraResidue> getResidues()
359 Collection<ChimeraResidue> residues = new ArrayList<ChimeraResidue>();
360 for (ChimeraChain chain : getChains())
362 residues.addAll(chain.getResidues());
368 * Get the number of residues in this model
370 * @return integer residues count
372 public int getResidueCount()
375 for (ChimeraChain chain : getChains())
377 count += chain.getResidueCount();
383 * Get a specific chain from the model
386 * the ID of the chain to return
387 * @return ChimeraChain associated with the chain
389 public ChimeraChain getChain(String chain)
391 if (chainMap.containsKey(chain))
393 return chainMap.get(chain);
399 * Return a specific residue based on its index
402 * of the residue to return
403 * @return the residue associated with that index
405 public ChimeraResidue getResidue(String chainId, String index)
407 if (chainMap.containsKey(chainId))
409 return chainMap.get(chainId).getResidue(index);
415 * Checks if this model has selected children.
417 public boolean hasSelectedChildren()
425 for (ChimeraChain chain : getChains())
427 if (chain.hasSelectedChildren())
437 * Return the list of selected residues
439 * @return all selected residues
441 public List<ChimeraResidue> getSelectedResidues()
443 List<ChimeraResidue> residueList = new ArrayList<ChimeraResidue>();
444 for (ChimeraChain chain : getChains())
448 residueList.addAll(chain.getSelectedResidues());
452 residueList.addAll(getResidues());
459 * Return the Chimera specification for this model.
461 public String toSpec()
463 if (subModelNumber == 0)
464 return ("#" + modelNumber);
465 return ("#" + modelNumber + "." + subModelNumber);
469 * Return a string representation for the model. Shorten if longer than 100
472 public String toString()
474 String modelName = "";
475 // TODO: [Optional] Change cutoff for shortening model names in the
476 // structure naviagator dialog
477 if (getChainCount() > 0)
479 modelName = "Model " + toSpec() + " " + name + " (" + getChainCount()
480 + " chains, " + getResidueCount() + " residues)";
482 else if (getResidueCount() > 0)
484 modelName = "Model " + toSpec() + " " + name + " ("
485 + getResidueCount() + " residues)";
489 modelName = "Model " + toSpec() + " " + name + "";
492 Set<String> networkNames = new HashSet<String>();
493 Set<String> nodeNames = new HashSet<String>();
494 Set<String> edgeNames = new HashSet<String>();
496 String cytoName = " [";
497 if (networkNames.size() > 0)
499 if (networkNames.size() == 1)
501 cytoName += "Network {";
503 else if (networkNames.size() > 1)
505 cytoName += "Networks {";
507 for (String cName : networkNames)
509 cytoName += cName + ",";
511 cytoName = cytoName.substring(0, cytoName.length() - 1) + "}, ";
513 if (nodeNames.size() > 0)
515 if (nodeNames.size() == 1)
517 cytoName += "Node {";
519 else if (nodeNames.size() > 1)
521 cytoName += "Nodes {";
523 for (String cName : nodeNames)
525 cytoName += cName + ",";
527 cytoName = cytoName.substring(0, cytoName.length() - 1) + "}, ";
529 if (edgeNames.size() > 0)
531 if (edgeNames.size() == 1)
533 cytoName += "Edge {";
535 else if (edgeNames.size() > 1)
537 cytoName += "Edges {";
539 for (String cName : edgeNames)
541 cytoName += cName + ",";
543 cytoName = cytoName.substring(0, cytoName.length() - 1) + "}, ";
545 if (cytoName.endsWith(", "))
547 cytoName = cytoName.substring(0, cytoName.length() - 2);
550 String nodeName = modelName + cytoName;
551 if (nodeName.length() > 100)
553 nodeName = nodeName.substring(0, 100) + "...";