annotate combine_output.py @ 51:684d2341968c

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