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;
26 import java.util.List;
28 public class ModellerDescription
31 * Translates between a String containing a set of colon-separated values on a
32 * single line, and sequence start/end and other properties. See PIRFile IO
35 final String[] seqTypes = { "sequence", "structure", "structureX",
38 final String[] Fields = { "objectType", "objectId", "startField",
39 "startCode", "endField", "endCode", "description1", "description2",
40 "resolutionField", "tailField" };
44 final int LOCALID = 1;
48 final int START_CHAIN = 3;
52 final int END_CHAIN = 5;
54 final int DESCRIPTION1 = 6;
56 final int DESCRIPTION2 = 7;
58 final int RESOLUTION = 8;
63 * 0 is free text or empty 1 is something that parses to an integer, or \@
65 final int Types[] = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
67 final char Padding[] = { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.',
70 java.util.Hashtable fields = new java.util.Hashtable();
74 fields.put(Fields[TAIL], "");
83 resCode(String f, Integer v)
92 field = val.toString();
96 private resCode validResidueCode(String field)
99 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex(
100 "\\s*((([-0-9]+).?)|FIRST|LAST|@)");
102 if (!r.search(field))
104 return null; // invalid
106 String value = r.stringMatched(3);
109 value = r.stringMatched(1);
111 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
115 val = Integer.valueOf(value);
116 return new resCode(field, val); // successful numeric extraction
117 } catch (Exception e)
120 return new resCode(field, null);
123 private java.util.Hashtable parseDescription(String desc)
125 java.util.Hashtable fields = new java.util.Hashtable();
126 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":",
131 if (st.countTokens() > 0)
133 // parse colon-fields
135 field = st.nextToken(":");
138 if (seqTypes[i].equalsIgnoreCase(field))
142 } while (++i < seqTypes.length);
144 if (i < seqTypes.length)
146 st.nextToken(); // skip ':'
147 // valid seqType for modeller
149 i = 1; // continue parsing fields
150 while (i < TAIL && st.hasMoreTokens())
152 if ((field = st.nextToken(":")) != null)
154 if (!field.equals(":"))
156 // validate residue field value
159 resCode val = validResidueCode(field);
162 fields.put(new String(Fields[i] + "num"), val);
166 // jalview.bin.Cache.log.debug(
167 // "Ignoring non-Modeller description: invalid integer-like
168 // field '" + field + "'");
169 type = -1; /* invalid field! - throw the FieldSet away */
173 fields.put(Fields[i++], field);
174 if (st.hasMoreTokens())
176 st.nextToken(); // skip token sep.
187 // slurp remaining fields
188 while (st.hasMoreTokens())
190 String tl = st.nextToken(":");
191 field += tl.equals(":") ? tl : (":" + tl);
193 fields.put(Fields[TAIL], field);
199 // object is not a proper ModellerPIR object
200 fields = new java.util.Hashtable();
201 fields.put(Fields[TAIL], new String(desc));
205 fields.put(Fields[TYPE], seqTypes[type]);
210 ModellerDescription(String desc)
216 fields = parseDescription(desc);
219 void setStartCode(int v)
222 fields.put(Fields[START] + "num", r = new resCode(v));
223 fields.put(Fields[START], r.field);
226 void setEndCode(int v)
229 fields.put(Fields[END] + "num", r = new resCode(v));
230 fields.put(Fields[END], r.field);
234 * make a possibly updated modeller field line for the sequence object
239 ModellerDescription(SequenceI seq)
242 if (seq.getDescription() != null)
244 fields = parseDescription(seq.getDescription());
247 if (isModellerFieldset())
249 // Set start and end before we update the type (in the case of a
250 // synthesized field set)
251 if (getStartCode() == null || (getStartNum() != seq.getStart()
252 && getStartCode().val != null))
254 // unset or user updated sequence start position
255 setStartCode(seq.getStart());
258 if (getEndCode() == null || (getEndNum() != seq.getEnd()
259 && getStartCode() != null && getStartCode().val != null))
261 setEndCode(seq.getEnd());
267 setStartCode(seq.getStart());
268 setEndCode(seq.getEnd());
269 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
271 // type - decide based on evidence of PDB database references - this also
272 // sets the local reference field
273 int t = 0; // sequence
274 if (seq.getDatasetSequence() != null
275 && seq.getDatasetSequence().getDBRefs() != null)
277 List<DBRefEntry> dbr = seq.getDatasetSequence().getDBRefs();
278 for (int i = 0, ni = dbr.size(); i < ni; i++)
280 DBRefEntry dbri = dbr.get(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.
288 .equals(jalview.datamodel.DBRefSource.PDB))
290 fields.put(Fields[LOCALID], dbri.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());