Mercurial > repos > prog > lcmsmatching
comparison list-chrom-cols.py @ 5:fb9c0409d85c draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 608d9e59a0d2dcf85a037968ddb2c61137fb9bce
author | prog |
---|---|
date | Wed, 19 Apr 2017 10:00:05 -0400 |
parents | 20d69a062da3 |
children |
comparison
equal
deleted
inserted
replaced
4:b34c14151f25 | 5:fb9c0409d85c |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # vi: fdm=marker | |
2 | 3 |
3 import argparse | 4 import argparse |
4 import subprocess | 5 import subprocess |
5 import re | 6 import re |
6 import urllib2 | 7 import urllib2 |
7 import json | 8 import json |
8 import csv | 9 import csv |
9 | 10 |
10 def get_chrom_cols(dbtype, dburl, dbtoken = None, dbfields = None): | 11 # Get chrom cols {{{1 |
12 ################################################################ | |
13 | |
14 def get_chrom_cols(dbtype, dburl, dbtoken = None, col_field = 'chromcol'): | |
11 | 15 |
12 cols = [] | 16 cols = [] |
13 | 17 |
14 if dbtype == 'peakforest': | 18 if dbtype == 'peakforest': |
15 url = dburl + ( '' if dburl[-1] == '/' else '/' ) + 'metadata/lc/list-code-columns' | 19 url = dburl + ( '' if dburl[-1] == '/' else '/' ) + 'metadata/lc/list-code-columns' |
17 url += '?token=' + dbtoken | 21 url += '?token=' + dbtoken |
18 result = urllib2.urlopen(url).read() | 22 result = urllib2.urlopen(url).read() |
19 v = json.JSONDecoder().decode(result) | 23 v = json.JSONDecoder().decode(result) |
20 i = 0 | 24 i = 0 |
21 for colid, coldesc in v.iteritems(): | 25 for colid, coldesc in v.iteritems(): |
22 cols.append( (coldesc['name'], colid, i == 0) ) | 26 s = coldesc['name'] + ' - ' + coldesc['constructor'] + ' - L' + str(coldesc['length']) + ' - diam. ' + str(coldesc['diameter']) + ' - part. ' + str(coldesc['particule_size']) + ' - flow ' + str(coldesc['flow_rate']) |
27 cols.append( (s , colid, i == 0) ) | |
23 ++i | 28 ++i |
24 | 29 |
25 elif dbtype == 'inhouse': | 30 elif dbtype == 'inhouse': |
26 # Get field for chromatographic column name | |
27 col_field = 'chromcol' | |
28 if dbfields is not None: | |
29 fields = dict(u.split("=") for u in dbfields.split(",")) | |
30 if 'chromcol' in fields: | |
31 col_field = fields['chromcol'] | |
32 | 31 |
33 # Get all column names from file | 32 # Get all column names from file |
34 with open(dburl if isinstance(dburl, str) else dburl.get_file_name(), 'rb') as dbfile: | 33 with open(dburl if isinstance(dburl, str) else dburl.get_file_name(), 'r') as dbfile: |
35 reader = csv.reader(dbfile, delimiter = "\t", quotechar='"') | 34 reader = csv.reader(dbfile, delimiter = "\t", quotechar='"') |
36 header = reader.next() | 35 header = reader.next() |
37 if col_field in header: | 36 if col_field in header: |
38 i = header.index(col_field) | 37 i = header.index(col_field) |
39 allcols = [] | 38 allcols = [] |
44 for i, c in enumerate(allcols): | 43 for i, c in enumerate(allcols): |
45 cols.append( (c, c, i == 0) ) | 44 cols.append( (c, c, i == 0) ) |
46 | 45 |
47 return cols | 46 return cols |
48 | 47 |
49 ######## | 48 # Main {{{1 |
50 # MAIN # | 49 ################################################################ |
51 ######## | |
52 | 50 |
53 if __name__ == '__main__': | 51 if __name__ == '__main__': |
54 | 52 |
55 # Parse command line arguments | 53 # Parse command line arguments |
56 parser = argparse.ArgumentParser(description='Script for getting chromatographic columns of an RMSDB database for Galaxy tool lcmsmatching.') | 54 parser = argparse.ArgumentParser(description='Script for getting chromatographic columns of an RMSDB database for Galaxy tool lcmsmatching.') |
57 parser.add_argument('-d', help = 'Database type', dest = 'dbtype', required = True) | 55 parser.add_argument('-d', help = 'Database type', dest = 'dbtype', required = True) |
58 parser.add_argument('-u', help = 'Database URL', dest = 'dburl', required = True) | 56 parser.add_argument('-u', help = 'Database URL', dest = 'dburl', required = True) |
59 parser.add_argument('-t', help = 'Database token', dest = 'dbtoken', required = False) | 57 parser.add_argument('-t', help = 'Database token', dest = 'dbtoken', required = False) |
60 parser.add_argument('-f', help = 'Database fields', dest = 'dbfields', required = False) | 58 parser.add_argument('-f', help = 'Chromatogrphic column field name', dest = 'col_field', required = False) |
61 args = parser.parse_args() | 59 args = parser.parse_args() |
62 args_dict = vars(args) | 60 args_dict = vars(args) |
63 | 61 |
64 print(get_chrom_cols(**args_dict)) | 62 print(get_chrom_cols(**args_dict)) |