aboutsummaryrefslogtreecommitdiff
path: root/workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py
diff options
context:
space:
mode:
authorAndreaGuarracino2021-01-07 23:50:01 +0100
committerAndreaGuarracino2021-01-07 23:50:01 +0100
commit4d841d279b2bf73da2ba815d53863c7f2861c956 (patch)
tree83b9ad136dabacbf7ed54e19b2db6df348bef904 /workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py
parent141e619929cee17018417d71111063015e73c366 (diff)
parentc080c3cffedcc0cc99496b5e70fcfdf998978f16 (diff)
downloadbh20-seq-resource-4d841d279b2bf73da2ba815d53863c7f2861c956.tar.gz
bh20-seq-resource-4d841d279b2bf73da2ba815d53863c7f2861c956.tar.lz
bh20-seq-resource-4d841d279b2bf73da2ba815d53863c7f2861c956.zip
Merge branch 'master' into yamlfa2ttl
Diffstat (limited to 'workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py')
-rwxr-xr-xworkflows/pull-data/genbank/transform-genbank-xml2yamlfa.py78
1 files changed, 41 insertions, 37 deletions
diff --git a/workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py b/workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py
index 9414864..1a8035d 100755
--- a/workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py
+++ b/workflows/pull-data/genbank/transform-genbank-xml2yamlfa.py
@@ -33,43 +33,47 @@ states = {}
for xmlfn in args.files:
print(f"--- Reading {xmlfn}")
- with gzip.open(xmlfn, 'r') as f:
- xml = f.read().decode()
- tree = ET.fromstring(xml)
- for gb in tree.findall('./GBSeq'):
- valid = None
- error = None
- meta = {}
- id = gb.find("GBSeq_locus").text
- basename = dir+"/"+id
- print(f" parsing {id}")
- try:
- valid,meta = genbank.get_metadata(id,gb)
- if valid:
- # --- write JSON
- jsonfn = basename + ".json"
- with open(jsonfn, 'w') as outfile:
- print(f" writing {jsonfn}")
- json.dump(meta, outfile, indent=4)
- # --- write FASTA
- fa = basename+".fa"
- seq = genbank.get_sequence(id,gb)
- print(f" writing {fa}")
- with open(fa,"w") as f2:
- f2.write(f"> {id}\n")
- f2.write(seq)
- # print(seq)
- except genbank.GBError as e:
- error = f"{e} for {id}"
- print(error,file=sys.stderr)
- valid = False
- state = {}
- state['valid'] = valid
- if error:
- state['error'] = error
- if meta['warnings']:
- state['warnings'] = meta['warnings']
- states[id] = state
+ try:
+ with gzip.open(xmlfn, 'r') as f:
+ xml = f.read().decode()
+ except Exception:
+ with open(xmlfn, 'r') as f:
+ xml = f.read()
+ tree = ET.fromstring(xml)
+ for gb in tree.findall('./GBSeq'):
+ valid = None
+ error = None
+ meta = {}
+ id = gb.find("GBSeq_locus").text
+ basename = dir+"/"+id
+ print(f" parsing {id}")
+ try:
+ valid,meta = genbank.get_metadata(id,gb)
+ if valid:
+ # --- write JSON
+ jsonfn = basename + ".json"
+ with open(jsonfn, 'w') as outfile:
+ print(f" writing {jsonfn}")
+ json.dump(meta, outfile, indent=4)
+ # --- write FASTA
+ fa = basename+".fa"
+ seq = genbank.get_sequence(id,gb)
+ print(f" writing {fa}")
+ with open(fa,"w") as f2:
+ f2.write(f"> {id}\n")
+ f2.write(seq)
+ # print(seq)
+ except genbank.GBError as e:
+ error = f"{e} for {id}"
+ print(error,file=sys.stderr)
+ valid = False
+ state = {}
+ state['valid'] = valid
+ if error:
+ state['error'] = error
+ if meta['warnings']:
+ state['warnings'] = meta['warnings']
+ states[id] = state
statefn = dir + '/state.json'
with open(statefn, 'w') as outfile: