Port submission.py to rdflib
[htsworkflow.git] / htsworkflow / submission / test / test_submission.py
1
2 import os
3 import shutil
4 from unittest import TestCase, TestSuite, defaultTestLoader
5
6 from rdflib import Graph, Namespace
7
8 from htsworkflow.util.rdfhelp import \
9      dafTermOntology, \
10      get_turtle_header
11 from htsworkflow.submission.submission import list_submissions, Submission
12 from htsworkflow.submission.results import ResultMap
13 from .submission_test_common import  (
14     generate_sample_results_tree,
15     S1_NAME,
16     S2_NAME,
17     S1_TURTLE,
18     S2_TURTLE,
19     MockAddDetails,
20 )
21
22 #import logging
23 #logging.basicConfig(level=logging.DEBUG)
24
25 class TestSubmissionModule(TestCase):
26     def test_empty_list_submission(self):
27         model = Graph()
28         self.assertEqual(len(list(list_submissions(model))), 0)
29
30     def test_one_submission(self):
31         model = Graph()
32         model.parse(data="""
33             @prefix subns: <http://jumpgate.caltech.edu/wiki/UcscSubmissionOntology#> .
34             @prefix test: <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test#> .
35
36             <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test#>
37                subns:has_submission test:lib1 ;
38                subns:has_submission test:lib2.
39             """, format='turtle')
40         submissions = list(list_submissions(model))
41         self.assertEqual(len(submissions), 1)
42         self.assertEqual(submissions[0], "test")
43
44     def test_two_submission(self):
45         model = Graph()
46         model.parse(data="""
47             @prefix subns: <http://jumpgate.caltech.edu/wiki/UcscSubmissionOntology#> .
48             @prefix test: <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test#> .
49
50             <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test1#>
51                subns:has_submission test:lib1 .
52             <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test2#>
53                subns:has_submission test:lib2 .
54             """, format="turtle")
55         submissions = list(list_submissions(model))
56         self.assertEqual(len(submissions), 2)
57         truth = set(["test1", "test2"])
58         testset = set()
59         for name in submissions:
60             testset.add(name)
61         self.assertEqual(testset, truth)
62
63 class TestSubmission(TestCase):
64     def setUp(self):
65         generate_sample_results_tree(self, 'submission_test')
66         self.model = Graph()
67
68     def tearDown(self):
69         shutil.rmtree(self.tempdir)
70
71     def test_create_submission(self):
72         s = Submission('foo', self.model, 'http://localhost')
73         self.assertEqual(str(s.submissionSet),
74                          "http://jumpgate.caltech.edu/wiki/SubmissionsLog/foo")
75         self.assertEqual(str(s.submissionSetNS['']),
76                          str(Namespace(str(s.submissionSet) + '#')['']))
77         self.assertEqual(str(s.libraryNS['']),
78                          str(Namespace('http://localhost/library/')['']))
79
80     def test_scan_submission_dirs(self):
81         turtle = get_turtle_header() + r"""
82 @prefix thisView: <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/> .
83 thisView:Fastq ucscDaf:filename_re ".*[^12]\\.fastq$" ;
84                a geoSoft:raw ;
85                geoSoft:fileTypeLabel "fastq" ;
86                ucscDaf:output_type "read" .
87 thisView:FastqRead1 ucscDaf:filename_re ".*r1\\.fastq$" ;
88                a geoSoft:raw ;
89                geoSoft:fileTypeLabel "fastq" ;
90                ucscDaf:output_type "read1" .
91 thisView:FastqRead2 ucscDaf:filename_re ".*r2\\.fastq$" ;
92                a geoSoft:raw ;
93                geoSoft:fileTypeLabel "fastq" ;
94                ucscDaf:output_type "read2" .
95 thisView:alignments ucscDaf:filename_re ".*\\.bam$" ;
96                a geoSoft:supplemental ;
97                geoSoft:fileTypeLabel "bam" ;
98                ucscDaf:output_type "alignments" .
99
100         """
101         resultmap = ResultMap()
102         resultmap['1000'] = os.path.join(self.sourcedir, S1_NAME)
103         resultmap['2000'] = os.path.join(self.sourcedir, S2_NAME)
104
105         s = Submission('foo', self.model, 'http://localhost')
106         mock = MockAddDetails(self.model, turtle)
107         mock.add_turtle(S1_TURTLE)
108         mock.add_turtle(S2_TURTLE)
109         #s._add_library_details_to_model(mock)
110         s.scan_submission_dirs(resultmap)
111
112         nodes = list(s.analysis_nodes(resultmap))
113         self.assertEqual(len(nodes), 2)
114         expected = set((
115             'http://jumpgate.caltech.edu/wiki/SubmissionsLog/foo#1000-sample',
116             'http://jumpgate.caltech.edu/wiki/SubmissionsLog/foo#2000-sample',
117         ))
118         got = set((str(nodes[0]), str(nodes[1])))
119         self.assertEqual(expected, got)
120
121     def test_find_best_match(self):
122         turtle = get_turtle_header() + r"""
123 @prefix thisView: <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/> .
124 thisView:Fastq ucscDaf:filename_re ".*[^12]\\.fastq\\.bz2$" ;
125                a geoSoft:raw ;
126                geoSoft:fileTypeLabel "fastq" ;
127                ucscDaf:output_type "read" .
128 thisView:FastqRead1 ucscDaf:filename_re ".*r1\\.fastq\\.bz2$" ;
129                a geoSoft:raw ;
130                geoSoft:fileTypeLabel "fastq" ;
131                ucscDaf:output_type "read1" .
132 thisView:FastqRead2 ucscDaf:filename_re ".*r2\\.fastq\\.bz2$" ;
133                a geoSoft:raw ;
134                geoSoft:fileTypeLabel "fastq" ;
135                ucscDaf:output_type "read2" .
136 thisView:alignments ucscDaf:filename_re ".*\\.bam$" ;
137                a geoSoft:supplemental ;
138                geoSoft:fileTypeLabel "bam" ;
139                ucscDaf:output_type "alignments" .
140
141         """
142         self.model.parse(data=turtle, format='turtle')
143         s = Submission('foo', self.model, 'http://localhost')
144         q = (None, dafTermOntology['filename_re'], None)
145         view_map = s._get_filename_view_map()
146         self.assertEqual(len(view_map), 4)
147
148         fastq = s.find_best_match("asdf.fastq.bz2")
149         self.assertEqual(
150             str(fastq),
151             "http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/Fastq")
152
153         fastq = s.find_best_match("asdf.r2.fastq.bz2")
154         self.assertEqual(
155             str(fastq),
156             "http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/FastqRead2")
157
158 def suite():
159     suite = TestSuite()
160     suite.addTests(
161         defaultTestLoader.loadTestsFromTestCase(TestSubmissionModule))
162     suite.addTests(
163         defaultTestLoader.loadTestsFromTestCase(TestSubmission))
164     return suite
165
166 if __name__ == "__main__":
167     from unittest import main
168     main(defaultTest='suite')