-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse-receipt.py
More file actions
executable file
·79 lines (57 loc) · 1.92 KB
/
Copy pathparse-receipt.py
File metadata and controls
executable file
·79 lines (57 loc) · 1.92 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
76
77
78
79
#!/usr/bin/env python3
import sys
import argparse
import untangle
def set_up_argparse():
parser = argparse.ArgumentParser(
description = """
Parse the XML data received from the submission server
""")
parser.add_argument("--tsv", "-t",
dest = "is_tabular", action='store_true',
help = "output in tabular format (space separated value)")
parser.add_argument("--out", "-o",
dest = "out_file", default = sys.stdout,
help = "optional output file. Default: stdout")
parser.add_argument("xml_file", metavar = "RECEIPT_XML",
help = "receipt xml file from ENA server")
opts = parser.parse_args()
return opts
def extract(element, store = { "SUBMISSION": [], "PROJECT": [], "SAMPLE": [],
"EXPERIMENT": [], "RUN": [], }):
for child in element.children:
if child._name in ["SUBMIT", "EXPERIMENT", "RUN"]:
store[child._name].append([child["alias"], child["accession"]])
if child._name in ["PROJECT", "SAMPLE"]:
store[child._name].append([child["alias"], child["accession"],
child.EXT_ID["accession"]])
return store
def output(data_dict, tabular = True, fh = sys.stdout):
if tabular == True:
for key, arrays in data_dict.items():
for array in arrays:
string = key + " " + " ".join( array )
print(string, file = fh)
else:
print(data_dict, file = fh)
def main(opts):
try:
xml = untangle.parse(opts.xml_file)
receipt = xml.RECEIPT
except:
print("Probably not a valid XML file!", file = sys.stderr)
sys.exit(1)
if receipt["success"] == "false":
print("Submission failed!", file = sys.stderr)
sys.exit(1)
# extract receipt data from xml
data = extract(receipt)
# output formated data
if opts.out_file == sys.stdout:
output(data, tabular = opts.is_tabular)
else:
with open(opts.out_file, "w") as fh:
output(data, tabular = opts.is_tabular, fh = fh)
if __name__ == "__main__":
opts = set_up_argparse()
main(opts)