001 /* 002 * CRIMSON 003 * Copyright (c) 2006, Stephen Fisher, Susan Davidson, and Junhyong Kim, 004 * University of Pennsylvania. 005 * 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License as 008 * published by the Free Software Foundation; either version 2 of the 009 * License, or (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, but 012 * WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 * 02110-1301 USA. 020 * 021 * @(#)Partition.java 022 */ 023 024 package edu.upenn.crimson; 025 026 import edu.upenn.crimson.io.*; 027 028 /** 029 * Partition header info. 030 * 031 * @author Stephen Fisher 032 * @version $Id: Partition.java,v 1.16 2007/05/16 18:55:58 fisher Exp $ 033 */ 034 035 public class Partition { 036 037 /** This is a unique name for the partition object. */ 038 private String id = ""; 039 040 /** This is the tree structure for this partition. */ 041 private String treeID = ""; 042 043 /** Model specific to this partition object. */ 044 private String modelID = ""; 045 046 /** Notes specific to this partition object. */ 047 private String notes = ""; 048 049 /** 050 * Total length of the partition's sequence. This shouldn't be 051 * changed after the partition is created. 052 */ 053 private int length = 0; 054 055 public Partition(String id, Tree tree) { 056 this(id, tree, null, 0, ""); 057 } 058 059 public Partition(String id, Tree tree, Model model, int length, String notes) throws InvalidIDException { 060 // if no ID, then error 061 if (CrimsonUtils.isEmpty(id)) { 062 CrimsonUtils.printError("Invalid partition ID."); 063 throw new InvalidIDException("Partition ID can not be empty."); 064 } else if (ObjectHandles.containsPartition(id)) { 065 CrimsonUtils.printError("Partition ID already exists, much specify a different ID:" + id); 066 throw new InvalidIDException("Partition ID already exists."); 067 068 } 069 070 this.id = id.trim().toUpperCase(); 071 this.length = length; 072 this.notes = notes; 073 074 // we require a model object rather than an ID, to make sure 075 // that the model really exists. 076 if (model != null) this.modelID = model.getID(); 077 078 // by protecting against null trees, we are effectively 079 // allowing for the creation of partitions that are not 080 // attached to trees. These will need to be attached to a 081 // tree manually. 082 if (tree != null) tree.addPartition(this); 083 084 // add the tree to the relevant lists 085 ObjectHandles.addPartition(this); 086 } 087 088 //-------------------------------------------------------------------------- 089 // Setters and Getters 090 091 public String getID() { return id; } 092 093 /** Get the ID for the tree containing this partition. */ 094 public String getTreeID() { return treeID; } 095 096 /** 097 * Sets the ID for the model underlying this partition. We require 098 * a model object rather than an ID, to make sure that the model 099 * really exists. 100 */ 101 public void setModelID(Model model) { 102 if (model == null) return; 103 104 this.modelID = model.getID(); 105 106 // update the PARTITIONS database 107 if (! Partitions.dbContains(id)) { 108 CrimsonUtils.printError("Partition (" + id + ") not in database, model ID not saved to database."); 109 return; 110 } 111 112 // UPDATE partitions SET model_id = modelID WHERE id = id 113 String sql = "UPDATE partitions SET model_id = '" + modelID.toUpperCase(); 114 sql += "' WHERE id = '" + id.toUpperCase() + "' AND tree_id = '" + treeID.toUpperCase() + "'"; 115 if (! Database.execUpdate(sql)) 116 CrimsonUtils.printError("Error updating model_id in partition " + id + "."); 117 } 118 119 /** Returns the ID for the model underlying this partition. */ 120 public String getModelID() { return modelID; } 121 122 /** 123 * Updates the notes field in the partition object and the 124 * PARTITIONS table. 125 */ 126 public void setNotes(String notes) { 127 this.notes = notes; 128 129 // update the TREES database 130 if (! Partitions.dbContains(id)) { 131 CrimsonUtils.printError("Partition (" + id + ") not in database, partition notes not updated."); 132 return; 133 } 134 135 // UPDATE partitions SET notes = notes WHERE id = id 136 String sql = "UPDATE partitions SET notes = '" + notes; 137 sql += "' WHERE id = '" + id.toUpperCase() + "' AND tree_id = '" + treeID.toUpperCase() + "'"; 138 if (! Database.execUpdate(sql)) 139 CrimsonUtils.printError("Error updating notes in partition " + id + "."); 140 } 141 142 public String getNotes() { return notes; } 143 144 public int getLength() { return length; } 145 146 //-------------------------------------------------------------------------- 147 // Miscellaneous Methods 148 149 /** Returns Partition information. */ 150 public String toString() { 151 if (CrimsonUtils.isEmpty(modelID)) { 152 return id + " (" + length + " bp)"; 153 } else { 154 return id + " (" + length + " bp, " + modelID + ")"; 155 } 156 } 157 158 } // Partition.java