annotate tools/ilmn_pacbio/quake.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/env python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 from optparse import OptionParser, SUPPRESS_HELP
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 import os, random, sys
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 import cov_model
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 # quake.py
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 # Launch pipeline to correct errors in Illumina sequencing
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 # reads.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 #r_dir = '/nfshomes/dakelley/research/error_correction/bin'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 quake_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 # main
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 def main():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 usage = 'usage: %prog [options]'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 parser = OptionParser(usage)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 parser.add_option('-r', dest='readsf', help='Fastq file of reads')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 parser.add_option('-f', dest='reads_listf', help='File containing fastq file names, one per line or two per line for paired end reads.')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 parser.add_option('-k', dest='k', type='int', help='Size of k-mers to correct')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 parser.add_option('-p', dest='proc', type='int', default=4, help='Number of processes [default: %default]')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 parser.add_option('-q', dest='quality_scale', type='int', default=-1, help='Quality value ascii scale, generally 64 or 33. If not specified, it will guess.')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 parser.add_option('--no_count', dest='no_count', action='store_true', default=False, help='Kmers are already counted and in expected file [reads file].qcts or [reads file].cts [default: %default]')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 parser.add_option('--no_cut', dest='no_cut', action='store_true', default=False, help='Coverage model is optimized and cutoff was printed to expected file cutoff.txt [default: %default]')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 parser.add_option('--int', dest='counted_kmers', action='store_true', default=False, help='Kmers were counted as integers w/o the use of quality values [default: %default]')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 parser.add_option('--ratio', dest='ratio', type='int', default=200, help='Likelihood ratio to set trusted/untrusted cutoff. Generally set between 10-1000 with lower numbers suggesting a lower threshold. [default: %default]')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 # help='Model kmer coverage as a function of GC content of kmers [default: %default]'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 parser.add_option('--gc', dest='model_gc', action='store_true', default=False, help=SUPPRESS_HELP)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 parser.add_option('--headers', action='store_true', default=False, help='Output original read headers (i.e. pass --headers to correct)' )
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 (options, args) = parser.parse_args()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if not options.readsf and not options.reads_listf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 parser.error('Must provide fastq file of reads with -r or file with list of fastq files of reads with -f')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 if not options.k:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 parser.error('Must provide k-mer size with -k')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 if options.quality_scale == -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 options.quality_scale = guess_quality_scale(options.readsf, options.reads_listf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 if options.counted_kmers:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 cts_suf = 'cts'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 cts_suf = 'qcts'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 if options.readsf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 ctsf = '%s.%s' % (os.path.splitext( os.path.split(options.readsf)[1] )[0], cts_suf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 reads_str = '-r %s' % options.readsf
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 ctsf = '%s.%s' % (os.path.split(options.reads_listf)[1], cts_suf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 reads_str = '-f %s' % options.reads_listf
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if not options.no_count and not options.no_cut:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 count_kmers(options.readsf, options.reads_listf, options.k, ctsf, options.quality_scale)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 if not options.no_cut:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 # model coverage
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 if options.counted_kmers:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 cov_model.model_cutoff(ctsf, options.ratio)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 if options.model_gc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 cov_model.model_q_gc_cutoffs(ctsf, 10000, options.ratio)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 cov_model.model_q_cutoff(ctsf, 25000, options.ratio)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if options.model_gc:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 # run correct C++ code
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 os.system('%s/correct %s -k %d -m %s -a cutoffs.gc.txt -p %d -q %d' % (quake_dir,reads_str, options.k, ctsf, options.proc, options.quality_scale))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 cutoff = open('cutoff.txt').readline().rstrip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 # run correct C++ code
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 headers = '--headers' if options.headers else ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 os.system('%s/correct %s %s -k %d -m %s -c %s -p %d -q %d' % (quake_dir,headers, reads_str, options.k, ctsf, cutoff, options.proc, options.quality_scale))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 ################################################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 # guess_quality_scale
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 # Guess at ascii scale of quality values by examining
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 # a bunch of reads and looking for quality values < 64,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 # in which case we set it to 33.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 ################################################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 def guess_quality_scale(readsf, reads_listf):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 reads_to_check = 1000
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 if not readsf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 readsf = open(reads_listf).readline().split()[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 fqf = open(readsf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 reads_checked = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 header = fqf.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 while header and reads_checked < reads_to_check:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 seq = fqf.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 mid = fqf.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 qual = fqf.readline().rstrip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 reads_checked += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 for q in qual:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 if ord(q) < 64:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 print 'Guessing quality values are on ascii 33 scale'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 return 33
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 header = fqf.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 print 'Guessing quality values are on ascii 64 scale'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 return 64
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 # count_kmers
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 # Count kmers in the reads file using AMOS count-kmers or
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 # count-qmers
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 def count_kmers(readsf, reads_listf, k, ctsf, quality_scale):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 # find files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 fq_files = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 if readsf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 fq_files.append(readsf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 for line in open(reads_listf):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 for fqf in line.split():
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 fq_files.append(fqf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 if ctsf[-4:] == 'qcts':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 os.system('cat %s | %s/count-qmers -k %d -q %d > %s' % (' '.join(fq_files), quake_dir, k, quality_scale, ctsf))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 os.system('cat %s | %s/count-kmers -k %d > %s' % (' '.join(fq_files), quake_dir, k, ctsf))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 # __main__
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 ############################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 if __name__ == '__main__':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 main()