annotate GCMS/combine_output.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 fe4682eb938c
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 combine output from two GCMS Galaxy tools (RankFilter and CasLookup)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
5 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
7 import csv
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
8 import sys
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
9 import math
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
10 import pprint
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
11
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
12 __author__ = "Marcel Kempenaar"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
13 __contact__ = "brs@nbic.nl"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
14 __copyright__ = "Copyright, 2012, Netherlands Bioinformatics Centre"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
15 __license__ = "MIT"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
16
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
17 def _process_data(in_csv):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
18 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
19 Generic method to parse a tab-separated file returning a dictionary with named columns
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
20 @param in_csv: input filename to be parsed
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
21 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
22 data = list(csv.reader(open(in_csv, 'rU'), delimiter='\t'))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
23 header = data.pop(0)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
24 # Create dictionary with column name as key
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
25 output = {}
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
26 for index in xrange(len(header)):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
27 output[header[index]] = [row[index] for row in data]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
28 return output
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
29
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
30
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
31 def _merge_data(rankfilter, caslookup):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
32 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
33 Merges data from both input dictionaries based on the Centrotype field. This method will
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
34 build up a new list containing the merged hits as the items.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
35 @param rankfilter: dictionary holding RankFilter output in the form of N lists (one list per attribute name)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
36 @param caslookup: dictionary holding CasLookup output in the form of N lists (one list per attribute name)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
37 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
38 # TODO: test for correct input files -> rankfilter and caslookup internal lists should have the same lenghts:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
39 if (len(rankfilter['ID']) != len(caslookup['Centrotype'])):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
40 raise Exception('rankfilter and caslookup files should have the same nr of rows/records ')
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
41
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
42 merged = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
43 processed = {}
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
44 for compound_id_idx in xrange(len(rankfilter['ID'])):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
45 compound_id = rankfilter['ID'][compound_id_idx]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
46 if not compound_id in processed :
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
47 # keep track of processed items to not repeat them
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
48 processed[compound_id] = compound_id
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
49 # get centrotype nr
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
50 centrotype = compound_id.split('-')[0]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
51 # Get the indices for current compound ID in both data-structures for proper matching
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
52 rindex = [index for index, value in enumerate(rankfilter['ID']) if value == compound_id]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
53 cindex = [index for index, value in enumerate(caslookup['Centrotype']) if value == centrotype]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
54
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
55 merged_hits = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
56 # Combine hits
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
57 for hit in xrange(len(rindex)):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
58 # Create records of hits to be merged ("keys" are the attribute names, so what the lines below do
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
59 # is create a new "dict" item with same "keys"/attributes, with each attribute filled with its
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
60 # corresponding value in the rankfilter or caslookup tables; i.e.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
61 # rankfilter[key] => returns the list/array with size = nrrows, with the values for the attribute
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
62 # represented by "key". rindex[hit] => points to the row nr=hit (hit is a rownr/index)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
63 rf_record = dict(zip(rankfilter.keys(), [rankfilter[key][rindex[hit]] for key in rankfilter.keys()]))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
64 cl_record = dict(zip(caslookup.keys(), [caslookup[key][cindex[hit]] for key in caslookup.keys()]))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
65
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
66 merged_hit = _add_hit(rf_record, cl_record)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
67 merged_hits.append(merged_hit)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
68
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
69 merged.append(merged_hits)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
70
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
71 return merged, len(rindex)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
72
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
73
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
74 def _add_hit(rankfilter, caslookup):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
75 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
76 Combines single records from both the RankFilter- and CasLookup-tools
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
77 @param rankfilter: record (dictionary) of one compound in the RankFilter output
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
78 @param caslookup: matching record (dictionary) of one compound in the CasLookup output
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
79 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
80 # The ID in the RankFilter output contains the following 5 fields:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
81 rf_id = rankfilter['ID'].split('-')
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
82 try:
16
fe4682eb938c small improvement
pieter.lukasse@wur.nl
parents: 14
diff changeset
83 if 'Formula' not in rankfilter:
fe4682eb938c small improvement
pieter.lukasse@wur.nl
parents: 14
diff changeset
84 raise Exception("Error: old Rankfilter format detected (the selected Rankfilter data does not contain the column 'Formula'). Solution: rerun Rankfilter again.")
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
85 hit = [rf_id[0], # Centrotype
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
86 rf_id[1], # cent.Factor
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
87 rf_id[2], # scan nr
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
88 rf_id[3], # R.T. (umin)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
89 rf_id[4], # nr. Peaks
16
fe4682eb938c small improvement
pieter.lukasse@wur.nl
parents: 14
diff changeset
90 rankfilter['R.T.'],
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
91 # Appending other fields
14
346ff9ad8c7a fix for rankfilter, removed pfd read functional
linda.bakker@wur.nl <linda.bakker@wur.nl>
parents: 6
diff changeset
92 rankfilter['Name'],
346ff9ad8c7a fix for rankfilter, removed pfd read functional
linda.bakker@wur.nl <linda.bakker@wur.nl>
parents: 6
diff changeset
93 rankfilter['Formula'],
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
94 rankfilter['Library'].strip(),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
95 rankfilter['CAS'].strip(),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
96 rankfilter['Forward'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
97 rankfilter['Reverse'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
98 ((float(rankfilter['Forward']) + float(rankfilter['Reverse'])) / 2),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
99 rankfilter['RIexp'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
100 caslookup['RI'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
101 rankfilter['RIsvr'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
102 # Calculate absolute differences
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
103 math.fabs(float(rankfilter['RIexp']) - float(rankfilter['RIsvr'])),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
104 math.fabs(float(caslookup['RI']) - float(rankfilter['RIexp'])),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
105 caslookup['Regression.Column.Name'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
106 caslookup['min'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
107 caslookup['max'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
108 caslookup['nr.duplicates'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
109 caslookup['Column.phase.type'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
110 caslookup['Column.name'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
111 rankfilter['Rank'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
112 rankfilter['%rel.err'],
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
113 rankfilter['Synonyms']]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
114 except KeyError as error:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
115 print "Problem reading in data from input file(s):\n",
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
116 print "Respective CasLookup entry: \n", pprint.pprint(caslookup), "\n"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
117 print "Respective RankFilter entry: \n", pprint.pprint(rankfilter), "\n"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
118 raise error
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
119
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
120 return hit
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
121
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
122
14
346ff9ad8c7a fix for rankfilter, removed pfd read functional
linda.bakker@wur.nl <linda.bakker@wur.nl>
parents: 6
diff changeset
123
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
124
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
125
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
126 def _get_default_caslookup():
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
127 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
128 The Cas Lookup tool might not have found all compounds in the library searched,
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
129 this default dict will be used to combine with the Rank Filter output
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
130 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
131 return {'FORMULA': 'N/A',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
132 'RI': '0.0',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
133 'Regression.Column.Name': 'None',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
134 'min': '0.0',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
135 'max': '0.0',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
136 'nr.duplicates': '0',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
137 'Column.phase.type': 'N/A',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
138 'Column.name': 'N/A'}
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
139
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
140
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
141 def _save_data(data, nhits, out_csv_single, out_csv_multi):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
142 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
143 Writes tab-separated data to file
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
144 @param data: dictionary containing merged dataset
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
145 @param out_csv: output csv file
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
146 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
147 # Columns we don't repeat:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
148 header_part1 = ['Centrotype',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
149 'cent.Factor',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
150 'scan nr.',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
151 'R.T. (umin)',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
152 'nr. Peaks',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
153 'R.T.']
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
154 # These are the headers/columns we repeat in case of
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
155 # combining hits in one line (see alternative_headers method below):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
156 header_part2 = [
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
157 'Name',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
158 'FORMULA',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
159 'Library',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
160 'CAS',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
161 'Forward',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
162 'Reverse',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
163 'Avg. (Forward, Reverse)',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
164 'RIexp',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
165 'RI',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
166 'RIsvr',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
167 'RIexp - RIsvr',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
168 'RI - RIexp',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
169 'Regression.Column.Name',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
170 'min',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
171 'max',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
172 'nr.duplicates',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
173 'Column.phase.type',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
174 'Column.name',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
175 'Rank',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
176 '%rel.err',
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
177 'Synonyms']
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
178
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
179 # Open output file for writing
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
180 outfile_single_handle = open(out_csv_single, 'wb')
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
181 outfile_multi_handle = open(out_csv_multi, 'wb')
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
182 output_single_handle = csv.writer(outfile_single_handle, delimiter="\t")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
183 output_multi_handle = csv.writer(outfile_multi_handle, delimiter="\t")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
184
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
185 # Write headers
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
186 output_single_handle.writerow(header_part1 + header_part2)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
187 output_multi_handle.writerow(header_part1 + header_part2 + alternative_headers(header_part2, nhits-1))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
188 # Combine all hits for each centrotype into one line
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
189 line = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
190 for centrotype_idx in xrange(len(data)):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
191 i = 0
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
192 for hit in data[centrotype_idx]:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
193 if i==0:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
194 line.extend(hit)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
195 else:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
196 line.extend(hit[6:])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
197 i = i+1
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
198 # small validation (if error, it is a programming error):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
199 if i > nhits:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
200 raise Exception('Error: more hits that expected for centrotype_idx ' + centrotype_idx)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
201 output_multi_handle.writerow(line)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
202 line = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
203
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
204 # Write one line for each centrotype
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
205 for centrotype_idx in xrange(len(data)):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
206 for hit in data[centrotype_idx]:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
207 output_single_handle.writerow(hit)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
208
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
209 def alternative_headers(header_part2, nr_alternative_hits):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
210 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
211 This method will iterate over the header names and add the string 'ALT#_' before each,
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
212 where # is the number of the alternative, according to number of alternative hits we want to add
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
213 to final csv/tsv
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
214 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
215 result = []
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
216 for i in xrange(nr_alternative_hits):
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
217 for header_name in header_part2:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
218 result.append("ALT" + str(i+1) + "_" + header_name)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
219 return result
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
220
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
221 def main():
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
222 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
223 Combine Output main function
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
224 It will merge the result files from "RankFilter" and "Lookup RI for CAS numbers"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
225 NB: the caslookup_result_file will typically have fewer lines than
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
226 rankfilter_result_file, so the merge has to consider this as well. The final file
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
227 should have the same nr of lines as rankfilter_result_file.
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
228 '''
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
229 rankfilter_result_file = sys.argv[1]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
230 caslookup_result_file = sys.argv[2]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
231 output_single_csv = sys.argv[3]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
232 output_multi_csv = sys.argv[4]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
233
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
234 # Read RankFilter and CasLookup output files
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
235 rankfilter = _process_data(rankfilter_result_file)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
236 caslookup = _process_data(caslookup_result_file)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
237 merged, nhits = _merge_data(rankfilter, caslookup)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
238 _save_data(merged, nhits, output_single_csv, output_multi_csv)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
239
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
240
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
241 if __name__ == '__main__':
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
242 main()