use absolute_import
[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 import RDF
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, get_model, fromTypedNode
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 = get_model()
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 = RDF.Uri(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.uri), 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.uri), 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 = fromTypedNode(dates[0].object)
68         self.assertEqual(object_date, datetime(2011,12,7,15,23,0))
69
70     def test_delete_simple_lane(self):
71         model = get_model()
72         parser = RDF.Parser(name='turtle')
73         parser.parse_string_into_model(model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
74 @prefix : <http://www.w3.org/1999/xhtml> .
75 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
76 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
77 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
78
79 <http://jumpgate.caltech.edu/lane/1232>
80     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
81     libns:total_unique_locations 5789938 .
82
83 ''', 'http://jumpgate.caltech.edu/library/')
84         errmsgs = list(self.inference.run_validation())
85         self.assertEqual(len(errmsgs), 0)
86         urn = RDF.Node(RDF.Uri('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         parser = RDF.Parser(name='turtle')
93         parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
94 @prefix : <http://www.w3.org/1999/xhtml> .
95 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
96 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
97 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
98
99 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
100 <http://jumpgate.caltech.edu/lane/1232>
101     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
102     libns:has_mappings _:bnode110110 ;
103     libns:total_unique_locations 5789938 ;
104     a libns:IlluminaLane .
105
106 _:bnode110110
107     a libns:MappedCount ;
108     libns:mapped_to "newcontam_UK.fa"@en ;
109     libns:reads 42473 .
110 ''', '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 = RDF.Node(RDF.Uri('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         parser = RDF.Parser(name='turtle')
121         parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
122 @prefix : <http://www.w3.org/1999/xhtml> .
123 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
124 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
125 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
126
127 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
128 <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> a libns:IlluminaFlowcell .
129
130 <http://jumpgate.caltech.edu/lane/1232>
131     a libns:IlluminaLane ;
132     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
133     libns:has_mappings _:bnode110110 ;
134     libns:total_unique_locations 5789938 .
135
136 <http://jumpgate.caltech.edu/library/11011/>
137     libns:affiliation "ENCODE"@en, "ENCODE_Tier1"@en, "Georgi Marinov"@en ;
138     libns:has_lane <http://jumpgate.caltech.edu/lane/1232> ;
139     libns:library_id "11011"@en ;
140     libns:library_type "None"@en ;
141     a libns:Library ;
142     <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> .
143
144 _:bnode110110
145     a libns:MappedCount ;
146     libns:mapped_to "newcontam_UK.fa"@en ;
147     libns:reads 42473 .
148
149 <http://jumpgate.caltech.edu/lane/1903>
150     a libns:IlluminaLane ;
151     libns:flowcell <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> ;
152     libns:has_mappings _:bnode120970 ;
153     libns:total_unique_locations 39172114 .
154
155 <http://jumpgate.caltech.edu/library/12097/>
156     libns:has_lane <http://jumpgate.caltech.edu/lane/1903> ;
157     libns:library_id "12097"@en ;
158     libns:library_type "Paired End (non-multiplexed)"@en ;
159     a libns:Library .
160
161 _:bnode120970
162     a libns:MappedCount ;
163     libns:mapped_to "newcontam_UK.fa"@en ;
164     libns:reads 64 .
165 ''', 'http://jumpgate.caltech.edu/library')
166         errmsgs = list(self.inference.run_validation())
167         self.assertEqual(len(errmsgs), 0)
168         urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/library/11011/'))
169         encode_find.delete_library(self.model, urn)
170         q = RDF.Statement(None, encode_find.libraryOntology['reads'], None)
171         stmts = list(self.model.find_statements(q))
172         self.failUnlessEqual(len(stmts), 1)
173         self.failUnlessEqual(fromTypedNode(stmts[0].object),
174                              64)
175
176         q = RDF.Statement(None, encode_find.libraryOntology['library_id'], None)
177         stmts = list(self.model.find_statements(q))
178         self.failUnlessEqual(len(stmts), 1)
179         self.failUnlessEqual(fromTypedNode(stmts[0].object),
180                              '12097')
181
182 def suite():
183     from unittest import TestSuite, defaultTestLoader
184     suite = TestSuite()
185     suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestEncodeFind))
186     return suite
187
188 if __name__ == "__main__":
189     from unittest import main
190     main()