Start writing a report for "so, what experiments have we submitted"?
[htsworkflow.git] / encode_submission / submission_report.py
1 import RDF
2 import jinja2
3
4 from htsworkflow.util.rdfhelp import \
5      dafTermOntology, \
6      dublinCoreNS, \
7      get_model, \
8      get_serializer, \
9      sparql_query, \
10      submissionOntology, \
11      libraryOntology, \
12      load_into_model, \
13      rdfNS, \
14      rdfsNS, \
15      xsdNS
16 TYPE_N = rdfNS['type']
17 CREATION_DATE = libraryOntology['date']
18
19 from encode_find import DBDIR
20
21 def main():
22     model = get_model('encode', DBDIR)
23     what_have_we_done(model)
24
25 SUBMISSION_QUERY = """
26 PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
27 PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
28 PREFIX ucscSubmission:<http://jumpgate.caltech.edu/wiki/UcscSubmissionOntology#>
29 PREFIX libraryOntology:<http://jumpgate.caltech.edu/wiki/LibraryOntology#>
30 PREFIX daf: <http://jumpgate.caltech.edu/wiki/UcscDaf#>
31 PREFIX ddf: <http://encodesubmit.ucsc.edu/pipeline/download_ddf#>
32
33 SELECT distinct ?library_urn ?library_name ?assembly ?submission ?submission_status ?date
34 WHERE {
35   ?submission ucscSubmission:library_urn ?library_urn ;
36               ucscSubmission:has_status ?status ;
37               libraryOntology:date ?date .
38   ?status daf:assembly ?assembly ;
39           ucscSubmission:status ?submission_status .
40   OPTIONAL { ?library_urn libraryOntology:name ?library_name . }
41   FILTER(!regex(?submission_status, "revoked", "i"))
42   FILTER(regex(?assembly, "mm9", "i"))
43   #FILTER(!BOUND(?library_name))
44 }
45 ORDER BY ?library_urn ?submission
46 """
47
48 SUBMISSION_TEMPLATE = """
49 <html>
50 <head>
51 <style type="text/css">
52 table { border-width: 0 0 1px 1px; border-style: solid; }
53 th,td { border-width: 1px 1px 0 0; border-style: solid; margin: 0;}
54 </style>
55 </head>
56 <body>
57 <table>
58 <thead>
59   <tr>
60   <td>Library ID</td><td>Submission ID</td><td>Last Updated</td><td>Status</td>
61   <td>Library Name</td>
62   </tr>
63 </thead>
64 <tbody>
65 {% for record in submissions %}
66   <tr>
67     <td><a href="{{record.library_urn}}">{{ record.library_urn | trim_rdf}}</a></td>
68     <td><a href="{{record.submission}}">{{record.submission|trim_rdf}}</a></td>
69     <td>{{ record.date|timestamp_to_date }}</td>
70     <td>{{ record.submission_status }}</td>
71     <td>{{ record.library_name }}</td>
72   </tr>
73 {% endfor %}
74 </tbody>
75 </table>
76 </body>
77 </html>
78 """
79
80 def what_have_we_done(model):
81     compiled_query = RDF.SPARQLQuery(SUBMISSION_QUERY)
82     submissions = compiled_query.execute(model)
83     environment = jinja2.Environment()
84     environment.filters['trim_rdf'] = trim_rdf
85     environment.filters['timestamp_to_date'] = timestamp_to_date
86     template = environment.from_string(SUBMISSION_TEMPLATE)
87     print template.render(submissions = submissions)
88
89 def trim_rdf(value):
90     if value is None:
91         return
92     value = str(value)
93     if value[-1] == '/':
94         value = value[:-1]
95     split_value = value.split('/')
96     if len(split_value) == 0:
97         return value
98     return split_value[-1]
99
100 def timestamp_to_date(value):
101     datestamp, timestamp = str(value).split('T')
102     return datestamp
103
104 if __name__ == "__main__":
105     main()