annotate run_fastq_scan.py @ 12:50867379763d draft

Uploaded
author estrain
date Fri, 10 Mar 2023 17:01:21 +0000
parents a53acd38d77e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a53acd38d77e Uploaded
estrain
parents:
diff changeset
1 #!/usr/bin/env
a53acd38d77e Uploaded
estrain
parents:
diff changeset
2
a53acd38d77e Uploaded
estrain
parents:
diff changeset
3 ## Run fastq-scan to get mean read length and mean quality score
a53acd38d77e Uploaded
estrain
parents:
diff changeset
4 ## author: errol strain, estrain@gmail.com
a53acd38d77e Uploaded
estrain
parents:
diff changeset
5
a53acd38d77e Uploaded
estrain
parents:
diff changeset
6 from argparse import (ArgumentParser, FileType)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
7 import sys
a53acd38d77e Uploaded
estrain
parents:
diff changeset
8 import glob
a53acd38d77e Uploaded
estrain
parents:
diff changeset
9 import subprocess
a53acd38d77e Uploaded
estrain
parents:
diff changeset
10 import json
a53acd38d77e Uploaded
estrain
parents:
diff changeset
11
a53acd38d77e Uploaded
estrain
parents:
diff changeset
12 def parse_args():
a53acd38d77e Uploaded
estrain
parents:
diff changeset
13 "Parse the input arguments, use '-h' for help."
a53acd38d77e Uploaded
estrain
parents:
diff changeset
14
a53acd38d77e Uploaded
estrain
parents:
diff changeset
15 parser = ArgumentParser(description='Run fastq-scan on a pair of gzipped FASTQ files')
a53acd38d77e Uploaded
estrain
parents:
diff changeset
16
a53acd38d77e Uploaded
estrain
parents:
diff changeset
17 # Read inputs
a53acd38d77e Uploaded
estrain
parents:
diff changeset
18 parser.add_argument('--fastq', type=str, required=True, nargs=2, help='FASTQ files')
a53acd38d77e Uploaded
estrain
parents:
diff changeset
19 parser.add_argument('--output', type=str, required=True, nargs=1, help='Output File')
a53acd38d77e Uploaded
estrain
parents:
diff changeset
20 parser.add_argument('--type', type=str, required=True, nargs=1, help='File Type (text or gz)')
a53acd38d77e Uploaded
estrain
parents:
diff changeset
21
a53acd38d77e Uploaded
estrain
parents:
diff changeset
22 return parser.parse_args()
a53acd38d77e Uploaded
estrain
parents:
diff changeset
23
a53acd38d77e Uploaded
estrain
parents:
diff changeset
24 args =parse_args()
a53acd38d77e Uploaded
estrain
parents:
diff changeset
25
a53acd38d77e Uploaded
estrain
parents:
diff changeset
26 # FASTA file
a53acd38d77e Uploaded
estrain
parents:
diff changeset
27 r1 = args.fastq[0]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
28 r2 = args.fastq[1]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
29
a53acd38d77e Uploaded
estrain
parents:
diff changeset
30 # Read 1
a53acd38d77e Uploaded
estrain
parents:
diff changeset
31 if str(args.type[0]) == "gz" :
a53acd38d77e Uploaded
estrain
parents:
diff changeset
32 cmd1 = ["zcat", r1]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
33 else :
a53acd38d77e Uploaded
estrain
parents:
diff changeset
34 cmd1 = ["cat", r1]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
35 cmd2 = ["fastq-scan"]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
36 pcmd1= subprocess.Popen(cmd1,stdout= subprocess.PIPE,shell=False)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
37 r1json = json.loads(subprocess.Popen(cmd2, stdin=pcmd1.stdout,stdout=subprocess.PIPE,shell=False).communicate()[0])
a53acd38d77e Uploaded
estrain
parents:
diff changeset
38 r1q = round(r1json["qc_stats"]["qual_mean"],1)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
39 r1l = round(r1json["qc_stats"]["read_mean"],1)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
40
a53acd38d77e Uploaded
estrain
parents:
diff changeset
41 # Read 2
a53acd38d77e Uploaded
estrain
parents:
diff changeset
42 if str(args.type[0]) == "gz" :
a53acd38d77e Uploaded
estrain
parents:
diff changeset
43 cmd1 = ["zcat", r2]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
44 else :
a53acd38d77e Uploaded
estrain
parents:
diff changeset
45 cmd1 = ["cat", r2]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
46 cmd2 = ["fastq-scan"]
a53acd38d77e Uploaded
estrain
parents:
diff changeset
47 pcmd1= subprocess.Popen(cmd1,stdout= subprocess.PIPE,shell=False)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
48 r2json = json.loads(subprocess.Popen(cmd2, stdin=pcmd1.stdout,stdout=subprocess.PIPE,shell=False).communicate()[0])
a53acd38d77e Uploaded
estrain
parents:
diff changeset
49 r2q = round(r2json["qc_stats"]["qual_mean"],1)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
50 r2l = round(r2json["qc_stats"]["read_mean"],1)
a53acd38d77e Uploaded
estrain
parents:
diff changeset
51
a53acd38d77e Uploaded
estrain
parents:
diff changeset
52 # Write output to be used by sum_mlst.py
a53acd38d77e Uploaded
estrain
parents:
diff changeset
53 output = open(args.output[0],"w")
a53acd38d77e Uploaded
estrain
parents:
diff changeset
54 output.write(str(r1l) + "\t" + str(r2l) + "\t" + str(r1q) + "\t" + str(r2q))