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.DBRefEntry;
24 import jalview.datamodel.SequenceI;
25 import jalview.util.Platform;
26 import jalview.bin.Console;
28 import java.util.List;
30 import com.stevesoft.pat.Regex;
32 public class ModellerDescription
35 * Translates between a String containing a set of colon-separated values on a
36 * single line, and sequence start/end and other properties. See PIRFile IO
39 final String[] seqTypes = { "sequence", "structure", "structureX",
42 final String[] Fields = { "objectType", "objectId", "startField",
43 "startCode", "endField", "endCode", "description1", "description2",
44 "resolutionField", "tailField" };
48 final int LOCALID = 1;
52 final int START_CHAIN = 3;
56 final int END_CHAIN = 5;
58 final int DESCRIPTION1 = 6;
60 final int DESCRIPTION2 = 7;
62 final int RESOLUTION = 8;
67 * 0 is free text or empty 1 is something that parses to an integer, or \@
69 final int Types[] = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
71 final char Padding[] = { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.',
74 java.util.Hashtable fields = new java.util.Hashtable();
78 fields.put(Fields[TAIL], "");
87 resCode(String f, Integer v)
95 val = Integer.valueOf(v);
96 field = val.toString();
100 private static Regex VALIDATION_REGEX;
102 private static Regex getRegex()
104 return (VALIDATION_REGEX == null
105 ? VALIDATION_REGEX = Platform
106 .newRegex("\\s*((([-0-9]+).?)|FIRST|LAST|@)", null)
109 private resCode validResidueCode(String field)
112 Regex r = getRegex();
114 if (!r.search(field))
116 return null; // invalid
118 String value = r.stringMatched(3);
121 value = r.stringMatched(1);
123 // jalview.bin.Console.debug("from '" + field + "' matched '" + value +
127 val = Integer.valueOf(value);
128 return new resCode(field, val); // successful numeric extraction
129 } catch (Exception e)
132 return new resCode(field, null);
135 private java.util.Hashtable parseDescription(String desc)
137 java.util.Hashtable fields = new java.util.Hashtable();
138 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":",
143 if (st.countTokens() > 0)
145 // parse colon-fields
147 field = st.nextToken(":");
150 if (seqTypes[i].equalsIgnoreCase(field))
154 } while (++i < seqTypes.length);
156 if (i < seqTypes.length)
158 st.nextToken(); // skip ':'
159 // valid seqType for modeller
161 i = 1; // continue parsing fields
162 while (i < TAIL && st.hasMoreTokens())
164 if ((field = st.nextToken(":")) != null)
166 if (!field.equals(":"))
168 // validate residue field value
171 resCode val = validResidueCode(field);
174 fields.put(new String(Fields[i] + "num"), val);
178 // jalview.bin.Console.debug(
179 // "Ignoring non-Modeller description: invalid integer-like
180 // field '" + field + "'");
181 type = -1; /* invalid field! - throw the FieldSet away */
185 fields.put(Fields[i++], field);
186 if (st.hasMoreTokens())
188 st.nextToken(); // skip token sep.
199 // slurp remaining fields
200 while (st.hasMoreTokens())
202 String tl = st.nextToken(":");
203 field += tl.equals(":") ? tl : (":" + tl);
205 fields.put(Fields[TAIL], field);
211 // object is not a proper ModellerPIR object
212 fields = new java.util.Hashtable();
213 fields.put(Fields[TAIL], new String(desc));
217 fields.put(Fields[TYPE], seqTypes[type]);
222 ModellerDescription(String desc)
228 fields = parseDescription(desc);
231 void setStartCode(int v)
234 fields.put(Fields[START] + "num", r = new resCode(v));
235 fields.put(Fields[START], r.field);
238 void setEndCode(int v)
241 fields.put(Fields[END] + "num", r = new resCode(v));
242 fields.put(Fields[END], r.field);
246 * make a possibly updated modeller field line for the sequence object
251 ModellerDescription(SequenceI seq)
254 if (seq.getDescription() != null)
256 fields = parseDescription(seq.getDescription());
259 if (isModellerFieldset())
261 // Set start and end before we update the type (in the case of a
262 // synthesized field set)
263 if (getStartCode() == null || (getStartNum() != seq.getStart()
264 && getStartCode().val != null))
266 // unset or user updated sequence start position
267 setStartCode(seq.getStart());
270 if (getEndCode() == null || (getEndNum() != seq.getEnd()
271 && getStartCode() != null && getStartCode().val != null))
273 setEndCode(seq.getEnd());
279 setStartCode(seq.getStart());
280 setEndCode(seq.getEnd());
281 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
283 // type - decide based on evidence of PDB database references - this also
284 // sets the local reference field
285 int t = 0; // sequence
286 if (seq.getDatasetSequence() != null
287 && seq.getDatasetSequence().getDBRefs() != null)
289 List<DBRefEntry> dbr = seq.getDatasetSequence().getDBRefs();
290 for (int i = 0, ni = dbr.size(); i < ni; i++)
292 DBRefEntry dbri = dbr.get(i);
295 // JBPNote PDB dbRefEntry needs properties to propagate onto
297 // JBPNote Need to get info from the user about whether the sequence
298 // is the one being modelled, or if it is a template.
300 .equals(jalview.datamodel.DBRefSource.PDB))
302 fields.put(Fields[LOCALID], dbri.getAccessionId());
309 fields.put(Fields[TYPE], seqTypes[t]);
315 * Indicate if fields parsed to a modeller-like colon-separated value line
319 boolean isModellerFieldset()
321 return (fields.containsKey(Fields[TYPE]));
324 String getDescriptionLine()
327 int lastfield = Fields.length - 1;
329 if (isModellerFieldset())
332 // try to write a minimal modeller field set, so..
334 // find the last valid field in the entry
336 for (; lastfield > 6; lastfield--)
338 if (fields.containsKey(Fields[lastfield]))
344 for (int i = 0; i < lastfield; i++)
346 value = (String) fields.get(Fields[i]);
347 if (value != null && value.length() > 0)
349 desc += ((String) fields.get(Fields[i])) + ":";
353 desc += Padding[i] + ":";
357 // just return the last field if no others were defined.
358 if (fields.containsKey(Fields[lastfield]))
360 desc += (String) fields.get(Fields[lastfield]);
372 resCode val = getStartCode();
373 if (val != null && val.val != null)
375 return val.val.intValue();
380 resCode getStartCode()
382 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
384 return (resCode) fields.get(Fields[START] + "num");
391 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
393 return (resCode) fields.get(Fields[END] + "num");
401 resCode val = getEndCode();
402 if (val != null && val.val != null)
404 return val.val.intValue();
410 * returns true if sequence object was modifed with a valid modellerField set
416 boolean updateSequenceI(SequenceI newSeq)
418 if (isModellerFieldset())
420 resCode rc = getStartCode();
421 if (rc != null && rc.val != null)
423 newSeq.setStart(getStartNum());
430 if (rc != null && rc.val != null)
432 newSeq.setEnd(getEndNum());
436 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());