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[] = { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.', '.' };
66 java.util.Hashtable fields = new java.util.Hashtable();
70 fields.put(Fields[TAIL], "");
79 resCode(String f, Integer v)
88 field = val.toString();
92 private resCode validResidueCode(String field)
95 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
96 "\\s*((([-0-9]+).?)|FIRST|LAST|@)");
100 return null; // invalid
102 String value = r.stringMatched(3);
105 value = r.stringMatched(1);
107 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
111 val = Integer.valueOf(value);
112 return new resCode(field, val); // successful numeric extraction
113 } catch (Exception e)
116 return new resCode(field, null);
119 private java.util.Hashtable parseDescription(String desc)
121 java.util.Hashtable fields = new java.util.Hashtable();
122 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":",
127 if (st.countTokens() > 0)
129 // parse colon-fields
131 field = st.nextToken(":");
134 if (seqTypes[i].equalsIgnoreCase(field))
138 } while (++i < seqTypes.length);
140 if (i < seqTypes.length)
142 st.nextToken(); // skip ':'
143 // valid seqType for modeller
145 i = 1; // continue parsing fields
146 while (i < TAIL && st.hasMoreTokens())
148 if ((field = st.nextToken(":")) != null)
150 if (!field.equals(":"))
152 // validate residue field value
155 resCode val = validResidueCode(field);
158 fields.put(new String(Fields[i] + "num"), val);
162 // jalview.bin.Cache.log.debug(
163 // "Ignoring non-Modeller description: invalid integer-like
164 // field '" + field + "'");
165 type = -1; /* invalid field! - throw the FieldSet away */
169 fields.put(Fields[i++], field);
170 if (st.hasMoreTokens())
172 st.nextToken(); // skip token sep.
183 // slurp remaining fields
184 while (st.hasMoreTokens())
186 String tl = st.nextToken(":");
187 field += tl.equals(":") ? tl : (":" + tl);
189 fields.put(Fields[TAIL], field);
195 // object is not a proper ModellerPIR object
196 fields = new java.util.Hashtable();
197 fields.put(Fields[TAIL], new String(desc));
201 fields.put(Fields[TYPE], seqTypes[type]);
206 ModellerDescription(String desc)
212 fields = parseDescription(desc);
215 void setStartCode(int v)
218 fields.put(Fields[START] + "num", r = new resCode(v));
219 fields.put(Fields[START], r.field);
222 void setEndCode(int v)
225 fields.put(Fields[END] + "num", r = new resCode(v));
226 fields.put(Fields[END], r.field);
230 * make a possibly updated modeller field line for the sequence object
235 ModellerDescription(SequenceI seq)
238 if (seq.getDescription() != null)
240 fields = parseDescription(seq.getDescription());
243 if (isModellerFieldset())
245 // Set start and end before we update the type (in the case of a
246 // synthesized field set)
247 if (getStartCode() == null
248 || (getStartNum() != seq.getStart() && getStartCode().val != null))
250 // unset or user updated sequence start position
251 setStartCode(seq.getStart());
254 if (getEndCode() == null
255 || (getEndNum() != seq.getEnd() && getStartCode() != null && getStartCode().val != null))
257 setEndCode(seq.getEnd());
263 setStartCode(seq.getStart());
264 setEndCode(seq.getEnd());
265 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
267 // type - decide based on evidence of PDB database references - this also
268 // sets the local reference field
269 int t = 0; // sequence
270 if (seq.getDatasetSequence() != null
271 && seq.getDatasetSequence().getDBRefs() != null)
273 jalview.datamodel.DBRefEntry[] dbr = seq.getDatasetSequence()
276 for (i = 0, j = dbr.length; i < j; i++)
280 // JBPNote PDB dbRefEntry needs properties to propagate onto
282 // JBPNote Need to get info from the user about whether the sequence
283 // is the one being modelled, or if it is a template.
284 if (dbr[i].getSource()
285 .equals(jalview.datamodel.DBRefSource.PDB))
287 fields.put(Fields[LOCALID], dbr[i].getAccessionId());
294 fields.put(Fields[TYPE], seqTypes[t]);
300 * Indicate if fields parsed to a modeller-like colon-separated value line
304 boolean isModellerFieldset()
306 return (fields.containsKey(Fields[TYPE]));
309 String getDescriptionLine()
312 int lastfield = Fields.length - 1;
314 if (isModellerFieldset())
317 // try to write a minimal modeller field set, so..
319 // find the last valid field in the entry
321 for (; lastfield > 6; lastfield--)
323 if (fields.containsKey(Fields[lastfield]))
329 for (int i = 0; i < lastfield; i++)
331 value = (String) fields.get(Fields[i]);
332 if (value != null && value.length() > 0)
334 desc += ((String) fields.get(Fields[i])) + ":";
338 desc += Padding[i] + ":";
342 // just return the last field if no others were defined.
343 if (fields.containsKey(Fields[lastfield]))
345 desc += (String) fields.get(Fields[lastfield]);
357 resCode val = getStartCode();
358 if (val != null && val.val != null)
360 return val.val.intValue();
365 resCode getStartCode()
367 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
369 return (resCode) fields.get(Fields[START] + "num");
376 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
378 return (resCode) fields.get(Fields[END] + "num");
386 resCode val = getEndCode();
387 if (val != null && val.val != null)
389 return val.val.intValue();
395 * returns true if sequence object was modifed with a valid modellerField set
401 boolean updateSequenceI(SequenceI newSeq)
403 if (isModellerFieldset())
405 resCode rc = getStartCode();
406 if (rc != null && rc.val != null)
408 newSeq.setStart(getStartNum());
415 if (rc != null && rc.val != null)
417 newSeq.setEnd(getEndNum());
421 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());