Get actual list of sequencers used for a library.
[htsworkflow.git] / htsworkflow / submission / geo.py
1 import logging
2 import os
3
4 import RDF
5
6 from htsworkflow.submission.submission import Submission
7
8 from htsworkflow.util.rdfhelp import \
9      fromTypedNode, \
10      geoSoftNS, \
11      simplifyUri, \
12      submissionOntology
13
14 from django.conf import settings
15 from django.template import Context, loader
16
17 LOGGER = logging.getLogger(__name__)
18
19 class GEOSubmission(Submission):
20     def __init__(self, name, model):
21         super(GEOSubmission, self).__init__(name, model)
22
23     def make_soft(self, result_map):
24         samples = []
25         platform = self.get_platform_metadata()
26         platform_attribs = dict(platform)
27         platform_id = platform_attribs['^platform']
28         series = self.get_series_metadata()
29         series_attribs = dict(series)
30         series_id = series_attribs['^series']
31         for lib_id, result_dir in result_map.items():
32             an_analysis = self.get_submission_node(result_dir)
33             metadata = self.get_sample_metadata(an_analysis)
34             if len(metadata) > 1:
35                 errmsg = 'Confused there are more than one samples for %s'
36                 LOGGER.debug(errmsg % (str(an_analysis,)))
37             metadata = metadata[0]
38             metadata['raw'] = self.get_sample_files(an_analysis,
39                                                     geoSoftNS['raw'])
40             metadata['supplimental'] = self.get_sample_files(
41                 an_analysis,
42                 geoSoftNS['supplemental'])
43             metadata['run'] = self.get_run_details(an_analysis)
44             samples.append(metadata)
45
46         soft_template = loader.get_template('geo_submission.soft')
47         context = Context({
48             'platform': platform,
49             'series': series,
50             'samples': samples,
51             'platform_id': platform_id,
52             'series_id': series_id,
53         })
54         print str(soft_template.render(context))
55
56     def check_for_name(self, analysis_node):
57         name = fromTypedNode(
58             self.model.get_target(analysis_node,
59                                   submissionOntology['name']))
60         if name is None:
61             logger.error("Need name for %s" % (str(analysis_node)))
62             return False
63         else:
64             return True
65
66     def get_platform_metadata(self):
67         """Gather information for filling out sample section of a SOFT file
68         """
69         query_template = loader.get_template('geo_platform.sparql')
70         submission = str(self.submissionSetNS[''].uri)
71         context = Context({
72             'submission': submission,
73             })
74
75         results = self.execute_query(query_template, context)
76         return self.query_to_soft_dictionary(results, 'platform')
77
78     def get_series_metadata(self):
79         """Gather information for filling out sample section of a SOFT file
80         """
81         query_template = loader.get_template('geo_series.sparql')
82         submission = str(self.submissionSetNS[''].uri)
83         context = Context({
84             'submission': submission,
85             })
86
87         results = self.execute_query(query_template, context)
88         return self.query_to_soft_dictionary(results, 'series')
89
90     def get_sample_metadata(self, analysis_node):
91         """Gather information for filling out sample section of a SOFT file
92         """
93         query_template = loader.get_template('geo_samples.sparql')
94
95         context = Context({
96             'submission': str(analysis_node.uri),
97             'submissionSet': str(self.submissionSetNS[''].uri),
98             })
99
100         results = self.execute_query(query_template, context)
101         for r in results:
102             r['dataProtocol'] = str(r['dataProtocol']).replace('\n', ' ')
103
104         return results
105
106     def get_sample_files(self, analysis_node, file_class):
107         """Gather files
108         """
109         query_template = loader.get_template('geo_files.sparql')
110
111         context = Context({
112             'submission': str(analysis_node.uri),
113             'file_class': str(file_class)
114             })
115
116         return self.execute_query(query_template, context)
117
118     def get_run_details(self, analysis_node):
119         """Get information about runs
120         """
121         query_template = loader.get_template('geo_run_details.sparql')
122
123         context = Context({
124             'submission': str(analysis_node.uri),
125             })
126
127         return self.execute_query(query_template, context)
128
129     def query_to_soft_dictionary(self, results, heading):
130         attributes = []
131         for r in results:
132             name = simplifyUri(geoSoftNS, r['name'])
133             if name is not None:
134                 if name.lower() == heading.lower():
135                     name = '^' + name
136                 else:
137                     name = '!' + name
138                 for v in fromTypedNode(r['value']).split(os.linesep):
139                     v = v.strip()
140                     if len(v) > 0:
141                         attributes.append((name, v))
142         return attributes