-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDO_recomptes_reads.py
283 lines (195 loc) · 8.19 KB
/
DO_recomptes_reads.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
#!/usr/bin/python
# Writen by Gabriel Cabot, Institut d'Investigació Sanitària Illes Balears (IdISBa)
# email: [email protected]
# Antibiotic Resistance and Pathogenicity of Bacterial Infections Group (https://arpbigidisba.com/)
# GitHub (https://github.com/GCabot)
import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pylab as plt
import os
import time
from timeit import default_timer as timer
from os import system
import csv
import math
import seaborn as sns
plt.style.use("seaborn")
###############################################################################
# 1. DEFINING VARIABLES
###############################################################################
numerical="0123456789"
alphabetical="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
alphanumerical=alphabetical+numerical
indelidentifier="+-"
equalasref=",."
trashyones="^$!"
anychar=alphabetical+numerical+indelidentifier+equalasref+trashyones
bases="AaTtCcGg"
###############################################################################
# 2. OPENING DATA_FILE
###############################################################################
system("clear")
print ("Writen by Gabriel Cabot, Institut d'Investigació Sanitària Illes Balears (IdISBa)")
print ()
print ("\x1B[3m" + "This script analyze the relative frequency of each event for a given totalpileup file" + "\x1B[0m")
print ("\x1B[3m" + "Please, use it UNDER YOUR OWN responsability" + "\x1B[0m")
print("{:>60}".format("G. Cabot"))
time.sleep(5)
system("clear")
print ()
datafile=input("Select a totalpileup file to be analysed: ")
outfolder=input("Select an output folder to store analysis: ")
outfile=(outfolder + "my_processed_table.csv")
print (f"File to analyse is: {datafile}")
print ()
print (f"Folder to store analysis is: {outfolder}")
confirmation=input("Is this correct?")
if confirmation == 'yes' :
pass
if confirmation == 'no':
quit()
# Opening & parsing datafile:
dataset = pd.read_csv(datafile, sep='\t', header=None)
dataset.columns=("NODE", "POSITION", "REFERENCE", "CHANGE", "Q1", "Q2", "Q3",
"READ_N", "READS", "QUAL")
#print("This is the DATASET")
#print(dataset)
#print()
positions=dataset.POSITION.sort_values().unique()
events=("REFERENCE", "POSITION", "NO_CHANGE", "A/a", "T/t", "C/c", "G/g", "Ins", "Del", "READ_N", "READS") # Between brakets to avoid an order change
eventsplot=("NO_CHANGE", "A/a", "T/t", "C/c", "G/g", "Ins", "Del")
counttable=pd.DataFrame(index=positions,columns=events)
plottable=pd.DataFrame(index=positions,columns=eventsplot)
#print("This is your empty table")
#print(counttable) # Empty table to fill with character count.
#print ()
#print (positions)
###############################################################################
# 3.
###############################################################################
for i in range (len(dataset)):
position=dataset.iloc[i].POSITION
reference=dataset.iloc[i].REFERENCE
readcount=dataset.iloc[i].READ_N
sequence=dataset.iloc[i].READS
print ("Position:", position, reference, readcount, sequence, len(sequence))
z=0
NO_CHANGE=0
A=0
T=0
G=0
C=0
Ins=0
Del=0
while z in range(len(sequence)):
if z < (len(sequence)-1):
charact=sequence[z]
checindel=sequence[z+1]
if z == (len(sequence)-1):
charact=sequence[z]
checindel=sequence[z]
if charact == 'A':
if checindel not in indelidentifier:
A+=1
if charact == 'a':
if checindel not in indelidentifier:
A+=1
if charact == 'T':
if checindel not in indelidentifier:
T+=1
if charact == 't':
if checindel not in indelidentifier:
T+=1
if charact == 'G':
if checindel not in indelidentifier:
G+=1
if charact == 'g':
if checindel not in indelidentifier:
G+=1
if charact == 'C':
if checindel not in indelidentifier:
C+=1
if charact == 'c':
if checindel not in indelidentifier:
C+=1
if charact in equalasref:
if checindel not in indelidentifier:
NO_CHANGE+=1
if charact in trashyones:
if checindel not in indelidentifier:
pass
if charact == "+":
print("Into the Insertion loop")
inschar1=sequence[z+1]
if inschar1 not in numerical:
pass
if inschar1 in numerical:
inschar2=sequence[z+2]
print (f"The first insertion character is {inschar1}")
if inschar2 not in numerical:
lenght=int(inschar1)
Ins+=1
z+=int(lenght+1)
if inschar2 in numerical:
print (f"The second insertion character is {inschar2}")
lenght=(int(inschar1)*10+int(inschar2))
Ins+=1
z+=int(lenght+2)
print (f"Insertion lenght is {lenght}")
if charact == "-":
print("Into the Deletion loop")
delchar1=sequence[z+1]
if delchar1 not in numerical:
pass
if delchar1 in numerical:
print (f"The first deletion character is {delchar1}")
delchar2=sequence[z+2]
if delchar2 not in numerical:
lenght=int(delchar1)
Del+=1
z+=int(lenght+1)
if delchar2 in numerical:
print (f"The second deletion character is {delchar2}")
lenght=(int(delchar1)*10+int(delchar2))
Del+=1
z+=int(lenght+2)
print (f"Deletion lenght is {lenght}")
z+=1
#print (f"El número de lecturas coincidentes con la referencia es {NO_CHANGE}")
Equalperc=round(NO_CHANGE/readcount, 3)
print (f"Percentage of reads equal to reference is {Equalperc}")
print ()
#print (f"El número de lecturas correspondientes a INSerciones es {Ins}")
Insperc=round(Ins/readcount, 3)
print (f"Percentage of reads with an Insertion is {Insperc}")
#print (f"El número de lecturas correspondientes a DELeciones es {Del}")
Delperc=round(Del/readcount, 3)
print (f"Percentage of reads with a Deletion is {Delperc}")
#print (f"El número de lecturas correspondientes a ADENINA es {A}")
Aperc=round(A/readcount, 3)
print (f"Percentage of reads of Adenine is {Aperc}")
#print (f"El número de lecturas correspondientes a TIMINA es {T}")
Tperc=round(T/readcount, 3)
print (f"Percentage of reads of Timine is {Tperc}")
#print (f"El número de lecturas correspondientes a CITOSINA es {C}")
Cperc=round(C/readcount, 3)
print (f"Percentage of reads of Citosine is {Cperc}")
#print (f"El número de lecturas correspondientes a GUANINA es {G}")
Gperc=round(G/readcount, 3)
print (f"Percentage of reads of Guanine is {Gperc}")
print ()
counttable.loc[[int(position)],['POSITION']]=position
counttable.loc[[int(position)],['REFERENCE']]=reference
counttable.loc[[int(position)],['NO_CHANGE']]=(Equalperc*100)
counttable.loc[[int(position)],['A/a']]=(Aperc*100)
counttable.loc[[int(position)],['T/t']]=(Tperc*100)
counttable.loc[[int(position)],['G/g']]=(Gperc*100)
counttable.loc[[int(position)],['C/c']]=(Cperc*100)
counttable.loc[[int(position)],['Ins']]=(Insperc*100)
counttable.loc[[int(position)],['Del']]=(Delperc*100)
counttable.loc[[int(position)],['READ_N']]=readcount
counttable.loc[[int(position)],['READS']]=sequence
print(counttable)
counttable.to_csv('/home/micro128g2/Documentos/COUNTREAD_SCRIPT/my_processed_table.csv', sep='\t', header=True, index=True)
counttable.to_excel('/home/micro128g2/Documentos/COUNTREAD_SCRIPT/my_processed_table.xlsx', sheet_name='Hoja1')