2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 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 jalview.datamodel.*;
23 public class ModellerDescription
26 * Translates between a String containing a set of colon-separated values
27 * on a single line, and sequence start/end and other properties.
28 * See PIRFile IO for its use.
30 final String[] seqTypes =
32 "sequence", "structure", "structureX", "structureN"};
33 final String[] Fields =
35 "objectType", "objectId",
36 "startField", "startCode",
37 "endField", "endCode",
38 "description1", "description2",
39 "resolutionField", "tailField"};
41 final int LOCALID = 1;
43 final int START_CHAIN = 3;
45 final int END_CHAIN = 5;
46 final int DESCRIPTION1 = 6;
47 final int DESCRIPTION2 = 7;
48 final int RESOLUTION = 8;
52 * 0 is free text or empty
53 * 1 is something that parses to an integer, or \@
57 0, 0, 1, 0, 1, 0, 0, 0, 0, 0
59 final char Padding[] =
61 ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.', '.'
64 java.util.Hashtable fields = new java.util.Hashtable();
67 fields.put(Fields[TAIL], "");
74 resCode(String f, Integer v)
83 field = val.toString();
87 private resCode validResidueCode(String field)
90 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
91 "\\s*((([-0-9]+).?)|FIRST|LAST|@)");
95 return null; // invalid
97 String value = r.stringMatched(3);
100 value = r.stringMatched(1);
102 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
106 val = Integer.valueOf(value);
107 return new resCode(field, val); // successful numeric extraction
112 return new resCode(field, null);
115 private java.util.Hashtable parseDescription(String desc)
117 java.util.Hashtable fields = new java.util.Hashtable();
118 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":");
121 if (st.countTokens() > 0)
123 // parse colon-fields
125 field = st.nextToken(":");
128 if (seqTypes[i].equalsIgnoreCase(field))
133 while (++i < seqTypes.length);
135 if (i < seqTypes.length)
137 // valid seqType for modeller
139 i = 1; // continue parsing fields
140 while (i < TAIL && st.hasMoreTokens())
142 if ( (field = st.nextToken(":")) != null)
144 // validate residue field value
147 resCode val = validResidueCode(field);
150 fields.put(new String(Fields[i] + "num"), val);
154 // jalview.bin.Cache.log.debug(
155 // "Ignoring non-Modeller description: invalid integer-like field '" + field + "'");
156 type = -1; /* invalid field! - throw the FieldSet away */
160 fields.put(Fields[i++], field);
165 // slurp remaining fields
166 while (st.hasMoreTokens())
168 field += ":" + st.nextToken(":");
170 fields.put(Fields[TAIL], field);
176 // object is not a proper ModellerPIR object
177 fields = new java.util.Hashtable();
178 fields.put(Fields[TAIL], new String(desc));
182 fields.put(Fields[TYPE], seqTypes[type]);
187 ModellerDescription(String desc)
193 fields = parseDescription(desc);
196 void setStartCode(int v)
199 fields.put(Fields[START] + "num", r = new resCode(v));
200 fields.put(Fields[START], r.field);
203 void setEndCode(int v)
206 fields.put(Fields[END] + "num", r = new resCode(v));
207 fields.put(Fields[END], r.field);
211 * make a possibly updated modeller field line for the sequence object
212 * @param seq SequenceI
214 ModellerDescription(SequenceI seq)
217 if (seq.getDescription() != null)
219 fields = parseDescription(seq.getDescription());
222 if (isModellerFieldset())
224 // Set start and end before we update the type (in the case of a synthesized field set)
225 if (getStartNum() != seq.getStart() && getStartCode().val != null)
227 setStartCode(seq.getStart());
230 if (getEndNum() != seq.getEnd() && getStartCode().val != null)
232 setEndCode(seq.getEnd());
238 setStartCode(seq.getStart());
239 setEndCode(seq.getEnd());
240 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten below...
241 // type - decide based on evidence of PDB database references - this also sets the local reference field
242 int t = 0; // sequence
243 if (seq.getDatasetSequence() != null &&
244 seq.getDatasetSequence().getDBRef() != null)
246 jalview.datamodel.DBRefEntry[] dbr = seq.getDatasetSequence().getDBRef();
248 for (i = 0, j = dbr.length; i < j; i++)
252 // JBPNote PDB dbRefEntry needs properties to propagate onto ModellerField
253 // JBPNote Need to get info from the user about whether the sequence is the one being modelled, or if it is a template.
254 if (dbr[i].getSource().equals(jalview.datamodel.DBRefSource.PDB))
256 fields.put(Fields[LOCALID], dbr[i].getAccessionId());
263 fields.put(Fields[TYPE], seqTypes[t]);
269 * Indicate if fields parsed to a modeller-like colon-separated value line
272 boolean isModellerFieldset()
274 return (fields.containsKey(Fields[TYPE]));
277 String getDescriptionLine()
280 int lastfield = Fields.length - 1;
282 if (isModellerFieldset())
285 // try to write a minimal modeller field set, so..
287 // find the last valid field in the entry
289 for (; lastfield > 6; lastfield--)
291 if (fields.containsKey(Fields[lastfield]))
297 for (int i = 0; i < lastfield; i++)
299 value = (String) fields.get(Fields[i]);
300 if (value != null && value.length() > 0)
302 desc += ( (String) fields.get(Fields[i])) + ":";
306 desc += Padding[i] + ":";
310 // just return the last field if no others were defined.
311 if (fields.containsKey(Fields[lastfield]))
313 desc += (String) fields.get(Fields[lastfield]);
325 resCode val = getStartCode();
328 return val.val.intValue();
333 resCode getStartCode()
335 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
337 return (resCode) fields.get(Fields[START] + "num");
344 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
346 return (resCode) fields.get(Fields[END] + "num");
354 resCode val = getEndCode();
357 return val.val.intValue();
363 * returns true if sequence object was modifed with a valid modellerField set
364 * @param newSeq SequenceI
367 boolean updateSequenceI(SequenceI newSeq)
369 if (isModellerFieldset())
371 if (getStartCode().val != null)
373 newSeq.setStart(getStartNum());
379 if (getEndCode().val != null)
381 newSeq.setEnd(getEndNum());
385 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());