f20cd31e4b44c6a4d1a98500724eaa43ad8ce139
[jalview.git] / src / jalview / structure / AtomSpec.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.structure;
22
23 /**
24  * Java bean representing an atom in a PDB (or similar) structure model or
25  * viewer
26  * 
27  * @author gmcarstairs
28  *
29  */
30 public class AtomSpec
31 {
32   int modelNo;
33
34   private String pdbFile;
35
36   private String chain;
37
38   private int pdbResNum;
39
40   private int atomIndex;
41
42   /**
43    * Parses a Chimera atomspec e.g. #1:12.A to construct an AtomSpec model (with
44    * null pdb file name)
45    * 
46    * @param spec
47    * @return
48    * @throw IllegalArgumentException if the spec cannot be parsed, or represents
49    *        more than one residue
50    */
51   public static AtomSpec fromChimeraAtomspec(String spec)
52   {
53     int colonPos = spec.indexOf(":");
54     if (colonPos == -1)
55     {
56       throw new IllegalArgumentException(spec);
57     }
58
59     int hashPos = spec.indexOf("#");
60     if (hashPos == -1 && colonPos != 0)
61     {
62       // # is missing but something precedes : - reject
63       throw new IllegalArgumentException(spec);
64     }
65
66     String modelSubmodel = spec.substring(hashPos + 1, colonPos);
67     int dotPos = modelSubmodel.indexOf(".");
68     int modelId = 0;
69     try
70     {
71       modelId = Integer.valueOf(dotPos == -1 ? modelSubmodel
72               : modelSubmodel.substring(0, dotPos));
73     } catch (NumberFormatException e)
74     {
75       // ignore, default to model 0
76     }
77
78     String residueChain = spec.substring(colonPos + 1);
79     dotPos = residueChain.indexOf(".");
80     int resNum = 0;
81     try
82     {
83       resNum = Integer.parseInt(dotPos == -1 ? residueChain
84               : residueChain.substring(0, dotPos));
85     } catch (NumberFormatException e)
86     {
87       // could be a range e.g. #1:4-7.B
88       throw new IllegalArgumentException(spec);
89     }
90
91     String chainId = dotPos == -1 ? "" : residueChain.substring(dotPos + 1);
92
93     return new AtomSpec(modelId, chainId, resNum, 0);
94   }
95
96   /**
97    * Constructor
98    * 
99    * @param pdbFile
100    * @param chain
101    * @param resNo
102    * @param atomNo
103    */
104   public AtomSpec(String pdbFile, String chain, int resNo, int atomNo)
105   {
106     this.pdbFile = pdbFile;
107     this.chain = chain;
108     this.pdbResNum = resNo;
109     this.atomIndex = atomNo;
110   }
111
112   /**
113    * Constructor
114    * 
115    * @param modelId
116    * @param chainId
117    * @param resNo
118    * @param atomNo
119    */
120   public AtomSpec(int modelId, String chainId, int resNo, int atomNo)
121   {
122     this.modelNo = modelId;
123     this.chain = chainId;
124     this.pdbResNum = resNo;
125     this.atomIndex = atomNo;
126   }
127
128   public String getPdbFile()
129   {
130     return pdbFile;
131   }
132
133   public String getChain()
134   {
135     return chain;
136   }
137
138   public int getPdbResNum()
139   {
140     return pdbResNum;
141   }
142
143   public int getAtomIndex()
144   {
145     return atomIndex;
146   }
147
148   public int getModelNumber()
149   {
150     return modelNo;
151   }
152
153   public void setPdbFile(String file)
154   {
155     pdbFile = file;
156   }
157
158   @Override
159   public String toString()
160   {
161     return "pdbFile: " + pdbFile + ", chain: " + chain + ", res: "
162             + pdbResNum + ", atom: " + atomIndex;
163   }
164 }