From 30d10985433035d062ada03beb960e63c03031dc Mon Sep 17 00:00:00 2001 From: Dave B Date: Mon, 21 Jan 2019 10:05:04 -0500 Subject: [PATCH 1/2] Add converters between tabular and CSV. --- config/datatypes_conf.xml.sample | 14 ++++-- .../datatypes/converters/csv_to_tabular.xml | 12 +++++ .../datatypes/converters/tabular_csv.py | 44 +++++++++++++++++++ .../datatypes/converters/tabular_to_csv.xml | 12 +++++ 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 lib/galaxy/datatypes/converters/csv_to_tabular.xml create mode 100644 lib/galaxy/datatypes/converters/tabular_csv.py create mode 100644 lib/galaxy/datatypes/converters/tabular_to_csv.xml diff --git a/config/datatypes_conf.xml.sample b/config/datatypes_conf.xml.sample index 70ab3228653..8ca2e47859d 100644 --- a/config/datatypes_conf.xml.sample +++ b/config/datatypes_conf.xml.sample @@ -72,8 +72,12 @@ - - + + + + + + @@ -96,7 +100,7 @@ - + @@ -304,7 +308,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..53b3148f5a8 --- /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 os.path + +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' + + + + + + + + + From f0ac15266aac6f8c3adcfce38e8aa49208e6ae87 Mon Sep 17 00:00:00 2001 From: Dave B Date: Mon, 21 Jan 2019 10:33:31 -0500 Subject: [PATCH 2/2] Fix imports, E302s --- lib/galaxy/datatypes/converters/tabular_csv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/galaxy/datatypes/converters/tabular_csv.py b/lib/galaxy/datatypes/converters/tabular_csv.py index 53b3148f5a8..0b18e1571e5 100644 --- a/lib/galaxy/datatypes/converters/tabular_csv.py +++ b/lib/galaxy/datatypes/converters/tabular_csv.py @@ -6,8 +6,6 @@ usage: %prog [--from-tabular] -i in_file -o out_file """ import argparse -import os.path - import csv @@ -25,6 +23,7 @@ def main(): 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: @@ -32,6 +31,7 @@ def convert_to_tsv(input_fname, output_fname): 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: