diff --git a/config/datatypes_conf.xml.sample b/config/datatypes_conf.xml.sample index e3a260b6a05..4a181ce15e8 100644 --- a/config/datatypes_conf.xml.sample +++ b/config/datatypes_conf.xml.sample @@ -85,8 +85,12 @@ - - + + + + + + @@ -326,7 +330,9 @@ - + + + diff --git a/lib/galaxy/datatypes/converters/csv_to_tabular.xml b/lib/galaxy/datatypes/converters/csv_to_tabular.xml new file mode 100644 index 00000000000..4a00b67e406 --- /dev/null +++ b/lib/galaxy/datatypes/converters/csv_to_tabular.xml @@ -0,0 +1,12 @@ + + + python '$__tool_directory__/tabular_csv.py' -o '$tabular' -i '$csv' + + + + + + + + + diff --git a/lib/galaxy/datatypes/converters/tabular_csv.py b/lib/galaxy/datatypes/converters/tabular_csv.py new file mode 100644 index 00000000000..0b18e1571e5 --- /dev/null +++ b/lib/galaxy/datatypes/converters/tabular_csv.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python +""" +Uses the python csv library to convert to and from tabular + +usage: %prog [--from-tabular] -i in_file -o out_file +""" + +import argparse +import csv + + +def main(): + usage = "Usage: %prog [options]" + parser = argparse.ArgumentParser(usage=usage) + parser.add_argument('-f', '--from-tabular', action='store_true', default=False, dest='fromtabular') + parser.add_argument('-i', '--input', type=str, dest='input') + parser.add_argument('-o', '--output', type=str, dest='output') + args = parser.parse_args() + input_fname = args.input + output_fname = args.output + if args.fromtabular: + convert_to_csv(input_fname, output_fname) + else: + convert_to_tsv(input_fname, output_fname) + + +def convert_to_tsv(input_fname, output_fname): + with open(input_fname, 'rb') as csvfile: + with open(output_fname, 'wb') as ofh: + reader = csv.reader(csvfile) + for line in reader: + ofh.write('\t'.join(line) + '\n') + + +def convert_to_csv(input_fname, output_fname): + with open(input_fname, 'rb') as tabfile: + with open(output_fname, 'wb') as ofh: + writer = csv.writer(ofh, delimiter=',') + for line in tabfile.readlines(): + writer.writerow(line.strip().split('\t')) + + +if __name__ == "__main__": + main() diff --git a/lib/galaxy/datatypes/converters/tabular_to_csv.xml b/lib/galaxy/datatypes/converters/tabular_to_csv.xml new file mode 100644 index 00000000000..2c853109184 --- /dev/null +++ b/lib/galaxy/datatypes/converters/tabular_to_csv.xml @@ -0,0 +1,12 @@ + + + python '$__tool_directory__/tabular_csv.py' --from-tabular -i '$tabular' -o '$csv' + + + + + + + + +