Properly constructing the geo soft file really needed multiple sparql queries.
[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             samples.append(metadata)
44
45         soft_template = loader.get_template('geo_submission.soft')
46         context = Context({
47             'platform': platform,
48             'series': series,
49             'samples': samples,
50             'platform_id': platform_id,
51             'series_id': series_id,
52         })
53         print str(soft_template.render(context))
54
55     def check_for_name(self, analysis_node):
56         name = fromTypedNode(
57             self.model.get_target(analysis_node,
58                                   submissionOntology['name']))
59         if name is None:
60             logger.error("Need name for %s" % (str(analysis_node)))
61             return False
62         else:
63             return True
64
65     def get_platform_metadata(self):
66         """Gather information for filling out sample section of a SOFT file
67         """
68         query_template = loader.get_template('geo_platform.sparql')
69         submission = str(self.submissionSetNS[''].uri)
70         context = Context({
71             'submission': submission,
72             })
73
74         results = self.execute_query(query_template, context)
75         return self.query_to_soft_dictionary(results, 'platform')
76
77     def get_series_metadata(self):
78         """Gather information for filling out sample section of a SOFT file
79         """
80         query_template = loader.get_template('geo_series.sparql')
81         submission = str(self.submissionSetNS[''].uri)
82         context = Context({
83             'submission': submission,
84             })
85
86         results = self.execute_query(query_template, context)
87         return self.query_to_soft_dictionary(results, 'series')
88
89     def get_sample_metadata(self, analysis_node):
90         """Gather information for filling out sample section of a SOFT file
91         """
92         query_template = loader.get_template('geo_samples.sparql')
93
94         context = Context({
95             'submission': str(analysis_node.uri),
96             'submissionSet': str(self.submissionSetNS[''].uri),
97             })
98
99         results = self.execute_query(query_template, context)
100         for r in results:
101
102             r['dataProtocol'] = str(r['dataProtocol']).replace('\n', ' ')
103         return results
104
105     def get_sample_files(self, analysis_node, file_class):
106         """Gather files
107         """
108         query_template = loader.get_template('geo_files.sparql')
109
110         context = Context({
111             'submission': str(analysis_node.uri),
112             'file_class': str(file_class)
113             })
114
115         return self.execute_query(query_template, context)
116
117     def query_to_soft_dictionary(self, results, heading):
118         attributes = []
119         for r in results:
120             name = simplifyUri(geoSoftNS, r['name'])
121             if name is not None:
122                 if name.lower() == heading.lower():
123                     name = '^' + name
124                 else:
125                     name = '!' + name
126                 for v in fromTypedNode(r['value']).split(os.linesep):
127                     v = v.strip()
128                     if len(v) > 0:
129                         attributes.append((name, v))
130         return attributes