ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/genDbGraph.java
Revision: 358
Committed: Wed Oct 17 13:22:01 2007 UTC (17 years ago) by duarte
File size: 6642 byte(s)
Log Message:
Now closing db connection. Better help string.
Line User Rev File contents
1 duarte 264 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 duarte 358 "\nIf pdbase_db not specified, the default pdbase will be used\n" +
55     "\nSecondary structure will be taken from pdbase database. If reading from pdb file and the pdb file is missing the secondary structure, then it will be assigned using dssp\n";
56 duarte 264
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 outputDb = "";
65    
66     Getopt g = new Getopt("genDbGraph", 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     outputDb = 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 (outputDb.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 duarte 285 if (line.length()>0) numLines++;
138 duarte 264 }
139     fpdb.reset();
140     pdbCodes = new String[numLines];
141     pdbChainCodes = new String[numLines];
142     numLines = 0;
143     while ((line = fpdb.readLine() ) != null ) {
144 duarte 285 pdbCodes[numLines] = line.split("\\s+")[0].toLowerCase();
145     pdbChainCodes[numLines] = line.split("\\s+")[1];
146 duarte 264 numLines++;
147     }
148 duarte 358 fpdb.close();
149 duarte 264 }
150    
151     int numPdbs = 0;
152    
153     for (int i=0;i<pdbCodes.length;i++) {
154     String pdbCode = pdbCodes[i];
155     String pdbChainCode = pdbChainCodes[i];
156    
157     try {
158 duarte 285
159     long start = System.currentTimeMillis();
160    
161 duarte 277 Pdb pdb = new PdbasePdb(pdbCode, pdbChainCode, pdbaseDb, conn);
162 duarte 264
163     // get graph
164     Graph graph = pdb.get_graph(edgeType, cutoff);
165    
166     graph.write_graph_to_db(conn,outputDb);
167    
168 duarte 285 long end = System.currentTimeMillis();
169     double time = (double) (end -start)/1000;
170    
171     System.out.printf(pdbCode+"_"+pdbChainCode+" - %5.3f s\n",time);
172 duarte 277 numPdbs++;
173    
174 duarte 264 } catch (PdbaseInconsistencyError e) {
175 duarte 277 System.err.println("Inconsistency in " + pdbCode + pdbChainCode);
176 duarte 264 } catch (PdbCodeNotFoundError e) {
177 duarte 277 System.err.println("Couldn't find pdb code "+pdbCode);
178 duarte 264 } catch (SQLException e) {
179 duarte 285 System.err.println("SQL error for structure "+pdbCode+"_"+pdbChainCode+", error: "+e.getMessage());
180 duarte 264 } catch (PdbChainCodeNotFoundError e) {
181 duarte 277 System.err.println("Couldn't find pdb chain code "+pdbChainCode+" for pdb code "+pdbCode);
182 duarte 264 }
183    
184     }
185    
186     // output results
187 duarte 277 System.out.println("Number of structures loaded successfully: " + numPdbs);
188 duarte 264
189    
190     } else {
191     String pdbChainCode = pdbChainCodes[0];
192     try {
193     Pdb pdb = new PdbfilePdb(pdbfile,pdbChainCode);
194     if (!pdb.hasSecondaryStructure()) {
195     pdb.runDssp(DSSP_EXE, DSSP_PARAMS);
196     }
197     Graph graph = pdb.get_graph(edgeType, cutoff);
198     try {
199     graph.write_graph_to_db(conn, outputDb);
200     System.out.println("Loaded to database graph for file "+pdbfile);
201     } catch (SQLException e) {
202     e.printStackTrace();
203     }
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 duarte 358
212     // closing db connection
213     conn.close();
214 duarte 264 }
215    
216     }