annotate galaxy-tools/history_tools/history_to_file.py @ 12:46f08bb8dd68 draft default tip

Uploaded
author ric
date Wed, 28 Sep 2016 04:59:02 -0400
parents 43be74e62bfe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
43be74e62bfe Uploaded
ric
parents:
diff changeset
1 #-------------------------
43be74e62bfe Uploaded
ric
parents:
diff changeset
2 # Export Galaxy history data to a JSON compatible text file
43be74e62bfe Uploaded
ric
parents:
diff changeset
3 #
43be74e62bfe Uploaded
ric
parents:
diff changeset
4 # Galaxy host can be retrieved using the GALAXY_HOST environment
43be74e62bfe Uploaded
ric
parents:
diff changeset
5 # variable
43be74e62bfe Uploaded
ric
parents:
diff changeset
6 # Galaxy API key can be retrieved using the GALAXY_API_KEY
43be74e62bfe Uploaded
ric
parents:
diff changeset
7 # environment variable
43be74e62bfe Uploaded
ric
parents:
diff changeset
8 #-------------------------
43be74e62bfe Uploaded
ric
parents:
diff changeset
9
43be74e62bfe Uploaded
ric
parents:
diff changeset
10 from blend.galaxy import GalaxyInstance
43be74e62bfe Uploaded
ric
parents:
diff changeset
11
43be74e62bfe Uploaded
ric
parents:
diff changeset
12 import argparse, sys, logging, os, json
43be74e62bfe Uploaded
ric
parents:
diff changeset
13
43be74e62bfe Uploaded
ric
parents:
diff changeset
14 LOG_FORMAT = '%(asctime)s|%(levelname)-8s|%(message)s'
43be74e62bfe Uploaded
ric
parents:
diff changeset
15 LOG_DATEFMT = '%Y-%m-%d %H:%M:%S'
43be74e62bfe Uploaded
ric
parents:
diff changeset
16 LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
43be74e62bfe Uploaded
ric
parents:
diff changeset
17
43be74e62bfe Uploaded
ric
parents:
diff changeset
18 def make_parser():
43be74e62bfe Uploaded
ric
parents:
diff changeset
19 parser = argparse.ArgumentParser(description='Export Galaxy history data')
43be74e62bfe Uploaded
ric
parents:
diff changeset
20 parser.add_argument('--loglevel', type=str, choices=LOG_LEVELS,
43be74e62bfe Uploaded
ric
parents:
diff changeset
21 help='logging level (default: INFO)', default='INFO')
43be74e62bfe Uploaded
ric
parents:
diff changeset
22 parser.add_argument('--logfile', type=str, help='log file (default=stderr)')
43be74e62bfe Uploaded
ric
parents:
diff changeset
23 parser.add_argument('--galaxy_host', type=str, help='galaxy host (with port)')
43be74e62bfe Uploaded
ric
parents:
diff changeset
24 parser.add_argument('--api_key', type=str, help='Galaxy API key')
43be74e62bfe Uploaded
ric
parents:
diff changeset
25 parser.add_argument('--history_id', type=str, required=True,
43be74e62bfe Uploaded
ric
parents:
diff changeset
26 help='ID fo the history that will be exported')
43be74e62bfe Uploaded
ric
parents:
diff changeset
27 parser.add_argument('--no_peek', action='store_true',
43be74e62bfe Uploaded
ric
parents:
diff changeset
28 help='remove preview from datasets details')
43be74e62bfe Uploaded
ric
parents:
diff changeset
29 parser.add_argument('--ofile', type=str, required=True,
43be74e62bfe Uploaded
ric
parents:
diff changeset
30 help='output file')
43be74e62bfe Uploaded
ric
parents:
diff changeset
31 return parser
43be74e62bfe Uploaded
ric
parents:
diff changeset
32
43be74e62bfe Uploaded
ric
parents:
diff changeset
33 def check_history_id(galaxy_instance, history_id):
43be74e62bfe Uploaded
ric
parents:
diff changeset
34 hmap = dict((str(h['id']), str(h['name']))
43be74e62bfe Uploaded
ric
parents:
diff changeset
35 for h in galaxy_instance.histories.get_histories())
43be74e62bfe Uploaded
ric
parents:
diff changeset
36 return history_id in hmap
43be74e62bfe Uploaded
ric
parents:
diff changeset
37
43be74e62bfe Uploaded
ric
parents:
diff changeset
38 def main(argv):
43be74e62bfe Uploaded
ric
parents:
diff changeset
39 parser = make_parser()
43be74e62bfe Uploaded
ric
parents:
diff changeset
40 args = parser.parse_args(argv)
43be74e62bfe Uploaded
ric
parents:
diff changeset
41
43be74e62bfe Uploaded
ric
parents:
diff changeset
42 log_level = getattr(logging, args.loglevel)
43be74e62bfe Uploaded
ric
parents:
diff changeset
43 kwargs = {'format' : LOG_FORMAT,
43be74e62bfe Uploaded
ric
parents:
diff changeset
44 'datefmt' : LOG_DATEFMT,
43be74e62bfe Uploaded
ric
parents:
diff changeset
45 'level' : log_level}
43be74e62bfe Uploaded
ric
parents:
diff changeset
46 if args.logfile:
43be74e62bfe Uploaded
ric
parents:
diff changeset
47 kwargs['filename'] = args.logfile
43be74e62bfe Uploaded
ric
parents:
diff changeset
48 logging.basicConfig(**kwargs)
43be74e62bfe Uploaded
ric
parents:
diff changeset
49 logger = logging.getLogger('history_to_file')
43be74e62bfe Uploaded
ric
parents:
diff changeset
50
43be74e62bfe Uploaded
ric
parents:
diff changeset
51 try:
43be74e62bfe Uploaded
ric
parents:
diff changeset
52 galaxy_host = args.galaxy_host or os.environ['GALAXY_HOST']
43be74e62bfe Uploaded
ric
parents:
diff changeset
53 api_key = args.api_key or os.environ['GALAXY_API_KEY']
43be74e62bfe Uploaded
ric
parents:
diff changeset
54 except KeyError, ke:
43be74e62bfe Uploaded
ric
parents:
diff changeset
55 msg = 'No argument passed and no global variable %s found' % ke
43be74e62bfe Uploaded
ric
parents:
diff changeset
56 logger.critical(msg)
43be74e62bfe Uploaded
ric
parents:
diff changeset
57 sys.exit(msg)
43be74e62bfe Uploaded
ric
parents:
diff changeset
58
43be74e62bfe Uploaded
ric
parents:
diff changeset
59 logger.info('opening connection to %s' % galaxy_host)
43be74e62bfe Uploaded
ric
parents:
diff changeset
60 gi = GalaxyInstance(galaxy_host, key=api_key)
43be74e62bfe Uploaded
ric
parents:
diff changeset
61
43be74e62bfe Uploaded
ric
parents:
diff changeset
62 if not check_history_id(gi, args.history_id):
43be74e62bfe Uploaded
ric
parents:
diff changeset
63 msg = 'Unable to find history with ID %s' % args.history_id
43be74e62bfe Uploaded
ric
parents:
diff changeset
64 logger.critical(msg)
43be74e62bfe Uploaded
ric
parents:
diff changeset
65 sys.exit(msg)
43be74e62bfe Uploaded
ric
parents:
diff changeset
66
43be74e62bfe Uploaded
ric
parents:
diff changeset
67 logger.info('getting data for history %s' % args.history_id)
43be74e62bfe Uploaded
ric
parents:
diff changeset
68 h_data = gi.histories.get_history_details(args.history_id, args.no_peek)
43be74e62bfe Uploaded
ric
parents:
diff changeset
69
43be74e62bfe Uploaded
ric
parents:
diff changeset
70 logger.info('saving to file %s' % args.ofile)
43be74e62bfe Uploaded
ric
parents:
diff changeset
71 with open(args.ofile, 'w') as out_file:
43be74e62bfe Uploaded
ric
parents:
diff changeset
72 out_file.write(json.dumps(h_data))
43be74e62bfe Uploaded
ric
parents:
diff changeset
73 logger.info('Job completed')
43be74e62bfe Uploaded
ric
parents:
diff changeset
74
43be74e62bfe Uploaded
ric
parents:
diff changeset
75 if __name__ == '__main__':
43be74e62bfe Uploaded
ric
parents:
diff changeset
76 main(sys.argv[1:])