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 import com.stevesoft.pat.Regex;
30 public class ModellerDescription
33 * Translates between a String containing a set of colon-separated values on a
34 * single line, and sequence start/end and other properties. See PIRFile IO
37 final String[] seqTypes = { "sequence", "structure", "structureX",
40 final String[] Fields = { "objectType", "objectId", "startField",
41 "startCode", "endField", "endCode", "description1", "description2",
42 "resolutionField", "tailField" };
46 final int LOCALID = 1;
50 final int START_CHAIN = 3;
54 final int END_CHAIN = 5;
56 final int DESCRIPTION1 = 6;
58 final int DESCRIPTION2 = 7;
60 final int RESOLUTION = 8;
65 * 0 is free text or empty 1 is something that parses to an integer, or \@
67 final int Types[] = { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
69 final char Padding[] = { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.',
72 java.util.Hashtable fields = new java.util.Hashtable();
76 fields.put(Fields[TAIL], "");
85 resCode(String f, Integer v)
93 val = Integer.valueOf(v);
94 field = val.toString();
98 private resCode validResidueCode(String field)
102 "\\s*((([-0-9]+).?)|FIRST|LAST|@)");
104 if (!r.search(field))
106 return null; // invalid
108 String value = r.stringMatched(3);
111 value = r.stringMatched(1);
113 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
117 val = Integer.valueOf(value);
118 return new resCode(field, val); // successful numeric extraction
119 } catch (Exception e)
122 return new resCode(field, null);
125 private java.util.Hashtable parseDescription(String desc)
127 java.util.Hashtable fields = new java.util.Hashtable();
128 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":",
133 if (st.countTokens() > 0)
135 // parse colon-fields
137 field = st.nextToken(":");
140 if (seqTypes[i].equalsIgnoreCase(field))
144 } while (++i < seqTypes.length);
146 if (i < seqTypes.length)
148 st.nextToken(); // skip ':'
149 // valid seqType for modeller
151 i = 1; // continue parsing fields
152 while (i < TAIL && st.hasMoreTokens())
154 if ((field = st.nextToken(":")) != null)
156 if (!field.equals(":"))
158 // validate residue field value
161 resCode val = validResidueCode(field);
164 fields.put(new String(Fields[i] + "num"), val);
168 // jalview.bin.Cache.log.debug(
169 // "Ignoring non-Modeller description: invalid integer-like
170 // field '" + field + "'");
171 type = -1; /* invalid field! - throw the FieldSet away */
175 fields.put(Fields[i++], field);
176 if (st.hasMoreTokens())
178 st.nextToken(); // skip token sep.
189 // slurp remaining fields
190 while (st.hasMoreTokens())
192 String tl = st.nextToken(":");
193 field += tl.equals(":") ? tl : (":" + tl);
195 fields.put(Fields[TAIL], field);
201 // object is not a proper ModellerPIR object
202 fields = new java.util.Hashtable();
203 fields.put(Fields[TAIL], new String(desc));
207 fields.put(Fields[TYPE], seqTypes[type]);
212 ModellerDescription(String desc)
218 fields = parseDescription(desc);
221 void setStartCode(int v)
224 fields.put(Fields[START] + "num", r = new resCode(v));
225 fields.put(Fields[START], r.field);
228 void setEndCode(int v)
231 fields.put(Fields[END] + "num", r = new resCode(v));
232 fields.put(Fields[END], r.field);
236 * make a possibly updated modeller field line for the sequence object
241 ModellerDescription(SequenceI seq)
244 if (seq.getDescription() != null)
246 fields = parseDescription(seq.getDescription());
249 if (isModellerFieldset())
251 // Set start and end before we update the type (in the case of a
252 // synthesized field set)
253 if (getStartCode() == null || (getStartNum() != seq.getStart()
254 && getStartCode().val != null))
256 // unset or user updated sequence start position
257 setStartCode(seq.getStart());
260 if (getEndCode() == null || (getEndNum() != seq.getEnd()
261 && getStartCode() != null && getStartCode().val != null))
263 setEndCode(seq.getEnd());
269 setStartCode(seq.getStart());
270 setEndCode(seq.getEnd());
271 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
273 // type - decide based on evidence of PDB database references - this also
274 // sets the local reference field
275 int t = 0; // sequence
276 if (seq.getDatasetSequence() != null
277 && seq.getDatasetSequence().getDBRefs() != null)
279 List<DBRefEntry> dbr = seq.getDatasetSequence().getDBRefs();
280 for (int i = 0, ni = dbr.size(); i < ni; i++)
282 DBRefEntry dbri = dbr.get(i);
285 // JBPNote PDB dbRefEntry needs properties to propagate onto
287 // JBPNote Need to get info from the user about whether the sequence
288 // is the one being modelled, or if it is a template.
290 .equals(jalview.datamodel.DBRefSource.PDB))
292 fields.put(Fields[LOCALID], dbri.getAccessionId());
299 fields.put(Fields[TYPE], seqTypes[t]);
305 * Indicate if fields parsed to a modeller-like colon-separated value line
309 boolean isModellerFieldset()
311 return (fields.containsKey(Fields[TYPE]));
314 String getDescriptionLine()
317 int lastfield = Fields.length - 1;
319 if (isModellerFieldset())
322 // try to write a minimal modeller field set, so..
324 // find the last valid field in the entry
326 for (; lastfield > 6; lastfield--)
328 if (fields.containsKey(Fields[lastfield]))
334 for (int i = 0; i < lastfield; i++)
336 value = (String) fields.get(Fields[i]);
337 if (value != null && value.length() > 0)
339 desc += ((String) fields.get(Fields[i])) + ":";
343 desc += Padding[i] + ":";
347 // just return the last field if no others were defined.
348 if (fields.containsKey(Fields[lastfield]))
350 desc += (String) fields.get(Fields[lastfield]);
362 resCode val = getStartCode();
363 if (val != null && val.val != null)
365 return val.val.intValue();
370 resCode getStartCode()
372 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
374 return (resCode) fields.get(Fields[START] + "num");
381 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
383 return (resCode) fields.get(Fields[END] + "num");
391 resCode val = getEndCode();
392 if (val != null && val.val != null)
394 return val.val.intValue();
400 * returns true if sequence object was modifed with a valid modellerField set
406 boolean updateSequenceI(SequenceI newSeq)
408 if (isModellerFieldset())
410 resCode rc = getStartCode();
411 if (rc != null && rc.val != null)
413 newSeq.setStart(getStartNum());
420 if (rc != null && rc.val != null)
422 newSeq.setEnd(getEndNum());
426 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());