annotate METEXPtools/query_metexp.py @ 25:9f03c8587d6b draft default tip

MetExp msclust upload format changed from tabular to csv
author linda-bakker
date Fri, 24 Aug 2018 09:56:05 -0400
parents 4393f982d18f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
1 #!/usr/bin/env python
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
2 # encoding: utf-8
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
3 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
4 Module to query a set of identifications against the METabolomics EXPlorer database.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
5
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
6 It will take the input file and for each record it will query the
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
7 molecular mass in the selected MetExp DB. If one or more compounds are found in the
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
8 MetExp DB then extra information regarding these compounds is added to the output file.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
9
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
10 The output file is thus the input file enriched with information about
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
11 related items found in the selected MetExp DB.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
12 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
13 import csv
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
14 import sys
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
15 import fileinput
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
16 import urllib2
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
17 import time
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
18 from collections import OrderedDict
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
19
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
20 __author__ = "Pieter Lukasse"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
21 __contact__ = "pieter.lukasse@wur.nl"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
22 __copyright__ = "Copyright, 2014, Plant Research International, WUR"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
23 __license__ = "Apache v2"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
24
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
25 def _process_file(in_xsv, delim='\t'):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
26 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
27 Generic method to parse a tab-separated file returning a dictionary with named columns
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
28 @param in_csv: input filename to be parsed
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
29 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
30 data = list(csv.reader(open(in_xsv, 'rU'), delimiter=delim))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
31 return _process_data(data)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
32
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
33 def _process_data(data):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
34
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
35 header = data.pop(0)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
36 # Create dictionary with column name as key
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
37 output = OrderedDict()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
38 for index in xrange(len(header)):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
39 output[header[index]] = [row[index] for row in data]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
40 return output
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
41
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
42
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
43 def _query_and_add_data(input_data, casid_col, formula_col, molecular_mass_col, metexp_dblink, separation_method):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
44 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
45 This method will iterate over the record in the input_data and
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
46 will enrich them with the related information found (if any) in the
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
47 MetExp Database.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
48
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
49 # TODO : could optimize this with multi-threading, see also nice example at http://stackoverflow.com/questions/2846653/python-multithreading-for-dummies
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
50 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
51 merged = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
52
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
53 for i in xrange(len(input_data[input_data.keys()[0]])):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
54 # Get the record in same dictionary format as input_data, but containing
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
55 # a value at each column instead of a list of all values of all records:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
56 input_data_record = OrderedDict(zip(input_data.keys(), [input_data[key][i] for key in input_data.keys()]))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
57
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
58 # read the molecular mass and formula:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
59 cas_id = input_data_record[casid_col]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
60 formula = input_data_record[formula_col]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
61 molecular_mass = input_data_record[molecular_mass_col]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
62
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
63 # search for related records in MetExp:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
64 data_found = None
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
65 if cas_id != "undef":
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
66 # 1- search for other experiments where this CAS id has been found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
67 query_link = metexp_dblink + "/find_entries/query?cas_nr="+ cas_id + "&method=" + separation_method
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
68 data_found = _fire_query_and_return_dict(query_link + "&_format_result=tsv")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
69 data_type_found = "CAS"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
70 if data_found == None:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
71 # 2- search for other experiments where this FORMULA has been found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
72 query_link = metexp_dblink + "/find_entries/query?molecule_formula="+ formula + "&method=" + separation_method
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
73 data_found = _fire_query_and_return_dict(query_link + "&_format_result=tsv")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
74 data_type_found = "FORMULA"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
75 if data_found == None:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
76 # 3- search for other experiments where this MM has been found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
77 query_link = metexp_dblink + "/find_entries/query?molecule_mass="+ molecular_mass + "&method=" + separation_method
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
78 data_found = _fire_query_and_return_dict(query_link + "&_format_result=tsv")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
79 data_type_found = "MM"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
80
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
81 if data_found == None:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
82 # If still nothing found, just add empty columns
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
83 extra_cols = ['', '','','','','','','']
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
84 else:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
85 # Add info found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
86 extra_cols = _get_extra_info_and_link_cols(data_found, data_type_found, query_link)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
87
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
88 # Take all data and merge it into a "flat"/simple array of values:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
89 field_values_list = _merge_data(input_data_record, extra_cols)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
90
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
91 merged.append(field_values_list)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
92
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
93 # return the merged/enriched records:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
94 return merged
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
95
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
96
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
97 def _get_extra_info_and_link_cols(data_found, data_type_found, query_link):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
98 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
99 This method will go over the data found and will return a
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
100 list with the following items:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
101 - Experiment details where hits have been found :
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
102 'organism', 'tissue','experiment_name','user_name','column_type'
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
103 - Link that executes same query
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
104
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
105 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
106 # set() makes a unique list:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
107 organism_set = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
108 tissue_set = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
109 experiment_name_set = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
110 user_name_set = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
111 column_type_set = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
112 cas_nr_set = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
113
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
114 if 'organism' in data_found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
115 organism_set = set(data_found['organism'])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
116 if 'tissue' in data_found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
117 tissue_set = set(data_found['tissue'])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
118 if 'experiment_name' in data_found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
119 experiment_name_set = set(data_found['experiment_name'])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
120 if 'user_name' in data_found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
121 user_name_set = set(data_found['user_name'])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
122 if 'column_type' in data_found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
123 column_type_set = set(data_found['column_type'])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
124 if 'CAS' in data_found:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
125 cas_nr_set = set(data_found['CAS'])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
126
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
127
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
128 result = [data_type_found,
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
129 _to_xsv(organism_set),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
130 _to_xsv(tissue_set),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
131 _to_xsv(experiment_name_set),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
132 _to_xsv(user_name_set),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
133 _to_xsv(column_type_set),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
134 _to_xsv(cas_nr_set),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
135 #To let Excel interpret as link, use e.g. =HYPERLINK("http://stackoverflow.com", "friendly name"):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
136 "=HYPERLINK(\""+ query_link + "\", \"Link to entries found in DB \")"]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
137 return result
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
138
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
139
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
140 def _to_xsv(data_set):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
141 result = ""
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
142 for item in data_set:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
143 result = result + str(item) + "|"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
144 return result
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
145
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
146
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
147 def _fire_query_and_return_dict(url):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
148 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
149 This method will fire the query as a web-service call and
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
150 return the results as a list of dictionary objects
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
151 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
152
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
153 try:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
154 data = urllib2.urlopen(url).read()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
155
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
156 # transform to dictionary:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
157 result = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
158 data_rows = data.split("\n")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
159
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
160 # check if there is any data in the response:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
161 if len(data_rows) <= 1 or data_rows[1].strip() == '':
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
162 # means there is only the header row...so no hits:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
163 return None
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
164
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
165 for data_row in data_rows:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
166 if not data_row.strip() == '':
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
167 row_as_list = _str_to_list(data_row, delimiter='\t')
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
168 result.append(row_as_list)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
169
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
170 # return result processed into a dict:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
171 return _process_data(result)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
172
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
173 except urllib2.HTTPError, e:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
174 raise Exception( "HTTP error for URL: " + url + " : %s - " % e.code + e.reason)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
175 except urllib2.URLError, e:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
176 raise Exception( "Network error: %s" % e.reason.args[1] + ". Administrator: please check if MetExp service [" + url + "] is accessible from your Galaxy server. ")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
177
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
178 def _str_to_list(data_row, delimiter='\t'):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
179 result = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
180 for column in data_row.split(delimiter):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
181 result.append(column)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
182 return result
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
183
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
184
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
185 # alternative: ?
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
186 # s = requests.Session()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
187 # s.verify = False
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
188 # #s.auth = (token01, token02)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
189 # resp = s.get(url, params={'name': 'anonymous'}, stream=True)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
190 # content = resp.content
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
191 # # transform to dictionary:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
192
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
193
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
194
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
195
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
196 def _merge_data(input_data_record, extra_cols):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
197 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
198 Adds the extra information to the existing data record and returns
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
199 the combined new record.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
200 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
201 record = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
202 for column in input_data_record:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
203 record.append(input_data_record[column])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
204
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
205
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
206 # add extra columns
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
207 for column in extra_cols:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
208 record.append(column)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
209
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
210 return record
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
211
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
212
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
213 def _save_data(data_rows, headers, out_csv):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
214 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
215 Writes tab-separated data to file
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
216 @param data_rows: dictionary containing merged/enriched dataset
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
217 @param out_csv: output csv file
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
218 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
219
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
220 # Open output file for writing
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
221 outfile_single_handle = open(out_csv, 'wb')
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
222 output_single_handle = csv.writer(outfile_single_handle, delimiter="\t")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
223
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
224 # Write headers
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
225 output_single_handle.writerow(headers)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
226
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
227 # Write one line for each row
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
228 for data_row in data_rows:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
229 output_single_handle.writerow(data_row)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
230
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
231 def _get_metexp_URL(metexp_dblink_file):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
232 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
233 Read out and return the URL stored in the given file.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
234 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
235 file_input = fileinput.input(metexp_dblink_file)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
236 try:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
237 for line in file_input:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
238 if line[0] != '#':
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
239 # just return the first line that is not a comment line:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
240 return line
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
241 finally:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
242 file_input.close()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
243
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
244
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
245 def main():
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
246 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
247 MetExp Query main function
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
248
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
249 The input file can be any tabular file, as long as it contains a column for the molecular mass
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
250 and one for the formula of the respective identification. These two columns are then
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
251 used to query against MetExp Database.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
252 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
253 seconds_start = int(round(time.time()))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
254
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
255 input_file = sys.argv[1]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
256 casid_col = sys.argv[2]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
257 formula_col = sys.argv[3]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
258 molecular_mass_col = sys.argv[4]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
259 metexp_dblink_file = sys.argv[5]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
260 separation_method = sys.argv[6]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
261 output_result = sys.argv[7]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
262
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
263 # Parse metexp_dblink_file to find the URL to the MetExp service:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
264 metexp_dblink = _get_metexp_URL(metexp_dblink_file)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
265
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
266 # Parse tabular input file into dictionary/array:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
267 input_data = _process_file(input_file)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
268
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
269 # Query data against MetExp DB :
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
270 enriched_data = _query_and_add_data(input_data, casid_col, formula_col, molecular_mass_col, metexp_dblink, separation_method)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
271 headers = input_data.keys() + ['METEXP hits for ','METEXP hits: organisms', 'METEXP hits: tissues',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
272 'METEXP hits: experiments','METEXP hits: user names','METEXP hits: column types', 'METEXP hits: CAS nrs', 'Link to METEXP hits']
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
273
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
274 _save_data(enriched_data, headers, output_result)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
275
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
276 seconds_end = int(round(time.time()))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
277 print "Took " + str(seconds_end - seconds_start) + " seconds"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
278
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
279
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
280
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
281 if __name__ == '__main__':
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
282 main()