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