2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.datamodel.*;
22 public class ModellerDescription
25 * Translates between a String containing a set of colon-separated values on a
26 * single line, and sequence start/end and other properties. See PIRFile IO
29 final String[] seqTypes =
30 { "sequence", "structure", "structureX", "structureN" };
32 final String[] Fields =
33 { "objectType", "objectId", "startField", "startCode", "endField",
34 "endCode", "description1", "description2", "resolutionField",
39 final int LOCALID = 1;
43 final int START_CHAIN = 3;
47 final int END_CHAIN = 5;
49 final int DESCRIPTION1 = 6;
51 final int DESCRIPTION2 = 7;
53 final int RESOLUTION = 8;
58 * 0 is free text or empty 1 is something that parses to an integer, or \@
61 { 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
63 final char Padding[] =
64 { ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.', '.' };
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, ":",true);
126 if (st.countTokens() > 0)
128 // parse colon-fields
130 field = st.nextToken(":");
133 if (seqTypes[i].equalsIgnoreCase(field))
137 } while (++i < seqTypes.length);
139 if (i < seqTypes.length)
141 st.nextToken(); // skip ':'
142 // valid seqType for modeller
144 i = 1; // continue parsing fields
145 while (i < TAIL && st.hasMoreTokens())
147 if ((field = st.nextToken(":")) != null)
149 if (!field.equals(":"))
151 // validate residue field value
154 resCode val = validResidueCode(field);
157 fields.put(new String(Fields[i] + "num"), val);
161 // jalview.bin.Cache.log.debug(
162 // "Ignoring non-Modeller description: invalid integer-like
163 // field '" + field + "'");
164 type = -1; /* invalid field! - throw the FieldSet away */
168 fields.put(Fields[i++], field);
169 if (st.hasMoreTokens()) {
170 st.nextToken(); // skip token sep.
179 // slurp remaining fields
180 while (st.hasMoreTokens())
182 String tl = st.nextToken(":");
183 field += tl.equals(":") ? tl : (":" + tl);
185 fields.put(Fields[TAIL], field);
191 // object is not a proper ModellerPIR object
192 fields = new java.util.Hashtable();
193 fields.put(Fields[TAIL], new String(desc));
197 fields.put(Fields[TYPE], seqTypes[type]);
202 ModellerDescription(String desc)
208 fields = parseDescription(desc);
211 void setStartCode(int v)
214 fields.put(Fields[START] + "num", r = new resCode(v));
215 fields.put(Fields[START], r.field);
218 void setEndCode(int v)
221 fields.put(Fields[END] + "num", r = new resCode(v));
222 fields.put(Fields[END], r.field);
226 * make a possibly updated modeller field line for the sequence object
231 ModellerDescription(SequenceI seq)
234 if (seq.getDescription() != null)
236 fields = parseDescription(seq.getDescription());
239 if (isModellerFieldset())
241 // Set start and end before we update the type (in the case of a
242 // synthesized field set)
243 if (getStartCode()==null || (getStartNum() != seq.getStart() && getStartCode().val != null))
245 // unset or user updated sequence start position
246 setStartCode(seq.getStart());
249 if (getEndCode()==null || (getEndNum() != seq.getEnd() && getStartCode()!=null && getStartCode().val != null))
251 setEndCode(seq.getEnd());
257 setStartCode(seq.getStart());
258 setEndCode(seq.getEnd());
259 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten
261 // type - decide based on evidence of PDB database references - this also
262 // sets the local reference field
263 int t = 0; // sequence
264 if (seq.getDatasetSequence() != null
265 && seq.getDatasetSequence().getDBRef() != null)
267 jalview.datamodel.DBRefEntry[] dbr = seq.getDatasetSequence()
270 for (i = 0, j = dbr.length; i < j; i++)
274 // JBPNote PDB dbRefEntry needs properties to propagate onto
276 // JBPNote Need to get info from the user about whether the sequence
277 // is the one being modelled, or if it is a template.
278 if (dbr[i].getSource()
279 .equals(jalview.datamodel.DBRefSource.PDB))
281 fields.put(Fields[LOCALID], dbr[i].getAccessionId());
288 fields.put(Fields[TYPE], seqTypes[t]);
294 * Indicate if fields parsed to a modeller-like colon-separated value line
298 boolean isModellerFieldset()
300 return (fields.containsKey(Fields[TYPE]));
303 String getDescriptionLine()
306 int lastfield = Fields.length - 1;
308 if (isModellerFieldset())
311 // try to write a minimal modeller field set, so..
313 // find the last valid field in the entry
315 for (; lastfield > 6; lastfield--)
317 if (fields.containsKey(Fields[lastfield]))
323 for (int i = 0; i < lastfield; i++)
325 value = (String) fields.get(Fields[i]);
326 if (value != null && value.length() > 0)
328 desc += ((String) fields.get(Fields[i])) + ":";
332 desc += Padding[i] + ":";
336 // just return the last field if no others were defined.
337 if (fields.containsKey(Fields[lastfield]))
339 desc += (String) fields.get(Fields[lastfield]);
351 resCode val = getStartCode();
352 if (val!=null && val.val != null)
354 return val.val.intValue();
359 resCode getStartCode()
361 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
363 return (resCode) fields.get(Fields[START] + "num");
370 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
372 return (resCode) fields.get(Fields[END] + "num");
380 resCode val = getEndCode();
381 if (val!=null && val.val != null)
383 return val.val.intValue();
389 * returns true if sequence object was modifed with a valid modellerField set
395 boolean updateSequenceI(SequenceI newSeq)
397 if (isModellerFieldset())
399 resCode rc=getStartCode();
400 if (rc!=null && rc.val != null)
402 newSeq.setStart(getStartNum());
409 if (rc!=null && rc.val != null)
411 newSeq.setEnd(getEndNum());
415 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());