Test htsworkflow under several different django & python versions
[htsworkflow.git] / encode_submission / test_encode_find.py
1 #!/usr/bin/env python
2 from __future__ import absolute_import
3
4 from datetime import datetime
5 import os
6 from unittest import TestCase
7
8 from rdflib import ConjunctiveGraph, URIRef
9
10 import keyring.backend
11
12 class MockKeyring(keyring.backend.KeyringBackend):
13     priority = 1
14     def set_password(self, servicename, username, password):
15         pass
16
17     def get_password(self, servicename, username):
18         return "example"
19
20     def delete_password(self, servicename, username, password):
21         pass
22
23 import keyring
24 keyring.set_keyring(MockKeyring())
25
26 from . import encode_find
27 from htsworkflow.submission.ucsc import submission_view_url
28 from htsworkflow.util.rdfhelp import add_default_schemas, \
29      dump_model
30 from htsworkflow.util.rdfinfer import Infer
31
32 SOURCE_PATH = os.path.split(os.path.abspath(__file__))[0]
33
34 class TestEncodeFind(TestCase):
35     def setUp(self):
36         self.model = ConjunctiveGraph()
37         add_default_schemas(self.model)
38         self.inference = Infer(self.model)
39
40     def test_create_status_node_with_uri(self):
41         subURL = submission_view_url('5136')
42         submissionUri = URIRef(subURL)
43         timestamp = '2011-12-19T12:42:53.048956'
44         manualUri = subURL + '/' + timestamp
45         nodeUri = encode_find.create_status_node(submissionUri, timestamp)
46         self.assertEqual(str(nodeUri), manualUri)
47
48     def test_create_status_node_with_str(self):
49         subURL = submission_view_url('5136')
50         timestamp = '2011-12-19T12:42:53.048956'
51         manualUri = subURL + '/' + timestamp
52         nodeUri = encode_find.create_status_node(subURL, timestamp)
53         self.assertEqual(str(nodeUri), manualUri)
54
55     def test_parse_submission_page(self):
56         timestamp = '2011-12-19T12:42:53.048956'
57         subURL = submission_view_url('5136')
58         subNode = encode_find.create_status_node(subURL, timestamp)
59         test_file = os.path.join(SOURCE_PATH, 'testdata', '5136SubDetail.html')
60         from lxml.html import parse
61         tree = parse(test_file)
62         dates = encode_find.get_creation_dates(self.model, subNode)
63         self.assertEqual(len(dates), 0)
64         encode_find.parse_submission_page(self.model, tree, subNode)
65         dates = encode_find.get_creation_dates(self.model, subNode)
66         self.assertEqual(len(dates), 1)
67         object_date = dates[0][2].toPython()
68         self.assertEqual(object_date, datetime(2011,12,7,15,23,0))
69
70     def test_delete_simple_lane(self):
71         model = ConjunctiveGraph()
72         lane_1232_ttl ='''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
73 @prefix : <http://www.w3.org/1999/xhtml> .
74 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
75 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
76 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
77
78 <http://jumpgate.caltech.edu/lane/1232>
79     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
80     libns:total_unique_locations 5789938 .
81
82 '''
83         model.parse(data=lane_1232_ttl, format='turtle', publicID='http://jumpgate.caltech.edu/library/')
84         errmsgs = list(self.inference.run_validation())
85         self.assertEqual(len(errmsgs), 0)
86         urn = URIRef('http://jumpgate.caltech.edu/lane/1232')
87         encode_find.delete_lane(model, urn)
88         self.failUnlessEqual(len(model), 0)
89
90     def test_delete_lane_with_mapping(self):
91         ontology_size = len(self.model)
92         lane_1232_ttl = '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
93 @prefix : <http://www.w3.org/1999/xhtml> .
94 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
95 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
96 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
97
98 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
99 <http://jumpgate.caltech.edu/lane/1232>
100     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
101     libns:has_mappings _:bnode110110 ;
102     libns:total_unique_locations 5789938 ;
103     a libns:IlluminaLane .
104
105 _:bnode110110
106     a libns:MappedCount ;
107     libns:mapped_to "newcontam_UK.fa"@en ;
108     libns:reads 42473 .
109 '''
110         self.model.parse(data=lane_1232_ttl, format='turtle', publicID='http://jumpgate.caltech.edu/library/')
111         errmsgs = list(self.inference.run_validation())
112         self.assertEqual(len(errmsgs), 0)
113         self.failUnlessEqual(len(self.model), 8 + ontology_size)
114         urn = URIRef('http://jumpgate.caltech.edu/lane/1232')
115         encode_find.delete_lane(self.model, urn)
116         self.failUnlessEqual(len(self.model), 1 + ontology_size)
117         # the flowcell triple wasn't deleted.
118
119     def test_delete_library(self):
120         ttl = '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
121 @prefix : <http://www.w3.org/1999/xhtml> .
122 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
123 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
124 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
125
126 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
127 <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> a libns:IlluminaFlowcell .
128
129 <http://jumpgate.caltech.edu/lane/1232>
130     a libns:IlluminaLane ;
131     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
132     libns:has_mappings _:bnode110110 ;
133     libns:total_unique_locations 5789938 .
134
135 <http://jumpgate.caltech.edu/library/11011/>
136     libns:affiliation "ENCODE"@en, "ENCODE_Tier1"@en, "Georgi Marinov"@en ;
137     libns:has_lane <http://jumpgate.caltech.edu/lane/1232> ;
138     libns:library_id "11011"@en ;
139     libns:library_type "None"@en ;
140     a libns:Library ;
141     <http://www.w3.org/1999/xhtml/vocab#stylesheet> <http://jumpgate.caltech.edu/static/css/app.css>, <http://jumpgate.caltech.edu/static/css/data-browse-index.css> .
142
143 _:bnode110110
144     a libns:MappedCount ;
145     libns:mapped_to "newcontam_UK.fa"@en ;
146     libns:reads 42473 .
147
148 <http://jumpgate.caltech.edu/lane/1903>
149     a libns:IlluminaLane ;
150     libns:flowcell <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> ;
151     libns:has_mappings _:bnode120970 ;
152     libns:total_unique_locations 39172114 .
153
154 <http://jumpgate.caltech.edu/library/12097/>
155     libns:has_lane <http://jumpgate.caltech.edu/lane/1903> ;
156     libns:library_id "12097"@en ;
157     libns:library_type "Paired End (non-multiplexed)"@en ;
158     a libns:Library .
159
160 _:bnode120970
161     a libns:MappedCount ;
162     libns:mapped_to "newcontam_UK.fa"@en ;
163     libns:reads 64 .
164 '''
165         self.model.parse(data=ttl, format='turtle', publicID='http://jumpgate.caltech.edu/library')
166         errmsgs = list(self.inference.run_validation())
167         self.assertEqual(len(errmsgs), 0)
168         urn = URIRef('http://jumpgate.caltech.edu/library/11011/')
169         encode_find.delete_library(self.model, urn)
170         q = (None, encode_find.libraryOntology['reads'], None)
171         stmts = list(self.model.triples(q))
172         self.failUnlessEqual(len(stmts), 1)
173         self.failUnlessEqual(stmts[0][2].toPython(), 64)
174
175         q = (None, encode_find.libraryOntology['library_id'], None)
176         stmts = list(self.model.triples(q))
177         self.failUnlessEqual(len(stmts), 1)
178         self.failUnlessEqual(stmts[0][2].toPython(), '12097')
179
180 def suite():
181     from unittest import TestSuite, defaultTestLoader
182     suite = TestSuite()
183     suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestEncodeFind))
184     return suite
185
186 if __name__ == "__main__":
187     from unittest import main
188     main()