mark the example submission rule files as being raw, so the escapes dont get confused
[htsworkflow.git] / encode_submission / test_encode_find.py
index 98bdb46d11f6e200b7a35567a16b4477cb9b7a69..a81f27a35058c6a6bb39e5fe21909af0c21d6f85 100644 (file)
@@ -1,17 +1,24 @@
 #!/usr/bin/env python
+from datetime import datetime
 import os
-import unittest
+from unittest import TestCase
 
 import RDF
 
 import encode_find
 from htsworkflow.submission.ucsc import submission_view_url
-from htsworkflow.util.rdfhelp import dump_model, get_model
+from htsworkflow.util.rdfhelp import add_default_schemas, \
+     dump_model, get_model, fromTypedNode
+from htsworkflow.util.rdfinfer import Infer
 
 SOURCE_PATH = os.path.split(os.path.abspath(__file__))[0]
-print SOURCE_PATH
 
-class TestEncodeFind(unittest.TestCase):
+class TestEncodeFind(TestCase):
+    def setUp(self):
+        self.model = get_model()
+        add_default_schemas(self.model)
+        self.inference = Infer(self.model)
+
     def test_create_status_node_with_uri(self):
         subURL = submission_view_url('5136')
         submissionUri = RDF.Uri(subURL)
@@ -34,16 +41,133 @@ class TestEncodeFind(unittest.TestCase):
         test_file = os.path.join(SOURCE_PATH, 'testdata', '5136SubDetail.html')
         from lxml.html import parse
         tree = parse(test_file)
-        model = get_model()
-        dates = encode_find.get_creation_dates(model, subNode)
+        dates = encode_find.get_creation_dates(self.model, subNode)
         self.assertEqual(len(dates), 0)
-        encode_find.parse_submission_page(model, tree, subNode)
-        dates = encode_find.get_creation_dates(model, subNode)
+        encode_find.parse_submission_page(self.model, tree, subNode)
+        dates = encode_find.get_creation_dates(self.model, subNode)
         self.assertEqual(len(dates), 1)
-        self.assertEqual(str(dates[0].object), '2011-12-07T15:23:00')
+        object_date = fromTypedNode(dates[0].object)
+        self.assertEqual(object_date, datetime(2011,12,7,15,23,0))
+
+    def test_delete_simple_lane(self):
+        model = get_model()
+        parser = RDF.Parser(name='turtle')
+        parser.parse_string_into_model(model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix : <http://www.w3.org/1999/xhtml> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+@prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
+
+<http://jumpgate.caltech.edu/lane/1232>
+    libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
+    libns:total_unique_locations 5789938 .
+
+''', 'http://jumpgate.caltech.edu/library/')
+        errmsgs = list(self.inference.run_validation())
+        self.assertEqual(len(errmsgs), 0)
+        urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/lane/1232'))
+        encode_find.delete_lane(model, urn)
+        self.failUnlessEqual(len(model), 0)
+
+    def test_delete_lane_with_mapping(self):
+        ontology_size = len(self.model)
+        parser = RDF.Parser(name='turtle')
+        parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix : <http://www.w3.org/1999/xhtml> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+@prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
+
+<http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
+<http://jumpgate.caltech.edu/lane/1232>
+    libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
+    libns:has_mappings _:bnode110110 ;
+    libns:total_unique_locations 5789938 ;
+    a libns:IlluminaLane .
+
+_:bnode110110
+    a libns:MappedCount ;
+    libns:mapped_to "newcontam_UK.fa"@en ;
+    libns:reads 42473 .
+''', 'http://jumpgate.caltech.edu/library/')
+        errmsgs = list(self.inference.run_validation())
+        self.assertEqual(len(errmsgs), 0)
+        self.failUnlessEqual(len(self.model), 8 + ontology_size)
+        urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/lane/1232'))
+        encode_find.delete_lane(self.model, urn)
+        self.failUnlessEqual(len(self.model), 1 + ontology_size)
+        # the flowcell triple wasn't deleted.
+
+    def test_delete_library(self):
+        parser = RDF.Parser(name='turtle')
+        parser.parse_string_into_model(self.model, '''@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix : <http://www.w3.org/1999/xhtml> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+@prefix libns: <http://jumpgate.caltech.edu/wiki/LibraryOntology#> .
+
+<http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> a libns:IlluminaFlowcell .
+<http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> a libns:IlluminaFlowcell .
+
+<http://jumpgate.caltech.edu/lane/1232>
+    a libns:IlluminaLane ;
+    libns:flowcell <http://jumpgate.caltech.edu/flowcell/42JV5AAXX/> ;
+    libns:has_mappings _:bnode110110 ;
+    libns:total_unique_locations 5789938 .
+
+<http://jumpgate.caltech.edu/library/11011/>
+    libns:affiliation "ENCODE"@en, "ENCODE_Tier1"@en, "Georgi Marinov"@en ;
+    libns:has_lane <http://jumpgate.caltech.edu/lane/1232> ;
+    libns:library_id "11011"@en ;
+    libns:library_type "None"@en ;
+    a libns:Library ;
+    <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> .
+
+_:bnode110110
+    a libns:MappedCount ;
+    libns:mapped_to "newcontam_UK.fa"@en ;
+    libns:reads 42473 .
+
+<http://jumpgate.caltech.edu/lane/1903>
+    a libns:IlluminaLane ;
+    libns:flowcell <http://jumpgate.caltech.edu/flowcell/62WCKAAXX/> ;
+    libns:has_mappings _:bnode120970 ;
+    libns:total_unique_locations 39172114 .
+
+<http://jumpgate.caltech.edu/library/12097/>
+    libns:has_lane <http://jumpgate.caltech.edu/lane/1903> ;
+    libns:library_id "12097"@en ;
+    libns:library_type "Paired End (non-multiplexed)"@en ;
+    a libns:Library .
+
+_:bnode120970
+    a libns:MappedCount ;
+    libns:mapped_to "newcontam_UK.fa"@en ;
+    libns:reads 64 .
+''', 'http://jumpgate.caltech.edu/library')
+        errmsgs = list(self.inference.run_validation())
+        self.assertEqual(len(errmsgs), 0)
+        urn = RDF.Node(RDF.Uri('http://jumpgate.caltech.edu/library/11011/'))
+        encode_find.delete_library(self.model, urn)
+        q = RDF.Statement(None, encode_find.libraryOntology['reads'], None)
+        stmts = list(self.model.find_statements(q))
+        self.failUnlessEqual(len(stmts), 1)
+        self.failUnlessEqual(fromTypedNode(stmts[0].object),
+                             64)
+
+        q = RDF.Statement(None, encode_find.libraryOntology['library_id'], None)
+        stmts = list(self.model.find_statements(q))
+        self.failUnlessEqual(len(stmts), 1)
+        self.failUnlessEqual(fromTypedNode(stmts[0].object),
+                             '12097')
 
 def suite():
-    return unittest.makeSuite(TestEncodeFind, "test")
+    from unittest import TestSuite, defaultTestLoader
+    suite = TestSuite()
+    suite.addTests(defaultTestLoader.loadTestsFromTestCase(TestEncodeFind))
+    return suite
+
 
 if __name__ == "__main__":
-    unittest.main(defaultTest="suite")
+    from unittest import main
+    main()