|
0
|
1 """
|
|
|
2 Map PlateWell labels written as PLATE_BARCODE:WELL_LABEL to labels written as
|
|
|
3 PLATE_LABEL:WELL_LABEL which is the PlateWell label format required by the map_vid
|
|
|
4 application.
|
|
|
5 The inputs are a TSV file and the label of the column of this file containing the
|
|
|
6 PlateWell labels that are going to be mapped.
|
|
|
7 """
|
|
|
8
|
|
|
9 import csv, argparse, sys, copy
|
|
|
10
|
|
|
11 from bl.vl.kb import KnowledgeBase as KB
|
|
|
12 from bl.vl.utils import LOG_LEVELS, get_logger
|
|
|
13 import bl.vl.utils.ome_utils as vlu
|
|
|
14
|
|
|
15
|
|
|
16 def get_wells_map(kb, plate_barcodes, logger):
|
|
|
17 wells_map = {}
|
|
|
18 logger.info('Start building PlateWells map')
|
|
|
19 res = kb.get_by_field(kb.TiterPlate, 'barcode', plate_barcodes)
|
|
|
20 logger.debug('Plates %r --- Results: %r', plate_barcodes, res)
|
|
|
21 for _, pl in res.iteritems():
|
|
|
22 if pl.OME_TABLE == 'TiterPlate':
|
|
|
23 if pl.barcode:
|
|
|
24 for w in kb.get_wells_by_plate(pl):
|
|
|
25 logger.debug('Mapping well %s of plate %s', w.label, w.container.label)
|
|
|
26 wells_map['%s:%s' % (w.container.barcode, w.label)] = '%s:%s' % (w.container.label,
|
|
|
27 w.label)
|
|
|
28 else:
|
|
|
29 logger.debug('TiterPlate %s has no barcode', pl.label)
|
|
|
30 else:
|
|
|
31 logger.debug('Object is a %r, skipping it', pl.OME_TABLE)
|
|
|
32 logger.info('Mapped %d PlateWells', len(wells_map))
|
|
|
33 return wells_map
|
|
|
34
|
|
|
35
|
|
|
36 def get_plates_list(records, plates_column, logger):
|
|
|
37 plates = set()
|
|
|
38 logger.info('Retrieving TiterPlate barcodes from %d records', len(records))
|
|
|
39 for r in records:
|
|
|
40 plates.add(r[plates_column].split(':')[0])
|
|
|
41 logger.info('Found %d TiterPlate objects', len(plates))
|
|
|
42 return list(plates)
|
|
|
43
|
|
|
44
|
|
|
45 def make_parser():
|
|
|
46 parser = argparse.ArgumentParser('Map barcodes in PlateWell labels to TiterPlate labels')
|
|
|
47 parser.add_argument('--logfile', type=str, help='log file (default=stderr)')
|
|
|
48 parser.add_argument('--loglevel', type=str, choices=LOG_LEVELS,
|
|
|
49 help='logging level', default='INFO')
|
|
|
50 parser.add_argument('-H', '--host', type=str, help='OMERO host')
|
|
|
51 parser.add_argument('-U', '--user', type=str, help='OMERO user')
|
|
|
52 parser.add_argument('-P', '--passwd', type=str, help='OMERO password')
|
|
|
53 parser.add_argument('--in-file', type=str, required=True,
|
|
|
54 help='input TSV file')
|
|
|
55 parser.add_argument('--column-label', type=str, required=True,
|
|
|
56 help='the label of the columun containing the values that will be mapped')
|
|
|
57 parser.add_argument('--out-file', type=str, required=True,
|
|
|
58 help='output TSV file')
|
|
|
59 parser.add_argument('--strict-mapping', action='store_true',
|
|
|
60 help='if output records are less than the input ones, raise an error')
|
|
|
61 return parser
|
|
|
62
|
|
|
63
|
|
|
64 def main(argv):
|
|
|
65 parser = make_parser()
|
|
|
66 args = parser.parse_args(argv)
|
|
|
67
|
|
|
68 logger = get_logger('wells_barcode_to_label', level=args.loglevel,
|
|
|
69 filename=args.logfile)
|
|
|
70 try:
|
|
|
71 host = args.host or vlu.ome_host()
|
|
|
72 user = args.user or vlu.ome_user()
|
|
|
73 passwd = args.passwd or vlu.ome_passwd()
|
|
|
74 except ValueError, ve:
|
|
|
75 logger.critical(ve)
|
|
|
76 sys.exit(ve)
|
|
|
77
|
|
|
78 logger.info('Starting job')
|
|
|
79
|
|
|
80 kb = KB(driver='omero')(host, user, passwd)
|
|
|
81 # wells_map = get_wells_map(kb, logger)
|
|
|
82
|
|
|
83 with open(args.in_file) as in_file, open(args.out_file, 'w') as out_file:
|
|
|
84 reader = csv.DictReader(in_file, delimiter='\t')
|
|
|
85 if args.column_label not in reader.fieldnames:
|
|
|
86 msg = 'No column %s in file %s' % (args.column_label, args.in_file)
|
|
|
87 logger.critical(msg)
|
|
|
88 raise RuntimeError(msg)
|
|
|
89 records = [row for row in reader]
|
|
|
90 plates = get_plates_list(records, args.column_label, logger)
|
|
|
91 wells_map = get_wells_map(kb, plates, logger)
|
|
|
92 logger.info('Mapping %d records', len(records))
|
|
|
93 writer = csv.DictWriter(out_file, reader.fieldnames, delimiter='\t')
|
|
|
94 writer.writeheader()
|
|
|
95 mapped_records = []
|
|
|
96 for rec in records:
|
|
|
97 mapped = copy.deepcopy(rec)
|
|
|
98 logger.debug('Mapping value %s', mapped[args.column_label])
|
|
|
99 if mapped[args.column_label] in wells_map:
|
|
|
100 mapped[args.column_label] = wells_map[mapped[args.column_label]]
|
|
|
101 mapped_records.append(mapped)
|
|
|
102 if args.strict_mapping and len(mapped_records) < len(records):
|
|
|
103 msg = 'Mapped %d record of %d' % (len(mapped_records), len(records))
|
|
|
104 logger.critical(msg)
|
|
|
105 sys.exit(msg)
|
|
|
106 logger.info('%d records mapped', len(mapped_records))
|
|
|
107 writer.writerows(mapped_records)
|
|
|
108 logger.info('Job completed')
|
|
|
109
|
|
|
110
|
|
|
111 if __name__ == '__main__':
|
|
|
112 main(sys.argv[1:]) |