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