forked from JunyueC/sci-RNA-seq3_pipeline
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UMI_barcode_attach_gzipped_with_dic_new_2RT.py
164 lines (133 loc) · 6.42 KB
/
UMI_barcode_attach_gzipped_with_dic_new_2RT.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
#chatgpt conversion to python3
import sys
import gzip
import pickle
import csv
from multiprocessing import Pool
from functools import partial
def UMI_attach_read2_barcode_list(pcrwell, input_folder, output_folder, ligation_barcode_list, RT_barcode_list, RTsamplename_dict, RTprimer_dict, RTwells_dict, LIGwells_dict, mismatch_rate=1):
Read1 = input_folder + "/" + pcrwell + ".R1.fastq.gz"
Read2 = input_folder + "/" + pcrwell + ".R2.fastq.gz"
output_file = output_folder + "/" + pcrwell + ".R2.fastq.gz"
mismatch_rate = int(mismatch_rate)
with gzip.open(Read1, 'rt') as f1, gzip.open(Read2, 'rt') as f2, gzip.open(output_file, 'wt') as f3:
line1 = f1.readline()
line2 = f2.readline()
total_line = 0
filtered_line = 0
while line1:
total_line += 1
line1 = f1.readline()
tmp_lig = line1[0:10]
if tmp_lig in ligation_barcode_list:
ligation_bc_match = ligation_barcode_list[tmp_lig]
if ligation_bc_match in LIGwells_dict:
ligindx = LIGwells_dict[ligation_bc_match]
target_RT = line1[len(ligation_bc_match) + 14: len(ligation_bc_match) + 24]
if target_RT in RT_barcode_list:
barcode = RT_barcode_list[target_RT]
if barcode in RTsamplename_dict:
samplename = RTsamplename_dict[barcode]
RTwell = RTwells_dict[barcode]
RTprimer = RTprimer_dict[barcode]
filtered_line += 1
UMI = line1[len(ligation_bc_match) + 6: len(ligation_bc_match) + 14]
first_line = f'@LIG-{ligindx}_RT-{RTwell},PCR-{pcrwell},{UMI},{samplename},{RTprimer}\n'
f3.write(first_line)
second_line = f2.readline()
f3.write(second_line)
third_line = f2.readline()
f3.write(third_line)
four_line = f2.readline()
f3.write(four_line)
line2 = f2.readline()
else:
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
else:
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
else:
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
else:
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
line2 = f2.readline()
line1 = f1.readline()
line1 = f1.readline()
line1 = f1.readline()
print(f"PCR well: {pcrwell}, total line: {total_line}, filtered line: {filtered_line}, filter rate: {float(filtered_line) / float(total_line)}")
def attach_UMI_files(input_folder, pcrwell, output_folder, ligation_barcode_file, LIGlist, RT_barcode_file, RT_sample, core):
init_message = f'''
--------------------------start attaching UMI-----------------------------
input folder: {input_folder}
PCR well list: {pcrwell}
output_folder: {output_folder}
ligation barcode file: {ligation_barcode_file}
ligation list: {LIGlist}
RT barcode file: {RT_barcode_file}
RT samplesheet: {RT_sample}
______________________________________________________________________________________________________________________________________________________
'''
print(init_message)
print("Load ligation barcode dictionary...")
with open(ligation_barcode_file, "rb") as barcodes:
ligation_barcode_list = pickle.load(barcodes)
print("Load RT barcode dictionary...")
with open(RT_barcode_file, "rb") as barcodes:
RT_barcode_list = pickle.load(barcodes)
print("Loading sample names...")
with open(RT_sample, "r") as RTsamplenames:
RTreader = csv.DictReader(RTsamplenames)
RTsamplename_dict = {rows["RTindex"]: rows["SampleName"] for rows in RTreader}
samplelist = list(set(val for dic in RTsamplename_dict for val in RTsamplename_dict.values()))
print(samplelist)
print("Loading RT primers")
#adding this for 2RT primer info
with open(RT_sample, "r") as RTprimernames:
RTreader2 = csv.DictReader(RTprimernames)
RTprimer_dict = {rows["RTindex"]:rows["RTprimer"] for rows in RTreader2}
RTprimerlist = list(set(val for dic in RTprimer_dict for val in RTprimer_dict.values()))
#print(RTprimer_dict)
with open(RT_sample, "r") as RTwells:
RTreader3 = csv.DictReader(RTwells)
RTwells_dict = {rows["RTindex"]: rows["RTwell"] for rows in RTreader3}
RTwellslist = list(set(val for dic in RTwells_dict for val in RTwells_dict.values()))
print(RTwells_dict)
with open(LIGlist, "r") as LIGwells:
RTreader4 = csv.DictReader(LIGwells)
LIGwells_dict = {rows["LIGindex"]: rows["LIGwell"] for rows in RTreader4}
pcrwell_file = open(pcrwell)
pcrwell_list = []
for line in pcrwell_file:
pcrwell = line.strip()
pcrwell_list.append(pcrwell)
pcrwell_file.close()
p = Pool(processes=int(core))
func = partial(
UMI_attach_read2_barcode_list, input_folder=input_folder, output_folder=output_folder, ligation_barcode_list=ligation_barcode_list,
RT_barcode_list=RT_barcode_list, RTsamplename_dict=RTsamplename_dict, RTprimer_dict = RTprimer_dict, RTwells_dict=RTwells_dict, LIGwells_dict=LIGwells_dict, mismatch_rate=1
)
result = p.map(func, pcrwell_list)
p.close()
p.join()
com_message = '''~~~~~~~~~~~~~~~UMI attachment done~~~~~~~~~~~~~~~~~~'''
print(com_message)
if __name__ == "__main__":
input_folder = sys.argv[1]
pcrwell = sys.argv[2]
output_folder = sys.argv[3]
ligation_barcode_file = sys.argv[4]
LIGlist = sys.argv[5]
RT_barcode_file = sys.argv[6]
RT_sample = sys.argv[7]
core = sys.argv[8]
attach_UMI_files(input_folder, pcrwell, output_folder, ligation_barcode_file, LIGlist, RT_barcode_file, RT_sample, core)