6263c9ae0916e28a5047b7c192a78586014aa353
[htsworkflow.git] / scripts / library.py
1 import datetime
2 from optparse import OptionParser
3 from pprint import pprint
4 import sqlite3
5 import sys
6 import time
7
8 class fctracker:
9     def __init__(self):
10         self.conn = sqlite3.connect('fctracker.db')
11         self._get_library()
12         self._get_species()
13
14     def _make_dict_from_table(self, table_name, pkey_name):
15         table = {}
16         c = self.conn.cursor()
17         c.execute('select * from %s;' % (table_name))
18         # extract just the field name
19         description = [ f[0] for f in c.description]
20         for row in c:
21             row_dict = dict(zip(description, row))
22             table[row_dict[pkey_name]] = row_dict
23         c.close()
24         return table
25
26     def _get_library(self):
27         self.library = self._make_dict_from_table(
28                          'fctracker_library', 
29                          'library_id')
30                                                   
31         
32     def _get_species(self):
33         self.species = self._make_dict_from_table(
34                          'fctracker_species',
35                          'id'
36                        )
37         
38     def _get_flowcells(self, where=None):
39         if where is None:
40             where = ""
41         self.flowcells = {}
42         c = self.conn.cursor()
43         c.execute('select * from fctracker_flowcell %s;' % (where))
44         # extract just the field name
45         description = [ f[0] for f in c.description ]
46         for row in c:
47             row_dict = dict(zip(description, row))
48             for lane in [ 'lane_%d_library' % (i) for i in range(1,9) ]:
49                 lane_library = self.library[row_dict[lane+"_id"]]
50                 species_id = lane_library['library_species_id']
51                 lane_library['library_species'] = self.species[species_id]
52                 row_dict[lane] = lane_library
53             # some useful parsing
54             run_date = time.strptime(row_dict['run_date'],  '%Y-%m-%d %H:%M:%S')
55             run_date = datetime.datetime(*run_date[:6])
56             row_dict['run_date'] = run_date
57             self.flowcells[row_dict['flowcell_id']] = row_dict
58         return self.flowcells
59
60
61 def flowcell_gone(name):
62     if 'failed' in name:
63         return True
64     if 'deleted' in name:
65         return True
66     if 'not run' in name:
67         return True
68     return False
69
70 def report(flowcells):
71     flowcell_list = []
72     for key, cell in flowcells.items():
73         flowcell_list.append( (cell['run_date'], key) )
74     flowcell_list.sort()
75     for run_date, flowcell_id in flowcell_list:
76         cell = flowcells[flowcell_id]
77         if flowcell_gone(cell['flowcell_id']):
78             continue
79         #pprint(cell)
80         #print cell['flowcell_id'], cell['run_date']
81         for l in range(1,9):
82             lane = 'lane_%d' % (l)
83             print cell['run_date'].strftime('%y-%b-%d'),
84             print cell['flowcell_id'],
85             #print "  ",
86             print l,cell['%s_library'%(lane)]['library_name'],
87             print '(%s)' % (cell['%s_library_id'%(lane)]),
88             print cell['%s_library'%(lane)]['library_species']['scientific_name']
89
90 def make_parser():
91     """
92     Make parser
93     """
94     parser = OptionParser()
95     parser.add_option("-w", "--where", dest="where",
96                       help="add a where clause",
97                       default=None)
98     return parser
99
100 def main(argv=None):
101     if argv is None:
102         argv = []
103     parser = make_parser()
104
105     opt, args = parser.parse_args(argv)
106     
107     fc = fctracker()
108     cells = fc._get_flowcells(opt.where)
109
110     report(cells)
111     return 0
112
113 if __name__ == "__main__":
114     sys.exit(main(sys.argv[1:]))