2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
21 import jalview.datamodel.SequenceI;
\r
22 import java.util.Vector;
\r
23 public class ModellerDescription
\r
26 * Translates between a String containing a set of colon-separated values
\r
27 * on a single line, and sequence start/end and other properties.
\r
28 * See PIRFile IO for its use.
\r
30 final String[] seqTypes =
\r
32 "sequence", "structure", "structureX", "structureN"};
\r
33 final String[] Fields =
\r
35 "objectType", "objectId",
\r
36 "startField", "startCode",
\r
37 "endField", "endCode",
\r
38 "description1", "description2",
\r
39 "resolutionField", "tailField"};
\r
41 final int LOCALID = 1;
\r
42 final int START = 2;
\r
43 final int START_CHAIN = 3;
\r
45 final int END_CHAIN = 5;
\r
46 final int DESCRIPTION1 = 6;
\r
47 final int DESCRIPTION2 = 7;
\r
48 final int RESOLUTION = 8;
\r
52 * 0 is free text or empty
\r
53 * 1 is something that parses to an integer, or \@
\r
57 0, 0, 1, 0, 1, 0, 0, 0, 0, 0
\r
59 final char Padding[] =
\r
61 ' ', ' ', ' ', '.', ' ', '.', '.', '.', '.', '.'
\r
64 java.util.Hashtable fields = new java.util.Hashtable();
\r
65 ModellerDescription()
\r
67 fields.put(Fields[TAIL], "");
\r
74 resCode(String f, Integer v)
\r
82 val = new Integer(v);
\r
83 field = val.toString();
\r
87 private resCode validResidueCode(String field)
\r
90 com.stevesoft.pat.Regex r = new com.stevesoft.pat.Regex("\\s*((([-0-9]+).?)|FIRST|LAST|@)");
\r
92 if (!r.search(field))
\r
94 return null; // invalid
\r
96 String value = r.stringMatched(3);
\r
99 value = r.stringMatched(1);
\r
101 // jalview.bin.Cache.log.debug("from '" + field + "' matched '" + value +
\r
105 val = Integer.valueOf(value);
\r
106 return new resCode(field, val); // successful numeric extraction
\r
108 catch (Exception e)
\r
111 return new resCode(field, null);
\r
114 private java.util.Hashtable parseDescription(String desc)
\r
116 java.util.Hashtable fields = new java.util.Hashtable();
\r
117 java.util.StringTokenizer st = new java.util.StringTokenizer(desc, ":");
\r
120 if (st.countTokens() > 0)
\r
122 // parse colon-fields
\r
124 field = st.nextToken(":");
\r
127 if (seqTypes[i].equalsIgnoreCase(field) )
\r
132 while (++i < seqTypes.length);
\r
134 if (i < seqTypes.length)
\r
136 // valid seqType for modeller
\r
138 i = 1; // continue parsing fields
\r
139 while (i < TAIL && st.hasMoreTokens())
\r
141 if ( (field = st.nextToken(":")) != null)
\r
143 // validate residue field value
\r
146 resCode val = validResidueCode(field);
\r
149 fields.put(new String(Fields[i] + "num"), val);
\r
153 // jalview.bin.Cache.log.debug(
\r
154 // "Ignoring non-Modeller description: invalid integer-like field '" + field + "'");
\r
155 type = -1; /* invalid field! - throw the FieldSet away */
\r
159 fields.put(Fields[i++], field);
\r
164 // slurp remaining fields
\r
165 while (st.hasMoreTokens())
\r
167 field += ":" + st.nextToken(":");
\r
169 fields.put(Fields[TAIL], field);
\r
175 // object is not a proper ModellerPIR object
\r
176 fields = new java.util.Hashtable();
\r
177 fields.put(Fields[TAIL], new String(desc));
\r
181 fields.put(Fields[TYPE], seqTypes[type]);
\r
186 ModellerDescription(String desc)
\r
192 fields = parseDescription(desc);
\r
195 void setStartCode(int v)
\r
198 fields.put(Fields[START] + "num", r = new resCode(v));
\r
199 fields.put(Fields[START], r.field);
\r
202 void setEndCode(int v)
\r
205 fields.put(Fields[END] + "num", r = new resCode(v));
\r
206 fields.put(Fields[END], r.field);
\r
210 * make a possibly updated modeller field line for the sequence object
\r
211 * @param seq SequenceI
\r
213 ModellerDescription(SequenceI seq)
\r
216 if (seq.getDescription() != null)
\r
218 fields = parseDescription(seq.getDescription());
\r
221 if (isModellerFieldset())
\r
223 // Set start and end before we update the type (in the case of a synthesized field set)
\r
224 if (getStartNum() != seq.getStart() && getStartCode().val != null)
\r
226 setStartCode(seq.getStart());
\r
229 if (getEndNum() != seq.getEnd() && getStartCode().val != null)
\r
231 setEndCode(seq.getEnd());
\r
236 // synthesize fields
\r
237 setStartCode(seq.getStart());
\r
238 setEndCode(seq.getEnd());
\r
239 fields.put(Fields[LOCALID], seq.getName()); // this may be overwritten below...
\r
240 // type - decide based on evidence of PDB database references - this also sets the local reference field
\r
241 int t = 0; // sequence
\r
242 if (seq.getDatasetSequence() != null &&
\r
243 seq.getDatasetSequence().getDBRef() != null)
\r
245 jalview.datamodel.DBRefEntry [] dbr = seq.getDatasetSequence().getDBRef();
\r
247 for (i = 0, j = dbr.length; i < j; i++)
\r
249 if (dbr[i] != null)
\r
251 // JBPNote PDB dbRefEntry needs properties to propagate onto ModellerField
\r
252 // JBPNote Need to get info from the user about whether the sequence is the one being modelled, or if it is a template.
\r
253 if (dbr[i].getSource().equals(jalview.datamodel.DBRefSource.PDB))
\r
255 fields.put(Fields[LOCALID], dbr[i].getAccessionId());
\r
262 fields.put(Fields[TYPE], seqTypes[t]);
\r
268 * Indicate if fields parsed to a modeller-like colon-separated value line
\r
271 boolean isModellerFieldset()
\r
273 return (fields.containsKey(Fields[TYPE]));
\r
276 String getDescriptionLine()
\r
279 int lastfield = Fields.length - 1;
\r
281 if (isModellerFieldset())
\r
284 // try to write a minimal modeller field set, so..
\r
286 // find the last valid field in the entry
\r
288 for (; lastfield > 6; lastfield--)
\r
290 if (fields.containsKey(Fields[lastfield]))
\r
296 for (int i = 0; i < lastfield; i++)
\r
298 value = (String) fields.get(Fields[i]);
\r
299 if (value != null && value.length() > 0)
\r
301 desc += ( (String) fields.get(Fields[i])) + ":";
\r
305 desc += Padding[i] + ":";
\r
309 // just return the last field if no others were defined.
\r
310 if (fields.containsKey(Fields[lastfield]))
\r
312 desc += (String) fields.get(Fields[lastfield]);
\r
324 resCode val = getStartCode();
\r
325 if (val.val != null)
\r
327 return val.val.intValue();
\r
332 resCode getStartCode()
\r
334 if (isModellerFieldset() && fields.containsKey(Fields[START] + "num"))
\r
336 return (resCode) fields.get(Fields[START] + "num");
\r
341 resCode getEndCode()
\r
343 if (isModellerFieldset() && fields.containsKey(Fields[END] + "num"))
\r
345 return (resCode) fields.get(Fields[END] + "num");
\r
353 resCode val = getEndCode();
\r
354 if (val.val != null)
\r
356 return val.val.intValue();
\r
362 * returns true if sequence object was modifed with a valid modellerField set
\r
363 * @param newSeq SequenceI
\r
366 boolean updateSequenceI(SequenceI newSeq)
\r
368 if (isModellerFieldset())
\r
370 if (getStartCode().val != null)
\r
372 newSeq.setStart(getStartNum());
\r
376 newSeq.setStart(1);
\r
378 if (getEndCode().val != null)
\r
380 newSeq.setEnd(getEndNum());
\r
384 newSeq.setEnd(newSeq.getStart() + newSeq.getLength());
\r