[fedac4]: / singlecellmultiomics / statistic / rejectionreasons.py

Download this file

56 lines (43 with data), 1.6 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from .statistic import StatisticHistogram
import singlecellmultiomics.pyutils as pyutils
import collections
import pandas as pd
import matplotlib
matplotlib.rcParams['figure.dpi'] = 160
matplotlib.use('Agg')
class RejectionReasonHistogram(StatisticHistogram):
def __init__(self, args):
StatisticHistogram.__init__(self, args)
self.histogram = collections.Counter()
def processRead(self, R1,R2):
for read in [R1,R2]:
if read is None:
continue
if read.has_tag('RR'):
self.histogram[read.get_tag('RR')] += 1
break
def __repr__(self):
rt = 'Rejection reasons:'
for reason, obs in self.histogram.most_common():
rt += f'{reason}\t:\t{obs}\n'
return rt
def __iter__(self):
return iter(self.histogram.most_common())
def plot(self, target_path, title=None):
if len(self.histogram) == 0:
print('Not plotting rejection reasons, rejection tag RR was never seen')
return
df = pd.DataFrame.from_dict({'Reason': dict(self)}).T
df.plot.bar(figsize=(10, 4)).legend(bbox_to_anchor=(1, 0.98))
if title is not None:
plt.title(title)
plt.tight_layout()
plt.subplots_adjust(right=0.6)
plt.savefig(target_path)
ax = plt.gca()
ax.set_yscale('log')
plt.savefig(target_path.replace('.png', '.log.png'))
plt.close()