forked from Jverma/Evaluating-genome-assembly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
209 lines (160 loc) · 4.15 KB
/
stats.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
# -*- coding: utf-8 -*-
# Computing statistics of Assembled Contigs.
# Author - Janu Verma
# jv367@cornell.edu
import sys
import numpy as np
import csv
import matplotlib
import pylab
from fastaParsing import FastaParser
class AssemblyStatistics:
"""
Computes basic statistics of the assembled contigs.
Parameter
---------
inputFile : The FASTA file containing contigs.
Example
-------
>>> import sys
>>> import numpy as np
>>> inputFile = sys.argv[1]
>>> out = AssemblyStatistics(inputFile)
>>> N50 = out.N50()
>>> minContigLength = out.minContigLength()
>>> maxContigLength = out.maxContigLength()
"""
def __init__(self, inputFile):
self.inFile = inputFile
self.fastaInfo = FastaParser(self.inFile)
self.contigsInfo = self.fastaInfo.sequenceDict()
def scores(self):
"""
Compute the basic statistics.
Returns
-------
Dictionary of the basic statistics of the assembly.
"""
seqLengths = []
for x in self.contigsInfo.keys():
seq = self.contigsInfo[x]
seqLengths.append(len(seq))
seqLengths = sorted(seqLengths)
max_length = max(seqLengths)
min_length = min(seqLengths)
mean_length = np.mean(seqLengths)
midLength = sum(seqLengths)/2
computedMidLength = 0
l50 = 0
n50 = 0
for i,x in enumerate(seqLengths):
if (midLength < computedMidLength):
n50 = i
l50 = x
break
computedMidLength += x
scoresDict = {'number_of_contigs':len(seqLengths), 'smallestContig':min_length, 'meanContig':mean_length,
'n50':n50, 'l50':l50, 'largestContig':max_length, 'lengthOfAssembly':sum(seqLengths)}
return scoresDict
def N50(self):
"""
Computes the N50 score of the assembly.
Returns
-------
Numerical value of the N50 statistics.
"""
stats = self.scores()
return stats['n50']
def L50(self):
"""
Computes the L50 score of the assembly.
Returns
-------
Numerical value of the L50 statistics.
"""
stats = self.scores()
return stats['l50']
def maxContigLength(self):
"""
Computes the length of the largest contig in the assembly.
Returns
-------
Numerical value of the max length.
"""
stats = self.scores()
return stats['largestContig']
def minContigLength(self):
"""
Computes the length of the smallest contig in the assembly.
Returns
-------
Numerical value of the min length.
"""
stats = self.scores()
return stats['smallestContig']
def meanContigLength(self):
"""
Computes the mean length of the contigs in the assembly.
Returns
-------
Numerical value of the mean length.
"""
stats = self.scores()
return stats['meanContig']
def lengths(self):
"""
Write the contig lengths in a csv file.
Returns
-------
A csv file (contigLengths.csv) containing the lengths of the contigs.
"""
output_file = csv.writer(open('contigLengths.csv', 'wb'))
for i,x in enumerate(self.contigsInfo.keys()):
seq = self.contigsInfo[x]
l = len(seq)
output_file.writerow([i,l])
def histogramOfContigLengths(self):
"""
Plots a histogram of the contig lengths.
Returns
------
A histogram of contig sizes, saved in the file contig_histogram.png
"""
seqLengths = []
for x in self.contigsInfo.keys():
seq = self.contigsInfo[x]
seqLengths.append(len(seq))
seqLengths = sorted(seqLengths)
l = seqLengths[0:540000]
matplotlib.use('Agg')
pylab.hist(l, bins=50)
pylab.title("Contigs historgram")
pylab.xlabel('Sequence Length (bp)')
pylab.ylabel('Count')
pylab.savefig('contig_histogram.png')
def boxPlot(self):
"""
Plots a box-plot of the contig lengths.
Returns
------
Box plot of contig sizes, saved in the file contig_boxplot.png
"""
seqLengths = []
for x in self.contigsInfo.keys():
seq = self.contigsInfo[x]
seqLengths.append(len(seq))
pylab.boxplot(seqLengths)
pylab.savefig('contig_boxplot.png')
def assemblyCoverage(self, genomeSize):
"""
Computes the coverage of the assembly.
Parameter
---------
genomeSize : (Expected )Size (in bp) of the genome.
Returns
-------
Numerical value of the fraction of the genome covered by the assembly.
"""
stats = self.scores()
assemblySize = stats['lengthOfAssembly']
return float(assemblySize)/genomeSize