Mercurial > repos > kellrott > tabular_edit
changeset 0:01454ded8907 draft default tip
Uploaded
author | kellrott |
---|---|
date | Wed, 31 Oct 2012 19:50:16 -0400 |
parents | |
children | |
files | tabular_edit.xml |
diffstat | 1 files changed, 115 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tabular_edit.xml Wed Oct 31 19:50:16 2012 -0400 @@ -0,0 +1,115 @@ +<tool id="tabular_edit" name="Tabular Edit" version="1.0.0"> + <description>Edit the contents and row/column labels of a tabular file using python statements</description> + <command interpreter="python">$script_file</command> + <inputs> + <param name="row_txt" type="text" area="True" size="5x35" label="Row Eval Code" optional="True"> + <sanitizer> + <valid initial="string.printable"> + <remove value="""/> + </valid> + <mapping initial="none"> + <add source=""" target="\""/> + <add source="\" target="\\"/> + </mapping> + </sanitizer> + </param> + <param name="col_txt" type="text" area="True" size="5x35" label="Column Eval Code" optional="True"> + <sanitizer> + <valid initial="string.printable"> + <remove value="""/> + </valid> + <mapping initial="none"> + <add source=""" target="\""/> + </mapping> + </sanitizer> + </param> + <param name="cell_txt" type="text" area="True" size="5x35" label="Cell Eval Code" optional="True"> + <sanitizer> + <valid initial="string.printable"> + <remove value="""/> + </valid> + <mapping initial="none"> + <add source=""" target="\""/> + </mapping> + </sanitizer> + </param> + + <param name="matrix" type="data" format="tabular" label="Matrix"/> + </inputs> + <outputs> + <data format="tabular" name="outfile" /> + </outputs> + <configfiles> + <configfile name="script_file"><![CDATA[#!/usr/bin/env python +import os +import sys +import csv +import re +import math + +def value_eval(code, value): + funcmap = { + "len":len, + "value" : value, + "re" : re, + "math" : math, + "float" : float + } + return str(eval(code,{"__builtins__":None},funcmap)) + + +row_text = """${row_txt}""" +col_text = """${col_txt}""" +cell_text = """${cell_txt}""" + +in_path = """${matrix}""" +out_path = """${outfile}""" + + +ohandle = open(out_path, "w") +ihandle = open(in_path) +reader = csv.reader(ihandle, delimiter="\t") +writer = csv.writer(ohandle, delimiter="\t", lineterminator="\n") + +header = True +for row in reader: + if header: + if len(col_text): + for i, val in enumerate(row[1:]): + row[i+1] = value_eval(col_text, val) + header = False + else: + if len(row_text): + row[0] = value_eval(row_text, row[0]) + if len(cell_text): + for i in range(1,len(row)): + row[i] = value_eval(cell_text,row[i]) + writer.writerow(row) + +ihandle.close() +ohandle.close() + + +]]></configfile> + </configfiles> + <help> +This is a utility to perform editing operations on the contents and column/row labels of a tabular file. + + - The 'Column Eval Code' operations occur on the first line. + - The 'Row Eval Code' operations occur on the first cell of every line + - The 'Cell Eval Code' operations occur on every non-label cell + - If any of the code blocks are empty, the operation is skipped + +Example + +Remove the '.CEL' string from sample names:: + + re.sub(r'.CEL$', '', value) + +Log Transform the matrix cells:: + + math.log(float(value)) + + + </help> +</tool>