forked from grenaud/freeIbis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTrainingSeqs_helper.py
executable file
·176 lines (140 loc) · 5.47 KB
/
createTrainingSeqs_helper.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
#!/usr/bin/env python
# -*- coding: ASCII -*-
"""
:Author: Martin Kircher
:Contact: [email protected]
:Date: *23.06.2009
"""
import sys,os
from optparse import OptionParser
from optparse import OptionGroup
import string
import createTrainingSeqs_reader as Reader
max_length_soap=60
max_frags_created = 1
table = string.maketrans('.','N')
# CALCULATE IDENTITY
def seq_identity(seq1,seq2):
length = min(30,len(seq1),len(seq2))
seq1 = seq1[:length]
seq2 = seq2[:length]
dist = length
for pos in range(length):
if (seq1[pos] != seq2[pos]): dist-=1
if length > 0:
return dist/float(length)
else:
return 1.0
def write_out_soap_fa(ofilestream,name,seq,adapter=None):
global max_length_soap
if adapter != None:
for pos in xrange(max(0,len(seq)-1)):
if seq_identity(seq[pos:],adapter) >= 0.9:
if ((len(seq)-pos) < len(adapter)):
seq = seq[:pos]
break
else:
seq = ""
break
frags=len(seq)//((len(seq)//max_length_soap)+1)
outseqs = []
while len(seq) > 0:
if len(seq) <= max_length_soap:
cseq = seq
seq = ""
else:
cseq = seq[:frags]
seq = seq[frags:]
outseqs.append(cseq)
for count,cseq in enumerate(outseqs):
ofilestream.write(">%s#%d/%d\n%s\n"%(name,count+1,len(outseqs),cseq))
#########################
### USER PARAMETER ...
#########################
parser = OptionParser(usage="usage: %prog [options]")
parser.add_option("--expID", dest="expID", help="Name of experiment to use for sequence names",default="SoapTraining")
parser.add_option("--infile", dest="infile", help="Input filename")
parser.add_option("--ftype", dest="ftype", help="Filetype")
parser.add_option("--start",dest="start",help="First base to include")
parser.add_option("--end",dest="end",help="Last base to include")
parser.add_option("--srange",dest="srange",help="SRANGE string")
parser.add_option("--start2",dest="start2",help="Second first base to include")
parser.add_option("--end2",dest="end2",help="Second last base to include")
parser.add_option("--srange2",dest="srange2",help="SRANGE2 string")
parser.add_option("--numberN", dest="numberN", help="Maximum number of missing bases to be accepted",type="int")
parser.add_option("--adapter", dest="adapter", help="Adapter sequence",default='')
parser.add_option("--control_index", dest="control_index", help="Sequence of index identifying control reads used for training/test data set (default '' = no filter)",default="")
parser.add_option("--index_seq1", dest="index_seq1", help="Index read is stored attached to read1 input file",default=False,action="store_true")
parser.add_option("--index_seq2", dest="index_seq2", help="Index read is stored in a separate input file",default=False,action="store_true")
parser.add_option("--outfile", dest="outfile", help="Path for output file")
parser.add_option("--outfile2", dest="outfile2", help="Path for second output file")
(options, args) = parser.parse_args()
if (options.infile == None) or (options.ftype == None) or (options.start == None) or (options.end == None) or (options.numberN == None) or (options.adapter == None) or (options.outfile == None) or (options.srange == None):
print "Not enough parameters."
sys.exit()
options.adapter = options.adapter.upper()
if (len(options.adapter) == 0) or (options.adapter == 'NONE'): options.adapter = None
start = int(options.start)
start2 = None
end = int(options.end)
end2 = None
second_in_first = False
if (options.start2 != None) and (options.end2 != None) and (options.srange2 != None) and (options.outfile2 != None):
second_in_first = True
start2 = int(options.start2)
end2 = int(options.end2)
outfile = open(options.outfile,'w')
if second_in_first:
outfile_r2 = open(options.outfile2,'w')
else: # SHOULD NEVER BE NECCESSARY
outfile_r2 = outfile
file_reader = None
if options.ftype == "qseq":
file_reader = Reader.read_qseq_file
elif options.ftype == "bcl":
file_reader = Reader.read_bcl_tile
elif options.ftype == "fastq":
file_reader = Reader.read_fastq_file
elif options.ftype == "seq_prb":
file_reader = Reader.read_seq_prb_file
else:
print "Unexpected file type list."
sys.exit()
ifile = None
ilength = len(options.control_index)
indexFilter = False
options.control_index = options.control_index.strip().upper()
if (len(options.control_index) > 1):
indexFilter = True
if (options.index_seq2):
ifile = file_reader("_".join(options.infile.split("_")[:-3]+['2']+options.infile.split("_")[-2:]))
elif (options.index_seq1):
ifile = file_reader("_".join(options.infile.split("_")[:-3]+['1']+options.infile.split("_")[-2:]))
#print ifile
cfailed = 0
ckept = 0
for name,seqorg,qualorg in file_reader(options.infile):
if indexFilter:
index= None
if ifile != None:
iname,iseqorg,iqualorg = ifile.next()
index = iseqorg[-ilength:].upper()
else:
index=seqorg[end:end+ilength].upper()
if index != options.control_index:
cfailed += 1
continue
else:
ckept += 1
seq=seqorg[start:end]
name = ":".join(name)
if (seq.count("N") <= options.numberN):
write_out_soap_fa(outfile,options.expID+":"+name+":"+options.srange,seq,options.adapter)
if second_in_first:
seq = seqorg[start2:end2]
if (seq.count("N") <= options.numberN):
write_out_soap_fa(outfile_r2,options.expID+":"+name+":"+options.srange2,seq,options.adapter)
outfile.close()
if second_in_first: outfile_r2.close()
if cfailed > 0:
print "Excluded %d (kept %d) sequences because index did not match."%(cfailed,ckept)