/[cl-godb]/cl-godb/godb.lisp
ViewVC logotype

Contents of /cl-godb/godb.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Mon Aug 15 18:59:31 2005 UTC (8 years, 8 months ago) by mantoniotti
Branch: NYUBIG, MAIN
CVS Tags: Start, HEAD
Changes since 1.1: +0 -0 lines
Initial import.
1 ;;; -*- Mode: Lisp -*-
2 (in-package "GODB")
3
4 (eval-when (:compile-toplevel :load-toplevel :execute)
5 (require "sql")
6 (require "odbc"))
7
8
9 #.(sql:enable-sql-reader-syntax)
10
11 ;;replaces Go-Apphandle
12
13
14 #| DEFCONSTANT causes a bug in SQL expressions.
15 (defconstant +rel-is-a+ 2)
16 (defconstant +rel-part-of+ 3)|#
17
18 (defvar +rel-is-a+ 2)
19 (defvar +rel-part-of+ 3)
20
21 (defclass o-handle ()
22 ()
23 )
24
25
26 (defclass go-handle (o-handle)
27 (
28 ;; GRAPH/DB maintainance.
29 (term-accession-index :reader term-accession-index
30 :initform (make-hash-table :test #'eq))
31 (term-id-index :reader term-id-index
32 :initform (make-hash-table :test #'eql))
33 (term-name-index :reader term-name-index
34 :initform (make-hash-table :test #'equal))
35 (association-accession-index :reader association-accession-index
36 :initform (make-hash-table :test #'eq))
37 (association-gene-prod-index :reader association-gene-prod-index
38 :initform (make-hash-table :test #'eql))
39 (gene-product-symbol-index :reader gene-product-symbol-index
40 :initform (make-hash-table :test #'equal))
41
42 ;; Connection information.
43 (connection :accessor connection :initform nil)
44 )
45 (:documentation "The GO Handle Class.
46 Class containing specifications for connecting to the database and
47 hash tables indexing current go terms."))
48
49
50 (defclass node ()
51 ((is-a :accessor is-a
52 :initarg :is-a)
53 (part-of :accessor part-of
54 :initarg :part-of)
55 (children :accessor children
56 :type list
57 :initform ())
58 (components :accessor components
59 :type list
60 :initform ())
61 )
62 (:default-initargs :is-a nil :part-of nil)
63 )
64
65
66 (defclass term (node)
67 ((term-id :accessor term-id :initarg :term-id)
68 (name :reader term-name :initarg :term-name)
69 (accession :accessor accession :initarg :accession)
70 (term-type :accessor term-type :initarg :term-type)
71 (definition :accessor definition :initarg :definition)
72 (is-obsolete :accessor is-obsolete :initarg :is-obsolete :initform 0)
73 (is-root :accessor is-root :initarg :is-root :initform 0))
74 )
75
76
77 (defgeneric go-handle-p (x)
78 (:method ((x go-handle)) t)
79 (:method ((x t)) nil))
80
81 ;;; Protocol.
82
83 (defgeneric connect (go-handle db-spec &optional connection)
84 (:documentation "Sets up a connection to an underlying GO database
85 for the handle.
86 arguments: go handle, connection string, optional connection
87 usage: (godb:connect mygohandle ""data source name/user/password"")
88 notes: Sets up a connection to an underlying GO database for the handle.
89 If you have inserted your information in sqlcon, you can call connect as follows:
90 (godb:connect mygohandle """" (godb:sqlcon))"))
91
92
93 (defgeneric get-term (go-handle go-id)
94 (:documentation "Gets information for a go term using the go id.
95 arguments: go handle, go id integer
96 usage: (godb:get-term mygohandle 2145)
97 returns: if term has been queried already, it simply returns the stored information.
98 else, it indexes the term and then returns its details"))
99
100 (defgeneric get-term-by-acc (go-handle go-acc)
101 (:documentation "Gets information for a go term using its accession number.
102 arguments: go handle, accession
103 usage: (godb:get-term mygohandle ""GO:1248020"")
104 returns: if term has been queried already, it simply returns the stored information.
105 else, it indexes the term and then returns its details"))
106
107 (defgeneric get-term-by-name (go-handle go-name)
108 (:documentation "Gets information for a go term using its name.
109 arguments: go handle, name string
110 usage: (godb:get-term mygohandle ""Killing of Host Cells"")
111 returns: if term has been queried already, it simply returns the stored information.
112 else, it indexes the term and then returns its details"))
113
114 (defgeneric get-name (go-handle go-id)
115 (:documentation "Gets the name for a go term using its go-id.
116 arguments: go handle, go id integer
117 usage: (godb:get-term mygohandle 2145)
118 returns: if term has been queried already, the name associated with the go id is returned.
119 else, the term is indexed and then the name is returned"))
120
121 (defgeneric has-synonym-p (go-handle go-id syn)
122 (:documentation "Checks if a synonym exists
123 arguments: go handle, go id, synonym
124 usage: (godb:has-synonym mygohandle, 2145 ""proposed synonym"")
125 returns: T if the proposed synonym is a synonym, NIL if not"))
126
127 (defgeneric index-term-by-id (go-handle new-term id)
128 (:documentation "Adds a term to the term-id table, indexed by its go id
129 arguments: go handle, term (type term), go id
130 usage: (godb:index-term-by-id mygohandle myterm 2145)
131 returns:
132 notes: This is a function that is called when a new term is created. It adds it to the
133 hash table, indexed by its id. This is not a function you'd normally call yourself."))
134
135 (defgeneric index-term-by-accession (go-handle new-term accession)
136 (:documentation "Adds a term to the term-accession hash table, indexed by accession.
137 arguments: go handle, term (type term), accession
138 usage: (godb:index-term-by-accession mygohandle myterm ""GO:0000134"")
139 returns:
140 notes: This is a function that is called when a new term is created. It adds it to the
141 hash table, indexed by its accession. This is not a function you'd normally call yourself."))
142
143 (defgeneric index-term-by-name (go-handle new-term name)
144 (:documentation "Adds a term to the term-name hash table, indexed by its name.
145 arguments: go handle, term (type term), name string
146 usage: (godb:index-term-by-accession mygohandle myterm ""Killing of Host Cells"")
147 returns:
148 notes: This is a function that is called when a new term is created. It adds it to the
149 hash table, indexed by its name. This is not a function you'd normally call yourself."))
150
151 (defgeneric is-term-id-indexed-p (gh go-id)
152 (:documentation "Checks to see if the input go-id is currently indexed.
153 arguments: go handle, go-id
154 usage: (godb:is-id-indexed mygohandle 2145)
155 returns: Object of type term if it's indexed, otherwise returns nothing"))
156
157 (defgeneric is-term-accession-indexed-p (gh go-acc)
158 (:documentation "Checks to see if the input accession number is currently indexed.
159 arguments: go handle, accession
160 usage: (godb:is-accession-indexed mygohandle ""GO:0000124"")
161 returns: Object of type term if it's indexed, otherwise returns nothing"))
162
163 (defgeneric is-term-name-indexed-p (gh go-name)
164 (:documentation "Checks to see if the input name is currently indexed.
165 arguments: go handle, name
166 usage: (godb:is-name-indexed mygohandle ""Killing of Host Cells"")
167 returns: Object of type term if it's indexed, otherwise returns nothing"))
168
169
170 (defun sqlcon ()
171 "Connects to sql using stored connection information."
172 (sql:connect "GODB_internal/root" :database-type :odbc :if-exists :old))
173
174
175 (defvar *current-godb-handle* nil)
176
177 (defun make-godb-handle ()
178 "Creates a new instance of the go handle class and assigns
179 it to the current-godb-handle variable"
180 (setf *current-godb-handle* (make-instance 'go-handle)))
181
182
183
184
185 (defmethod is-term-id-indexed-p ((gh go-handle) (go-id integer))
186 (multiple-value-bind (value found)
187 (gethash go-id (term-id-index gh))
188 (when found value)
189 )
190 )
191
192 (defmethod is-term-accession-indexed-p ((gh go-handle) (go-acc symbol))
193 (multiple-value-bind (value found)
194 (gethash go-acc (term-accession-index gh))
195 (when found value)
196 )
197 )
198
199
200 (defmethod is-term-accession-indexed-p ((gh go-handle) (go-acc string))
201 (multiple-value-bind (value found)
202 (gethash (intern go-acc "GODB") (term-accession-index gh))
203 (when found value)
204 )
205 )
206
207
208 (defmethod is-term-name-indexed-p ((gh go-handle) go-name)
209 (multiple-value-bind (value found)
210 (gethash go-name (term-name-index gh))
211 (when found value)
212 )
213 )
214
215 (defmethod connect ((gh go-handle) (db-spec string) &optional connection)
216 (format t "~S connecting as ~S~%" gh db-spec)
217 (if connection
218 (setf (connection gh) connection)
219 (setf (connection gh) (sql:connect db-spec))))
220
221
222 (defun create-term (id name term-type accession is-obsolete is-root)
223 "This function makes a new instance of the term class given input for its slots"
224 (make-instance 'term
225 :term-name name
226 :term-id id
227 :term-type term-type
228 :accession accession
229 :is-obsolete is-obsolete
230 :is-root is-root
231 )
232 )
233
234 (defmethod get-term ((go-handle go-handle) (go-id integer))
235 ;; Step 1.
236 ;; See if we already have it in the GO-HANDLE.
237 (let ((index (is-term-id-indexed-p go-handle go-id)))
238 (if index
239 index
240 ;; Step 2.
241 ;; query db, get info for term, make instance with that data
242 (destructuring-bind (id name term-type acc is-obsolete is-root)
243 (first (sql:select [id] [name] [term_type] [acc] [is_obsolete] [is_root]
244 :from [|term|]
245 :where [= go-id [id]]
246 :database (connection go-handle)
247 ))
248 (when id
249 (let* ((acc (intern acc "GODB"))
250 (new-term (create-term id name term-type acc
251 is-obsolete is-root)))
252 (index-term-by-id go-handle new-term id)
253 (index-term-by-name go-handle new-term name)
254 (index-term-by-accession go-handle new-term acc)
255 new-term
256 ))))))
257
258
259 (defmethod get-name ((go-handle go-handle) (go-id integer))
260
261 ;;select returns two lists, one of values, followed by one of column names
262 ;;this prints the first value of the list of values
263
264 ;;check hashtable
265 (let ((index (is-term-id-indexed-p go-handle go-id)))
266 (if index
267 (term-name index)
268 ;;if not in table, query db
269 (destructuring-bind (id name term-type acc is-obsolete is-root)
270 (first (sql:select [id] [name] [term_type] [acc] [is_obsolete] [is_root]
271 :from [|term|]
272 :where [= go-id [id]]
273 :database (connection go-handle)
274 ))
275 (when id
276 (let* ((acc (intern acc "GODB"))
277 (new-term (create-term id name term-type acc
278 is-obsolete is-root)))
279 (index-term-by-id go-handle new-term id)
280 (index-term-by-name go-handle new-term name)
281 (index-term-by-accession go-handle new-term acc)
282 name
283 ))))))
284
285 (defmethod get-name ((go-handle go-handle) (go-acc string))
286
287 ;;select returns two lists, one of values, followed by one of column names
288 ;;this prints the first value of the list of values
289
290 ;;check hashtable
291 (let ((index (is-term-accession-indexed-p go-handle go-acc)))
292 (if index
293 (term-name index)
294 ;;if not in table, query db
295 (destructuring-bind (id name term-type acc is-obsolete is-root)
296 (first (sql:select [id] [name] [term_type] [acc] [is_obsolete] [is_root]
297 :from [|term|]
298 :where [= go-acc [acc]]
299 :database (connection go-handle)
300 ))
301 (when id
302 (let* ((acc (intern acc "GODB"))
303 (new-term (create-term id name term-type acc
304 is-obsolete is-root)))
305 (index-term-by-id go-handle new-term id)
306 (index-term-by-name go-handle new-term name)
307 (index-term-by-accession go-handle new-term acc)
308 name
309 ))))))
310
311
312 (defmethod get-term-by-acc ((go-handle go-handle) (go-acc symbol))
313 (get-term-by-acc go-handle (string go-acc)))
314
315
316 (defmethod get-term-by-acc ((go-handle go-handle) (go-acc string))
317
318 ;;check hash table indexed by accession
319 ;;if not in table, query db, add to table
320 (let ((index (is-term-accession-indexed-p go-handle go-acc)))
321 (if index
322 index
323 (destructuring-bind (id name term-type acc is-obsolete is-root)
324 (first (sql:select [id] [name] [term_type] [acc] [is_obsolete] [is_root]
325 :from [|term|]
326 :where [= go-acc [acc]]
327 :database (connection go-handle)
328 ))
329 (when id
330 (let* ((acc (intern acc "GODB"))
331 (new-term (create-term id name term-type acc
332 is-obsolete is-root)))
333 (index-term-by-id go-handle new-term id)
334 (index-term-by-name go-handle new-term name)
335 (index-term-by-accession go-handle new-term acc)
336 new-term
337 ))))))
338
339
340 (defmethod get-term-by-name ((go-handle go-handle) go-name)
341 (let ((index (is-term-name-indexed-p go-handle go-name)))
342 (if index
343 index
344 (destructuring-bind (id name term-type acc is-obsolete is-root)
345 (first (sql:select [id] [name] [term_type] [acc] [is_obsolete] [is_root]
346 :from [|term|]
347 :where [= go-name [name]]
348 ))
349 (when id
350 (let* ((acc (intern acc "GODB"))
351 (new-term (create-term id name term-type acc
352 is-obsolete is-root)))
353 (index-term-by-id go-handle new-term id)
354 (index-term-by-name go-handle new-term name)
355 (index-term-by-accession go-handle new-term acc)
356 new-term
357 ))))))
358
359 (defmethod has-synonym-p (go-handle go-id syn)
360 (let ((synonyms (sql:select [term_synonym.term_synonym]
361 :from [|term_synonym|]
362 :where [and [= [term_synonym.term_id] go-id]
363 [= [term_synonym.term_synonym] syn]])))
364 (if synonyms T NIL)))
365
366
367 (defmethod index-term-by-id (go-handle new-term id)
368 (setf (gethash id (term-id-index go-handle)) new-term)
369 )
370
371 (defmethod index-term-by-accession (go-handle new-term accession)
372 (setf (gethash accession (term-accession-index go-handle)) new-term)
373 )
374
375 (defmethod index-term-by-name (go-handle new-term name)
376 (setf (gethash name (term-name-index go-handle)) new-term)
377 )
378
379
380 (defun get-root-terms ()
381 "Returns all root terms."
382 (first (sql:select [*] :from [|term|] :where [= [is_root] 1])))
383
384 #.(sql:disable-sql-reader-syntax)
385
386 ;;; end of file -- godb.lisp --

  ViewVC Help
Powered by ViewVC 1.1.5