1 |
import java.io.*; |
2 |
import java.sql.*; |
3 |
import java.util.*; |
4 |
import proteinstructure.*; |
5 |
import tools.MySQLConnection; |
6 |
|
7 |
// TODO: |
8 |
// - how will the distribution look like for randomly drawing from the unit square? (use Ioannis' random graphs?) |
9 |
// - what is the maximum and expected number of points per square? how does that translate into runtime |
10 |
// Problems: |
11 |
// - surface effect (distinguish surface/core) |
12 |
// - translation of grid (-> kdtrees) |
13 |
// Space of grid: |
14 |
// - when assuming spherical proteins and constant density, grid size grows linearly |
15 |
// - in the worst case of completely linear proteins, grid size growth is cubic |
16 |
// - for the protein case, do benchmark (to prove linear runtime, estimate runtime constants and space exponent) |
17 |
// - test whether rotating (by using principle components?) improves runtime/space |
18 |
|
19 |
public class calculateGridDensity { |
20 |
|
21 |
/*------------------------------ constants ------------------------------*/ |
22 |
|
23 |
// database with a list of pdb codes and chain codes to process |
24 |
public static String DB_NAME = "pdb_reps"; |
25 |
public static String DB_TABLE = "reps"; |
26 |
public static String DB_COL_PDB = "accession_code"; |
27 |
public static String DB_COL_CHAIN = "chain_pdb_code"; |
28 |
|
29 |
public static String PDB_DB = "pdbase"; |
30 |
public static String DB_HOST = "white"; |
31 |
public static String DB_USER = getUserName(); |
32 |
public static String DB_PWD = "nieve"; |
33 |
|
34 |
public static String PDB_CODE = "1tdr"; |
35 |
public static String CHAIN_CODE = "B"; |
36 |
public static String edgeType = "Ca"; |
37 |
public static double cutoff_from = 4.0; |
38 |
public static double cutoff_to = 15.0; |
39 |
public static double cutoff_step = 1.0; |
40 |
public static int limit = 100; |
41 |
|
42 |
public static String outFileName = "grid_nbs_pdbreps100"; |
43 |
|
44 |
/*---------------------------- private methods --------------------------*/ |
45 |
/** |
46 |
* Get user name from operating system (for use as database username). |
47 |
* */ |
48 |
private static String getUserName() { |
49 |
String user = null; |
50 |
user = System.getProperty("user.name"); |
51 |
if(user == null) { |
52 |
System.err.println("Could not get user name from operating system."); |
53 |
} |
54 |
return user; |
55 |
} |
56 |
|
57 |
private static void calcDensity(String pdbCode, String chainCode, double cutoff, String egdeType, MySQLConnection conn, Map<Integer, Integer> densityCount) { |
58 |
Pdb pdb = null; |
59 |
try { |
60 |
pdb = new PdbasePdb(pdbCode, PDB_DB, conn); |
61 |
pdb.load(chainCode); |
62 |
// add to density count vector |
63 |
pdb.calcGridDensity(edgeType, cutoff, densityCount); |
64 |
|
65 |
} catch (PdbLoadError e) { |
66 |
System.out.println("Error loading " + pdbCode + chainCode+", specific error: "+e.getMessage()); |
67 |
} catch (PdbCodeNotFoundError e) { |
68 |
e.printStackTrace(); |
69 |
} catch (SQLException e) { |
70 |
e.printStackTrace(); |
71 |
} |
72 |
|
73 |
} |
74 |
|
75 |
public static void printValues(Map<Integer, Integer> v, PrintStream out) { |
76 |
int atoms = 0; |
77 |
for(int size:v.keySet()) { |
78 |
out.println(size + "\t" + v.get(size)); |
79 |
atoms += size*v.get(size); |
80 |
} |
81 |
//out.println("Atoms: " + atoms); |
82 |
} |
83 |
|
84 |
|
85 |
public static void writeResultToFile(Map<Integer, Integer> v, String baseName, String edgeType, double cutoff) { |
86 |
try { |
87 |
File outFile = new File(baseName + "_" + edgeType + "_" + cutoff + ".out"); |
88 |
if(outFile.exists()) { |
89 |
outFile.delete(); |
90 |
} |
91 |
outFile.createNewFile(); |
92 |
printValues(v, new PrintStream(outFile)); |
93 |
System.out.println("Results written to file " + outFile.getName()); |
94 |
|
95 |
} catch (FileNotFoundException e) { |
96 |
e.printStackTrace(); |
97 |
} catch (IOException e) { |
98 |
e.printStackTrace(); |
99 |
} |
100 |
} |
101 |
|
102 |
|
103 |
public static void writeResultToDb(Map<Integer, Integer> v) { |
104 |
// insert into runs(run_id, edgeType, cutoff, timestamp, proteins, points, cells, avg_pts_per_cell, max_pts_per_cell, avg_pts_per_area, max_pts_per_area) |
105 |
// insert into density_distr(run_id, points_per_cell, num_cells) |
106 |
// do another run for values per protein (run_id, pdb_id, chain_id, num_res, num_atoms, num_cells, surface_cells, core_cells, surface_area, volume) |
107 |
|
108 |
} |
109 |
|
110 |
public static Map<Integer, Integer> calcDensity(MySQLConnection conn, String edgeType, double cutoff, boolean verbose) { |
111 |
Map<Integer, Integer> densityCount = new TreeMap<Integer,Integer>(); |
112 |
String pdbCode, chainCode; |
113 |
int numPdbs = 0; |
114 |
|
115 |
if(verbose) { |
116 |
System.out.print(edgeType + " " + cutoff + ": "); |
117 |
} |
118 |
|
119 |
// read structures from database |
120 |
String query = "SELECT DISTINCT " + DB_COL_PDB + "," + DB_COL_CHAIN + " FROM " + DB_NAME + "." + DB_TABLE + " LIMIT " + limit + ";" ; |
121 |
Statement stmt; |
122 |
try { |
123 |
stmt = conn.createStatement(); |
124 |
ResultSet rs = stmt.executeQuery(query); |
125 |
while(rs.next()) { |
126 |
pdbCode = rs.getString(1); |
127 |
chainCode = rs.getString(2); |
128 |
|
129 |
if(chainCode == null) { |
130 |
chainCode = "NULL"; |
131 |
} |
132 |
numPdbs++; |
133 |
|
134 |
// calculate statistics |
135 |
calcDensity(pdbCode, chainCode, cutoff, edgeType, conn, densityCount); // will add to densityCount |
136 |
if(verbose) { |
137 |
if(numPdbs %2 == 0) { |
138 |
System.out.print('\b'); |
139 |
} else { |
140 |
System.out.print("."); |
141 |
} |
142 |
if(numPdbs % 500 == 0) System.out.print(numPdbs + " "); |
143 |
} |
144 |
|
145 |
// for each protein write to db: pdb, chain, num_res, volume, max_density |
146 |
} |
147 |
rs.close(); |
148 |
stmt.close(); |
149 |
if(verbose) System.out.println("."); |
150 |
|
151 |
} catch (SQLException e) { |
152 |
e.printStackTrace(); |
153 |
} |
154 |
return densityCount; |
155 |
} |
156 |
|
157 |
public static void main(String[] args) { |
158 |
MySQLConnection conn = null; |
159 |
Map<Integer, Integer> densityCount = null; |
160 |
double cutoff; |
161 |
|
162 |
// opening db connection |
163 |
try{ |
164 |
conn = new MySQLConnection(DB_HOST, DB_USER, DB_PWD); |
165 |
} catch (Exception e) { |
166 |
System.err.println("Error opening database connection. Exiting"); |
167 |
System.exit(1); |
168 |
} |
169 |
|
170 |
for(cutoff = cutoff_from; cutoff <= cutoff_to; cutoff += cutoff_step) { |
171 |
// run calculation |
172 |
densityCount = calcDensity(conn, edgeType, cutoff, true); |
173 |
|
174 |
// output results |
175 |
//printValues(densityCount, System.out); |
176 |
writeResultToFile(densityCount, outFileName, edgeType, cutoff); |
177 |
} |
178 |
System.out.println("Done."); |
179 |
|
180 |
} |
181 |
|
182 |
} |