-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastq-read-count.py
executable file
·164 lines (124 loc) · 4.81 KB
/
fastq-read-count.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# ----------------------------------------------------------------------------------
# Script counts total amount of reads ans bases in fastq files
#-----------------------------------------------------------------------------------
__version__ = "1.1.a"
__last_update_date__ = "2020-04-16"
import sys
if sys.version_info.major < 3:
print( "\nYour python interpreter version is " + "%d.%d" % (sys.version_info.major,
sys.version_info.minor) )
print(" Please, use Python 3.\a")
# In python 2 'raw_input' does the same thing as 'input' in python 3.
# Neither does 'input' in python2.
if sys.platform.startswith("win"):
raw_input("Press ENTER to exit:")
# end if
sys.exit(1)
# end if
def platf_depend_exit(exit_code=0):
"""
Function asks to press ENTER press on Windows
and exits after that.
:type exit_code: int;
"""
if sys.platform.startswith("win"):
input("Press ENTER to exit:")
# end if
sys.exit(exit_code)
# end def platf_depend_exit
def print_help():
print("\nScript 'fastq-read-count.py' counts amount of reads and bases in fastq file(s).\n")
print("Version {}; {} edition.".format(__version__, __last_update_date__))
print("\nUsage:")
print(" python3 fastq-read-count.py first.fastq second.fastq.gz third.fq.gz")
print("Following command will process all *.fastq(.gz) and *.fq(.gz) files in the working directory:")
print(" python3 fastq-read-count.py")
print("\nOptions:")
print(" -h (--help): print help message.")
print(" -v (--version): print version.")
# end if
# Firstly check for information-providing flags
if "-h" in sys.argv[1:] or "--help" in sys.argv[1:]:
print_help()
platf_depend_exit()
# end if
if "-v" in sys.argv[1:] or "--version" in sys.argv[1:]:
print(__version__)
platf_depend_exit()
# end if
import os
from re import search as re_search
is_fastq = lambda f: False if re_search(r".*\.f(ast)?q(\.gz)?$", f) is None else True
fpaths = list()
valid_options = ("-h", "--help", "-v", "--version")
for arg in sys.argv[1:]:
if not arg in valid_options:
if not os.path.exists(arg):
print("File '{}' does not exist!".format(arg))
platf_depend_exit(1)
# end if
if not is_fastq(arg):
print("File '{}' does not look like a fastq file".format(arg))
print("Script understands only '*.fastq(.gz)'' and '*.fq(.gz)' extentions")
platf_depend_exit(1)
# end if
fpaths.append(arg)
# end for
del valid_options
# If no files are specified in command line
if len(fpaths) == 0:
# Get all fastq files in working dir
fpaths = tuple(filter(is_fastq, os.listdir('.')))
# end if
if len(fpaths) == 0:
print_help()
platf_depend_exit(1)
# end if
print("fastq-read-count. Version {}; {} edition.\n".format(__version__, __last_update_date__))
print("Following files are found and will be processed:")
for i, f in enumerate(fpaths):
print("{}. '{}'".format(i+1, os.path.abspath(f)))
# end for
print('-' * 45 + '\n')
import gzip
LINES_IN_READ = 4 # 4 lines per fastq record
total_read_num = 0
total_base_num = 0
with open("fastq-read-count_result.tsv", 'w') as outfile:
outfile.write('\t'.join(("File", "Number of reads", "Number of bases")) + '\n')
for i, fpath in enumerate(fpaths):
line_counter = 0
base_counter = 0
# Select open function
open_func = gzip.open if fpath.endswith(".gz") else open
with open_func(fpath) as infile:
j = 1
line_counter = 0
for line in infile:
if j == 2: # count bases
base_counter += len(line.strip())
elif j == 4:
j = 0 # reset
# end if
j += 1
line_counter += 1
read_num = int(line_counter // LINES_IN_READ) # count lines
# end with
total_read_num += read_num
total_base_num += base_counter
# Configure numbers with space-separated digit triades:
str_read_num = "{:,}".format(read_num).replace(',', ' ')
str_base_num = "{:,}".format(base_counter).replace(',', ' ')
print("{}. '{}' - {} reads; {} bases".format(i + 1, os.path.basename(fpath), str_read_num, str_base_num))
outfile.write('\t'.join((os.path.basename(fpath), str(read_num),str(base_counter))) + '\n')
# end for
# Write total number of reads and bases
print("Total: {:,} reads.".format(total_read_num).replace(',', ' '))
print("Total: {:,} bases.".format(total_base_num).replace(',', ' '))
outfile.write('\t'.join(("Total: ", str(total_read_num))) + ' reads\n')
outfile.write('\t'.join(("Total: ", str(total_base_num))) + ' bases\n')
# end with
print("Completed!")
platf_depend_exit()