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