X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fstructure%2FAtomSpec.java;fp=src%2Fjalview%2Fstructure%2FAtomSpec.java;h=f404d35b915ebe5e5c9e075b6f50c2dffc166b72;hb=304e64fb34b32659be1bbfd39fb4e15b2f79586e;hp=f20cd31e4b44c6a4d1a98500724eaa43ad8ce139;hpb=cfb79b69d9fa44595560659bd95d1d1cd27677ad;p=jalview.git diff --git a/src/jalview/structure/AtomSpec.java b/src/jalview/structure/AtomSpec.java index f20cd31..f404d35 100644 --- a/src/jalview/structure/AtomSpec.java +++ b/src/jalview/structure/AtomSpec.java @@ -43,52 +43,64 @@ public class AtomSpec * Parses a Chimera atomspec e.g. #1:12.A to construct an AtomSpec model (with * null pdb file name) * + *
+   * Chimera format: 
+   *    #1.2:12-20.A     model 1, submodel 2, chain A, atoms 12-20
+   * 
+ * * @param spec * @return * @throw IllegalArgumentException if the spec cannot be parsed, or represents * more than one residue + * @see https://www.cgl.ucsf.edu/chimera/current/docs/UsersGuide/midas/frameatom_spec.html */ public static AtomSpec fromChimeraAtomspec(String spec) { - int colonPos = spec.indexOf(":"); - if (colonPos == -1) + int modelSeparatorPos = spec.indexOf(":"); + if (modelSeparatorPos == -1) { throw new IllegalArgumentException(spec); } int hashPos = spec.indexOf("#"); - if (hashPos == -1 && colonPos != 0) + if (hashPos == -1 && modelSeparatorPos != 0) { // # is missing but something precedes : - reject throw new IllegalArgumentException(spec); } - String modelSubmodel = spec.substring(hashPos + 1, colonPos); - int dotPos = modelSubmodel.indexOf("."); + String modelSubmodel = spec.substring(hashPos + 1, modelSeparatorPos); int modelId = 0; try { - modelId = Integer.valueOf(dotPos == -1 ? modelSubmodel - : modelSubmodel.substring(0, dotPos)); + int subModelPos = modelSubmodel.indexOf("."); + modelId = Integer.valueOf( + subModelPos > 0 ? modelSubmodel.substring(0, subModelPos) + : modelSubmodel); } catch (NumberFormatException e) { // ignore, default to model 0 } - String residueChain = spec.substring(colonPos + 1); - dotPos = residueChain.indexOf("."); + /* + * now process what follows the model, either + * Chimera: atoms.chain + * ChimeraX: chain:atoms + */ + String atomsAndChain = spec.substring(modelSeparatorPos + 1); + String[] tokens = atomsAndChain.split("\\."); + String atoms = tokens.length == 1 ? atomsAndChain : (tokens[0]); int resNum = 0; try { - resNum = Integer.parseInt(dotPos == -1 ? residueChain - : residueChain.substring(0, dotPos)); + resNum = Integer.parseInt(atoms); } catch (NumberFormatException e) { // could be a range e.g. #1:4-7.B throw new IllegalArgumentException(spec); } - String chainId = dotPos == -1 ? "" : residueChain.substring(dotPos + 1); + String chainId = tokens.length == 1 ? "" : (tokens[1]); return new AtomSpec(modelId, chainId, resNum, 0); } @@ -161,4 +173,70 @@ public class AtomSpec return "pdbFile: " + pdbFile + ", chain: " + chain + ", res: " + pdbResNum + ", atom: " + atomIndex; } + + /** + * Parses a ChimeraX atomspec to construct an AtomSpec model (with + * null pdb file name) + * + *
+   * ChimeraX format:
+   *    #1.2/A:12-20     model 1, submodel 2, chain A, atoms 12-20
+   * 
+ * + * @param spec + * @return + * @throw IllegalArgumentException if the spec cannot be parsed, or represents + * more than one residue + * @see http://rbvi.ucsf.edu/chimerax/docs/user/commands/atomspec.html + */ + public static AtomSpec fromChimeraXAtomspec(String spec) + { + int modelSeparatorPos = spec.indexOf("/"); + if (modelSeparatorPos == -1) + { + throw new IllegalArgumentException(spec); + } + + int hashPos = spec.indexOf("#"); + if (hashPos == -1 && modelSeparatorPos != 0) + { + // # is missing but something precedes : - reject + throw new IllegalArgumentException(spec); + } + + String modelSubmodel = spec.substring(hashPos + 1, modelSeparatorPos); + int modelId = 0; + try + { + int subModelPos = modelSubmodel.indexOf("."); + modelId = Integer.valueOf( + subModelPos > 0 ? modelSubmodel.substring(0, subModelPos) + : modelSubmodel); + } catch (NumberFormatException e) + { + // ignore, default to model 0 + } + + /* + * now process what follows the model, either + * Chimera: atoms.chain + * ChimeraX: chain:atoms + */ + String atomsAndChain = spec.substring(modelSeparatorPos + 1); + String[] tokens = atomsAndChain.split("\\:"); + String atoms = tokens.length == 1 ? atomsAndChain : (tokens[1]); + int resNum = 0; + try + { + resNum = Integer.parseInt(atoms); + } catch (NumberFormatException e) + { + // could be a range e.g. #1:4-7.B + throw new IllegalArgumentException(spec); + } + + String chainId = tokens.length == 1 ? "" : (tokens[0]); + + return new AtomSpec(modelId, chainId, resNum, 0); + } }