-
Notifications
You must be signed in to change notification settings - Fork 4
/
ncrf_cat.py
executable file
·94 lines (72 loc) · 2.6 KB
/
ncrf_cat.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
#!/usr/bin/env python
"""
Concatenate several output files from Noise Cancelling Repeat Finder.
"""
from sys import argv,stdin,stdout,stderr,exit
from os import path as os_path
from errno import EPIPE
from gzip import open as gzip_open
def usage(s=None):
message = """
usage: ncrf_cat <file1> [<file2> ...] [--markend]
<file1> an output file from Noise Cancelling Repeat Finder
<file2> another output file from Noise Cancelling Repeat Finder
--markend assume end-of-file markers are absent in the input, and add an
end-of-file marker to the output
(by default we require inputs to have proper end-of-file markers)
Concatenate several output files from Noise Cancelling Repeat Finder. This
is little more than copying the files and adding a blank line between the
files.
It can also be used to verify that the input files contain end-of-file markers
i.e. that they were not truncated when created."""
if (s == None): exit (message)
else: exit ("%s\n%s" % (s,message))
def main():
# parse the command line
requireEof = True
markEndOfFile = False
filenames = []
for arg in argv[1:]:
if (arg in ["--noendmark","--noeof","--nomark"]): # (unadvertised)
requireEof = False
elif (arg in ["--markend]","--markeof"]):
requireEof = False
markEndOfFile = True
else:
filenames += [arg]
if (filenames == []):
usage("you have to give me at least one file")
# copy the files; note that we don't bother (or care) to verify that they
# are really output from ncrf
for (ix,filename) in enumerate(filenames):
if (ix > 0): print
eofMarkerSeen = False
if (filename.endswith(".gz")) or (filename.endswith(".gzip")):
f = gzip_open(filename,"rt")
else:
f = file(filename,"rt")
for line in f:
line = line.rstrip("\n")
if (eofMarkerSeen) and (line != ""):
exit("%s: \"%s\" contains additional stuff after end marker (starting with \"%s\")"
% (os_path.basename(argv[0]),filename,line[:10]))
if (line == "# ncrf end-of-file"):
eofMarkerSeen = True
markEndOfFile = True
continue
if (not eofMarkerSeen):
try:
print line
except IOError,ex:
# "Broken pipe" can happen when downstream tools reject
# our output as their input
if (ex.errno == EPIPE):
exit("%s: [Errno %d] Broken pipe"
% (os_path.basename(argv[0]),ex.errno))
f.close()
if (requireEof) and (not eofMarkerSeen):
exit("%s: \"%s\" may have been truncated (end marker is absent)"
% (os_path.basename(argv[0]),filename))
if (markEndOfFile):
print "# ncrf end-of-file"
if __name__ == "__main__": main()