-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_umi.py
136 lines (131 loc) · 5.29 KB
/
extract_umi.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
#!/usr/bin/env python
from __future__ import division
__author__='dh'
__version__='v2.20'
'''
use for extract umi from sequence and add to header
'''
import os,re,gzip,sys
import numpy as np
if '2.' not in sys.version:
print('based on python2')
'''
global function
'''
def get_file(fqs_file_path):
with open(fqs_file_path,'r')as f:
fqs=[n.strip('\n').split()for n in f.readlines()]
return fqs
class extract_umi(object):
def __init__(self,fqs,pattern='.{0,8}(.{8})T{3}'):
self.fqs=fqs
self.pattern=pattern
# @staticmethod
# def word_count(strings,list):
# wordlist=list
# wordcount={}
# for string in strings:
# if word not in wordcount:
# wordcount.setdefault(word,1)
# else:
# wordcount[word]+=1
# return wordcount
def extract_umi(self):
pattern=re.compile(self.pattern)
umi={}
umi_uniq={}
umi_uniq2=[]
for fq in self.fqs:
fq1=fq[0]
fq2=fq[1]
h_tol=[]
seq_tol=[]
h2_tol=[]
sc_tol=[]
fq1_h = []
fq1_seq = []
fq1_h2 = []
fq1_sc = []
if fq2.endswith('gz'):
pass
elif fq2.endswith(('fq','fastq')):
with open(fq2,'r')as f:
with open(fq1, 'r')as f1:
h=f.readline()
seq=f.readline()
h2=f.readline()
sc=f.readline()
h1=f1.readline()
seq1=f1.readline()
h21=f1.readline()
sc1=f1.readline()
while h:
res=pattern.search(seq)
if res:
h_tol.append(' '.join([h.split(' ')[0]+'_'+res.group(1),h.split(' ')[1]]))
seq_tol.append(seq[res.end():].lstrip('T'))
h2_tol.append('+\n')
sc_tol.append(sc[-len(seq[res.end():].lstrip('T')):])
fq1_h.append(' '.join([h1.split(' ')[0]+'_'+res.group(1),h1.split(' ')[1]]))
fq1_seq.append(seq1)
fq1_h2.append(h21)
fq1_sc.append(sc1)
umi.setdefault(fq2,[])
umi[fq2].append(res.group(1)+'\n')
# finished=(fqs.index(fq)+1)/len(fqs)*()
# process_bar='['+'#'*finished+' '*(1-finished)+']'+'%.2f'%finished+'%'+'\r'
# sys.stdout.write(process_bar)
# sys.stdout.flush()
h=f.readline()
seq=f.readline()
h2=f.readline()
sc=f.readline()
h1=f1.readline()
seq1=f1.readline()
h21=f1.readline()
sc1=f1.readline()
with open(fq2[:-3]+'_extract.fq','w')as f:
for n in range(len(h_tol)):
f.write(h_tol[n])
f.write(seq_tol[n])
f.write(h2_tol[n])
f.write(sc_tol[n])
print '%s finished'%fq2
with open(fq1[:-3]+'_extract.fq','w')as f:
for n in range(len(fq1_h)):
f.write(fq1_h[n])
f.write(fq1_seq[n])
f.write(fq1_h2[n])
f.write(fq1_sc[n])
print '%s finished'%fq1
umi_np=np.array(umi[fq2])
umi_uniq.setdefault(fq2,np.unique(umi_np))
print "%s count"%fq2
t=len(umi_uniq[fq2])
count=1
print "start count umi in %s"%fq2
for n in umi_uniq[fq2]:
umi_uniq2.append("%s %d\n"%(n.strip('\n'),len(np.where(umi_np==n)[0])))
finished=count/t
count+=1
process_bar='['+'#'*int(finished*80)+' '*int((1-finished)*80)+']'+'%.2f'%(finished*100)+'%'+'\r'
sys.stdout.write(process_bar)
sys.stdout.flush()
with open(fq2[:-3]+'_umi_uniq.txt','w')as f:
f.writelines(umi_uniq2)
else:
print 'must be fasq(fq) or fq.gz'
with open('umi_sum.txt','w')as f:
umi_sum_out=[]
umi_sum=np.array([m for n in umi_uniq for m in n])
umi_uniq_sum=np.unique(umi_sum)
umi_uniq_count_sum=[len(np.where(umi_sum==n)[0])for n in umi_uniq_sum]
for u,n in zip(umi_uniq_sum,umi_uniq_count_sum):
umi_sum_out.append("%s %d\n"%(u,n))
f.writelines(umi_sum_out)
if __name__=='__main__':
fqs_file_path=sys.argv[1]
fqs=get_file(fqs_file_path)
#pattern=sys.argv[2]
ext=extract_umi(fqs)
ext.extract_umi()