Repository navigation
Expand file tree
/
Copy pathcrispressoPooled_allele_edit_eff.py
More file actions
executable file
·145 lines (119 loc) · 3.02 KB
/
Copy pathcrispressoPooled_allele_edit_eff.py
File metadata and controls
executable file
·145 lines (119 loc) · 3.02 KB
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
#!/hpcf/apps/python/install/2.7.13/bin/python
"""
Program to get average editing frequency from CrispEsso.
"""
import matplotlib
import pandas as pd
matplotlib.use('agg')
import matplotlib.pyplot as plt
import seaborn as sns
import string
import sys
import glob
import numpy as np
import os
def revcomp(seq):
try: ## python2
tab = string.maketrans(b"ACTG", b"TGAC")
except: ## python3
tab = bytes.maketrans(b"ACTG", b"TGAC")
return seq.translate(tab)[::-1]
def check_header(input_gRNA,table_header_list):
table_header=[x.split(".")[0] for x in table_header_list]
table_header = "".join(table_header)
if input_gRNA != table_header:
print ("something is wrong, Exist!")
exit()
def is_edit2(r,ref,alt,gRNA_length,SNP_dict=None):
# update: fix for 19bp and 21 bp sgRNA
offset = 20-gRNA_length
read = r.Aligned_Sequence[10+offset:30]
gRNA = r.Reference_Sequence[10+offset:30]
if read == gRNA:
return False
count = 1
for i in range(3,10): # specificaly modified for revision, original was all protospacer sequence
a = read[i]
b = gRNA[i]
try:
SNP_base = SNP_dict[count]
b = SNP_base
except:
pass
if (b == ref) and (a == alt):
return True
count += 1
return False
def get_SNP_dict(df):
try:
df.shape[1]
except:
df = pd.DataFrame(df).T
df.index = df[1].tolist()
return df[2].to_dict()
def parse_df(f,gRNA,ref,alt,snp):
df = pd.read_csv(f,sep="\t")
df['is_gRNA'] = [gRNA in x for x in df.Reference_Sequence]
# print (df.head())
df = df[df.is_gRNA == True]
# print (df.head())
if df.shape[0]==0:
print (f,gRNA,"gRNA not found, Exit!")
exit()
try:
tmp = snp.loc[gRNA]
flag = True
except:
tmp = None
flag = False
if flag:
SNP_dict = get_SNP_dict(tmp)
# print (SNP_dict)
else:
SNP_dict = None
# print (f)
df['is_edit'] = df.apply(lambda r: is_edit2(r,ref,alt,len(gRNA),SNP_dict),axis=1)
edited_N = df[(df.Unedited == False)&(df.is_edit == True)]['#Reads'].sum()
edited_P = df[(df.Unedited == False)&(df.is_edit == True)]['%Reads'].sum()
return edited_N,edited_P
def parse_snp(f):
df = pd.read_csv(f,sep="\t",header=None,index_col=0)
return df
sample_id_list = [sys.argv[1]]
df = pd.read_csv(sys.argv[2],sep="\t",header=None)
ref=sys.argv[3]
alt=sys.argv[4]
try:
SNP = parse_snp(sys.argv[5])
except:
SNP=None
def correct_name(x):
x = x.replace(":","_")
x = x.replace(".","_")
return x
for s in sample_id_list:
outfile = "%s.allele.edit.tsv"%(s)
N_list = []
P_list = []
gRNA_list = []
name_list = []
for name,_,gRNA in df.values:
# print (name,gRNA)
file = "{0}_results/CRISPRessoPooled_on_{0}/CRISPResso_on_{1}/Alleles_frequency_table_around_sgRNA_{2}.txt".format(s,correct_name(name),gRNA)
# print (file)
if os.path.isfile(file):
# print (name,gRNA)
N,P = parse_df(file,gRNA,ref,alt,SNP)
else:
N=-1
P=-1
N_list.append(N)
P_list.append(P)
gRNA_list.append(gRNA)
name_list.append(name)
out = pd.DataFrame()
out['Sample'] = name_list
out['gRNA'] = gRNA_list
out['#Reads'] = N_list
out['%Reads'] = P_list
out.to_csv(outfile,sep="\t",index=False)