ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/Graph.java
Revision: 141
Committed: Mon May 14 15:51:54 2007 UTC (17 years, 4 months ago) by duarte
File size: 8536 byte(s)
Log Message:
Changed input in constructor for reading from db: chain->chaincode
Now getgraphid initialises too the internal chain code (pchain_code from graph db)
Line User Rev File contents
1 duarte 123 package proteinstructure;
2 duarte 134 import java.io.BufferedReader;
3     import java.io.File;
4     import java.io.FileNotFoundException;
5 duarte 123 import java.io.FileOutputStream;
6 duarte 134 import java.io.FileReader;
7 duarte 123 import java.io.PrintStream;
8     import java.io.IOException;
9 duarte 135 import java.sql.ResultSet;
10     import java.sql.SQLException;
11     import java.sql.Statement;
12 duarte 123 import java.util.ArrayList;
13 duarte 129 import java.util.TreeMap;
14 duarte 135 import tools.MySQLConnection;
15 duarte 123
16    
17     public class Graph {
18    
19 duarte 135 public final static String MYSQLSERVER="white";
20     public final static String MYSQLUSER=getUserName();
21     public final static String MYSQLPWD="nieve";
22    
23 duarte 123 ArrayList<Contact> contacts;
24 duarte 135 // nodes is a TreeMap of residue serials to residue types (3 letter code)
25 duarte 129 TreeMap<Integer,String> nodes;
26     String sequence;
27     String accode;
28     String chain;
29 duarte 123 double cutoff;
30     String ct;
31 duarte 135 boolean directed=false;
32 duarte 123
33 duarte 135 // these 2 fields only used when reading from db
34     int graphid=0;
35     int sm_id=0;
36    
37 duarte 134 /**
38     * Constructs Graph object by passing ArrayList with contacts and TreeMap with nodes (res serials and types)
39     * Must also pass contact type, cutoff, accession code and chain
40     * @param contacts
41     * @param nodes
42     * @param sequence
43     * @param cutoff
44     * @param ct
45     * @param accode
46     * @param chain
47     */
48 duarte 129 public Graph (ArrayList<Contact> contacts, TreeMap<Integer,String> nodes, String sequence, double cutoff,String ct, String accode, String chain) {
49 duarte 123 this.contacts=contacts;
50     this.cutoff=cutoff;
51 duarte 129 this.nodes=nodes;
52     this.sequence=sequence;
53     this.accode=accode;
54     this.chain=chain;
55 duarte 123 this.ct=ct;
56 duarte 129 if (ct.contains("/")){
57     directed=true;
58     }
59 duarte 123 }
60 duarte 135
61     /**
62 duarte 141 * Constructs Graph object from graph db, given the dbname, accode, chaincode (classic pdb chain code), ct and cutoff
63 duarte 135 * @param dbname
64     * @param accode
65 duarte 141 * @param chaincode
66 duarte 135 * @param cutoff
67     * @param ct
68     */
69 duarte 141 public Graph(String dbname, String accode, String chaincode, double cutoff, String ct) throws GraphIdNotFoundError{
70 duarte 135 this.cutoff=cutoff;
71     this.accode=accode;
72     this.ct=ct;
73     //TODO graphs in db are never directed, so this doesn't really apply here. Must solve all this!
74     if (ct.contains("/")){
75     directed=true;
76     }
77     MySQLConnection conn = new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD,dbname);
78 duarte 141 getgraphid(conn, chaincode); // initialises graphid, sm_id and chain
79     read_graph_from_db(conn); // gets contacts, nodes and sequence
80 duarte 135 conn.close();
81     }
82 duarte 129
83 duarte 134 /**
84     * Constructs Graph object by reading a file with contacts
85     * An object created with this constructor will be missing the fields sequence and nodes
86     * That means it's not possible to get a ContactMap from it using getCM because CM needs both sequence and nodes
87     * @param contactsfile
88     * @param cutoff
89     * @param ct
90     * @throws IOException
91     * @throws FileNotFoundException
92     */
93     public Graph (String contactsfile, double cutoff,String ct) throws IOException, FileNotFoundException{
94     this.cutoff=cutoff;
95     this.ct=ct;
96     if (ct.contains("/")){
97     directed=true;
98     }
99     read_contacts_from_file(contactsfile);
100     }
101    
102 duarte 135 //TODO implement (from python) write_graph_to_db, do we really need it here??
103    
104     /** get user name from operating system (for use as database username) */
105     private static String getUserName() {
106     String user = null;
107     user = System.getProperty("user.name");
108     if(user == null) {
109     System.err.println("Could not get user name from operating system. Exiting");
110     System.exit(1);
111     }
112     return user;
113     }
114    
115 duarte 134 public void read_contacts_from_file (String contactsfile) throws FileNotFoundException, IOException {
116     contacts = new ArrayList<Contact>();
117     System.out.println("Reading contacts from file "+contactsfile);
118     BufferedReader fcont = new BufferedReader(new FileReader(new File(contactsfile)));
119     String line;
120     while ((line = fcont.readLine() ) != null ) {
121     int i = Integer.parseInt(line.split("\\s+")[0]);
122     int j = Integer.parseInt(line.split("\\s+")[1]);
123     contacts.add(new Contact(i,j));
124     }
125     fcont.close();
126     }
127    
128 duarte 135 /**
129     * Reads contacts and nodes from db.
130     * The db must be a graph db following our standard format, i.e. must have tables:
131     * chain_graph, single_model_graph, single_model_node, single_model_edge
132     * We don't care here about the origin of the data (msdsd, pdbase, predicted) for the generation of the graph as long as it follows our data format
133     * We read both edges and nodes from single_model_edge and single_model_node.
134     * The sequence is taken from nodes, thus it won't have unobserved or non standard aas.
135     * @param conn
136     */
137     public void read_graph_from_db(MySQLConnection conn){
138     contacts = new ArrayList<Contact>();
139     nodes = new TreeMap<Integer, String>();
140     sequence = "";
141     try {
142     String sql="SELECT i_num,j_num FROM single_model_edge WHERE graph_id="+graphid+" AND j_num>i_num ORDER BY i_num,j_num ";
143     Statement stmt = conn.createStatement();
144     ResultSet rsst = stmt.executeQuery(sql);
145     while (rsst.next()) {
146     int i=rsst.getInt(1);
147     int j=rsst.getInt(2);
148     contacts.add(new Contact(i,j));
149     }
150     rsst.close();
151     stmt.close();
152     sql="SELECT num,res FROM single_model_node WHERE graph_id="+graphid+" ORDER BY num ";
153     stmt = conn.createStatement();
154     rsst = stmt.executeQuery(sql);
155     while (rsst.next()){
156     int num=rsst.getInt(1);
157     String res=rsst.getString(2);
158     nodes.put(num, AA.oneletter2threeletter(res));
159     sequence+=res;
160     }
161     rsst.close();
162     stmt.close();
163     } catch (SQLException e) {
164     e.printStackTrace();
165     }
166    
167     }
168    
169 duarte 141 public void getgraphid (MySQLConnection conn, String chaincode) throws GraphIdNotFoundError{
170     // input is chaincode i.e. pdb chain code
171     // we take chain (internal chain identifier, pchain_code for msdsd and asym_id for pdbase) from pchain_code field in chain_graph
172     // (in the chain_graph table the internal chain identifier is called 'pchain_code')
173 duarte 135 int pgraphid=0;
174 duarte 141 String chainstr="='"+chaincode+"' ";
175     if (chaincode.equals("NULL")){
176     chainstr=" IS NULL ";
177     }
178 duarte 135 try {
179 duarte 141 String sql="SELECT graph_id, pchain_code FROM chain_graph WHERE accession_code='"+accode+"' AND chain_pdb_code"+chainstr+" AND dist="+cutoff;
180 duarte 135 Statement stmt = conn.createStatement();
181     ResultSet rsst = stmt.executeQuery(sql);
182     int check=0;
183     while (rsst.next()) {
184     check++;
185     pgraphid=rsst.getInt(1);
186 duarte 141 chain=rsst.getString(2);
187 duarte 135 }
188     if (check!=1){
189 duarte 141 System.err.println("No pgraph_id match or more than 1 match for accession_code="+accode+", chain_pdb_code="+chaincode+", dist="+cutoff);
190 duarte 135 }
191     rsst.close();
192     stmt.close();
193     // we set the ctstr to the same as ct except in ALL case, where it is BB+SC+BB/SC
194     String ctstr=ct;
195     if (ct.equals("ALL")){
196     ctstr="BB+SC+BB/SC";
197     }
198     sql="SELECT graph_id,single_model_id FROM single_model_graph WHERE pgraph_id="+pgraphid+" AND CT='"+ctstr+"' AND dist="+cutoff+" AND CR='(true)' AND CW=1";
199     stmt = conn.createStatement();
200     rsst = stmt.executeQuery(sql);
201     check=0;
202     while (rsst.next()){
203     check++;
204     graphid=rsst.getInt(1);
205     sm_id=rsst.getInt(2);
206     }
207     if (check!=1){
208     System.err.println("No graph_id match or more than 1 match for pgraph_id="+pgraphid+", CT="+ctstr+" and cutoff="+cutoff);
209     throw new GraphIdNotFoundError("No graph_id match or more than 1 match for pgraph_id="+pgraphid+", CT="+ctstr+" and cutoff="+cutoff);
210     }
211     } catch (SQLException e) {
212     e.printStackTrace();
213     }
214    
215     }
216    
217 duarte 123 public void write_contacts_to_file (String outfile) throws IOException {
218     PrintStream Out = new PrintStream(new FileOutputStream(outfile));
219     for (Contact pair:contacts){
220     int i_resser=pair.i;
221     int j_resser=pair.j;
222     Out.println(i_resser+"\t"+j_resser);
223     }
224     Out.close();
225     }
226 duarte 129
227     public ContactMap getCM() {
228     // residues is the map from residue nums to residue types used in ContactMap class, i.e. it is the same as Pdb.resser2restype or Graph.nodes
229     TreeMap<Integer,String> residues = new TreeMap<Integer,String>();
230     // we copy residues from nodes (deep copy)
231     for (int node:nodes.keySet()){
232     residues.put(node, nodes.get(node));
233     }
234     // check if we are in directed or undirected case. If undirected we fill the opposite contacts to pass a full list of contacts to ContactMap (which contains full matrix)
235     ArrayList<Contact> contacts2pass = new ArrayList<Contact>();
236     if (directed){
237     contacts2pass=contacts;
238     } else {
239     for (Contact cont:contacts){
240     int i_resser = cont.i;
241     int j_resser = cont.j;
242     contacts2pass.add(new Contact(i_resser,j_resser));
243     contacts2pass.add(new Contact(j_resser,i_resser));
244     }
245     }
246     // construct the ContactMap object and return it
247     ContactMap cm = new ContactMap(contacts2pass,residues,sequence);
248     return cm;
249    
250     }
251    
252 duarte 123 }