-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabcat.py
55 lines (47 loc) · 1.8 KB
/
tabcat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright (c) 2023 Dr. K. D. Murray/Gekkonid Consulting <spam@gekkonid.com>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import csv
import sys
import math
import argparse
def tabcat_main(argv=None):
"""Concatenate table (c/tsv) files, adding the filename as a column"""
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--delim", default=",",
help="Column delimiter.")
ap.add_argument("-o", "--output", default="-", type=argparse.FileType("wt"),
help="Column delimiter.")
ap.add_argument("-N", "--add-name", action="store_true",
help="Add a column with the file name")
ap.add_argument("-f", "--fofn", action="store_true",
help="Each input is a file of filenames")
ap.add_argument("xsvs", help="Input {C,T}SVs to concatenate", nargs="+")
args = ap.parse_args(argv)
class xsv(csv.unix_dialect):
delimiter = args.delim
quotechar = '"'
doublequote = True
skipinitialspace = True
quoting=csv.QUOTE_MINIMAL
if args.fofn:
files = []
for fofn in args.xsvs:
with open(fofn) as fh:
for file in fh:
files.append(file.rstrip())
else:
files = args.xsvs
outcsv = None
for file in files:
with open(file) as fh:
for rec in csv.DictReader(fh, dialect=xsv):
rec["tabcat_filename"] = file
if outcsv is None:
outcsv = csv.DictWriter(args.output, dialect=xsv, fieldnames=[k for k in rec])
outcsv.writeheader()
outcsv.writerow(rec)
if __name__ == "__main__":
tabcat_main()