-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_bkg_seqs.py
executable file
·372 lines (330 loc) · 11.6 KB
/
create_bkg_seqs.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
"""
Create background sequences from input FASTA files.
"""
__author__ = "Akshay Paropkari"
__version__ = "0.3.4"
import argparse
from os import mkdir
from os.path import abspath, exists, isfile, join
from random import choice, randint, random
from sys import exit
from time import strftime
from utils import (
dna_iupac_codes,
gc_len_matched_bkg_seq_gen,
get_transmat,
parse_fasta,
random_dna,
)
try:
from rpy2.robjects.packages import importr
except ImportError:
exit("\nPlease install rpy2 package.\n")
else:
try:
dnashaper = importr("DNAshapeR")
except Exception:
exit("\nPlease install bioconductor-dnashaper package.\n")
try:
import pandas as pd
except ImportError:
exit("\nPlease install pandas package\n")
def handle_program_options():
parser = argparse.ArgumentParser(
description="Using foreground sequences, generate background sequences. "
"Background sequences will be generated firstly by matching foreground motif "
"GC-percent and length. Secondly, the foregound sequences will be shuffled to "
"keep dinucleotide composition constant.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"fg_fasta_file",
metavar="/path/to/true_binding_seqeuences.fasta",
type=str,
help="Path to foreground/true positive sequence dataset FASTA "
"format file [REQUIRED]",
)
parser.add_argument(
"protein_name",
type=str,
choices=["bcr1", "brg1", "efg1", "ndt80", "rob1", "tec1"],
help="Specify the name of transcription factor. Please see the "
"list of valid choices for this parameter [REQUIRED]",
)
parser.add_argument(
"genome_fasta_files",
type=str,
nargs="+",
metavar="/path/to/other_organism_CDS_exonic_sequences.fasta",
help="Specify path to one or more genome files to use as template"
" from which to generate random background sequences. These genome"
"file(s) must be FASTA file with very low probability of "
"containing sequences with binding motifs. For example, these "
"files can be FASTA file of exonic regions of non-related "
"species. Please do not supply gzipped files. [REQUIRED]",
)
parser.add_argument(
"output_dir",
type=str,
metavar="/path/to/descriptive_output_folder_name",
help="Specify a directory to save background sequence data. [REQUIRED]",
)
parser.add_argument(
"-m",
"--mononucleotide_shuffle",
action="store_true",
help="Supply this parameter to generate mononucleotide shuffled background "
"sequences. Mononucleotide shuffled sequences will not be generated by default."
"Write `-m` to turn on this parameter.",
)
parser.add_argument(
"-t",
"--tolerance",
type=int,
default=1,
help="Percent tolerance allowed for matching GC content of "
"background sequence with foreground sequence. The default value "
"is one percent difference between background and foreground "
"sequence. A value of zero will increase eexecution time for this"
" script.",
)
return parser.parse_args()
##########################################################################################
# altschulEriksonDinuclShuffle.py
# P. Clote, Oct 2003
def computeCountAndLists(s):
# WARNING: Use of function count(s,"UU") returns 1 on word UUU since it apparently
# counts only nonoverlapping words UU. For this reason, we work with the indices.
# Initialize lists and mono- and dinucleotide dictionaries
nuclList = ["A", "C", "G", "T"]
List = {nt: [] for nt in nuclList} # List is a dictionary of lists
s = s.upper()
nuclCnt = {} # empty dictionary
dinuclCnt = {} # empty dictionary
for x in nuclList:
nuclCnt[x] = 0
dinuclCnt[x] = {y: 0 for y in nuclList}
# Compute count and lists
nuclCnt[s[0]] = 1
nuclTotal = 1
dinuclTotal = 0
for i in range(len(s) - 1):
x = s[i]
y = s[i + 1]
List[x].append(y)
nuclCnt[y] += 1
nuclTotal += 1
dinuclCnt[x][y] += 1
dinuclTotal += 1
assert nuclTotal == len(s)
assert dinuclTotal == len(s) - 1
return nuclCnt, dinuclCnt, List
def chooseEdge(x, dinuclCnt):
numInList = 0
for y in ["A", "C", "G", "T"]:
numInList += dinuclCnt[x][y]
z = random()
denom = (
dinuclCnt[x]["A"] + dinuclCnt[x]["C"] + dinuclCnt[x]["G"] + dinuclCnt[x]["T"]
)
numerator = dinuclCnt[x]["A"]
if z < float(numerator) / float(denom):
dinuclCnt[x]["A"] -= 1
return "A"
numerator += dinuclCnt[x]["C"]
if z < float(numerator) / float(denom):
dinuclCnt[x]["C"] -= 1
return "C"
numerator += dinuclCnt[x]["G"]
if z < float(numerator) / float(denom):
dinuclCnt[x]["G"] -= 1
return "G"
dinuclCnt[x]["T"] -= 1
return "T"
def connectedToLast(edgeList, nuclList, lastCh):
D = {x: 0 for x in nuclList}
for edge in edgeList:
a = edge[0]
b = edge[1]
if b == lastCh:
D[a] = 1
for i in range(2):
for edge in edgeList:
a = edge[0]
b = edge[1]
if D[b] == 1:
D[a] = 1
# ok = 0
for x in nuclList:
if x != lastCh and D[x] == 0:
return 0
return 1
def eulerian(s):
nuclCnt, dinuclCnt, List = computeCountAndLists(s)
# compute nucleotides appearing in s
nuclList = []
for x in ["A", "C", "G", "T"]:
if x in s:
nuclList.append(x)
# compute numInList[x] = number of dinucleotides beginning with x
numInList = {}
for x in nuclList:
numInList[x] = 0
for y in nuclList:
numInList[x] += dinuclCnt[x][y]
# create dinucleotide shuffle L
# firstCh = s[0] # start with first letter of s
lastCh = s[-1]
edgeList = []
for x in nuclList:
if x != lastCh:
edgeList.append([x, chooseEdge(x, dinuclCnt)])
ok = connectedToLast(edgeList, nuclList, lastCh)
return ok, edgeList, nuclList, lastCh
def shuffleEdgeList(L):
n = len(L)
barrier = n
for i in range(n - 1):
z = int(random() * barrier)
tmp = L[z]
L[z] = L[barrier - 1]
L[barrier - 1] = tmp
barrier -= 1
return L
def dinuclShuffle(s):
s = s if len(dna_iupac_codes(s)) == 1 else choice(dna_iupac_codes(s))
ok = 0
while not ok:
ok, edgeList, nuclList, lastCh = eulerian(s)
nuclCnt, dinuclCnt, List = computeCountAndLists(s)
# remove last edges from each vertex list, shuffle, then add back
# the removed edges at end of vertex lists.
for [x, y] in edgeList:
List[x].remove(y)
for x in nuclList:
shuffleEdgeList(List[x])
for [x, y] in edgeList:
List[x].append(y)
# construct the eulerian path
L = [s[0]]
prevCh = s[0]
for i in range(len(s) - 2):
ch = List[prevCh][0]
L.append(ch)
del List[prevCh][0]
prevCh = ch
L.append(s[-1])
t = "".join(L)
return random_dna(2, False) + t + random_dna(2, False)
##########################################################################################
def mono_nt_shuffle(seqA: str) -> str:
"""
Given a input sequence seqA, return its Durstenfeld shuffled version
"""
shuffled_seqA = []
for nt in seqA:
shuffled_seqA_len = len(shuffled_seqA)
j = randint(0, shuffled_seqA_len)
if j == shuffled_seqA_len:
shuffled_seqA.append(nt)
else:
shuffled_seqA.append(shuffled_seqA[j])
shuffled_seqA[j] = nt
return random_dna(2, False) + "".join(shuffled_seqA) + random_dna(2, False)
def main():
print("#" * 90, "\n\n", strftime("%x %X | START BACKGROUND SEQUENCES GENERATION\n"))
args = handle_program_options()
try:
assert isfile(args.fg_fasta_file)
except AssertionError as e:
print(
"Error with input foreground FASTA file(s). Please check supplied FASTA "
"file - {0}".format(e)
)
exit()
else:
# parse foreground sequence FASTA file
print(
strftime(
f"%x %X | Parsing foreground sequence FASTA file {args.fg_fasta_file}"
)
)
fg_seqs_df = (
pd.DataFrame.from_dict(
{header: seq for header, seq in parse_fasta(args.fg_fasta_file)},
orient="index",
columns=["sequence"],
)
.reset_index()
.rename(columns={"index": "location"})
)
try:
outdir = abspath(args.output_dir)
assert exists(outdir)
except AssertionError:
# output directory doesn't exist, create it
mkdir(outdir)
else:
# get output file path and name
outfnh = join(outdir, f"{args.protein_name}_bkg_seqs.fasta")
if args.mononucleotide_shuffle:
################################################
# MONONUCLEOTIDE SHUFFLED FOREGROUND SEQUENCES #
# Durstenfeld shuffle #
################################################
print(
strftime("%x %X | Generating mononucleotide shuffled background sequences")
)
fg_seqs_df["mononuc_shuffled_bkg_seq"] = fg_seqs_df["sequence"].apply(
mono_nt_shuffle
)
##############################################
# DINUCLEOTIDE SHUFFLED FOREGROUND SEQUENCES #
##############################################
print(strftime("%x %X | Generating dinucleotide shuffled background sequences"))
fg_seqs_df["dinuc_shuffled_bkg_seq"] = fg_seqs_df["sequence"].apply(dinuclShuffle)
################################################################
# GC CONTENT AND LENGTH MATCHED BACKGROUND SEQUENCE GENERATION #
################################################################
# parse unrelated genome FASTA files containing CDS sequences
print(
strftime("%x %X | Calculating transition probability for non-Candida genomes")
)
degree = 2
cds_transmat = {
f.split("/")[-1].split("_")[0]: get_transmat(f, degree)
for f in args.genome_fasta_files
}
print(
strftime(
"%x %X | Generating GC content and length matched background sequences"
)
)
fg_seqs_df["gc_len_matched_bkg"] = fg_seqs_df["sequence"].apply(
gc_len_matched_bkg_seq_gen, args=(cds_transmat, list(fg_seqs_df["sequence"]),)
)
print(strftime(f"%x %X | Writing background sequences to {outfnh}"))
fg_seqs_df = fg_seqs_df.sample(frac=1.0, random_state=39)
with open(outfnh, "w") as outf:
for bkg, data in (
fg_seqs_df.set_index("location", verify_integrity=True)
.drop(columns=["sequence"])
.to_dict()
.items()
):
for location, seq in data.items():
outf.write(f">{bkg + '_for_' + location}\n{seq}\n")
######################################################
# CALCULATE DNA SHAPE VALUE FOR BACKGROUND SEQUENCES #
######################################################
print(
strftime(
f"%x %X | Writing background sequences shape data to {abspath(args.output_dir)}"
)
)
for shape in ["MGW", "Roll", "HelT", "ProT", "EP"]:
dnashaper.getDNAShape(outfnh, shape)
print(strftime("\n%x %X | END BACKGROUND SEQUENCES GENERATION\n"))
if __name__ == "__main__":
exit(main())