| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import sys |
| 5 |
import os |
| 6 |
import re |
| 7 |
import gzip |
| 8 |
import argparse |
| 9 |
|
| 10 |
pipe_root = os.path.abspath(os.path.dirname(os.path.abspath(__file__)) + '/' + os.pardir) |
| 11 |
sys.path.append(pipe_root + '/lib') |
| 12 |
|
| 13 |
from common import run_command |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
""" |
| 18 |
These are functions for fastq file analysis |
| 19 |
|
| 20 |
|
| 21 |
""" |
| 22 |
|
| 23 |
def _err_return(msg, log=None): |
| 24 |
if log: |
| 25 |
log.error(msg) |
| 26 |
print msg |
| 27 |
return None |
| 28 |
|
| 29 |
|
| 30 |
''' |
| 31 |
|
| 32 |
For a given fastq file (zipped or unzipped), check the format by comparing the string lengths of seq and quality |
| 33 |
|
| 34 |
''' |
| 35 |
def check_fastq_format(fastq, log=None): |
| 36 |
if not fastq: |
| 37 |
return _err_return('Function read_length_from_file() requires a fastq file.', log) |
| 38 |
|
| 39 |
if not os.path.isfile(fastq): |
| 40 |
return _err_return('The given fastq file [%s] does not exist' % fastq, log) |
| 41 |
|
| 42 |
if fastq.endswith(".gz"): |
| 43 |
fh = gzip.open(fastq, 'r') |
| 44 |
else: |
| 45 |
fh = open(fastq, 'r') |
| 46 |
|
| 47 |
lCnt = 0 |
| 48 |
seq = None |
| 49 |
lstr = None |
| 50 |
missing = False |
| 51 |
|
| 52 |
while 1: |
| 53 |
lstr = fh.readline() |
| 54 |
if not lstr: |
| 55 |
break |
| 56 |
|
| 57 |
lCnt += 1 |
| 58 |
seqLen = 0 |
| 59 |
qualLen = 0 |
| 60 |
|
| 61 |
idx = lCnt % 4 |
| 62 |
|
| 63 |
if idx == 1: |
| 64 |
continue |
| 65 |
elif idx == 2: |
| 66 |
seq = lstr.strip() |
| 67 |
elif idx == 3: |
| 68 |
continue |
| 69 |
elif idx == 0: |
| 70 |
seqLen = len(seq) |
| 71 |
qualLen = len(lstr.strip()) |
| 72 |
|
| 73 |
if seqLen != qualLen: |
| 74 |
missing = True |
| 75 |
break |
| 76 |
|
| 77 |
fh.close() |
| 78 |
|
| 79 |
if missing: |
| 80 |
log.error("Incorrect fastq file: missing quality score character or base character.\n seq=%s\n qual=%s" % (seq, lstr)) |
| 81 |
return -1 |
| 82 |
|
| 83 |
if lCnt % 4 != 0: |
| 84 |
log.error("Incorrect fastq file: missing fastq record item. Number of lines in the output fastq file = %d" % (lCnt)) |
| 85 |
return -2 |
| 86 |
|
| 87 |
|
| 88 |
return lCnt |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
''' |
| 93 |
Ref : JGI_Utility::read_length_from_file |
| 94 |
The first read count (read 1 and read 2) meets the sszie will stop the record reading. |
| 95 |
''' |
| 96 |
def read_length_from_file(fastq, log=None, ssize=10000): |
| 97 |
''' For a given fastq file (zipped or unzipped), reads the first *ssize* reads to compute average length |
| 98 |
and return (avgLen1, avgLen2, isPE) |
| 99 |
The file scanning will stop when the ssize is met by either read (1 or 2). |
| 100 |
''' |
| 101 |
|
| 102 |
is_pe = False |
| 103 |
readi = 0 |
| 104 |
readj = 0 |
| 105 |
leni = 0 |
| 106 |
lenj = 0 |
| 107 |
|
| 108 |
if not fastq: |
| 109 |
return _err_return('Function read_length_from_file() requires a fastq file.', log) |
| 110 |
|
| 111 |
if not os.path.isfile(fastq): |
| 112 |
return _err_return('The given fastq file [%s] does not exist' % fastq, log) |
| 113 |
|
| 114 |
if fastq.endswith(".gz"): |
| 115 |
fh = gzip.open(fastq, 'r') |
| 116 |
else: |
| 117 |
fh = open(fastq, 'r') |
| 118 |
|
| 119 |
lCnt = 0 |
| 120 |
done = False |
| 121 |
|
| 122 |
while not done: |
| 123 |
lstr = fh.readline() |
| 124 |
lstr = lstr.rstrip() |
| 125 |
|
| 126 |
lCnt += 1 |
| 127 |
idx = lCnt % 4 |
| 128 |
if idx == 1: |
| 129 |
header = lstr |
| 130 |
elif idx == 2: |
| 131 |
seq = lstr |
| 132 |
elif idx == 3: |
| 133 |
plus = lstr |
| 134 |
elif idx == 4: |
| 135 |
quality = lstr |
| 136 |
else: |
| 137 |
if not header or not seq: |
| 138 |
done = True |
| 139 |
|
| 140 |
if header.find('enter') != -1 and header.find('exit') != -1: |
| 141 |
continue |
| 142 |
|
| 143 |
match = re.match(r'^@([^/]+)([ |/]1)', header) |
| 144 |
aLen = len(seq.strip()) |
| 145 |
if match: |
| 146 |
|
| 147 |
readi += 1 |
| 148 |
if readi > ssize: |
| 149 |
readi -= 1 |
| 150 |
done = True |
| 151 |
else: |
| 152 |
leni += aLen |
| 153 |
else: |
| 154 |
match = re.match(r'^@([^/]+)([ |/]2)', header) |
| 155 |
if match: |
| 156 |
readj += 1 |
| 157 |
if readj > ssize: |
| 158 |
readj -= 1 |
| 159 |
done = True |
| 160 |
else: |
| 161 |
lenj += aLen |
| 162 |
if leni > 0: |
| 163 |
is_pe = True |
| 164 |
fh.close() |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if leni > 0 and readi > 0: |
| 169 |
leni = leni / readi |
| 170 |
if lenj > 0 and readj > 0: |
| 171 |
lenj = lenj / readj |
| 172 |
|
| 173 |
return (leni, lenj, is_pe) |
| 174 |
|
| 175 |
|
| 176 |
def get_working_read_length(fastq, log): |
| 177 |
read_length = 0 |
| 178 |
read_length_1 = 0 |
| 179 |
read_length_2 = 0 |
| 180 |
|
| 181 |
(read_length_1, read_length_2, is_pe) = read_length_from_file(fastq, log) |
| 182 |
|
| 183 |
if not is_pe: |
| 184 |
log.info("It is NOT pair-ended. read_length_1 %s" % (read_length_1)) |
| 185 |
read_length = read_length_1 |
| 186 |
else: |
| 187 |
log.info("It is pair-ended. read_length_1 %s read_length_2 %s" % (read_length_1, read_length_2)) |
| 188 |
if read_length_1 != read_length_2: |
| 189 |
log.warning("The lengths of read 1 (" + str(read_length_1) + ") and read 2 (" + str(read_length_2) + ") are not equal") |
| 190 |
|
| 191 |
if read_length_1 < read_length_2: |
| 192 |
read_length = read_length_1 |
| 193 |
else: |
| 194 |
read_length = read_length_2 |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
return (read_length, read_length_1, read_length_2, is_pe) |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
def read_count(fpath): |
| 205 |
'return the raw read count in a fastq file, assuming each record occupy 4 lines in file.' |
| 206 |
if os.path.isfile(fpath): |
| 207 |
cdir = os.path.dirname(__file__) |
| 208 |
cmd = os.path.join(cdir, '../../testformat2.sh') |
| 209 |
cmd = '%s in=%s | grep "^Reads"' % (cmd, fpath) |
| 210 |
stdOut, stdErr, exitCode = run_command(cmd, True) |
| 211 |
|
| 212 |
if exitCode == 0: |
| 213 |
toks = stdOut.strip().split() |
| 214 |
if len(toks) == 2: |
| 215 |
return int(toks[1]) |
| 216 |
else: |
| 217 |
print('error in %s:read_count (%s): wrong run output [%s]' % (__file__, cmd, stdOut.strip())) |
| 218 |
else: |
| 219 |
print('error in %s:read_count (%s): %s' % (__file__, cmd, stdErr.strip())) |
| 220 |
|
| 221 |
return None |