Use htsworkflow ontology to validate various RDF using components.
[htsworkflow.git] / encode_submission / test_encode_find.py
1 #!/usr/bin/env python
2 from datetime import datetime
3 import os
4 import unittest
5
6 import RDF
7
8 import encode_find
9 from htsworkflow.submission.ucsc import submission_view_url
10 from htsworkflow.util.rdfhelp import add_default_schemas, \
11      dump_model, get_model, fromTypedNode
12 from htsworkflow.util.rdfinfer import Infer
13
14 SOURCE_PATH = os.path.split(os.path.abspath(__file__))[0]
15 print SOURCE_PATH
16
17 class TestEncodeFind(unittest.TestCase):
18     def setUp(self):
19         self.model = get_model()
20         add_default_schemas(self.model)
21         self.inference = Infer(self.model)
22
23     def test_create_status_node_with_uri(self):
24         subURL = submission_view_url('5136')
25         submissionUri = RDF.Uri(subURL)
26         timestamp = '2011-12-19T12:42:53.048956'
27         manualUri = subURL + '/' + timestamp
28         nodeUri = encode_find.create_status_node(submissionUri, timestamp)
29         self.assertEqual(str(nodeUri.uri), manualUri)
30
31     def test_create_status_node_with_str(self):
32         subURL = submission_view_url('5136')
33         timestamp = '2011-12-19T12:42:53.048956'
34         manualUri = subURL + '/' + timestamp
35         nodeUri = encode_find.create_status_node(subURL, timestamp)
36         self.assertEqual(str(nodeUri.uri), manualUri)
37
38     def test_parse_submission_page(self):
39         timestamp = '2011-12-19T12:42:53.048956'
40         subURL = submission_view_url('5136')
41         subNode = encode_find.create_status_node(subURL, timestamp)
42         test_file = os.path.join(SOURCE_PATH, 'testdata', '5136SubDetail.html')
43         from lxml.html import parse
44         tree = parse(test_file)
45         dates = encode_find.get_creation_dates(self.model, subNode)
46         self.assertEqual(len(dates), 0)
47         encode_find.parse_submission_page(self.model, tree, subNode)
48         dates = encode_find.get_creation_dates(self.model, subNode)
49         self.assertEqual(len(dates), 1)
50         object_date = fromTypedNode(dates[0].object)
51         self.assertEqual(object_date, datetime(2011,12,7,15,23,0))
52
53     def test_delete_simple_lane(self):
54         model = get_model()
55         parser = RDF.Parser(name='turtle')
56         parser.parse_string_into_model(model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
57 @prefix : <http://www.w3.org/1999/xhtml> .
58 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
59 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
60 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
61
62 <http://jumpgate.caltech.edu/lane/1232>
63     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
64     libns:total_unique_locations 5789938 .
65
66 ''', 'http://jumpgate.caltech.edu/library/')
67         errmsgs = list(self.inference.run_validation())
68         self.assertEqual(len(errmsgs), 0)
69         urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/lane/1232'))
70         encode_find.delete_lane(model, urn)
71         self.failUnlessEqual(len(model), 0)
72
73     def test_delete_lane_with_mapping(self):
74         ontology_size = len(self.model)
75         parser = RDF.Parser(name='turtle')
76         parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
77 @prefix : <http://www.w3.org/1999/xhtml> .
78 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
79 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
80 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
81
82 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
83 <http://jumpgate.caltech.edu/lane/1232>
84     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
85     libns:has_mappings _:bnode110110 ;
86     libns:total_unique_locations 5789938 ;
87     a libns:IlluminaLane .
88
89 _:bnode110110
90     a libns:MappedCount ;
91     libns:mapped_to "newcontam_UK.fa"@en ;
92     libns:reads 42473 .
93 ''', 'http://jumpgate.caltech.edu/library/')
94         errmsgs = list(self.inference.run_validation())
95         self.assertEqual(len(errmsgs), 0)
96         self.failUnlessEqual(len(self.model), 8 + ontology_size)
97         urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/lane/1232'))
98         encode_find.delete_lane(self.model, urn)
99         self.failUnlessEqual(len(self.model), 1 + ontology_size)
100         # the flowcell triple wasn't deleted.
101
102     def test_delete_library(self):
103         parser = RDF.Parser(name='turtle')
104         parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
105 @prefix : <http://www.w3.org/1999/xhtml> .
106 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
107 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
108 @prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
109
110 <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
111 <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> a libns:IlluminaFlowcell .
112
113 <http://jumpgate.caltech.edu/lane/1232>
114     a libns:IlluminaLane ;
115     libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
116     libns:has_mappings _:bnode110110 ;
117     libns:total_unique_locations 5789938 .
118
119 <http://jumpgate.caltech.edu/library/11011/>
120     libns:affiliation "ENCODE"@en, "ENCODE_Tier1"@en, "Georgi Marinov"@en ;
121     libns:has_lane <http://jumpgate.caltech.edu/lane/1232> ;
122     libns:library_id "11011"@en ;
123     libns:library_type "None"@en ;
124     a libns:Library ;
125     <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> .
126
127 _:bnode110110
128     a libns:MappedCount ;
129     libns:mapped_to "newcontam_UK.fa"@en ;
130     libns:reads 42473 .
131
132 <http://jumpgate.caltech.edu/lane/1903>
133     a libns:IlluminaLane ;
134     libns:flowcell <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> ;
135     libns:has_mappings _:bnode120970 ;
136     libns:total_unique_locations 39172114 .
137
138 <http://jumpgate.caltech.edu/library/12097/>
139     libns:has_lane <http://jumpgate.caltech.edu/lane/1903> ;
140     libns:library_id "12097"@en ;
141     libns:library_type "Paired End (non-multiplexed)"@en ;
142     a libns:Library .
143
144 _:bnode120970
145     a libns:MappedCount ;
146     libns:mapped_to "newcontam_UK.fa"@en ;
147     libns:reads 64 .
148 ''', 'http://jumpgate.caltech.edu/library')
149         errmsgs = list(self.inference.run_validation())
150         self.assertEqual(len(errmsgs), 0)
151         urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/library/11011/'))
152         encode_find.delete_library(self.model, urn)
153         q = RDF.Statement(None, encode_find.libraryOntology['reads'], None)
154         stmts = list(self.model.find_statements(q))
155         self.failUnlessEqual(len(stmts), 1)
156         self.failUnlessEqual(fromTypedNode(stmts[0].object),
157                              64)
158
159         q = RDF.Statement(None, encode_find.libraryOntology['library_id'], None)
160         stmts = list(self.model.find_statements(q))
161         self.failUnlessEqual(len(stmts), 1)
162         self.failUnlessEqual(fromTypedNode(stmts[0].object),
163                              '12097')
164
165 def suite():
166     return unittest.makeSuite(TestEncodeFind, "test")
167
168 if __name__ == "__main__":
169     unittest.main(defaultTest="suite")