ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/genGraph.java
Revision: 285
Committed: Thu Aug 16 15:10:11 2007 UTC (17 years, 1 month ago) by duarte
File size: 6642 byte(s)
Log Message:
Fixed a few bugs in reading of list file
Better output and error messages
Line File contents
1 import gnu.getopt.Getopt;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.sql.SQLException;
8
9
10 import proteinstructure.Graph;
11 import proteinstructure.Pdb;
12 import proteinstructure.PdbChainCodeNotFoundError;
13 import proteinstructure.PdbCodeNotFoundError;
14 import proteinstructure.PdbaseInconsistencyError;
15 import proteinstructure.PdbasePdb;
16 import proteinstructure.PdbfileFormatError;
17 import proteinstructure.PdbfilePdb;
18 import tools.MySQLConnection;
19
20
21 public class genGraph {
22 /*------------------------------ constants ------------------------------*/
23
24 public static final String PDB_DB = "pdbase";
25 public static final String DB_HOST = "white";
26 public static final String DB_USER = getUserName();
27 public static final String DB_PWD = "nieve";
28 public static final String DSSP_EXE = "/project/StruPPi/bin/dssp";
29 public static final String DSSP_PARAMS = "--";
30
31 //public static double cutoff = 4.2;
32 //public static String edgeType = "ALL";
33
34 /*---------------------------- private methods --------------------------*/
35 /**
36 * Get user name from operating system (for use as database username).
37 * */
38 private static String getUserName() {
39 String user = null;
40 user = System.getProperty("user.name");
41 if(user == null) {
42 System.err.println("Could not get user name from operating system.");
43 }
44 return user;
45 }
46
47 public static void main(String[] args) throws IOException {
48
49
50 String help = "Usage, 3 options:\n" +
51 "1) genGraph -i <listfile> -d <distance_cutoff> -t <contact_type> -o <output_dir> [-D <pdbase_db>] \n" +
52 "2) genGraph -p <pdb_code> -c <chain_pdb_code> -d <distance_cutoff> -t <contact_type> -o <output_dir> [-D <pdbase_db>] \n" +
53 "3) genGraph -f <pdbfile> -c <chain_pdb_code> -d <distance_cutoff> -t <contact_type> -o <output_dir> \n" +
54 "In case 2) also a list of comma separated pdb codes and chain codes can be specified, e.g. -p 1bxy,1jos -c A,A\n" +
55 "If pdbase_db not specified, the default pdbase will be used\n";
56
57 String listfile = "";
58 String[] pdbCodes = null;
59 String[] pdbChainCodes = null;
60 String pdbfile = "";
61 String pdbaseDb = PDB_DB;
62 String edgeType = "";
63 double cutoff = 0.0;
64 String outputDir = "";
65
66 Getopt g = new Getopt("genGraph", args, "i:p:c:f:d:t:o:D:h?");
67 int c;
68 while ((c = g.getopt()) != -1) {
69 switch(c){
70 case 'i':
71 listfile = g.getOptarg();
72 break;
73 case 'p':
74 pdbCodes = g.getOptarg().split(",");
75 break;
76 case 'c':
77 pdbChainCodes = g.getOptarg().split(",");
78 break;
79 case 'f':
80 pdbfile = g.getOptarg();
81 break;
82 case 'd':
83 cutoff = Double.valueOf(g.getOptarg());
84 break;
85 case 't':
86 edgeType = g.getOptarg();
87 break;
88 case 'o':
89 outputDir = g.getOptarg();
90 break;
91 case 'D':
92 pdbaseDb = g.getOptarg();
93 break;
94 case 'h':
95 case '?':
96 System.out.println(help);
97 System.exit(0);
98 break; // getopt() already printed an error
99 }
100 }
101
102 if (outputDir.equals("") || edgeType.equals("") || cutoff==0.0) {
103 System.err.println("Some missing option");
104 System.err.println(help);
105 System.exit(1);
106 }
107 if (listfile.equals("") && pdbCodes==null && pdbfile.equals("")){
108 System.err.println("Either a listfile, some pdb codes/chain codes or a pdbfile must be given");
109 System.err.println(help);
110 System.exit(1);
111 }
112 if ((!listfile.equals("") && pdbCodes!=null) || (!listfile.equals("") && !pdbfile.equals("")) || (pdbCodes!=null && !pdbfile.equals(""))) {
113 System.err.println("Options -p/-c, -i and -f/-c are exclusive. Use only one of them");
114 System.err.println(help);
115 System.exit(1);
116 }
117
118
119 MySQLConnection conn = null;
120
121 try{
122 conn = new MySQLConnection(DB_HOST, DB_USER, DB_PWD);
123 } catch (Exception e) {
124 System.err.println("Error opening database connection. Exiting");
125 System.exit(1);
126 }
127
128
129 if (pdbfile.equals("")){
130
131 if (!listfile.equals("")) {
132 BufferedReader fpdb = new BufferedReader(new FileReader(listfile));
133 String line = "";
134 int numLines = 0;
135 fpdb.mark(100000);
136 while ((line = fpdb.readLine() ) != null ) {
137 if (line.length()>0) numLines++;
138 }
139 fpdb.reset();
140 pdbCodes = new String[numLines];
141 pdbChainCodes = new String[numLines];
142 numLines = 0;
143 while ((line = fpdb.readLine() ) != null ) {
144 pdbCodes[numLines] = line.split("\\s+")[0].toLowerCase();
145 pdbChainCodes[numLines] = line.split("\\s+")[1];
146 numLines++;
147 }
148 }
149
150 int numPdbs = 0;
151
152 for (int i=0;i<pdbCodes.length;i++) {
153 String pdbCode = pdbCodes[i];
154 String pdbChainCode = pdbChainCodes[i];
155
156 try {
157
158 long start = System.currentTimeMillis();
159
160 Pdb pdb = new PdbasePdb(pdbCode, pdbChainCode, pdbaseDb, conn);
161
162 // get graph
163 Graph graph = pdb.get_graph(edgeType, cutoff);
164
165 File outputFile = new File(outputDir,pdbCode+"_"+pdbChainCode+"_"+edgeType+"_"+cutoff+".graph");
166 graph.write_graph_to_file(outputFile.getAbsolutePath());
167
168 long end = System.currentTimeMillis();
169 double time = (double) (end -start)/1000;
170
171 System.out.println("Wrote "+outputFile.getAbsolutePath());
172 System.out.printf("%5.3f s\n",time);
173
174 numPdbs++;
175
176 } catch (PdbaseInconsistencyError e) {
177 System.err.println("Inconsistency in " + pdbCode + pdbChainCode);
178 } catch (PdbCodeNotFoundError e) {
179 System.err.println("Couldn't find pdb code "+pdbCode);
180 } catch (SQLException e) {
181 System.err.println("SQL error for structure "+pdbCode+"_"+pdbChainCode+", error: "+e.getMessage());
182 } catch (PdbChainCodeNotFoundError e) {
183 System.err.println("Couldn't find pdb chain code "+pdbChainCode+" for pdb code "+pdbCode);
184 }
185
186 }
187
188 // output results
189 System.out.println("Number of structures done successfully: " + numPdbs);
190
191
192 } else {
193 String pdbChainCode = pdbChainCodes[0];
194 try {
195 Pdb pdb = new PdbfilePdb(pdbfile,pdbChainCode);
196 if (!pdb.hasSecondaryStructure()) {
197 pdb.runDssp(DSSP_EXE, DSSP_PARAMS);
198 }
199 Graph graph = pdb.get_graph(edgeType, cutoff);
200
201 File outputFile = new File(outputDir,pdb.getPdbCode()+"_"+pdbChainCode+"_"+edgeType+"_"+cutoff+".graph");
202 graph.write_graph_to_file(outputFile.getAbsolutePath());
203 System.out.println("Wrote graph file "+outputFile.getAbsolutePath()+" from pdb file "+pdbfile);
204
205 } catch (PdbfileFormatError e) {
206 System.err.println("pdb file "+pdbfile+" doesn't have right format");
207 } catch (PdbChainCodeNotFoundError e) {
208 System.err.println("chain code "+pdbChainCode+" wasn't found in file "+pdbfile);
209 }
210 }
211 }
212
213 }