forked from sc-zhang/bioscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeFileSeqStat.py
executable file
·140 lines (127 loc) · 3.1 KB
/
mergeFileSeqStat.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
#!/usr/bin/env python
import sys
import os
import pysam
import gzip
def read_fasta(in_file):
if in_file.split('.')[-1] == 'gz':
fin = gzip.open(in_file, 'rt')
else:
fin = open(in_file, 'r')
seq_len = []
for line in fin:
if line[0] == '>':
seq_len.append(0)
else:
seq_len[-1] += len(line.strip())
fin.close()
return seq_len
def read_fastq(in_file):
if in_file.split('.')[-1] == 'gz':
fin = gzip.open(in_file, 'rt')
else:
fin = open(in_file)
seq_len = []
cnt = 0
for line in fin:
if cnt%4 == 1:
seq_len.append(len(line.strip()))
cnt += 1
fin.close()
return seq_len
def read_bam(in_file):
seq_len = []
'''
with os.popen("samtools view %s"%in_file, 'r') as fin:
for line in fin:
seq_len.append(len(line.strip().split()[9]))
'''
with pysam.AlignmentFile(in_file, 'rb', check_sq=False) as fin:
for line in fin:
seq_len.append(line.query_length)
return seq_len
def check_file_type(in_file):
data = in_file.split('.')
if data[-1] == 'gz':
with gzip.open(in_file, 'rt') as fin:
for line in fin:
break
if line[0] == '>':
return "fa"
elif line[0] == '@':
return "fq"
else:
return ""
elif data[-1] == 'bam':
return "bam"
else:
with open(in_file, 'r') as fin:
for line in fin:
break
if line[0] == '>':
return "fa"
elif line[0] == '@':
return "fq"
else:
return ""
def seq_stat(in_files, out_stat):
seq_len = []
for in_file in in_files.split(','):
print("Reading %s"%in_file)
file_type = check_file_type(in_file)
if file_type == 'fa':
seq_len.extend(read_fasta(in_file))
elif file_type == 'fq':
seq_len.extend(read_fastq(in_file))
elif file_type == 'bam':
seq_len.extend(read_bam(in_file))
else:
print("Unsupport file type")
continue
seq_len = sorted(seq_len, reverse=True)
seq_cnt = len(seq_len)
if seq_cnt == 0:
print("No seq load")
sys.exit()
min_len = seq_len[-1]
max_len = seq_len[0]
total_size = sum(seq_len)
ave_len = total_size*1.0/seq_cnt
n_threshold = []
n_values = []
n_labels = []
for i in range(90, 40, -10):
n_threshold.append(i/100.0*total_size)
n_values.append(0)
n_labels.append("N%d:\t"%i)
cur_size = 0
cnt_500 = 0
cnt_2k = 0
for i in range(0, seq_cnt):
cur_size += seq_len[i]
for j in range(0, len(n_values)):
if n_values[j] == 0 and cur_size >= n_threshold[j]:
n_values[j] = seq_len[i]
if seq_len[i] > 500:
cnt_500 += 1
if seq_len[i] > 2000:
cnt_2k += 1
n_info = []
for i in range(0, len(n_values)):
n_info.append("%s%d\n"%(n_labels[i], n_values[i]))
info = "number of seq:\t%d\nmin length:\t%d\nmax length:\t%d\ntotal size:\t%d\n%sAverage length:\t%d\nTotal number (>500bp):\t%d\nTotal number (>2000bp):\t%d"%(seq_cnt, min_len, max_len, total_size, ''.join(n_info), ave_len, cnt_500, cnt_2k)
if out_stat == "":
print(info)
else:
with open(out_stat, 'w') as fout:
fout.write("%s\n"%info)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python %s <in_files> [out_stat]"%sys.argv[0])
else:
if len(sys.argv) == 2:
in_files = sys.argv[1]
out_stat = ""
else:
in_files, out_stat = sys.argv[1:]
seq_stat(in_files, out_stat)