annotate list-ms-mode-values.py @ 6:b8f70d8216b3 draft

planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
author prog
date Mon, 27 Mar 2017 06:27:29 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
1 #!/usr/bin/env python
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
2 # vi: fdm=marker
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
3
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
4 import csv
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
5 import re
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
6 import argparse
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
7
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
8 # Get MS mode values {{{1
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
9 ################################################################
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
10
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
11 def get_ms_mode_value(file, col, preferred):
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
12
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
13 modes = []
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
14 cols = []
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
15 preferred = preferred.split(',')
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
16
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
17 with open(file if isinstance(file, str) else file.get_file_name(), 'r') as f:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
18
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
19 # Read file header
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
20 reader = csv.reader(f, delimiter = "\t", quotechar='"')
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
21 header = reader.next()
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
22 try:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
23 index = header.index(col)
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
24 for row in reader:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
25 v = row[index]
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
26 if v not in modes:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
27 modes.append(v)
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
28
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
29 # Find default value
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
30 default = 0
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
31 for p in preferred:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
32 for i, m in enumerate(modes):
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
33 if m == p:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
34 default = i
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
35 break
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
36 if default != 0:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
37 break
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
38
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
39 # Build list of cols
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
40 for i, c in enumerate(modes):
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
41 cols.append( (c, c, i == default) )
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
42 except:
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
43 pass
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
44
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
45 return cols
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
46
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
47 # Main {{{1
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
48 ################################################################
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
49
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
50 if __name__ == '__main__':
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
51
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
52 # Parse command line arguments
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
53 parser = argparse.ArgumentParser(description='Script for getting column names in a csv file.')
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
54 parser.add_argument('-f', help = 'CSV File (separator must be TAB)', dest = 'file', required = True)
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
55 parser.add_argument('-c', help = 'MS mode column name.', dest = 'col', required = True)
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
56 parser.add_argument('-p', help = 'List (comma separated values) of preferred column names for default one.', dest = 'preferred', required = True)
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
57 args = parser.parse_args()
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
58 args_dict = vars(args)
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
59
b8f70d8216b3 planemo upload for repository https://github.com/workflow4metabolomics/lcmsmatching.git commit b3a4e34cf9356447ae3507cc6fe2ff6a1f24afbc-dirty
prog
parents:
diff changeset
60 print(get_ms_mode_value(**args_dict))