-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranspose.py
99 lines (69 loc) · 2.01 KB
/
transpose.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
"""
Description
Transpose text file columns and rows
Usage
python transpose.py file
gunzip -c file.gz | python transpose.py -
Note
- works with python 2.7 and 3.6
Author
David Laperriere <[email protected]>
"""
from __future__ import print_function
import itertools
import os
import sys
import tempfile
__version_info__ = (1, 0)
__version__ = '.'.join(map(str, __version_info__))
__author__ = "David Laperriere [email protected]"
missing_value = "NA"
sep = '\t'
def usage():
print("Usage: python transpose.py file")
sys.exit(-1)
def read_file(tsv_file):
"""read file or stdin"""
if(tsv_file == "-" or tsv_file == "stdin"):
return sys.stdin
if(Py3):
return open(tsv_file, 'r')
return open(tsv_file, 'rU')
# Python version compat
if sys.version_info[0] <= 2:
Py3 = False
elif sys.version_info[0] >= 3:
Py3 = True
def zipl(*lis):
""" Python 2/3 zip_longest """
if(Py3):
return itertools.zip_longest(*lis, fillvalue=missing_value)
return itertools.izip_longest(*lis, fillvalue=missing_value)
# MAIN
def main():
""" Main check parameters & transpose file """
# check parameters
if len(sys.argv) >= 2:
afile = sys.argv[1]
else:
print("Usage: python transpose.py file")
sys.exit(0)
# replace blanks
tmp = tempfile.NamedTemporaryFile(delete=False)
tmpfile = tmp.name
with read_file(afile) as f:
data = f.read()
data = data.replace(sep + sep, sep + missing_value + sep)
data = data.replace('\n' + sep, '\n' + missing_value + sep)
with open(tmpfile, 'w') as w:
w.write(data)
# transpose rows and columns
with read_file(tmpfile) as f:
lis = [x.split() for x in f]
for x in zipl(*lis):
print(sep.join(x))
tmp.close()
os.unlink(tmpfile)
if __name__ == "__main__":
main()