Mercurial > repos > prog > lcmsmatching
comparison list-chrom-cols.py @ 6:f86fec07f392 draft default tip
planemo upload commit c397cd8a93953798d733fd62653f7098caac30ce
author | prog |
---|---|
date | Fri, 22 Feb 2019 16:04:22 -0500 |
parents | fb9c0409d85c |
children |
comparison
equal
deleted
inserted
replaced
5:fb9c0409d85c | 6:f86fec07f392 |
---|---|
1 #!/usr/bin/env python | |
2 # vi: fdm=marker | |
3 | |
4 import argparse | |
5 import subprocess | |
6 import re | |
7 import urllib2 | |
8 import json | |
9 import csv | |
10 | |
11 # Get chrom cols {{{1 | |
12 ################################################################ | |
13 | |
14 def get_chrom_cols(dbtype, dburl, dbtoken = None, col_field = 'chromcol'): | |
15 | |
16 cols = [] | |
17 | |
18 if dbtype == 'peakforest': | |
19 url = dburl + ( '' if dburl[-1] == '/' else '/' ) + 'metadata/lc/list-code-columns' | |
20 if dbtoken is not None: | |
21 url += '?token=' + dbtoken | |
22 result = urllib2.urlopen(url).read() | |
23 v = json.JSONDecoder().decode(result) | |
24 i = 0 | |
25 for colid, coldesc in v.iteritems(): | |
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) ) | |
28 ++i | |
29 | |
30 elif dbtype == 'inhouse': | |
31 | |
32 # Get all column names from file | |
33 with open(dburl if isinstance(dburl, str) else dburl.get_file_name(), 'r') as dbfile: | |
34 reader = csv.reader(dbfile, delimiter = "\t", quotechar='"') | |
35 header = reader.next() | |
36 if col_field in header: | |
37 i = header.index(col_field) | |
38 allcols = [] | |
39 for row in reader: | |
40 col = row[i] | |
41 if col not in allcols: | |
42 allcols.append(col) | |
43 for i, c in enumerate(allcols): | |
44 cols.append( (c, c, i == 0) ) | |
45 | |
46 return cols | |
47 | |
48 # Main {{{1 | |
49 ################################################################ | |
50 | |
51 if __name__ == '__main__': | |
52 | |
53 # Parse command line arguments | |
54 parser = argparse.ArgumentParser(description='Script for getting chromatographic columns of an RMSDB database for Galaxy tool lcmsmatching.') | |
55 parser.add_argument('-d', help = 'Database type', dest = 'dbtype', required = True) | |
56 parser.add_argument('-u', help = 'Database URL', dest = 'dburl', required = True) | |
57 parser.add_argument('-t', help = 'Database token', dest = 'dbtoken', required = False) | |
58 parser.add_argument('-f', help = 'Chromatogrphic column field name', dest = 'col_field', required = False) | |
59 args = parser.parse_args() | |
60 args_dict = vars(args) | |
61 | |
62 print(get_chrom_cols(**args_dict)) |