ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/reconstruct.java
Revision: 347
Committed: Thu Oct 11 08:54:50 2007 UTC (16 years, 11 months ago) by duarte
File size: 10446 byte(s)
Log Message:
Writing report to file instead of standard output
Catching unknown errors in runDistgeom (all other non-zero exit values)
Line User Rev File contents
1 duarte 338 import gnu.getopt.Getopt;
2    
3     import java.io.File;
4     import java.io.FileNotFoundException;
5 duarte 347 import java.io.FileOutputStream;
6 duarte 338 import java.io.IOException;
7 duarte 347 import java.io.PrintWriter;
8 duarte 338 import java.sql.SQLException;
9     import java.util.Formatter;
10    
11 duarte 346 import proteinstructure.AAinfo;
12 duarte 338 import proteinstructure.ConformationsNotSameSizeError;
13     import proteinstructure.Graph;
14     import proteinstructure.Pdb;
15     import proteinstructure.PdbChainCodeNotFoundError;
16     import proteinstructure.PdbCodeNotFoundError;
17     import proteinstructure.PdbaseInconsistencyError;
18     import proteinstructure.PdbasePdb;
19     import proteinstructure.PdbfileFormatError;
20     import proteinstructure.PdbfilePdb;
21    
22     import tinker.ConstraintsMaker;
23     import tinker.TinkerError;
24     import tinker.TinkerRunner;
25    
26    
27     public class reconstruct {
28    
29    
30     private static final String TINKERBINDIR = "/project/StruPPi/Software/tinker/bin";
31     private static final String PRMFILE = "/project/StruPPi/Software/tinker/amber/amber99.prm";
32     private static final String PRMTYPE = "amber";
33    
34     public static void main(String[] args) {
35    
36     String programName = reconstruct.class.getName();
37     String help = "Usage:\n" +
38     programName+" -p <pdb code> -c <pdb chain code> -t <contact_type> [-r] -d <distance cutoff 1> -D <distance cutoff 2> -i <distance cutoff 3> -b <base name> -o <output dir> [-n <number of models>]\n";
39    
40     String pdbCode = "";
41     String pdbChainCode = "";
42     String ct = "";
43     double cutoff1 = 0.0;
44     double cutoff2 = 0.0;
45     double cutoff3 = 0.0;
46     String outputDir = "";
47     String baseName = "";
48     boolean cross = false;
49     int n = 1;
50    
51     Getopt g = new Getopt(programName, args, "p:c:d:t:rb:o:d:D:i:n:h?");
52     int c;
53     while ((c = g.getopt()) != -1) {
54     switch(c){
55     case 'p':
56     pdbCode = g.getOptarg();
57     break;
58     case 'c':
59     pdbChainCode = g.getOptarg();
60     break;
61     case 't':
62     ct = g.getOptarg();
63     break;
64     case 'r':
65     cross = true;
66     break;
67     case 'd':
68     cutoff1 = Double.valueOf(g.getOptarg());
69     break;
70     case 'D':
71     cutoff2 = Double.valueOf(g.getOptarg());
72     break;
73     case 'i':
74     cutoff3 = Double.valueOf(g.getOptarg());
75     break;
76     case 'b':
77     baseName = g.getOptarg();
78     break;
79     case 'o':
80     outputDir = g.getOptarg();
81     break;
82     case 'n':
83     n = Integer.parseInt(g.getOptarg());
84     break;
85     case 'h':
86     case '?':
87     System.out.println(help);
88     System.exit(0);
89     break; // getopt() already printed an error
90     }
91     }
92    
93     if (pdbCode.equals("") || pdbChainCode.equals("") || ct.equals("") || cutoff1==0.0 || outputDir.equals("") || baseName.equals("")){
94     System.err.println("Must specify at least -p, -c, -t, -d, -o and -b");
95     System.err.println(help);
96     System.exit(1);
97     }
98    
99 duarte 346 if (baseName.contains(".")) {
100     System.err.println("Basename can't contain a dot (not allowed by tinker). Exiting");
101     System.exit(1);
102     }
103    
104     if (!AAinfo.isValidContactType(ct) || ct.contains("ALL")) {
105     System.err.println("Invalid contact type specified. Exiting");
106     System.exit(1);
107     }
108    
109     if (n>999) {
110     System.err.println("Maximum number of models is 999. Specify a lower value. Exiting");
111     System.exit(1);
112     }
113    
114 duarte 338 boolean doublecm = false;
115     String ct1 = ct;
116     String ct2 = ct;
117     String ct3 = null;
118     if (ct.contains("_")) {
119     ct1 = ct.split("_")[0];
120     ct2 = ct.split("_")[1];
121     doublecm = true;
122     }
123     if (cross) {
124     ct3 = ct1+"/"+ct2;
125     }
126    
127     if (cutoff2==0.0) {
128     cutoff2 = cutoff1;
129     }
130     if (cutoff3==0.0) {
131     cutoff3 = cutoff1;
132     }
133    
134    
135     Pdb pdb = null;
136     try {
137     pdb = new PdbasePdb(pdbCode,pdbChainCode);
138     } catch (PdbaseInconsistencyError e) {
139     System.err.println("Pdbase inconsistency for structure "+pdbCode+". Can't continue, exiting");
140     System.exit(1);
141     } catch (PdbCodeNotFoundError e) {
142     System.err.println("Given pdb code "+pdbCode+" couldn't be found in pdbase. Exiting");
143     System.exit(1);
144     } catch (SQLException e) {
145     System.err.println("Problems connecting to database for getting pdb data for "+pdbCode+". Exiting");
146     System.exit(1);
147     } catch (PdbChainCodeNotFoundError e) {
148     System.err.println("Given pdb chain code "+pdbChainCode+" couldn't be found for pdb code "+pdbCode+". Exiting");
149     System.exit(1);
150     }
151     String sequence = pdb.getSequence();
152    
153     Graph graph1 = pdb.get_graph(ct1, cutoff1);
154     Graph graph2 = null;
155     Graph graph3 = null;
156     if (doublecm) {
157     graph2 = pdb.get_graph(ct2, cutoff2);
158     }
159     if (cross) {
160     graph3 = pdb.get_graph(ct3, cutoff3);
161     }
162    
163    
164     // defining files
165     File logFile = new File(outputDir,"tinker.log");
166     File prmFile = new File(PRMFILE);
167     File xyzFile = new File(outputDir,baseName+".xyz");
168     File seqFile = new File(outputDir,baseName+".seq");
169     File pdbFile = new File(outputDir,baseName+".pdb");
170     File keyFile = new File(outputDir,baseName+".key");
171 duarte 347 File reportFile = new File(outputDir,baseName+".report");
172 duarte 338
173     // creating TinkerRunner object
174     TinkerRunner tr = null;
175     try {
176     tr = new TinkerRunner(TINKERBINDIR,PRMFILE,logFile);
177     } catch (FileNotFoundException e3) {
178     System.err.println("Couldn't find tinker bin dir "+TINKERBINDIR+". Exiting");
179     System.exit(1);
180     }
181    
182     // 1. run tinker's protein program
183     try {
184     tr.runProtein(sequence, outputDir, baseName );
185     } catch (IOException e2) {
186     System.err.println("Couldn't read file to run 'protein' "+seqFile.getAbsolutePath());
187     System.err.println("Exiting");
188     System.exit(1);
189     } catch (TinkerError e2) {
190     System.err.println("Tinker error while running 'protein', check log file "+logFile.getAbsolutePath()+". Exiting");
191     System.exit(1);
192     }
193    
194     // 1a. convert xyz file to pdb to be able to map atom serials after
195     try {
196     tr.runXyzpdb(xyzFile, seqFile, pdbFile);
197     } catch (IOException e1) {
198     System.err.println("Couldn't read files "+xyzFile.getAbsolutePath()+" or "+seqFile.getAbsolutePath()+" or write to "+pdbFile.getAbsolutePath()+" for running 'xyzpdb'");
199     System.err.println("Exiting");
200     System.exit(1);
201     } catch (TinkerError e1) {
202     System.err.println("Tinker error while running xyzpdb, check log file "+logFile.getAbsolutePath()+". Exiting");
203     System.exit(1);
204     }
205    
206     // 2. creating constraints into key file
207     ConstraintsMaker cm = null;
208     try {
209     cm = new ConstraintsMaker(pdbFile,xyzFile,prmFile,PRMTYPE,keyFile);
210     } catch (IOException e3) {
211     System.err.println("Couldn't read files "+xyzFile.getAbsolutePath()+", "+pdbFile.getAbsolutePath()+" or, "+prmFile.getAbsolutePath()+" write to "+keyFile.getAbsolutePath()+" for creating distance constraints");
212     System.err.println("Exiting");
213     System.exit(1);
214     } catch (PdbfileFormatError e3) {
215     System.err.println("pdb file "+pdbFile.getAbsolutePath()+" converted from "+xyzFile.getAbsolutePath()+" doesn't seem to be in the right format. Check log? ("+logFile.getAbsolutePath()+"). Exiting");
216     System.exit(1);
217     }
218 duarte 346
219     cm.createConstraints(graph1);
220     if (doublecm) cm.createConstraints(graph2);
221     if (cross) cm.createConstraints(graph3);
222    
223 duarte 338 cm.closeKeyFile();
224    
225     // 3. run tinker's distgeom
226     try {
227     System.out.println("Running distgeom...");
228     tr.runDistgeom(xyzFile, outputDir, baseName, n);
229     } catch (TinkerError e1) {
230 duarte 340 System.err.println(e1.getMessage());
231     System.err.println("Exiting");
232 duarte 338 System.exit(1);
233     } catch (IOException e1) {
234     System.err.println("Couldn't read files "+xyzFile.getAbsolutePath()+" or write 'distgeom' output files to output dir "+outputDir);
235     System.err.println("Exiting");
236     System.exit(1);
237 stehr 344 } catch(InterruptedException e) {
238     System.err.println("Distgeom was interrupted:" + e.getMessage());
239     System.err.println("Exiting.");
240     System.exit(1);
241 duarte 338 }
242    
243     double[] mubv = tr.getMaxUpperBoundViol();
244     double[] mlbv = tr.getMaxLowerBoundViol();
245     double[] muv = tr.getMaxUpperViol();
246     double[] mlv = tr.getMaxLowerViol();
247     int[] nubv = tr.getNumUpperBoundViol();
248     int[] nlbv = tr.getNumLowerBoundViol();
249     int[] nuv = tr.getNumUpperViol();
250     int[] nlv = tr.getNumLowerViol();
251     double[] rbv = tr.getRmsBoundViol();
252     double[] rrv = tr.getRmsRestViol();
253    
254    
255     // 4. converting xyz output files to pdb files and calculating rmsds
256    
257     double[] rmsds = new double[n+1];
258    
259 duarte 347 for (int i = 1; i<=n; i++) {
260     String ext = new Formatter().format(".%03d",i).toString();
261     File outputXyzFile = new File(outputDir, baseName+ext);
262     File outputPdbFile = new File(outputDir, baseName+ext+".pdb");
263     try {
264     tr.runXyzpdb(outputXyzFile, seqFile, outputPdbFile);
265 duarte 338
266 duarte 347 Pdb outputPdb = new PdbfilePdb(outputPdbFile.getAbsolutePath(),"NULL");
267     rmsds[i] = pdb.rmsd(outputPdb, "Ca");
268 duarte 338
269 duarte 347 } catch (IOException e) {
270     System.err.println("Couldn't read file "+outputXyzFile.getAbsolutePath()+", or "+seqFile.getAbsolutePath()+", or write to "+outputPdbFile.getAbsolutePath()+" while converting with 'xyzpdb'");
271     System.err.println("Can't calculate rmsd for it");
272     } catch (TinkerError e) {
273     System.err.println("Tinker error while running 'xyzpdb' to convert"+outputXyzFile.getAbsolutePath()+", check log file "+logFile.getAbsolutePath());
274     System.err.println("Can't calculate rmsd for it");
275     }
276     catch (PdbfileFormatError e) {
277     System.err.println("Output pdb file "+outputPdbFile.getAbsolutePath()+" doesn't seem to be in the correcet format. Can't calculate rmsd for it");
278     } catch (PdbChainCodeNotFoundError e) {
279     // this shouldn't happen, chain code is hard coded, we throw stack trace and continue if it happens
280     e.printStackTrace();
281     } catch (ConformationsNotSameSizeError e) {
282     System.err.println(pdbFile.getAbsolutePath()+" and "+outputPdbFile.getAbsolutePath()+" don't have the same conformation size, can't calculate rmsd for them.");
283     }
284    
285     tr.closeLog();
286     }
287    
288 duarte 338
289     // 6. report
290 duarte 347 try {
291     PrintWriter reportOut = new PrintWriter(new FileOutputStream(reportFile));
292     reportOut.println("accession_code\tchain_pdb_code\tcutoff\tcutoff2\tcutoff3\tct\tct2\tct3" +
293     "\tresult_id\tup_bound_viol\tlow_bound_viol\tmax_bound_up\tmax_bound_low\trms_bound" +
294     "\tup_viol\tlow_viol\tmax_up\tmax_low\trms_viol\trmsd_to_orig");
295    
296     for (int i=1;i<=n;i++){
297     reportOut.println(pdbCode+"\t"+pdbChainCode+"\t"+cutoff1+"\t"+cutoff2+"\t"+cutoff3+"\t"+ct1+"\t"+ct2+"\t"+ct3+"\t"+
298     i+"\t"+nubv[i]+"\t"+nlbv[i]+"\t"+mubv[i]+"\t"+mlbv[i]+"\t"+muv[i]+"\t"+mlv[i]+"\t"+rbv[i]+"\t"+
299     nuv[i]+"\t"+nlv[i]+"\t"+rrv[i]+"\t"+rmsds[i]);
300     }
301     reportOut.close();
302     } catch (FileNotFoundException e) {
303     System.err.println("Couldn't write to report file "+reportFile.getAbsolutePath() +". Error: "+e.getMessage());
304 duarte 338 }
305    
306     }
307    
308     }