-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtf_groundtruth.py
478 lines (334 loc) · 14.8 KB
/
gtf_groundtruth.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
from basics import *
from gtf_modifiers import *
from alias_work import *
import glob
# Description:
# Create a ground truth database that
# represents how different transcripts
# Will map to each other with varying
# Mismatch levels
reference_genome = "hg38_std.fa"
# information dictionary should be in the format
# note zero index for column
# {info:[gen=0 or attribute=1, column index or name if attribute]}
pd.set_option("display.max.colwidth", 10000000)
def generate_groundtruth(gtf, ref, new_dir, primary_key, information_dict, hisat = False, num_mismatch = 2, input_fasta = "sequences.fasta ", p = 1):
seq_list = add_sequence_fast(gtf, ref)
os.system("mkdir " + new_dir)
with open(gtf, "r") as gtf:
i = 1
# Generate the PRIMARY table
# Minimum Info: Primary Key: Sequence
primary_table = []
primary_table_headers = [primary_key, "sequence", "length"]
for key in information_dict:
primary_table_headers.append(key)
for line in gtf:
table_row = []
# split the line
line_data = separate_gtf_line(line)
general_data = line_data[0]
attribute_data = line_data[1]
# extract the sequence
sequence = seq_list[i]
# get the index for the primary key
primary_key_index = attribute_data.index(primary_key)
primary_key_value = attribute_data[primary_key_index + 1]
primary_key_value = primary_key_value.replace('"', '')
# add to row
table_row.append(primary_key_value)
# add the sequence
table_row.append(sequence)
# add the length
table_row.append(len(sequence))
# now we can add extra data
for key in information_dict:
# go through the general data
if information_dict[key][0] == 0:
value = general_data[information_dict[key][1]]
table_row.append(value)
# go through the attribute data
elif information_dict[key][0] == 1:
value_index = attribute_data.index(information_dict[key][1])
value = attribute_data[value_index + 1]
value = value.replace('"', "")
table_row.append(value)
else:
raise ValueError("There is an issue with your information dictionary")
# now we add this line to the primary table
primary_table.append(table_row)
i += 2
primary_df = pd.DataFrame(primary_table, columns=primary_table_headers)
# filter out too short transcripts
df_tooshort = primary_df.loc[primary_df["length"] <= 10]
primary_df = primary_df.loc[primary_df["length"] > 10]
# filter out too long transcripts
df_toolong = primary_df.loc[primary_df["length"] >= 1000]
primary_df = primary_df.loc[primary_df["length"] < 1000]
# make capital
primary_df["sequence"] = primary_df["sequence"].str.upper()
# filter out transcripts with N in sequence
df_n = primary_df.loc[primary_df["sequence"].str.contains("N")]
primary_df = primary_df.loc[~(primary_df["sequence"].str.contains("N"))]
# remove duplicates
primary_df = eliminate_duplicates(primary_df, information_dict, new_dir)
primary_df.to_csv(new_dir + "/" + "primary.csv", index = False)
df_tooshort.to_csv(new_dir + "/tooshort.csv", index = False)
df_toolong.to_csv(new_dir + "/toolong.csv", index = False)
df_n.to_csv(new_dir + "/n_exist.csv", index = False)
# Begin the fasta creation
fasta_entry = ">" + primary_df["transcript_id"] + "SPACER" + primary_df["sequence"]
with open(new_dir + "/sequences.fasta", "w") as f:
stringed = fasta_entry.to_string(header = False, index = False)
stringed = stringed.replace(" ", "")
stringed = stringed.replace("SPACER", "\n")
f.write(stringed)
# Begin the alignment section
# Check if index is built
built = index_exist(new_dir)
# 1) Check if HISAT2 is installed
if hisat == True:
try:
os.system("hisat2")
except:
raise ModuleNotFoundError("HISAT2 is not installed.")
# 2) Check if indexed
# Build the index if it doesn't exist
# Give the user the choice to rebuild if needed
if built == True:
print("Index has previously been built")
print("Do you want to rebuild the index?")
rebuild = input("y/n")
if rebuild == "y":
generate_index_hisat(new_dir)
else:
print("Continuing")
else:
generate_index_hisat(new_dir)
# 3) Begin the alignment
hisat_align(new_dir, num_mismatch, input_fasta)
# Use bowtie by default => better results
else:
try:
os.system("bowtie2")
except:
raise ModuleNotFoundError("bowtie2 is not installed.")
if built == True:
print("Index has previously been built")
print("Do you want to rebuild the index?")
rebuild = input("y/n")
if rebuild == "y":
generate_index_bowtie2(new_dir)
else:
print("Continuing")
else:
generate_index_bowtie2(new_dir)
# Align
bowtie_align(new_dir, num_mismatch, input_fasta)
# Parse the SAM file to create the mapping
parse_sam(new_dir, num_mismatch)
# Create the table
create_table(new_dir, num_mismatch)
# Create the primary table
def create_primary_table(gtf, ref, primary_key, information_dict, new_dir):
seq_list = add_sequence_fast(gtf, ref)
os.system("mkdir " + new_dir)
with open(gtf, "r") as gtf:
i = 1
# Generate the PRIMARY table
# Minimum Info: Primary Key: Sequence
primary_table = []
primary_table_headers = [primary_key, "sequence", "length"]
for key in information_dict:
primary_table_headers.append(key)
for line in gtf:
table_row = []
# split the line
line_data = separate_gtf_line(line)
general_data = line_data[0]
attribute_data = line_data[1]
# extract the sequence
sequence = seq_list[i]
# get the index for the primary key
primary_key_index = attribute_data.index(primary_key)
primary_key_value = attribute_data[primary_key_index + 1]
primary_key_value = primary_key_value.replace('"', '')
# add to row
table_row.append(primary_key_value)
# add the sequence
table_row.append(sequence)
# add the length
table_row.append(len(sequence))
# now we can add extra data
for key in information_dict:
# go through the general data
if information_dict[key][0] == 0:
value = general_data[information_dict[key][1]]
table_row.append(value)
# go through the attribute data
elif information_dict[key][0] == 1:
value_index = attribute_data.index(information_dict[key][1])
value = attribute_data[value_index + 1]
value = value.replace('"', "")
table_row.append(value)
else:
raise ValueError("There is an issue with your information dictionary")
# now we add this line to the primary table
primary_table.append(table_row)
i += 2
primary_df = pd.DataFrame(primary_table, columns=primary_table_headers)
return primary_df
# Generate an index
def generate_index_hisat(new_dir):
os.system("hisat2-build " + new_dir + "/" + "sequences.fasta" + " " + new_dir + "/" + "seqs")
def generate_index_bowtie2(new_dir):
os.system("bowtie2-build " + new_dir + "/" + "sequences.fasta" + " " + new_dir + "/" + "seqs")
def generate_index_bowtie(fasta, outname):
os.system("bowtie-build " + fasta + " " + outname)
# Check if index exists
def index_exist(new_dir):
files = glob.glob(new_dir + "/" + "seqs.*")
num_index = len(files)
if num_index >= 6:
return True
else:
return False
# Align
def hisat_align(new_dir, num_mismatch = 2, input_fasta = "sequences.fasta ", p = 1):
os.system("hisat2 --rfg 50000,50000 --rdg 50000,50000 --np 50000 --no-softclip --mp 20,20 -k 9223372036854775807 --max-seeds 9223372036854775807 --rna-strandness F --no-spliced-alignment --secondary -p " + str(p) + " --score-min L," +
str(num_mismatch*-20) + ",0 -x " + new_dir + "/seqs -f " + input_fasta +
" -S " + new_dir + "/sequences.sam")
index = "/seqs"
def bowtie_align(new_dir, num_mismatch = 2, input_fasta = "sequences.fasta ", p = 1, index = index):
if index == "/seqs":
index2 = new_dir + index
else:
index2 = index
os.system("bowtie2 --ma 0 --rfg 50000,50000 --rdg 50000,50000 --np 50000 -L 10 -R 20 --mp 20,20 --norc -p " + str(p) + " --score-min L," +
str(num_mismatch*-20) + ",0 -a -x " + index2 + " -U " + input_fasta + " -f " +
"-S " + new_dir + "/sequences.sam")
def bowtie_align_pipeline(index_name, working_dir, fasta):
os.system('cd ' + working_dir + '; \
bowtie -f -x ' + index_name + ' ' + fasta +' -k 101 --best --strata -v 0 -S lookup_filtered.sam --reorder --norc')
# Parse SAM File
def parse_sam(new_dir, num_mismatch = 2):
for i in range(0,num_mismatch + 1):
os.system("fgrep 'AS:i:" + str(i * -20) + "' " + new_dir + "/sequences.sam > " + new_dir + "/seq_" + str(i) + "_mm.sam")
# Create Table
def create_table(new_dir, num_mismatch = 2):
for i in range(0, num_mismatch + 1):
# import dataset
sam_df = pd.read_csv(new_dir + "/seq_" + str(i) + "_mm.sam", sep = "\t", header = None, usecols=[0,1])
sam_df.to_csv(new_dir + "/mapping_" + str(i) + "_mm.sam")
# Generate aliases and remove duplicates
def eliminate_duplicates(df, information_dict, new_dir):
# replace within the first function with the primary df
dataset = df
dataset["sequence"] = dataset["sequence"].str.upper()
# Create the alias table
## Store sequence info
sequence_tid = my_dictionary()
alias_table = []
# Honestly such a bad habit to iterate over rows...
for row in dataset.iterrows():
new_row = []
# Add if it doesn't exist
if row[1][1] not in sequence_tid:
# Add seq to dict
sequence_tid.add(row[1][1], "")
# Add TID to dict
sequence_tid[row[1][1]] = row[1][0]
# If it doesn't exist, we map to self
new_row.append(row[1][0])
new_row.append(row[1][0])
# Add in information regarding the other transcript
i = 3
for key in information_dict:
new_row.append(row[1][i])
i += 1
# If already there, create a new row with existing tid
else:
# add the primary key to the main table
new_row.append(sequence_tid[row[1][1]])
new_row.append(row[1][0])
# Add in information about the other transcript
i = 3
for key in information_dict:
new_row.append(row[1][i])
i += 1
alias_table.append(new_row)
# Assemble the headers
header = ["primary_transcript_id", "alias_transcript_id"] + list(information_dict.keys())
# Export the alias table
alias_as = pd.DataFrame(alias_table, columns=header)
alias_as.to_csv(new_dir + "/alias.csv",index = False)
return df.drop_duplicates(subset = ["sequence"], keep = "first")
# Compare DF to TSV
def eliminate_duplicates_tsv(df, tsv, new_dir, tsv_source, df_source):
# replace within the first function with the primary df
dataset = df
dataset2 = pd.read_csv(tsv, sep = "\t")
dataset["sequence"] = dataset["sequence"].str.upper()
# Create the alias table
## Store sequence info
sequence_tid = my_dictionary()
alias_table = []
# Honestly such a bad habit to iterate over rows...
for row in dataset.iterrows():
new_row = []
# Add if it doesn't exist
if row[1][1] not in sequence_tid:
# Add seq to dict
sequence_tid.add(row[1][1], "")
# Add TID to dict
sequence_tid[row[1][1]] = row[1][0]
# If it doesn't exist, we map to self
new_row.append(row[1][0])
new_row.append(row[1][0])
# Add in information regarding the other transcript
new_row.append(df_source)
# If already there, create a new row with existing tid
else:
# add the primary key to the main table
new_row.append(sequence_tid[row[1][1]])
new_row.append(row[1][0])
# Add in information about the other transcript
new_row.append(df_source)
alias_table.append(new_row)
for row in dataset2.iterrows():
new_row = []
# Add if it doesn't exist
if row[1][1] not in sequence_tid:
# Add seq to dict
sequence_tid.add(row[1][1], "")
# Add TID to dict
sequence_tid[row[1][1]] = row[1][0]
# If it doesn't exist, we map to self
new_row.append(row[1][0])
new_row.append(row[1][0])
# Add in information regarding the other transcript
new_row.append(tsv_source)
# If already there, create a new row with existing tid
else:
# add the primary key to the main table
new_row.append(sequence_tid[row[1][1]])
new_row.append(row[1][0])
# Add in information about the other transcript
new_row.append(tsv_source)
alias_table.append(new_row)
# Assemble the headers
header = ["primary_transcript_id", "alias_transcript_id", "source"]
# Export the alias table
alias_as = pd.DataFrame(alias_table, columns=header)
alias_as.to_csv(new_dir + "/alias.csv",index = False)
return df.drop_duplicates(subset = ["sequence"], keep = "first")
def generate_fasta(primary_df, new_dir):
os.system("mkdir " + new_dir)
fasta_entry = ">" + primary_df["transcript_id"] + "SPACER" + primary_df["sequence"]
with open(new_dir + "/sequences.fasta", "w") as f:
stringed = fasta_entry.to_string(header = False, index = False)
stringed = stringed.replace(" ", "")
stringed = stringed.replace("SPACER", "\n")
f.write(stringed)
if __name__ == '__main__':
fire.Fire()