Mercurial > repos > ric > test2
comparison galaxy-tools/biobank/updater/update_parents.py @ 0:ba6cf6ede027 draft default tip
Uploaded
| author | ric |
|---|---|
| date | Wed, 28 Sep 2016 06:03:30 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:ba6cf6ede027 |
|---|---|
| 1 import sys, csv, argparse, time, json | |
| 2 | |
| 3 from bl.vl.kb import KnowledgeBase as KB | |
| 4 import bl.vl.utils.ome_utils as vlu | |
| 5 from bl.vl.utils import get_logger, LOG_LEVELS | |
| 6 | |
| 7 | |
| 8 def make_parser(): | |
| 9 parser = argparse.ArgumentParser(description='update parents') | |
| 10 parser.add_argument('--logfile', type=str, help='log file (default=stderr)') | |
| 11 parser.add_argument('--loglevel', type=str, choices=LOG_LEVELS, | |
| 12 help='logging level (default=INFO)', default='INFO') | |
| 13 parser.add_argument('-H', '--host', type=str, help='omero hostname') | |
| 14 parser.add_argument('-U', '--user', type=str, help='omero user') | |
| 15 parser.add_argument('-P', '--passwd', type=str, help='omero password') | |
| 16 parser.add_argument('-O', '--operator', type=str, help='operator', | |
| 17 required=True) | |
| 18 parser.add_argument('--in_file', type=str, required=True, | |
| 19 help='input file with individual, father and mother') | |
| 20 return parser | |
| 21 | |
| 22 | |
| 23 def update_parents(individual, father, mother, operator, kb, logger): | |
| 24 backup = {} | |
| 25 logger.info('Updating parents for individual %s', individual.id) | |
| 26 if individual.father != father: | |
| 27 backup['father'] = individual.father.id if individual.father else None | |
| 28 logger.info('Setting father to %s (old value %s)' % (father.id if father else None, | |
| 29 backup['father'])) | |
| 30 individual.father = father | |
| 31 if individual.mother != mother: | |
| 32 backup['mother'] = individual.mother.id if individual.mother else None | |
| 33 logger.info('Setting mother to %s (old value %s)' % (mother.id if mother else None, | |
| 34 backup['mother'])) | |
| 35 individual.mother = mother | |
| 36 if len(backup.items()) > 0: | |
| 37 update_object(individual, backup, operator, kb, logger) | |
| 38 return individual | |
| 39 else: | |
| 40 logger.info('No update needed for individual %s' % individual.id) | |
| 41 return None | |
| 42 | |
| 43 | |
| 44 def update_object(obj, backup_values, operator, kb, logger): | |
| 45 logger.debug('Building ActionOnAction for object %s' % obj.id) | |
| 46 act_setup = build_action_setup('update-parents-%f' % time.time(), | |
| 47 backup_values, kb, logger) | |
| 48 aoa_conf = { | |
| 49 'setup': act_setup, | |
| 50 'actionCategory': kb.ActionCategory.UPDATE, | |
| 51 'operator': operator, | |
| 52 'target': obj.lastUpdate if obj.lastUpdate else obj.action, | |
| 53 'context': obj.action.context | |
| 54 } | |
| 55 logger.debug('Updating object with new ActionOnAction') | |
| 56 obj.lastUpdate = kb.factory.create(kb.ActionOnAction, aoa_conf) | |
| 57 | |
| 58 | |
| 59 def build_action_setup(label, backup, kb, logger): | |
| 60 logger.debug('Creating a new ActionSetup with label %s and backup %r' % (label, | |
| 61 backup)) | |
| 62 conf = { | |
| 63 'label': label, | |
| 64 'conf': json.dumps({'backup': backup}) | |
| 65 } | |
| 66 asetup = kb.factory.create(kb.ActionSetup, conf) | |
| 67 return asetup | |
| 68 | |
| 69 | |
| 70 def main(argv): | |
| 71 parser = make_parser() | |
| 72 args = parser.parse_args(argv) | |
| 73 | |
| 74 logger = get_logger('update_parents', level=args.loglevel, | |
| 75 filename=args.logfile) | |
| 76 | |
| 77 try: | |
| 78 host = args.host or vlu.ome_host() | |
| 79 user = args.user or vlu.ome_user() | |
| 80 passwd = args.passwd or vlu.ome_passwd() | |
| 81 except ValueError, ve: | |
| 82 logger.critical(ve) | |
| 83 sys.exit(ve) | |
| 84 | |
| 85 kb = KB(driver='omero')(host, user, passwd) | |
| 86 | |
| 87 logger.info('Retrieving individuals') | |
| 88 inds = kb.get_objects(kb.Individual) | |
| 89 logger.info('Retrieved %d individuals' % len(inds)) | |
| 90 inds_lookup = {} | |
| 91 for i in inds: | |
| 92 inds_lookup[i.id] = i | |
| 93 | |
| 94 with open(args.in_file) as in_file: | |
| 95 to_be_updated = [] | |
| 96 reader = csv.DictReader(in_file, delimiter='\t') | |
| 97 for row in reader: | |
| 98 ind = inds_lookup[row['individual']] | |
| 99 father = inds_lookup[row['father']] if row['father'] != 'None' else None | |
| 100 mother = inds_lookup[row['mother']] if row['mother'] != 'None' else None | |
| 101 ind = update_parents(ind, father, mother, args.operator, kb, logger) | |
| 102 if ind: | |
| 103 to_be_updated.append(ind) | |
| 104 | |
| 105 logger.info('%d individuals are going to be updated' % len(to_be_updated)) | |
| 106 kb.save_array(to_be_updated) | |
| 107 logger.info('Update complete') | |
| 108 | |
| 109 if __name__ == '__main__': | |
| 110 main(sys.argv[1:]) |
