Mercurial > repos > prog > lcmsmatching
comparison list-chrom-cols.py @ 1:253d531a0193 draft
planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit 36c9d8099c20a1ae848f1337c16564335dd8fb2b
author | prog |
---|---|
date | Sat, 03 Sep 2016 17:02:01 -0400 |
parents | |
children | 20d69a062da3 |
comparison
equal
deleted
inserted
replaced
0:e66bb061af06 | 1:253d531a0193 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import argparse | |
4 import subprocess | |
5 import re | |
6 import urllib2 | |
7 import json | |
8 import csv | |
9 | |
10 def get_chrom_cols(dbtype, dburl, dbtoken = None, dbfields = None): | |
11 | |
12 cols = [] | |
13 | |
14 if dbtype == 'peakforest': | |
15 url = dburl + ( '' if dburl[-1] == '/' else '/' ) + 'metadata/lc/list-code-columns' | |
16 if dbtoken is not None: | |
17 url += '?token=' + dbtoken | |
18 result = urllib2.urlopen(url).read() | |
19 v = json.JSONDecoder().decode(result) | |
20 i = 0 | |
21 for colid, coldesc in v.iteritems(): | |
22 cols.append( (coldesc['name'], colid, i == 0) ) | |
23 ++i | |
24 | |
25 elif dbtype == 'inhouse': | |
26 # Get field for chromatographic column name | |
27 col_field = 'col' | |
28 if dbfields is not None: | |
29 fields = dict(u.split("=") for u in dbfields.split(",")) | |
30 if 'col' in fields: | |
31 col_field = fields['col'] | |
32 | |
33 # Get all column names from file | |
34 with open(dburl.get_file_name(), 'rb') as dbfile: | |
35 reader = csv.reader(dbfile, delimiter = "\t", quotechar='"') | |
36 header = reader.next() | |
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 ######## | |
49 # MAIN # | |
50 ######## | |
51 | |
52 if __name__ == '__main__': | |
53 | |
54 # Parse command line arguments | |
55 parser = argparse.ArgumentParser(description='Script for getting chromatographic columns of an RMSDB database for Galaxy tool lcmsmatching.') | |
56 parser.add_argument('-d', help = 'Database type', dest = 'dbtype', required = True) | |
57 parser.add_argument('-u', help = 'Database URL', dest = 'dburl', required = True) | |
58 parser.add_argument('-t', help = 'Database token', dest = 'dbtoken', required = False) | |
59 parser.add_argument('-f', help = 'Database fields', dest = 'dbfields', required = False) | |
60 args = parser.parse_args() | |
61 args_dict = vars(args) | |
62 | |
63 print(get_chrom_cols(**args_dict)) |