-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreads_generation.py
209 lines (177 loc) · 6.17 KB
/
reads_generation.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
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 1 09:44:03 2019
@author: Guilherme Neumann
"""
from Bio import SeqIO
import logging
import importlib
import threading as thr
import multiprocessing
import time
import os
from abc import ABC, abstractmethod
class Reads_generation_Controller():
"""
This Class generates artifitial sequencing reads through third-party
algorithms. Not only one can generate reads for a unique assemblies,
but also multiple read datasets, for multiple assemblies, varying
parameters. For that, just provide a list o values for the selected
parameter.
Methods
-------
fasta2fastq(file,ql,name='')
Converts a fasta file to a fastq file
generate(alg,parameters)
The method where the reads are generated
"""
def __init__(self, exp, out ):
"""
Parameters
----------
exp : str
The Experiment Name
out : str
The output directory to store the results and where the reads
are stored
"""
self.exp=exp
self.output=out
logging.basicConfig(format='%(asctime)s %(message)s',filename= out+ exp + '.log',level=logging.DEBUG)
def generate_reads(self,alg,parameters):
"""
Here the reads are generated.
Parameters
----------
alg : str
The selected algorithm. Please be sure the alg.py is provided on
read_generators/.
parameters : dict
A dictionary containing all the generation parameters. Be sure of
providing the same syntax for the keys from the selected algorithm.
Returns
-------
reads object
an object instance of the algorithm. It is worth for getting
parameters and sample names later.
"""
if not (os.path.exists(self.output+"reads")):
os.system("mkdir "+self.output+"reads")
module = importlib.import_module('read_generators.'+alg.lower())
my_class = getattr(module, alg.capitalize() )
reads = my_class(self.exp ,self.output)
t=multiprocessing.cpu_count()-1
samples=[]
for par,value in parameters.items():
if type(value) !=list:
reads.parameters[par]=value
else:
samples=value
var=par
threads=[]
exists = os.path.isfile(self.output+"reads/run_generator.log")
done=[]
if exists:
last_run=open(self.output+"reads/run_generator.log","r")
done=last_run.read().split("\n")
run_log=open(self.output+"reads/run_generator.log","a")
if samples!=[]:
reads.t=t//len(samples)
if reads.t<0:
reads.t=1
for sample in samples:
if '.' in str(sample):
sample_name=str(sample).split('.')
sample_name=sample_name[0]+"-"+sample_name[1]
reads.parameters[var]=sample
sample=var+"_"+ sample_name
if sample not in done:
threads.append(thr.Thread(target=reads.generate,args=(sample,)))
threads[-1].daemon=True
threads[-1].setName(sample)
threads[-1].start()
else:
reads.datasets_generated.append(sample)
elif self.exp not in done:
reads.t=t
threads.append(thr.Thread(target=reads.generate,args=(self.exp,)))
threads[-1].daemon=True
threads[-1].setName(self.exp)
threads[-1].start()
for thread in threads:
while(thread.is_alive()):
time.sleep(.10)
run_log.write(thread.getName()+"\n")
run_log.close()
print(" Reads generated")
logging.info("Reads generated")
return reads
class Generator(ABC):
"""
Abstract class responsable for generating reads
Attributes
----------
exp : str
The Experiment Name
out : str
The output directory to store the results and where the reads
are stored
parameters : dict
A dictionary containing all the generation parameters
datasets_generated : list
The list of the samples generated
Methods
-------
fasta2fastq(file,ql,name='')
Converts a fasta file to a fastq file
command(sample)
Run the command to the sample
"""
t=multiprocessing.cpu_count()-2
parameters=dict()
datasets_generated = []
def __init__(self,exp,out):
"""
Parameters
----------
exp : str
The Experiment Name
out : str
The output directory to store the results and where the reads
are stored
"""
self.exp=exp
self.out=out
super(Generator,self).__init__()
logging.basicConfig(format='%(asctime)s %(message)s',filename= out+ exp + '.log',level=logging.DEBUG)
def fasta2fastq(self,file,ql,name=''):
"""
It converts a fasta file to a fastq file.
Parameters
----------
file : str
file name, including format, e.g. 'reads_1.fa'
ql : int
phred number
"""
try:
if name!='':
fq=name+".fastq"
else:
fq=file.split(".")
fq=fq[0] + ".fastq"
with open(file, "r") as fasta, open(fq, "w") as fastq:
for record in SeqIO.parse(fasta, "fasta"):
record.letter_annotations["phred_quality"] = [ql] * len(record)
SeqIO.write(record, fastq, "fastq")
fasta.close()
fastq.close()
except IOError:
logging.error(IOError)
exit()
@abstractmethod
def generate(self,sample):
"""
Run the command.
"""
pass