ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/genGraph.java
Revision: 424
Committed: Fri Nov 23 14:21:33 2007 UTC (16 years, 11 months ago) by duarte
Original Path: branches/aglappe-jung/genGraph.java
File size: 6774 byte(s)
Log Message:
Added last i/o method from old Graph (write to file in Ioannis network format)
Using now the more correct degree, indegree, outdegree methods instead of count neighbor methods
Fixed the write_graph_to_file method name in executables in default package

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