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