2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.*;
25 public class ModellerDescription
28 * Translates between a String containing a set of colon-separated values on a
29 * single line, and sequence start/end and other properties. See PIRFile IO
32 final String[] seqTypes =
33 { "sequence", "structure", "structureX", "structureN" };
35 final String[] Fields =
36 { "objectType", "objectId", "startField", "startCode", "endField",
37 "endCode", "description1", "description2", "resolutionField",
42 final int LOCALID = 1;
46 final int START_CHAIN = 3;
50 final int END_CHAIN = 5;
52 final int DESCRIPTION1 = 6;
54 final int DESCRIPTION2 = 7;
56 final int RESOLUTION = 8;
61 * 0 is free text or empty 1 is something that parses to an integer, or \@
64 { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
66 final char Padding[] =
67 { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.', '.' };
69 java.util.Hashtable fields = new java.util.Hashtable();
73 fields.put(Fields[TAIL], "");
82 resCode(String f, Integer v)
91 field = val.toString();
95 private resCode validResidueCode(String field)
98 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
99 "\\s*((([-0-9]+).?)|FIRST|LAST|@)");
101 if (!r.search(field))
103 return null; // invalid
105 String value = r.stringMatched(3);
108 value = r.stringMatched(1);
110 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
114 val = Integer.valueOf(value);
115 return new resCode(field, val); // successful numeric extraction
116 } catch (Exception e)
119 return new resCode(field, null);
122 private java.util.Hashtable parseDescription(String desc)
124 java.util.Hashtable fields = new java.util.Hashtable();
125 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":",
130 if (st.countTokens() > 0)
132 // parse colon-fields
134 field = st.nextToken(":");
137 if (seqTypes[i].equalsIgnoreCase(field))
141 } while (++i < seqTypes.length);
143 if (i < seqTypes.length)
145 st.nextToken(); // skip ':'
146 // valid seqType for modeller
148 i = 1; // continue parsing fields
149 while (i < TAIL && st.hasMoreTokens())
151 if ((field = st.nextToken(":")) != null)
153 if (!field.equals(":"))
155 // validate residue field value
158 resCode val = validResidueCode(field);
161 fields.put(new String(Fields[i] + "num"), val);
165 // jalview.bin.Cache.log.debug(
166 // "Ignoring non-Modeller description: invalid integer-like
167 // field '" + field + "'");
168 type = -1; /* invalid field! - throw the FieldSet away */
172 fields.put(Fields[i++], field);
173 if (st.hasMoreTokens())
175 st.nextToken(); // skip token sep.
186 // slurp remaining fields
187 while (st.hasMoreTokens())
189 String tl = st.nextToken(":");
190 field += tl.equals(":") ? tl : (":" + tl);
192 fields.put(Fields[TAIL], field);
198 // object is not a proper ModellerPIR object
199 fields = new java.util.Hashtable();
200 fields.put(Fields[TAIL], new String(desc));
204 fields.put(Fields[TYPE], seqTypes[type]);
209 ModellerDescription(String desc)
215 fields = parseDescription(desc);
218 void setStartCode(int v)
221 fields.put(Fields[START] + "num", r = new resCode(v));
222 fields.put(Fields[START], r.field);
225 void setEndCode(int v)
228 fields.put(Fields[END] + "num", r = new resCode(v));
229 fields.put(Fields[END], r.field);
233 * make a possibly updated modeller field line for the sequence object
238 ModellerDescription(SequenceI seq)
241 if (seq.getDescription() != null)
243 fields = parseDescription(seq.getDescription());
246 if (isModellerFieldset())
248 // Set start and end before we update the type (in the case of a
249 // synthesized field set)
250 if (getStartCode() == null
251 || (getStartNum() != seq.getStart() && getStartCode().val != null))
253 // unset or user updated sequence start position
254 setStartCode(seq.getStart());
257 if (getEndCode() == null
258 || (getEndNum() != seq.getEnd() && getStartCode() != null && getStartCode().val != null))
260 setEndCode(seq.getEnd());
266 setStartCode(seq.getStart());
267 setEndCode(seq.getEnd());
268 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
270 // type - decide based on evidence of PDB database references - this also
271 // sets the local reference field
272 int t = 0; // sequence
273 if (seq.getDatasetSequence() != null
274 && seq.getDatasetSequence().getDBRef() != null)
276 jalview.datamodel.DBRefEntry[] dbr = seq.getDatasetSequence()
279 for (i = 0, j = dbr.length; i < j; i++)
283 // JBPNote PDB dbRefEntry needs properties to propagate onto
285 // JBPNote Need to get info from the user about whether the sequence
286 // is the one being modelled, or if it is a template.
287 if (dbr[i].getSource()
288 .equals(jalview.datamodel.DBRefSource.PDB))
290 fields.put(Fields[LOCALID], dbr[i].getAccessionId());
297 fields.put(Fields[TYPE], seqTypes[t]);
303 * Indicate if fields parsed to a modeller-like colon-separated value line
307 boolean isModellerFieldset()
309 return (fields.containsKey(Fields[TYPE]));
312 String getDescriptionLine()
315 int lastfield = Fields.length - 1;
317 if (isModellerFieldset())
320 // try to write a minimal modeller field set, so..
322 // find the last valid field in the entry
324 for (; lastfield > 6; lastfield--)
326 if (fields.containsKey(Fields[lastfield]))
332 for (int i = 0; i < lastfield; i++)
334 value = (String) fields.get(Fields[i]);
335 if (value != null && value.length() > 0)
337 desc += ((String) fields.get(Fields[i])) + ":";
341 desc += Padding[i] + ":";
345 // just return the last field if no others were defined.
346 if (fields.containsKey(Fields[lastfield]))
348 desc += (String) fields.get(Fields[lastfield]);
360 resCode val = getStartCode();
361 if (val != null && val.val != null)
363 return val.val.intValue();
368 resCode getStartCode()
370 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
372 return (resCode) fields.get(Fields[START] + "num");
379 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
381 return (resCode) fields.get(Fields[END] + "num");
389 resCode val = getEndCode();
390 if (val != null && val.val != null)
392 return val.val.intValue();
398 * returns true if sequence object was modifed with a valid modellerField set
404 boolean updateSequenceI(SequenceI newSeq)
406 if (isModellerFieldset())
408 resCode rc = getStartCode();
409 if (rc != null && rc.val != null)
411 newSeq.setStart(getStartNum());
418 if (rc != null && rc.val != null)
420 newSeq.setEnd(getEndNum());
424 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());