Warning
This document is for an in-development version of Galaxy. You can alternatively view this page in the latest release if it exists or view the top of the latest release's documentation.
Source code for galaxy.datatypes.converters.tabular_csv
#!/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
[docs]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)
[docs]def convert_to_tsv(input_fname, output_fname):
with open(input_fname, newline="") as csvfile, open(output_fname, 'w') as ofh:
reader = csv.reader(csvfile)
for line in reader:
ofh.write('\t'.join(line) + '\n')
[docs]def convert_to_csv(input_fname, output_fname):
with open(input_fname) as tabfile, open(output_fname, 'w', newline='') as ofh:
writer = csv.writer(ofh, delimiter=',')
for line in tabfile.readlines():
writer.writerow(line.strip().split('\t'))
if __name__ == "__main__":
main()