whitespace cleanup
[htsworkflow.git] / htsworkflow / frontend / samples / test_samples.py
1 import datetime
2
3 try:
4     import json
5 except ImportError, e:
6     import simplejson as json
7
8 from django.test import TestCase
9 from django.test.utils import setup_test_environment, \
10      teardown_test_environment
11 from django.db import connection
12 from django.conf import settings
13
14 from htsworkflow.frontend.samples.models import \
15         Affiliation, \
16         ExperimentType, \
17         Species, \
18         Library
19
20 from htsworkflow.frontend.samples.views import \
21      library_dict, \
22      library_json
23
24 from htsworkflow.frontend.auth import apidata
25 from htsworkflow.util.conversion import unicode_or_none
26 from htsworkflow.util.ethelp import validate_xhtml
27
28 class LibraryTestCase(TestCase):
29     fixtures = ['initial_data.json',
30                 'woldlab.json',
31                 'test_samples.json']
32
33     def setUp(self):
34         create_db(self)
35
36     def testOrganism(self):
37         self.assertEquals(self.library_10001.organism(), 'human')
38
39     def testAffiliations(self):
40         self.library_10001.affiliations.add(self.affiliation_alice)
41         self.library_10002.affiliations.add(
42                 self.affiliation_alice,
43                 self.affiliation_bob
44         )
45         self.failUnless(len(self.library_10001.affiliations.all()), 1)
46         self.failUnless(self.library_10001.affiliation(), 'Alice')
47
48         self.failUnless(len(self.library_10002.affiliations.all()), 2)
49         self.failUnless(self.library_10001.affiliation(), 'Alice, Bob')
50
51
52 class SampleWebTestCase(TestCase):
53     """
54     Test returning data from our database in rest like ways.
55     (like returning json objects)
56     """
57     fixtures = ['initial_data.json',
58                 'woldlab.json',
59                 'test_samples.json']
60
61     def test_library_info(self):
62         for lib in Library.objects.all():
63             lib_dict = library_dict(lib.id)
64             url = '/samples/library/%s/json' % (lib.id,)
65             lib_response = self.client.get(url, apidata)
66             self.failUnlessEqual(lib_response.status_code, 200)
67             lib_json = json.loads(lib_response.content)
68
69             for d in [lib_dict, lib_json]:
70                 # amplified_from_sample is a link to the library table,
71                 # I want to use the "id" for the data lookups not
72                 # the embedded primary key.
73                 # It gets slightly confusing on how to implement sending the right id
74                 # since amplified_from_sample can be null
75                 #self.failUnlessEqual(d['amplified_from_sample'], lib.amplified_from_sample)
76                 self.failUnlessEqual(d['antibody_id'], lib.antibody_id)
77                 self.failUnlessEqual(d['cell_line_id'], lib.cell_line_id)
78                 self.failUnlessEqual(d['cell_line'], unicode_or_none(lib.cell_line))
79                 self.failUnlessEqual(d['experiment_type'], lib.experiment_type.name)
80                 self.failUnlessEqual(d['experiment_type_id'], lib.experiment_type_id)
81                 self.failUnlessEqual(d['gel_cut_size'], lib.gel_cut_size)
82                 self.failUnlessEqual(d['hidden'], lib.hidden)
83                 self.failUnlessEqual(d['id'], lib.id)
84                 self.failUnlessEqual(d['insert_size'], lib.insert_size)
85                 self.failUnlessEqual(d['library_name'], lib.library_name)
86                 self.failUnlessEqual(d['library_species'], lib.library_species.scientific_name)
87                 self.failUnlessEqual(d['library_species_id'], lib.library_species_id)
88                 self.failUnlessEqual(d['library_type_id'], lib.library_type_id)
89                 if lib.library_type_id is not None:
90                     self.failUnlessEqual(d['library_type'], lib.library_type.name)
91                 else:
92                     self.failUnlessEqual(d['library_type'], None)
93                     self.failUnlessEqual(d['made_for'], lib.made_for)
94                     self.failUnlessEqual(d['made_by'], lib.made_by)
95                     self.failUnlessEqual(d['notes'], lib.notes)
96                     self.failUnlessEqual(d['replicate'], lib.replicate)
97                     self.failUnlessEqual(d['stopping_point'], lib.stopping_point)
98                     self.failUnlessEqual(d['successful_pM'], lib.successful_pM)
99                     self.failUnlessEqual(d['undiluted_concentration'],
100                                          unicode(lib.undiluted_concentration))
101                 # some specific tests
102                 if lib.id == '10981':
103                     # test a case where there is no known status
104                     lane_set = {u'status': u'Unknown',
105                                 u'paired_end': True,
106                                 u'read_length': 75,
107                                 u'lane_number': 1,
108                                 u'lane_id': 1193,
109                                 u'flowcell': u'303TUAAXX',
110                                 u'status_code': None}
111                     self.failUnlessEqual(len(d['lane_set']), 1)
112                     self.failUnlessEqual(d['lane_set'][0], lane_set)
113                 elif lib.id == '11016':
114                     # test a case where there is a status
115                     lane_set = {u'status': 'Good',
116                                 u'paired_end': True,
117                                 u'read_length': 75,
118                                 u'lane_number': 5,
119                                 u'lane_id': 1197,
120                                 u'flowcell': u'303TUAAXX',
121                                 u'status_code': 2}
122                     self.failUnlessEqual(len(d['lane_set']), 1)
123                     self.failUnlessEqual(d['lane_set'][0], lane_set)
124
125
126     def test_invalid_library_json(self):
127         """
128         Make sure we get a 404 if we request an invalid library id
129         """
130         response = self.client.get('/samples/library/nottheone/json', apidata)
131         self.failUnlessEqual(response.status_code, 404)
132
133
134     def test_invalid_library(self):
135         response = self.client.get('/library/nottheone/')
136         self.failUnlessEqual(response.status_code, 404)
137
138
139     def test_library_no_key(self):
140         """
141         Make sure we get a 302 if we're not logged in
142         """
143         response = self.client.get('/samples/library/10981/json')
144         self.failUnlessEqual(response.status_code, 403)
145         response = self.client.get('/samples/library/10981/json', apidata)
146         self.failUnlessEqual(response.status_code, 200)
147
148     def test_library_rdf(self):
149         import RDF
150         from htsworkflow.util.rdfhelp import get_model, \
151              dump_model, \
152              fromTypedNode, \
153              load_string_into_model, \
154              rdfNS, \
155              libraryOntology
156         model = get_model()
157
158         response = self.client.get('/library/10981/')
159         self.assertEqual(response.status_code, 200)
160         content = response.content
161         load_string_into_model(model, 'rdfa', content)
162
163         body = """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
164         prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>
165
166         select ?library ?name ?library_id ?gel_cut ?made_by
167         where {
168            ?library a libns:library ;
169                     libns:name ?name ;
170                     libns:library_id ?library_id ;
171                     libns:gel_cut ?gel_cut ;
172                     libns:made_by ?made_by
173         }"""
174         query = RDF.SPARQLQuery(body)
175         for r in query.execute(model):
176             self.assertEqual(fromTypedNode(r['library_id']), u'10981')
177             self.assertEqual(fromTypedNode(r['name']),
178                              u'Paired End Multiplexed Sp-BAC')
179             self.assertEqual(fromTypedNode(r['gel_cut']), 400)
180             self.assertEqual(fromTypedNode(r['made_by']), u'Igor')
181
182         state = validate_xhtml(content)
183         if state is not None:
184             self.assertTrue(state)
185
186         # validate a library page.
187         from htsworkflow.util.rdfhelp import add_default_schemas
188         from htsworkflow.util.rdfinfer import Infer
189         add_default_schemas(model)
190         inference = Infer(model)
191         errmsgs = list(inference.run_validation())
192         self.assertEqual(len(errmsgs), 0)
193
194     def test_library_index_rdfa(self):
195         from htsworkflow.util.rdfhelp import \
196              add_default_schemas, get_model, load_string_into_model, \
197              dump_model
198         from htsworkflow.util.rdfinfer import Infer
199
200         model = get_model()
201         add_default_schemas(model)
202         inference = Infer(model)
203
204         response = self.client.get('/library/')
205         self.assertEqual(response.status_code, 200)
206         load_string_into_model(model, 'rdfa', response.content)
207
208         errmsgs = list(inference.run_validation())
209         self.assertEqual(len(errmsgs), 0)
210
211         body =  """prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
212         prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#>
213
214         select ?library ?library_id ?name ?species ?species_name
215         where {
216            ?library a libns:Library .
217            OPTIONAL { ?library libns:library_id ?library_id . }
218            OPTIONAL { ?library libns:species ?species .
219                       ?species libns:species_name ?species_name . }
220            OPTIONAL { ?library libns:name ?name . }
221         }"""
222         bindings = set(['library', 'library_id', 'name', 'species', 'species_name'])
223         query = RDF.SPARQLQuery(body)
224         count = 0
225         for r in query.execute(model):
226             count += 1
227             for name, value in r.items():
228                 self.assertTrue(name in bindings)
229                 self.assertTrue(value is not None)
230
231         self.assertEqual(count, len(Library.objects.filter(hidden=False)))
232
233         state = validate_xhtml(response.content)
234         if state is not None: self.assertTrue(state)
235
236
237 # The django test runner flushes the database between test suites not cases,
238 # so to be more compatible with running via nose we flush the database tables
239 # of interest before creating our sample data.
240 def create_db(obj):
241     obj.species_human = Species.objects.get(pk=8)
242     obj.experiment_rna_seq = ExperimentType.objects.get(pk=4)
243     obj.affiliation_alice = Affiliation.objects.get(pk=1)
244     obj.affiliation_bob = Affiliation.objects.get(pk=2)
245
246     Library.objects.all().delete()
247     obj.library_10001 = Library(
248         id = "10001",
249         library_name = 'C2C12 named poorly',
250         library_species = obj.species_human,
251         experiment_type = obj.experiment_rna_seq,
252         creation_date = datetime.datetime.now(),
253         made_for = 'scientist unit 2007',
254         made_by = 'microfludics system 7321',
255         stopping_point = '2A',
256         undiluted_concentration = '5.01',
257         hidden = False,
258     )
259     obj.library_10001.save()
260     obj.library_10002 = Library(
261         id = "10002",
262         library_name = 'Worm named poorly',
263         library_species = obj.species_human,
264         experiment_type = obj.experiment_rna_seq,
265         creation_date = datetime.datetime.now(),
266         made_for = 'scientist unit 2007',
267         made_by = 'microfludics system 7321',
268         stopping_point = '2A',
269         undiluted_concentration = '5.01',
270         hidden = False,
271     )
272     obj.library_10002.save()
273
274 try:
275     import RDF
276     HAVE_RDF = True
277
278     rdfNS = RDF.NS("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
279     xsdNS = RDF.NS("http://www.w3.org/2001/XMLSchema#")
280     libNS = RDF.NS("http://jumpgate.caltech.edu/wiki/LibraryOntology#")
281 except ImportError,e:
282     HAVE_RDF = False
283
284
285 class TestRDFaLibrary(TestCase):
286     fixtures = ['initial_data.json',
287                 'woldlab.json',
288                 'test_samples.json']
289
290     def test_parse_rdfa(self):
291         model = get_rdf_memory_model()
292         parser = RDF.Parser(name='rdfa')
293         url = '/library/10981/'
294         lib_response = self.client.get(url)
295         self.failIfEqual(len(lib_response.content), 0)
296
297         parser.parse_string_into_model(model,
298                                        lib_response.content,
299                                        'http://localhost'+url)
300         # http://jumpgate.caltech.edu/wiki/LibraryOntology#affiliation>
301         self.check_literal_object(model, ['Bob'], p=libNS['affiliation'])
302         self.check_literal_object(model, ['Multiplexed'], p=libNS['experiment_type'])
303         self.check_literal_object(model, ['400'], p=libNS['gel_cut'])
304         self.check_literal_object(model, ['Igor'], p=libNS['made_by'])
305         self.check_literal_object(model, ['Paired End Multiplexed Sp-BAC'], p=libNS['name'])
306         self.check_literal_object(model, ['Drosophila melanogaster'], p=libNS['species_name'])
307
308         self.check_uri_object(model,
309                               [u'http://localhost/lane/1193'],
310                               p=libNS['has_lane'])
311
312         fc_uri = RDF.Uri('http://localhost/flowcell/303TUAAXX/')
313         self.check_literal_object(model,
314                                   [u"303TUAAXX"],
315                                   s=fc_uri, p=libNS['flowcell_id'])
316
317     def check_literal_object(self, model, values, s=None, p=None, o=None):
318         statements = list(model.find_statements(
319             RDF.Statement(s,p,o)))
320         self.failUnlessEqual(len(statements), len(values),
321                         "Couln't find %s %s %s" % (s,p,o))
322         for s in statements:
323             self.failUnless(s.object.literal_value['string'] in values)
324
325
326     def check_uri_object(self, model, values, s=None, p=None, o=None):
327         statements = list(model.find_statements(
328             RDF.Statement(s,p,o)))
329         self.failUnlessEqual(len(statements), len(values),
330                         "Couln't find %s %s %s" % (s,p,o))
331         for s in statements:
332             self.failUnless(unicode(s.object.uri) in values)
333
334
335
336 def get_rdf_memory_model():
337     storage = RDF.MemoryStorage()
338     model = RDF.Model(storage)
339     return model
340
341 def suite():
342     from unittest import TestSuite, defaultTestLoader
343     suite = TestSuite()
344     suite.addTests(defaultTestLoader.loadTestsFromTestCase(LibraryTestCase))
345     suite.addTests(defaultTestLoader.loadTestsFromTestCase(SampleWebTestCase))
346     suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestRDFaLibrary))
347     return suite
348
349 if __name__ == "__main__":
350     from unittest import main
351     main(defaultTest="suite")