comparison tabular_edit.xml @ 0:01454ded8907 draft default tip

Uploaded
author kellrott
date Wed, 31 Oct 2012 19:50:16 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:01454ded8907
1 <tool id="tabular_edit" name="Tabular Edit" version="1.0.0">
2 <description>Edit the contents and row/column labels of a tabular file using python statements</description>
3 <command interpreter="python">$script_file</command>
4 <inputs>
5 <param name="row_txt" type="text" area="True" size="5x35" label="Row Eval Code" optional="True">
6 <sanitizer>
7 <valid initial="string.printable">
8 <remove value="&quot;"/>
9 </valid>
10 <mapping initial="none">
11 <add source="&quot;" target="\&quot;"/>
12 <add source="\" target="\\"/>
13 </mapping>
14 </sanitizer>
15 </param>
16 <param name="col_txt" type="text" area="True" size="5x35" label="Column Eval Code" optional="True">
17 <sanitizer>
18 <valid initial="string.printable">
19 <remove value="&quot;"/>
20 </valid>
21 <mapping initial="none">
22 <add source="&quot;" target="\&quot;"/>
23 </mapping>
24 </sanitizer>
25 </param>
26 <param name="cell_txt" type="text" area="True" size="5x35" label="Cell Eval Code" optional="True">
27 <sanitizer>
28 <valid initial="string.printable">
29 <remove value="&quot;"/>
30 </valid>
31 <mapping initial="none">
32 <add source="&quot;" target="\&quot;"/>
33 </mapping>
34 </sanitizer>
35 </param>
36
37 <param name="matrix" type="data" format="tabular" label="Matrix"/>
38 </inputs>
39 <outputs>
40 <data format="tabular" name="outfile" />
41 </outputs>
42 <configfiles>
43 <configfile name="script_file"><![CDATA[#!/usr/bin/env python
44 import os
45 import sys
46 import csv
47 import re
48 import math
49
50 def value_eval(code, value):
51 funcmap = {
52 "len":len,
53 "value" : value,
54 "re" : re,
55 "math" : math,
56 "float" : float
57 }
58 return str(eval(code,{"__builtins__":None},funcmap))
59
60
61 row_text = """${row_txt}"""
62 col_text = """${col_txt}"""
63 cell_text = """${cell_txt}"""
64
65 in_path = """${matrix}"""
66 out_path = """${outfile}"""
67
68
69 ohandle = open(out_path, "w")
70 ihandle = open(in_path)
71 reader = csv.reader(ihandle, delimiter="\t")
72 writer = csv.writer(ohandle, delimiter="\t", lineterminator="\n")
73
74 header = True
75 for row in reader:
76 if header:
77 if len(col_text):
78 for i, val in enumerate(row[1:]):
79 row[i+1] = value_eval(col_text, val)
80 header = False
81 else:
82 if len(row_text):
83 row[0] = value_eval(row_text, row[0])
84 if len(cell_text):
85 for i in range(1,len(row)):
86 row[i] = value_eval(cell_text,row[i])
87 writer.writerow(row)
88
89 ihandle.close()
90 ohandle.close()
91
92
93 ]]></configfile>
94 </configfiles>
95 <help>
96 This is a utility to perform editing operations on the contents and column/row labels of a tabular file.
97
98 - The 'Column Eval Code' operations occur on the first line.
99 - The 'Row Eval Code' operations occur on the first cell of every line
100 - The 'Cell Eval Code' operations occur on every non-label cell
101 - If any of the code blocks are empty, the operation is skipped
102
103 Example
104
105 Remove the '.CEL' string from sample names::
106
107 re.sub(r'.CEL$', '', value)
108
109 Log Transform the matrix cells::
110
111 math.log(float(value))
112
113
114 </help>
115 </tool>