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