-
Notifications
You must be signed in to change notification settings - Fork 47
/
extractGuides.py
235 lines (156 loc) · 6.4 KB
/
extractGuides.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/python
'''
The MIT License (MIT)
Copyright (c) 2013 Charles Lin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
#pythonTemplate.py <- change to title of your script
#130801 <- date
#Name
#Description:
'''
Extracts putative guide RNAs out of a fastq for processing in the GECKO pipeline
'''
#================================================================================
#=============================DEPENDENCIES=======================================
#================================================================================
import sys
print "Using python version %s" % sys.version
#importing utils package
sys.path.append('/ark/home/cl512/src/pipeline/')
import utils
import string
import os
from collections import defaultdict
#================================================================================
#============================GLOBAL PARAMETERS===================================
#================================================================================
#GUIDE EXTRACTION PARAMETERS
cutSeq = 'CACCG'
cutOffset = 5
guideLength = 20
#================================================================================
#===================================CLASSES======================================
#================================================================================
#user defined classes here
#================================================================================
#=================================FUNCTIONS======================================
#================================================================================
def extractGuideFastq(fastqFile,outputFolder='',gzip=False):
'''
takes a fastq and extracts candidate guide RNAs
'''
#get the full absolute path for the fastq File
fastqFile = os.path.abspath(fastqFile)
fastq = utils.open(fastqFile,'r')
#get the fastq name and root
if len(outputFolder) == 0:
outputFolder = utils.getParentFolder(fastqFile)
#makes sure the output folder exists
utils.formatFolder(outputFolder,True)
#grab the name info from the fastq
fastqName = fastqFile.split('/')[-1]
fastqRoot = string.replace(fastqName,'.fastq','')
fastqRoot = string.replace(fastqRoot,'.gz','')
#guideFastqFile output
guideFastqFile = '%s%s.gecko.fastq' % (outputFolder,fastqRoot)
guideFastq = utils.open(guideFastqFile,'w')
print('processing %s' % (fastqName))
print('million reads processed:')
ticker = 0
found = 0
while True:
if ticker%1000000 == 0:
print(ticker/1000000)
fastqLines = []
#now load the fastq lines
try:
for i in range(4):
fastqLines.append(fastq.next())
except StopIteration:
break
#see if you can find a cut site
seq = fastqLines[1].rstrip()
try:
cutPosition = seq.index(cutSeq)
found+=1
except ValueError:
ticker+=1
continue
guideStart = cutPosition + cutOffset
guideStop = guideStart + guideLength
#pulling out the guide seq in the fastqLines
fastqLines[1] = fastqLines[1][guideStart:guideStop] + '\n'
fastqLines[3] = fastqLines[3][guideStart:guideStop] + '\n'
for line in fastqLines:
guideFastq.write(line)
ticker+=1
# if ticker == 100000:
# print(ticker)
# print(found)
# print(float(found)/float(ticker))
# break
print('SUMMARY STATISTICS')
print(ticker)
print(found)
print(float(found)/float(ticker))
#close the fastq
guideFastq.close()
#gzip the fastq
if gzip:
os.system('gzip %s &' % (guideFastqFile))
guideFastqFile += '.gz'
return guideFastqFile
#================================================================================
#===============================MAIN RUN=========================================
#================================================================================
#write the actual script here
def main():
'''
main run function
'''
from optparse import OptionParser
usage = "usage: %prog [options] -f [FASTQFILE]"
parser = OptionParser(usage = usage)
#required flags
parser.add_option("-f","--fastq", dest="fastq",nargs = 1, default=None,
help = "Enter a comma separated list of fastq files to be extracted")
#optional arguments
parser.add_option("-o","--output",dest="output",nargs =1, default = 0,
help = "specify an output folder. default is the same directory as the input fastq")
parser.add_option("-z","--gzip",dest="gzip",action = 'store_true', default = False,
help = "If flagged gzips the guide extracted fastq. Default is False")
(options,args) = parser.parse_args()
if not options.fastq:
parser.print_help()
exit()
fastqFileList = options.fastq.split(',')
if options.output:
outputFolder = options.output
else:
outputFolder = ''
if options.gzip:
gzip = True
else:
gzip = False
for fastqFile in fastqFileList:
print("EXTRACTING GUIDES FROM %s" % (fastqFile))
guideFastqFile = extractGuideFastq(fastqFile,outputFolder,gzip)
print("GUIDES EXTRACTED TO %s" % (guideFastqFile))
sys.stdout.write(guideFastqFile)
if __name__ == "__main__":
main()