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.SequenceI;
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 = { "sequence", "structure", "structureX",
35 final String[] Fields = { "objectType", "objectId", "startField",
36 "startCode", "endField", "endCode", "description1", "description2",
37 "resolutionField", "tailField" };
41 final int LOCALID = 1;
45 final int START_CHAIN = 3;
49 final int END_CHAIN = 5;
51 final int DESCRIPTION1 = 6;
53 final int DESCRIPTION2 = 7;
55 final int RESOLUTION = 8;
60 * 0 is free text or empty 1 is something that parses to an integer, or \@
62 final int Types[] = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
64 final char Padding[] = { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.',
67 java.util.Hashtable fields = new java.util.Hashtable();
71 fields.put(Fields[TAIL], "");
80 resCode(String f, Integer v)
89 field = val.toString();
93 private resCode validResidueCode(String field)
96 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
97 "\\s*((([-0-9]+).?)|FIRST|LAST|@)");
101 return null; // invalid
103 String value = r.stringMatched(3);
106 value = r.stringMatched(1);
108 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
112 val = Integer.valueOf(value);
113 return new resCode(field, val); // successful numeric extraction
114 } catch (Exception e)
117 return new resCode(field, null);
120 private java.util.Hashtable parseDescription(String desc)
122 java.util.Hashtable fields = new java.util.Hashtable();
123 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":",
128 if (st.countTokens() > 0)
130 // parse colon-fields
132 field = st.nextToken(":");
135 if (seqTypes[i].equalsIgnoreCase(field))
139 } while (++i < seqTypes.length);
141 if (i < seqTypes.length)
143 st.nextToken(); // skip ':'
144 // valid seqType for modeller
146 i = 1; // continue parsing fields
147 while (i < TAIL && st.hasMoreTokens())
149 if ((field = st.nextToken(":")) != null)
151 if (!field.equals(":"))
153 // validate residue field value
156 resCode val = validResidueCode(field);
159 fields.put(new String(Fields[i] + "num"), val);
163 // jalview.bin.Cache.log.debug(
164 // "Ignoring non-Modeller description: invalid integer-like
165 // field '" + field + "'");
166 type = -1; /* invalid field! - throw the FieldSet away */
170 fields.put(Fields[i++], field);
171 if (st.hasMoreTokens())
173 st.nextToken(); // skip token sep.
184 // slurp remaining fields
185 while (st.hasMoreTokens())
187 String tl = st.nextToken(":");
188 field += tl.equals(":") ? tl : (":" + tl);
190 fields.put(Fields[TAIL], field);
196 // object is not a proper ModellerPIR object
197 fields = new java.util.Hashtable();
198 fields.put(Fields[TAIL], new String(desc));
202 fields.put(Fields[TYPE], seqTypes[type]);
207 ModellerDescription(String desc)
213 fields = parseDescription(desc);
216 void setStartCode(int v)
219 fields.put(Fields[START] + "num", r = new resCode(v));
220 fields.put(Fields[START], r.field);
223 void setEndCode(int v)
226 fields.put(Fields[END] + "num", r = new resCode(v));
227 fields.put(Fields[END], r.field);
231 * make a possibly updated modeller field line for the sequence object
236 ModellerDescription(SequenceI seq)
239 if (seq.getDescription() != null)
241 fields = parseDescription(seq.getDescription());
244 if (isModellerFieldset())
246 // Set start and end before we update the type (in the case of a
247 // synthesized field set)
248 if (getStartCode() == null || (getStartNum() != seq.getStart()
249 && getStartCode().val != null))
251 // unset or user updated sequence start position
252 setStartCode(seq.getStart());
255 if (getEndCode() == null || (getEndNum() != seq.getEnd()
256 && getStartCode() != null && getStartCode().val != null))
258 setEndCode(seq.getEnd());
264 setStartCode(seq.getStart());
265 setEndCode(seq.getEnd());
266 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
268 // type - decide based on evidence of PDB database references - this also
269 // sets the local reference field
270 int t = 0; // sequence
271 if (seq.getDatasetSequence() != null
272 && seq.getDatasetSequence().getDBRefs() != null)
274 jalview.datamodel.DBRefEntry[] dbr = seq.getDatasetSequence()
277 for (i = 0, j = dbr.length; i < j; i++)
281 // JBPNote PDB dbRefEntry needs properties to propagate onto
283 // JBPNote Need to get info from the user about whether the sequence
284 // is the one being modelled, or if it is a template.
285 if (dbr[i].getSource()
286 .equals(jalview.datamodel.DBRefSource.PDB))
288 fields.put(Fields[LOCALID], dbr[i].getAccessionId());
295 fields.put(Fields[TYPE], seqTypes[t]);
301 * Indicate if fields parsed to a modeller-like colon-separated value line
305 boolean isModellerFieldset()
307 return (fields.containsKey(Fields[TYPE]));
310 String getDescriptionLine()
313 int lastfield = Fields.length - 1;
315 if (isModellerFieldset())
318 // try to write a minimal modeller field set, so..
320 // find the last valid field in the entry
322 for (; lastfield > 6; lastfield--)
324 if (fields.containsKey(Fields[lastfield]))
330 for (int i = 0; i < lastfield; i++)
332 value = (String) fields.get(Fields[i]);
333 if (value != null && value.length() > 0)
335 desc += ((String) fields.get(Fields[i])) + ":";
339 desc += Padding[i] + ":";
343 // just return the last field if no others were defined.
344 if (fields.containsKey(Fields[lastfield]))
346 desc += (String) fields.get(Fields[lastfield]);
358 resCode val = getStartCode();
359 if (val != null && val.val != null)
361 return val.val.intValue();
366 resCode getStartCode()
368 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
370 return (resCode) fields.get(Fields[START] + "num");
377 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
379 return (resCode) fields.get(Fields[END] + "num");
387 resCode val = getEndCode();
388 if (val != null && val.val != null)
390 return val.val.intValue();
396 * returns true if sequence object was modifed with a valid modellerField set
402 boolean updateSequenceI(SequenceI newSeq)
404 if (isModellerFieldset())
406 resCode rc = getStartCode();
407 if (rc != null && rc.val != null)
409 newSeq.setStart(getStartNum());
416 if (rc != null && rc.val != null)
418 newSeq.setEnd(getEndNum());
422 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());