ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/PdbfilePdb.java
Revision: 226
Committed: Fri Jul 6 13:43:30 2007 UTC (17 years, 2 months ago) by duarte
File size: 10170 byte(s)
Log Message:
First geometric hashing implementation for calculating graph (with new class Box). Only does non-crossed cts
Geometrich hashing graph calculation kept in a separate method than normal get_graph. Still either of the methods can be used.
Using javax.vectormath objects for the vectors (distance method also from that)
Using TreeMap for get_coords_for_ct
Line User Rev File contents
1 duarte 207 package proteinstructure;
2    
3     import java.io.BufferedReader;
4     import java.io.File;
5     import java.io.FileNotFoundException;
6     import java.io.FileReader;
7     import java.io.IOException;
8     import java.util.ArrayList;
9     import java.util.Collections;
10     import java.util.HashMap;
11 duarte 222 import java.util.TreeMap;
12 duarte 207 import java.util.regex.Matcher;
13     import java.util.regex.Pattern;
14    
15 duarte 226 import javax.vecmath.Point3d;
16    
17 duarte 207 public class PdbfilePdb extends Pdb {
18    
19 duarte 208 private static final String UNKNOWN_STRING ="Unknown";
20     private static final String NULL_chainCode = "A";
21    
22 duarte 207 private String pdbfile;
23    
24     /**
25     * Constructs Pdb object reading from pdb file given pdb chain code. Model will be DEFAULT_MODEL
26     * @param pdbfile
27     * @param pdbChainCode
28     * @throws FileNotFoundException
29     * @throws IOException
30 duarte 208 * @throws PdbfileFormatError
31 stehr 215 * @throws PdbChainCodeNotFoundError
32 duarte 207 */
33 stehr 215 public PdbfilePdb (String pdbfile, String pdbChainCode) throws FileNotFoundException, IOException, PdbfileFormatError, PdbChainCodeNotFoundError {
34 duarte 207 this(pdbfile,pdbChainCode,DEFAULT_MODEL);
35     }
36    
37     /**
38     * Constructs Pdb object reading from pdb file given pdb chain code and model serial
39     * @param pdbfile
40     * @param pdbChainCode
41     * @param model_serial
42     * @throws FileNotFoundException
43     * @throws IOException
44 duarte 208 * @throws PdbfileFormatError
45 stehr 215 * @throws PdbChainCodeNotFoundError
46 duarte 207 */
47 stehr 215 public PdbfilePdb (String pdbfile, String pdbChainCode, int model_serial) throws FileNotFoundException, IOException, PdbfileFormatError, PdbChainCodeNotFoundError {
48 duarte 207 this.pdbfile = pdbfile;
49     this.model=model_serial;
50 duarte 208 this.pdbCode=UNKNOWN_STRING; // we initialise to unknown in case we don't find it in pdb file
51 stehr 217 this.pdbChainCode=pdbChainCode.toUpperCase(); // our convention: chain codes are upper case
52 duarte 208 // we set chainCode to pdbChainCode except for case NULL where we use "A"
53     this.chainCode=pdbChainCode;
54     if (pdbChainCode.equals("NULL")) this.chainCode=NULL_chainCode;
55    
56     this.sequence=""; // we initialise it to empty string, then is set inread_pdb_data_from_file
57    
58 duarte 222 // we initialise the resser2secstruct and secstruct2resinterval Maps to empty, if no sec structure info is found then it remains empty
59 duarte 219 this.resser2secstruct = new HashMap<Integer, String>();
60 duarte 222 this.secstruct2resinterval = new TreeMap<String, Interval>();
61 duarte 219
62 duarte 207 read_pdb_data_from_file();
63 duarte 208
64     // when reading from pdb file we have no information of residue numbers or author's (original) pdb residue number, so we fill the mapping with the residue numbers we know
65     //TODO eventually we could assign our own internal residue numbers when reading from pdb and thus this map would be used
66     this.resser2pdbresser = new HashMap<Integer, String>();
67     this.pdbresser2resser = new HashMap<String, Integer>();
68     for (int resser:resser2restype.keySet()){
69     resser2pdbresser.put(resser, String.valueOf(resser));
70     pdbresser2resser.put(String.valueOf(resser), resser);
71     }
72 duarte 219
73 duarte 207 }
74    
75    
76    
77     /**
78 duarte 219 * To read the pdb data (atom coordinates, residue serials, atom serials) from file.
79     * chainCode gets set to same as pdbChainCode, except if input chain code NULL then chainCode will be 'A'
80 duarte 207 * pdbCode gets set to the one parsed in HEADER or to 'Unknown' if not found
81     * sequence gets set to the sequence read from ATOM lines (i.e. observed resdiues only)
82 duarte 219 * No insertion codes are parsed or taken into account at the moment. Thus files with
83     * insertion codes will be incorrectly read
84 duarte 207 * @param pdbfile
85     * @throws FileNotFoundException
86     * @throws IOException
87 duarte 208 * @throws PdbfileFormatError
88 stehr 215 * @throws PdbChainCodeNotFoundError
89 duarte 207 */
90 stehr 215 private void read_pdb_data_from_file() throws FileNotFoundException, IOException, PdbfileFormatError, PdbChainCodeNotFoundError {
91 duarte 207 resser_atom2atomserial = new HashMap<String,Integer>();
92     resser2restype = new HashMap<Integer,String>();
93 duarte 226 atomser2coord = new HashMap<Integer,Point3d>();
94 duarte 207 atomser2resser = new HashMap<Integer,Integer>();
95     boolean empty = true; // controls whether we don't find any atom line for given pdbChainCode and model
96 duarte 208 // we set chainCodeStr (for regex) to pdbChainCode except for case NULL where we use " " (NULL is a blank chain code in pdb files)
97     String chainCodeStr=pdbChainCode;
98     if (pdbChainCode.equals("NULL")) chainCodeStr=" ";
99    
100 duarte 207 int thismodel=DEFAULT_MODEL; // we initialise to DEFAULT_MODEL, in case file doesn't have MODEL lines
101     BufferedReader fpdb = new BufferedReader(new FileReader(new File(pdbfile)));
102 duarte 208 int linecount=0;
103 duarte 207 String line;
104     while ((line = fpdb.readLine() ) != null ) {
105 duarte 208 linecount++;
106 duarte 219 // HEADER
107 duarte 207 Pattern p = Pattern.compile("^HEADER");
108     Matcher m = p.matcher(line);
109     if (m.find()){
110     Pattern ph = Pattern.compile("^HEADER.{56}(\\d\\w{3})");
111     Matcher mh = ph.matcher(line);
112     if (mh.find()) {
113     pdbCode=mh.group(1).toLowerCase();
114     }
115 duarte 208 } else if (linecount==1) { // header not found and we are in line 1
116     // a HEADER is the minimum we ask at the moment for a pdb file to have, if we don't find it in line 1 we throw an exception
117     throw new PdbfileFormatError("The pdb file "+pdbfile+" doesn't seem to have the right format");
118 duarte 207 }
119 duarte 219 // SECONDARY STRUCTURE
120     // helix
121     //HELIX 1 1 LYS A 17 LEU A 26 1
122     // helix ser beg res ser end res ser
123     p = Pattern.compile("^HELIX..(...).{9}"+chainCodeStr+".(....).{6}"+chainCodeStr+".(....)");
124     m = p.matcher(line);
125     if (m.find()){
126     int serial = Integer.valueOf(m.group(1).trim());
127     int beg = Integer.valueOf(m.group(2).trim());
128     int end = Integer.valueOf(m.group(3).trim());
129     String ssId = "H"+serial;
130 duarte 222 secstruct2resinterval.put(ssId, new Interval(beg,end));
131 duarte 219 for (int i=beg;i<=end;i++){
132     if (resser2secstruct.containsKey(i)){// if already assigned we print a warning and then assign it
133     System.err.println("Inconsistency in secondary structure assignment. " +
134     "Residue "+i+" is getting reassigned from "+resser2secstruct.get(i)+" to "+ssId);
135     }
136     resser2secstruct.put(i,ssId);
137     }
138     }
139     // sheet
140     //SHEET 2 A 5 ILE A 96 THR A 99 -1 N LYS A 98 O THR A 107
141     // strand ser sheet id beg res ser end res ser
142     p = Pattern.compile("^SHEET..(...).(...).{7}"+chainCodeStr+"(....).{6}"+chainCodeStr+"(....)");
143     m = p.matcher(line);
144     if (m.find()){
145     int strandSerial = Integer.valueOf(m.group(1).trim());
146     String sheetId = m.group(2).trim();
147     int beg = Integer.valueOf(m.group(3).trim());
148     int end = Integer.valueOf(m.group(4).trim());
149     String ssId = "S"+sheetId+strandSerial;
150 duarte 222 secstruct2resinterval.put(ssId, new Interval(beg,end));
151 duarte 219 for (int i=beg;i<=end;i++){
152     if (resser2secstruct.containsKey(i)){// if already assigned we print a warning and then assign it
153     System.err.println("Inconsistency in secondary structure assignment. " +
154     "Residue "+i+" is getting reassigned from "+resser2secstruct.get(i)+" to "+ssId);
155     }
156     resser2secstruct.put(i,ssId);
157     }
158     }
159     // we've stored the sec structure info in the strands2begEnd and sheets2strands maps.
160     // the assignment to resser2secstruct is done when we reach the ATOM lines, see below
161     // turn
162     //TURN 1 S1A GLY A 16 GLN A 18 SURFACE
163     // turn ser beg res ser end res ser
164     p = Pattern.compile("^TURN...(...).{9}"+chainCodeStr+"(....).{6}"+chainCodeStr+"(....)");
165     m = p.matcher(line);
166     if (m.find()){
167     int serial = Integer.valueOf(m.group(1).trim());
168     int beg = Integer.valueOf(m.group(2).trim());
169     int end = Integer.valueOf(m.group(3).trim());
170     String ssId = "T"+serial;
171 duarte 222 secstruct2resinterval.put(ssId, new Interval(beg,end));
172 duarte 219 for (int i=beg;i<=end;i++){
173     if (resser2secstruct.containsKey(i)){// if already assigned we print a warning and then assign it
174     System.err.println("Inconsistency in secondary structure assignment. " +
175     "Residue "+i+" is getting reassigned from "+resser2secstruct.get(i)+" to "+ssId);
176     }
177     resser2secstruct.put(i,ssId);
178     }
179     }
180     // MODEL
181 duarte 207 p = Pattern.compile("^MODEL\\s+(\\d+)");
182     m = p.matcher(line);
183     if (m.find()){
184     thismodel=Integer.parseInt(m.group(1));
185     }
186     if (thismodel!=model) continue; // we skip reading of atom lines if we are not in the desired model
187 duarte 219 // ATOM
188 duarte 207 p = Pattern.compile("^ATOM");
189     m = p.matcher(line);
190     if (m.find()){
191 duarte 208 // serial atom res_type chain res_ser x y z
192     Pattern pl = Pattern.compile(".{6}(.....).{2}(...).{1}(...).{1}"+chainCodeStr+"(.{4}).{4}(.{8})(.{8})(.{8})",Pattern.CASE_INSENSITIVE);
193 duarte 207 Matcher ml = pl.matcher(line);
194     if (ml.find()) {
195     empty=false;
196     int atomserial=Integer.parseInt(ml.group(1).trim());
197     String atom = ml.group(2).trim();
198     String res_type = ml.group(3).trim();
199     int res_serial = Integer.parseInt(ml.group(4).trim());
200     double x = Double.parseDouble(ml.group(5).trim());
201     double y = Double.parseDouble(ml.group(6).trim());
202     double z = Double.parseDouble(ml.group(7).trim());
203 duarte 226 Point3d coords = new Point3d(x,y,z);
204 duarte 207 ArrayList<String> aalist=AA.aas();
205     if (aalist.contains(res_type)) {
206     atomser2coord.put(atomserial, coords);
207     atomser2resser.put(atomserial, res_serial);
208     resser2restype.put(res_serial, res_type);
209     ArrayList<String> atomlist = aas2atoms.get(res_type);
210     if (atomlist.contains(atom)){
211     resser_atom2atomserial.put(res_serial+"_"+atom, atomserial);
212     }
213     }
214     }
215     }
216     }
217     fpdb.close();
218 stehr 215 if (empty) {
219     //System.err.println("Couldn't find any atom line for given pdbChainCode: "+pdbChainCode+", model: "+model);
220     throw new PdbChainCodeNotFoundError("Couldn't find any ATOM line for given pdbChainCode: "+pdbChainCode+", model: "+model);
221     }
222 duarte 207 // now we read the sequence from the resser2restype HashMap
223     // NOTE: we must make sure elsewhere that there are no unobserved residues, we can't check that here!
224     ArrayList<Integer> ressers = new ArrayList<Integer>();
225     for (int resser:resser2restype.keySet()) {
226     ressers.add(resser);
227     }
228     Collections.sort(ressers);
229     for (int resser:ressers){
230     String oneletter = AA.threeletter2oneletter(resser2restype.get(resser));
231     sequence += oneletter;
232     }
233     }
234 duarte 219
235 duarte 207 }