ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/genDbGraph.java
Revision: 277
Committed: Tue Aug 14 10:54:24 2007 UTC (17 years, 1 month ago) by duarte
File size: 6189 byte(s)
Log Message:
New script genGraph (generates graph to files)
Better error messages in both
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     "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     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     if(pdbChainCode == null) {
156     pdbChainCode = "NULL";
157     }
158    
159 duarte 277
160 duarte 264 try {
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     System.out.println(pdbCode+"_"+pdbChainCode);
169 duarte 277 numPdbs++;
170    
171 duarte 264 } catch (PdbaseInconsistencyError e) {
172 duarte 277 System.err.println("Inconsistency in " + pdbCode + pdbChainCode);
173 duarte 264 } catch (PdbCodeNotFoundError e) {
174 duarte 277 System.err.println("Couldn't find pdb code "+pdbCode);
175 duarte 264 } catch (SQLException e) {
176     e.printStackTrace();
177     } catch (PdbChainCodeNotFoundError e) {
178 duarte 277 System.err.println("Couldn't find pdb chain code "+pdbChainCode+" for pdb code "+pdbCode);
179 duarte 264 }
180    
181     }
182    
183     // output results
184 duarte 277 System.out.println("Number of structures loaded successfully: " + numPdbs);
185 duarte 264
186    
187     } else {
188     String pdbChainCode = pdbChainCodes[0];
189     try {
190     Pdb pdb = new PdbfilePdb(pdbfile,pdbChainCode);
191     if (!pdb.hasSecondaryStructure()) {
192     pdb.runDssp(DSSP_EXE, DSSP_PARAMS);
193     }
194     Graph graph = pdb.get_graph(edgeType, cutoff);
195     try {
196     graph.write_graph_to_db(conn, outputDb);
197     System.out.println("Loaded to database graph for file "+pdbfile);
198     } catch (SQLException e) {
199     e.printStackTrace();
200     }
201    
202     } catch (PdbfileFormatError e) {
203     System.err.println("pdb file "+pdbfile+" doesn't have right format");
204     } catch (PdbChainCodeNotFoundError e) {
205     System.err.println("chain code "+pdbChainCode+" wasn't found in file "+pdbfile);
206     }
207     }
208     }
209    
210     }