-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmergeparallel.py
More file actions
executable file
·75 lines (63 loc) · 2.49 KB
/
mergeparallel.py
File metadata and controls
executable file
·75 lines (63 loc) · 2.49 KB
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
#!/usr/bin/env python3
# code by Jon May [jonmay@isi.edu]
import argparse
import sys
import codecs
if sys.version_info[0] == 2:
from itertools import izip
else:
izip = zip
from collections import defaultdict as dd
import re
import os.path
import gzip
import tempfile
import shutil
import atexit
import lxml.etree as ET
scriptdir = os.path.dirname(os.path.abspath(__file__))
reader = codecs.getreader('utf8')
writer = codecs.getwriter('utf8')
def prepfile(fh, code):
ret = gzip.open(fh.name, code) if fh.name.endswith(".gz") else fh
if sys.version_info[0] == 2:
if code.startswith('r'):
ret = reader(fh)
elif code.startswith('w'):
ret = writer(fh)
else:
sys.stderr.write("I didn't understand code "+code+"\n")
sys.exit(1)
return ret
def addonoffarg(parser, arg, dest=None, default=True, help="TODO"):
''' add the switches --arg and --no-arg that set parser.arg to true/false, respectively'''
group = parser.add_mutually_exclusive_group()
dest = arg if dest is None else dest
group.add_argument('--%s' % arg, dest=dest, action='store_true', default=default, help=help)
group.add_argument('--no-%s' % arg, dest=dest, action='store_false', default=default, help="See --%s" % arg)
def main():
parser = argparse.ArgumentParser(description="merge elisa xml files with different targets of the same source, assuming source id is consistent per segment",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--infiles", "-i", nargs='+', type=argparse.FileType('r'), default=sys.stdin, help="input files")
parser.add_argument("--outfile", "-o", nargs='?', type=argparse.FileType('w'), default=sys.stdout, help="output file")
workdir = tempfile.mkdtemp(prefix=os.path.basename(__file__), dir=os.getenv('TMPDIR', '/tmp'))
def cleanwork():
shutil.rmtree(workdir, ignore_errors=True)
atexit.register(cleanwork)
try:
args = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
infiles = [prepfile(x, 'r') for x in args.infiles]
outfile = prepfile(args.outfile, 'w')
roots = [ET.parse(x) for x in infiles]
prime = roots[0]
rest = roots[1:]
for seg in prime.findall(".//SEGMENT"):
srcid = seg.find(".//SOURCE").get('id')
for doc in rest:
trgnode = doc.find(".//SOURCE[@id='%s']" % srcid).getparent().find(".//TARGET")
seg.append(trgnode)
outfile.write(ET.tostring(prime, pretty_print=True, encoding='utf-8').decode('utf-8'))
if __name__ == '__main__':
main()