diff options
author | Pjotr Prins | 2020-04-10 12:30:04 -0500 |
---|---|---|
committer | Pjotr Prins | 2020-04-10 12:30:04 -0500 |
commit | 9798e5d898b1996e6537cc4d8ca804b91f595920 (patch) | |
tree | d80e01d733fddb79a3c59b255eeb24d9e2dd27cb /bh20sequploader/main.py | |
parent | f27a6a3e3be2446660e2f59c1106dcaba32971cf (diff) | |
parent | b2d3b410cb5d9e0621cfb32bf71ea84d44bf19b1 (diff) | |
download | bh20-seq-resource-9798e5d898b1996e6537cc4d8ca804b91f595920.tar.gz bh20-seq-resource-9798e5d898b1996e6537cc4d8ca804b91f595920.tar.lz bh20-seq-resource-9798e5d898b1996e6537cc4d8ca804b91f595920.zip |
Merge branch 'master' of github.com:arvados/bh20-seq-resource
Diffstat (limited to 'bh20sequploader/main.py')
-rw-r--r-- | bh20sequploader/main.py | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/bh20sequploader/main.py b/bh20sequploader/main.py index ede9f38..bfb8c51 100644 --- a/bh20sequploader/main.py +++ b/bh20sequploader/main.py @@ -3,6 +3,8 @@ import time import arvados import arvados.collection import json +import magic +from pathlib import Path import urllib.request import socket import getpass @@ -14,7 +16,7 @@ UPLOAD_PROJECT='lugli-j7d0g-n5clictpuvwk8aa' def main(): parser = argparse.ArgumentParser(description='Upload SARS-CoV-19 sequences for analysis') - parser.add_argument('sequence', type=argparse.FileType('r'), help='sequence FASTA') + parser.add_argument('sequence', type=argparse.FileType('r'), help='sequence FASTA/FASTQ') parser.add_argument('metadata', type=argparse.FileType('r'), help='sequence metadata json') args = parser.parse_args() @@ -26,10 +28,27 @@ def main(): col = arvados.collection.Collection(api_client=api) - if args.sequence.name.endswith("fasta") or args.sequence.name.endswith("fa"): - target = "sequence.fasta" - elif args.sequence.name.endswith("fastq") or args.sequence.name.endswith("fq"): + magic_file = Path(__file__).parent / "validation" / "formats.mgc" + val = magic.Magic(magic_file=magic_file.resolve().as_posix(), + uncompress=False, mime=True) + seq_type = val.from_file(args.sequence.name).lower() + print(f"Sequence type: {seq_type}") + if seq_type == "text/fasta": + # ensure that contains only one entry + entries = 0 + for line in args.sequence: + if line.startswith(">"): + entries += 1 + if entries > 1: + raise ValueError("FASTA file contains multiple entries") + break + args.sequence.close() + args.sequence = open(args.sequence.name, "r") target = "reads.fastq" + elif seq_type == "text/fastq": + target = "sequence.fasta" + else: + raise ValueError("Sequence file does not look like FASTA or FASTQ") with col.open(target, "w") as f: r = args.sequence.read(65536) @@ -37,6 +56,7 @@ def main(): while r: f.write(r) r = args.sequence.read(65536) + args.sequence.close() print("Reading metadata") with col.open("metadata.yaml", "w") as f: @@ -45,6 +65,7 @@ def main(): while r: f.write(r) r = args.metadata.read(65536) + args.metadata.close() external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8') |