| 1 |
|
| 2 |
|
| 3 |
from __future__ import division |
| 4 |
import sys |
| 5 |
import os |
| 6 |
import argparse |
| 7 |
import string |
| 8 |
from collections import Counter |
| 9 |
import random |
| 10 |
sys.path.append(os.path.dirname(__file__)); |
| 11 |
import readSeq |
| 12 |
import multiprocessing |
| 13 |
|
| 14 |
import matplotlib as mpl |
| 15 |
mpl.use('Agg') |
| 16 |
import matplotlib.pyplot as plt |
| 17 |
|
| 18 |
KMER = 16 |
| 19 |
SUBLEN = 1000000 |
| 20 |
|
| 21 |
|
| 22 |
def getArgs(): |
| 23 |
parser = argparse.ArgumentParser(description="Count occurance of database kmers in reads", formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 24 |
parser.add_argument('-k', default=KMER, dest='kmer', metavar='<int>', type=int, help="kmer length") |
| 25 |
parser.add_argument('-l', dest='sublen', metavar='<int>', type=int, help="perform analysis on first <int> bases [RDLEN - K + 1]") |
| 26 |
parser.add_argument('-c', default=2, dest='cutoff', metavar='<int>', type=int, help="minimum allowed coverage") |
| 27 |
parser.add_argument('-t', default=30, dest='targetCov', metavar='<int>', type=int, help="target coverage") |
| 28 |
parser.add_argument('-p', '--plot', dest='plot', type=str, default=None, metavar="<file>", help='plot data and save as png to <file>') |
| 29 |
parser.add_argument('fastaFile', type=str, help='Input FASTA file(s). Text or gzip') |
| 30 |
parser.add_argument('fastqFile', nargs='+', help='Input FASTQ file(s). Text or gzip') |
| 31 |
args = parser.parse_args() |
| 32 |
return args |
| 33 |
|
| 34 |
def getMers(seq, merLen): |
| 35 |
for i in xrange(min(len(seq) - merLen + 1,SUBLEN)): |
| 36 |
yield seq[i:i+merLen] |
| 37 |
|
| 38 |
complements = string.maketrans('acgtACGT', 'tgcaTGCA') |
| 39 |
def revComp(seq): |
| 40 |
revCompSeq = seq.translate(complements)[::-1] |
| 41 |
return revCompSeq |
| 42 |
|
| 43 |
def tallyPositions(seq,counts,sublen=None): |
| 44 |
readPos = 0 |
| 45 |
for mer in getMers(seq, KMER): |
| 46 |
if mer in merCnts: |
| 47 |
if readPos > len(counts): |
| 48 |
counts.append(1) |
| 49 |
else: |
| 50 |
counts[readPos] += 1 |
| 51 |
readPos += 1 |
| 52 |
if sublen: |
| 53 |
if readPos == sublen: |
| 54 |
break |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
def main(): |
| 59 |
|
| 60 |
""" |
| 61 |
kmer based normization |
| 62 |
call rand once |
| 63 |
004 |
| 64 |
if avecov1|2 < mincov: |
| 65 |
trash |
| 66 |
elif avecov1|2 < target: |
| 67 |
keep |
| 68 |
elif random < target/avecov1|2: |
| 69 |
keep |
| 70 |
else: |
| 71 |
trash |
| 72 |
""" |
| 73 |
|
| 74 |
args = getArgs() |
| 75 |
|
| 76 |
global KMER |
| 77 |
KMER = args.kmer |
| 78 |
|
| 79 |
out=sys.stdout |
| 80 |
|
| 81 |
|
| 82 |
for fq in args.fastqFile: |
| 83 |
if not os.path.exists(fq): |
| 84 |
sys.stderr.write("ERROR: Input file '%s' does not exist. Exiting.\n" % fq) |
| 85 |
sys.exit() |
| 86 |
|
| 87 |
|
| 88 |
sys.stderr.write("Making k-mer database\n") |
| 89 |
global merCnts |
| 90 |
merCnts = Counter() |
| 91 |
for record in readSeq.readSeq(args.fastaFile,fileType='fasta'): |
| 92 |
for mer in getMers(record.seq, args.kmer): |
| 93 |
sortMers = sorted([mer, revComp(mer)]) |
| 94 |
merCnts[mer] += 1 |
| 95 |
merCnts[revComp(mer)] += 1 |
| 96 |
|
| 97 |
|
| 98 |
sys.stderr.write("Tallying occurrences of database kmers in reads\n") |
| 99 |
|
| 100 |
seqIt = readSeq.readSeq(fq,paired=True) |
| 101 |
record1,record2 = seqIt.next() |
| 102 |
readLen = len(record1.seq) |
| 103 |
sys.stderr.write("Read length = %d\n" % readLen) |
| 104 |
|
| 105 |
tallyLen = readLen - args.kmer + 1 |
| 106 |
if args.sublen: |
| 107 |
if args.sublen > tallyLen: |
| 108 |
sys.stderr.write("sublen (-l) must be less than readlen - k + 1 : (found reads of length %d\n" % readLen) |
| 109 |
sys.exit(-1) |
| 110 |
tallyLen = args.sublen |
| 111 |
|
| 112 |
counts1 = [0 for x in range(tallyLen)] |
| 113 |
counts2 = [0 for x in range(tallyLen)] |
| 114 |
|
| 115 |
tallyPositions(record1.seq,counts1,tallyLen) |
| 116 |
tallyPositions(record2.seq,counts2,tallyLen) |
| 117 |
total_reads = 1 |
| 118 |
|
| 119 |
fqName = "" |
| 120 |
|
| 121 |
for fq in args.fastqFile: |
| 122 |
for record1, record2 in seqIt: |
| 123 |
tallyPositions(record1.seq,counts1,tallyLen) |
| 124 |
tallyPositions(record2.seq,counts2,tallyLen) |
| 125 |
total_reads += 1 |
| 126 |
fqName += fq+" " |
| 127 |
|
| 128 |
|
| 129 |
counts1_perc = [ 100 * float(x)/total_reads for x in counts1 ] |
| 130 |
counts2_perc = [ 100 * float(x)/total_reads for x in counts2 ] |
| 131 |
out.write("#pos\tread1_count\tread1_perc\tread2_count\tread2_perc\n") |
| 132 |
for i in range(tallyLen): |
| 133 |
out.write("%i\t%i\t%0.2f\t%i\t%0.2f\n" % (i+1,counts1[i],counts1_perc[i],counts2[i],counts2_perc[i])) |
| 134 |
|
| 135 |
if args.plot: |
| 136 |
sys.stderr.write("Plotting data. Saving to %s\n" % args.plot) |
| 137 |
plt.ioff() |
| 138 |
xcoord = range(1,tallyLen+1) |
| 139 |
plt.plot(xcoord,counts1_perc,color="red",linewidth=1.0,linestyle="-",label="Read 1") |
| 140 |
plt.plot(xcoord,counts2_perc,color="green",linewidth=1.0,linestyle="-",label="Read 2") |
| 141 |
leg_loc="upper right" |
| 142 |
max_cnt = 0 |
| 143 |
max_pos = 0 |
| 144 |
for i in range(len(counts1)): |
| 145 |
if counts1[i] > max_cnt or counts2[i] > max_cnt: |
| 146 |
max_cnt = counts1[i] if counts1[i] > counts2[i] else counts2[i] |
| 147 |
max_pos = i |
| 148 |
if max_pos > 0.5*len(counts1) : |
| 149 |
leg_loc="upper left" |
| 150 |
plt.legend(loc=leg_loc,prop={'size':10}) |
| 151 |
plt.xlabel("Read Position") |
| 152 |
plt.ylabel("Percent Reads with Database k-mer") |
| 153 |
plt.title("Occurrence of reference k-mers (k = %i) in \n%s (# reads = %d)" % (args.kmer,fqName,total_reads)) |
| 154 |
plt.savefig(args.plot) |
| 155 |
|
| 156 |
|
| 157 |
if __name__ == '__main__': |
| 158 |
|
| 159 |
try: |
| 160 |
main() |
| 161 |
except KeyboardInterrupt: |
| 162 |
pass |
| 163 |
|