Match what generate_sample_results_tree creates
[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         s = Submission('foo', self.model, 'http://localhost')
76         self.assertEqual(str(s.submissionSet),
77                          "http://jumpgate.caltech.edu/wiki/SubmissionsLog/foo")
78         self.assertEqual(str(s.submissionSetNS['']),
79                          str(RDF.NS(str(s.submissionSet) + '#')['']))
80         self.assertEqual(str(s.libraryNS['']),
81                          str(RDF.NS('http://localhost/library/')['']))
82
83     def test_scan_submission_dirs(self):
84         turtle = get_turtle_header() + """
85 @prefix thisView: <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/> .
86 thisView:Fastq ucscDaf:filename_re ".*[^12]\\.fastq$" ;
87                a geoSoft:raw ;
88                geoSoft:fileTypeLabel "fastq" ;
89                ucscDaf:output_type "read" .
90 thisView:FastqRead1 ucscDaf:filename_re ".*r1\\.fastq$" ;
91                a geoSoft:raw ;
92                geoSoft:fileTypeLabel "fastq" ;
93                ucscDaf:output_type "read1" .
94 thisView:FastqRead2 ucscDaf:filename_re ".*r2\\.fastq$" ;
95                a geoSoft:raw ;
96                geoSoft:fileTypeLabel "fastq" ;
97                ucscDaf:output_type "read2" .
98 thisView:alignments ucscDaf:filename_re ".*\\.bam$" ;
99                a geoSoft:supplemental ;
100                geoSoft:fileTypeLabel "bam" ;
101                ucscDaf:output_type "alignments" .
102
103         """
104         map = ResultMap()
105         map['1000'] = os.path.join(self.sourcedir, S1_NAME)
106         map['2000'] = os.path.join(self.sourcedir, S2_NAME)
107
108         s = Submission('foo', self.model, 'http://localhost')
109         mock = MockAddDetails(self.model, turtle)
110         mock.add_turtle(S1_TURTLE)
111         mock.add_turtle(S2_TURTLE)
112         s._add_library_details_to_model =  mock
113         s.scan_submission_dirs(map)
114
115         nodes = list(s.analysis_nodes(map))
116         self.assertEqual(len(nodes), 2)
117         expected = set((
118             'http://jumpgate.caltech.edu/wiki/SubmissionsLog/foo#1000-sample',
119             'http://jumpgate.caltech.edu/wiki/SubmissionsLog/foo#2000-sample',
120         ))
121         got = set((str(nodes[0]), str(nodes[1])))
122         self.assertEqual(expected, got)
123
124     def test_find_best_match(self):
125         turtle = get_turtle_header() + """
126 @prefix thisView: <http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/> .
127 thisView:Fastq ucscDaf:filename_re ".*[^12]\\.fastq\\.bz2$" ;
128                a geoSoft:raw ;
129                geoSoft:fileTypeLabel "fastq" ;
130                ucscDaf:output_type "read" .
131 thisView:FastqRead1 ucscDaf:filename_re ".*r1\\.fastq\\.bz2$" ;
132                a geoSoft:raw ;
133                geoSoft:fileTypeLabel "fastq" ;
134                ucscDaf:output_type "read1" .
135 thisView:FastqRead2 ucscDaf:filename_re ".*r2\\.fastq\\.bz2$" ;
136                a geoSoft:raw ;
137                geoSoft:fileTypeLabel "fastq" ;
138                ucscDaf:output_type "read2" .
139 thisView:alignments ucscDaf:filename_re ".*\\.bam$" ;
140                a geoSoft:supplemental ;
141                geoSoft:fileTypeLabel "bam" ;
142                ucscDaf:output_type "alignments" .
143
144         """
145         load_string_into_model(self.model, 'turtle', turtle)
146         s = Submission('foo', self.model, 'http://localhost')
147         q = RDF.Statement(None, dafTermOntology['filename_re'], None)
148         view_map = s._get_filename_view_map()
149         self.assertEqual(len(view_map), 4)
150
151         fastq = s.find_best_match("asdf.fastq.bz2")
152         self.assertEqual(
153             str(fastq),
154             "http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/Fastq")
155
156         fastq = s.find_best_match("asdf.r2.fastq.bz2")
157         self.assertEqual(
158             str(fastq),
159             "http://jumpgate.caltech.edu/wiki/SubmissionsLog/test/view/FastqRead2")
160
161 def suite():
162     suite = TestSuite()
163     suite.addTests(
164         defaultTestLoader.loadTestsFromTestCase(TestSubmissionModule))
165     suite.addTests(
166         defaultTestLoader.loadTestsFromTestCase(TestSubmission))
167     return suite
168
169 if __name__ == "__main__":
170     from unittest import main
171     main(defaultTest='suite')