resolved merge conflict
[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
92             .substring(dotPos + 1);
93     if (chainId.length() == 0)
94     {
95       chainId = " ";
96     }
97     return new AtomSpec(modelId, chainId, resNum, 0);
98   }
99
100   /**
101    * Constructor
102    * 
103    * @param pdbFile
104    * @param chain
105    * @param resNo
106    * @param atomNo
107    */
108   public AtomSpec(String pdbFile, String chain, int resNo, int atomNo)
109   {
110     this.pdbFile = pdbFile;
111     this.chain = chain;
112     this.pdbResNum = resNo;
113     this.atomIndex = atomNo;
114   }
115
116   /**
117    * Constructor
118    * 
119    * @param modelId
120    * @param chainId
121    * @param resNo
122    * @param atomNo
123    */
124   public AtomSpec(int modelId, String chainId, int resNo, int atomNo)
125   {
126     this.modelNo = modelId;
127     this.chain = chainId;
128     this.pdbResNum = resNo;
129     this.atomIndex = atomNo;
130   }
131
132   public String getPdbFile()
133   {
134     return pdbFile;
135   }
136
137   public String getChain()
138   {
139     return chain;
140   }
141
142   public int getPdbResNum()
143   {
144     return pdbResNum;
145   }
146
147   public int getAtomIndex()
148   {
149     return atomIndex;
150   }
151
152   public int getModelNumber()
153   {
154     return modelNo;
155   }
156
157   public void setPdbFile(String file)
158   {
159     pdbFile = file;
160   }
161
162   @Override
163   public String toString()
164   {
165     return "pdbFile: " + pdbFile + ", chain: " + chain + ", res: "
166             + pdbResNum + ", atom: " + atomIndex;
167   }
168 }