ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/genGraph.java
Revision: 277
Committed: Tue Aug 14 10:54:24 2007 UTC (17 years, 1 month ago) by duarte
File size: 6413 byte(s)
Log Message:
New script genGraph (generates graph to files)
Better error messages in both
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    
31     //public static double cutoff = 4.2;
32     //public static String edgeType = "ALL";
33    
34     /*---------------------------- private methods --------------------------*/
35     /**
36     * Get user name from operating system (for use as database username).
37     * */
38     private static String getUserName() {
39     String user = null;
40     user = System.getProperty("user.name");
41     if(user == null) {
42     System.err.println("Could not get user name from operating system.");
43     }
44     return user;
45     }
46    
47     public static void main(String[] args) throws IOException {
48    
49    
50     String help = "Usage, 3 options:\n" +
51     "1) genGraph -i <listfile> -d <distance_cutoff> -t <contact_type> -o <output_dir> [-D <pdbase_db>] \n" +
52     "2) genGraph -p <pdb_code> -c <chain_pdb_code> -d <distance_cutoff> -t <contact_type> -o <output_dir> [-D <pdbase_db>] \n" +
53     "3) genGraph -f <pdbfile> -c <chain_pdb_code> -d <distance_cutoff> -t <contact_type> -o <output_dir> \n" +
54     "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" +
55     "If pdbase_db not specified, the default pdbase will be used\n";
56    
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 outputDir = "";
65    
66     Getopt g = new Getopt("genGraph", 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     outputDir = 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 (outputDir.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     numLines++;
138     }
139     fpdb.reset();
140     pdbCodes = new String[numLines];
141     pdbChainCodes = new String[numLines];
142     numLines = 0;
143     while ((line = fpdb.readLine() ) != null ) {
144     pdbCodes[numLines] = line.split("\\s")[0].toLowerCase();
145     pdbChainCodes[numLines] = line.split("\\s")[1];
146     numLines++;
147     }
148     }
149    
150     int numPdbs = 0;
151    
152     for (int i=0;i<pdbCodes.length;i++) {
153     String pdbCode = pdbCodes[i];
154     String pdbChainCode = pdbChainCodes[i];
155    
156     if(pdbChainCode == null) {
157     pdbChainCode = "NULL";
158     }
159    
160    
161     try {
162     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     System.out.println("Wrote "+outputFile.getAbsolutePath());
171     numPdbs++;
172    
173     } catch (PdbaseInconsistencyError e) {
174     System.err.println("Inconsistency in " + pdbCode + pdbChainCode);
175     } catch (PdbCodeNotFoundError e) {
176     System.err.println("Couldn't find pdb code "+pdbCode);
177     } catch (SQLException e) {
178     e.printStackTrace();
179     } catch (PdbChainCodeNotFoundError e) {
180     System.err.println("Couldn't find pdb chain code "+pdbChainCode+" for pdb code "+pdbCode);
181     }
182    
183     }
184    
185     // output results
186     System.out.println("Number of structures done successfully: " + numPdbs);
187    
188    
189     } else {
190     String pdbChainCode = pdbChainCodes[0];
191     try {
192     Pdb pdb = new PdbfilePdb(pdbfile,pdbChainCode);
193     if (!pdb.hasSecondaryStructure()) {
194     pdb.runDssp(DSSP_EXE, DSSP_PARAMS);
195     }
196     Graph graph = pdb.get_graph(edgeType, cutoff);
197    
198     File outputFile = new File(outputDir,pdb.getPdbCode()+"_"+pdbChainCode+"_"+edgeType+"_"+cutoff+".graph");
199     graph.write_graph_to_file(outputFile.getAbsolutePath());
200     System.out.println("Wrote graph file "+outputFile.getAbsolutePath()+" from pdb file "+pdbfile);
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     }