annotate export_to_metexp_tabular.py @ 1:223d1167de58

added comment
author pieter.lukasse@wur.nl
date Thu, 19 Mar 2015 10:38:48 +0100
parents dffc38727496
children 0d1557b3d540
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
1 #!/usr/bin/env python
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
2 # encoding: utf-8
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
3 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
4 Module to combine output from the GCMS Galaxy tools RankFilter, CasLookup and MsClust
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
5 into a tabular file that can be uploaded to the MetExp database.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
6
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
7 RankFilter, CasLookup are already combined by combine_output.py so here we will use
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
8 this result. Furthermore here one of the MsClust
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
9 quantification files containing the respective spectra details are to be combined as well.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
10
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
11 Extra calculations performed:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
12 - The column MW is also added here and is derived from the column FORMULA found
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
13 in RankFilter, CasLookup combined result.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
14
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
15 So in total here we merge 2 files and calculate one new column.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
16 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
17 from pkg_resources import resource_filename # @UnresolvedImport # pylint: disable=E0611
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
18 import csv
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
19 import re
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
20 import sys
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
21 from collections import OrderedDict
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
22
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
23 __author__ = "Pieter Lukasse"
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
24 __contact__ = "pieter.lukasse@wur.nl"
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
25 __copyright__ = "Copyright, 2013, Plant Research International, WUR"
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
26 __license__ = "Apache v2"
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
27
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
28 def _process_data(in_csv, delim='\t'):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
29 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
30 Generic method to parse a tab-separated file returning a dictionary with named columns
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
31 @param in_csv: input filename to be parsed
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
32 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
33 data = list(csv.reader(open(in_csv, 'rU'), delimiter=delim))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
34 header = data.pop(0)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
35 # Create dictionary with column name as key
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
36 output = OrderedDict()
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
37 for index in xrange(len(header)):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
38 output[header[index]] = [row[index] for row in data]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
39 return output
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
40
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
41 ONE_TO_ONE = 'one_to_one'
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
42 N_TO_ONE = 'n_to_one'
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
43
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
44 def _merge_data(set1, link_field_set1, set2, link_field_set2, compare_function, merge_function, metadata, relation_type=ONE_TO_ONE):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
45 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
46 Merges data from both input dictionaries based on the link fields. This method will
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
47 build up a new list containing the merged hits as the items.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
48 @param set1: dictionary holding set1 in the form of N lists (one list per attribute name)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
49 @param set2: dictionary holding set2 in the form of N lists (one list per attribute name)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
50 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
51 # TODO test for correct input files -> same link_field values should be there
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
52 # (test at least number of unique link_field values):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
53 #
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
54 # if (len(set1[link_field_set1]) != len(set2[link_field_set2])):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
55 # raise Exception('input files should have the same nr of key values ')
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
56
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
57
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
58 merged = []
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
59 processed = {}
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
60 for link_field_set1_idx in xrange(len(set1[link_field_set1])):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
61 link_field_set1_value = set1[link_field_set1][link_field_set1_idx]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
62 if not link_field_set1_value in processed :
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
63 # keep track of processed items to not repeat them
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
64 processed[link_field_set1_value] = link_field_set1_value
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
65
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
66 # Get the indices for current link_field_set1_value in both data-structures for proper matching
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
67 set1index = [index for index, value in enumerate(set1[link_field_set1]) if value == link_field_set1_value]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
68 set2index = [index for index, value in enumerate(set2[link_field_set2]) if compare_function(value, link_field_set1_value)==True ]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
69 # Validation :
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
70 if len(set2index) == 0:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
71 # means that corresponding data could not be found in set2, then throw error
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
72 raise Exception("Datasets not compatible, merge not possible. " + link_field_set1 + "=" +
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
73 link_field_set1_value + " only found in first dataset. ")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
74
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
75 merged_hits = []
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
76 # Combine hits
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
77 for hit in xrange(len(set1index)):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
78 # Create records of hits to be merged ("keys" are the attribute names, so what the lines below do
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
79 # is create a new "dict" item with same "keys"/attributes, with each attribute filled with its
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
80 # corresponding value in the sets; i.e.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
81 # set1[key] => returns the list/array with size = nrrows, with the values for the attribute
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
82 # represented by "key".
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
83 # set1index[hit] => points to the row nr=hit (hit is a rownr/index)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
84 # So set1[x][set1index[n]] = set1.attributeX.instanceN
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
85 #
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
86 # It just ensures the entry is made available as a plain named array for easy access.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
87 rf_record = OrderedDict(zip(set1.keys(), [set1[key][set1index[hit]] for key in set1.keys()]))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
88 if relation_type == ONE_TO_ONE :
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
89 cl_record = OrderedDict(zip(set2.keys(), [set2[key][set2index[hit]] for key in set2.keys()]))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
90 else:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
91 # is N to 1:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
92 cl_record = OrderedDict(zip(set2.keys(), [set2[key][set2index[0]] for key in set2.keys()]))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
93
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
94 merged_hit = merge_function(rf_record, cl_record, metadata)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
95 merged_hits.append(merged_hit)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
96
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
97 merged.append(merged_hits)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
98
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
99 return merged, len(set1index)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
100
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
101
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
102 def _compare_records(key1, key2):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
103 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
104 in this case the compare method is really simple as both keys are expected to contain
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
105 same value when records are the same
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
106 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
107 if key1 == key2:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
108 return True
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
109 else:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
110 return False
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
111
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
112
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
113
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
114 def _merge_records(rank_caslookup_combi, msclust_quant_record, metadata):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
115 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
116 Combines single records from both the RankFilter+CasLookup combi file and from MsClust file
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
117
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
118 @param rank_caslookup_combi: rankfilter and caslookup combined record (see combine_output.py)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
119 @param msclust_quant_record: msclust quantification + spectrum record
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
120 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
121 record = []
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
122 for column in rank_caslookup_combi:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
123 record.append(rank_caslookup_combi[column])
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
124
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
125 for column in msclust_quant_record:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
126 record.append(msclust_quant_record[column])
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
127
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
128 for column in metadata:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
129 record.append(metadata[column])
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
130
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
131 # add MOLECULAR MASS (MM)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
132 molecular_mass = get_molecular_mass(rank_caslookup_combi['FORMULA'])
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
133 # limit to two decimals:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
134 record.append("{0:.2f}".format(molecular_mass))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
135
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
136 # add MOLECULAR WEIGHT (MW) - TODO - calculate this
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
137 record.append('0.0')
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
138
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
139 # level of identification and Location of reference standard
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
140 record.append('0')
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
141 record.append('')
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
142
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
143 return record
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
144
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
145
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
146 def get_molecular_mass(formula):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
147 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
148 Calculates the molecular mass (MM).
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
149 E.g. MM of H2O = (relative)atomic mass of H x2 + (relative)atomic mass of O
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
150 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
151
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
152 # Each element is represented by a capital letter, followed optionally by
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
153 # lower case, with one or more digits as for how many elements:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
154 element_pattern = re.compile("([A-Z][a-z]?)(\d*)")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
155
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
156 total_mass = 0
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
157 for (element_name, count) in element_pattern.findall(formula):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
158 if count == "":
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
159 count = 1
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
160 else:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
161 count = int(count)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
162 element_mass = float(elements_and_masses_map[element_name]) # "found: Python's built-in float type has double precision " (? check if really correct ?)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
163 total_mass += element_mass * count
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
164
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
165 return total_mass
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
166
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
167
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
168
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
169 def _save_data(data, headers, out_csv):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
170 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
171 Writes tab-separated data to file
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
172 @param data: dictionary containing merged dataset
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
173 @param out_csv: output csv file
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
174 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
175
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
176 # Open output file for writing
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
177 outfile_single_handle = open(out_csv, 'wb')
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
178 output_single_handle = csv.writer(outfile_single_handle, delimiter="\t")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
179
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
180 # Write headers
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
181 output_single_handle.writerow(headers)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
182
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
183 # Write
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
184 for item_idx in xrange(len(data)):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
185 for hit in data[item_idx]:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
186 output_single_handle.writerow(hit)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
187
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
188
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
189 def _get_map_for_elements_and_masses(elements_and_masses):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
190 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
191 This method will read out the column 'Chemical symbol' and make a map
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
192 of this, storing the column 'Relative atomic mass' as its value
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
193 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
194 resultMap = {}
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
195 index = 0
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
196 for entry in elements_and_masses['Chemical symbol']:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
197 resultMap[entry] = elements_and_masses['Relative atomic mass'][index]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
198 index += 1
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
199
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
200 return resultMap
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
201
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
202
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
203 def init_elements_and_masses_map():
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
204 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
205 Initializes the lookup map containing the elements and their respective masses
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
206 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
207 elements_and_masses = _process_data(resource_filename(__name__, "static_resources/elements_and_masses.tab"))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
208 global elements_and_masses_map
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
209 elements_and_masses_map = _get_map_for_elements_and_masses(elements_and_masses)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
210
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
211
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
212 def main():
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
213 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
214 Combine Output main function
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
215
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
216 RankFilter, CasLookup are already combined by combine_output.py so here we will use
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
217 this result. Furthermore here the MsClust spectra file (.MSP) and one of the MsClust
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
218 quantification files are to be combined with combine_output.py result as well.
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
219 '''
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
220 rankfilter_and_caslookup_combined_file = sys.argv[1]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
221 msclust_quantification_and_spectra_file = sys.argv[2]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
222 output_csv = sys.argv[3]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
223 # metadata
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
224 metadata = OrderedDict()
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
225 metadata['organism'] = sys.argv[4]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
226 metadata['tissue'] = sys.argv[5]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
227 metadata['experiment_name'] = sys.argv[6]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
228 metadata['user_name'] = sys.argv[7]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
229 metadata['column_type'] = sys.argv[8]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
230
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
231 # Read RankFilter and CasLookup output files
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
232 rankfilter_and_caslookup_combined = _process_data(rankfilter_and_caslookup_combined_file)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
233 msclust_quantification_and_spectra = _process_data(msclust_quantification_and_spectra_file, ',')
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
234
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
235 # Read elements and masses to use for the MW/MM calculation :
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
236 init_elements_and_masses_map()
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
237
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
238 merged, nhits = _merge_data(rankfilter_and_caslookup_combined, 'Centrotype',
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
239 msclust_quantification_and_spectra, 'centrotype',
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
240 _compare_records, _merge_records, metadata,
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
241 N_TO_ONE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
242 headers = rankfilter_and_caslookup_combined.keys() + msclust_quantification_and_spectra.keys() + metadata.keys() + ['MM','MW', 'Level of identification', 'Location of reference standard']
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
243 _save_data(merged, headers, output_csv)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
244
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
245
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
246 if __name__ == '__main__':
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
247 main()