2 from __future__ import absolute_import
4 from datetime import datetime
6 from unittest import TestCase
10 import keyring.backend
12 class MockKeyring(keyring.backend.KeyringBackend):
14 def set_password(self, servicename, username, password):
17 def get_password(self, servicename, username):
20 def delete_password(self, servicename, username, password):
24 keyring.set_keyring(MockKeyring())
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
32 SOURCE_PATH = os.path.split(os.path.abspath(__file__))[0]
34 class TestEncodeFind(TestCase):
36 self.model = get_model()
37 add_default_schemas(self.model)
38 self.inference = Infer(self.model)
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)
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)
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))
70 def test_delete_simple_lane(self):
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#> .
79 <http://jumpgate.caltech.edu/lane/1232>
80 libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
81 libns:total_unique_locations 5789938 .
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)
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#> .
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 .
107 a libns:MappedCount ;
108 libns:mapped_to "newcontam_UK.fa"@en ;
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.
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#> .
127 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
128 <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> a libns:IlluminaFlowcell .
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 .
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 ;
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> .
145 a libns:MappedCount ;
146 libns:mapped_to "newcontam_UK.fa"@en ;
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 .
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 ;
162 a libns:MappedCount ;
163 libns:mapped_to "newcontam_UK.fa"@en ;
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),
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),
183 from unittest import TestSuite, defaultTestLoader
185 suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestEncodeFind))
188 if __name__ == "__main__":
189 from unittest import main