|
a |
|
b/singlecellmultiomics/statistic/rejectionreasons.py |
|
|
1 |
#!/usr/bin/env python3 |
|
|
2 |
# -*- coding: utf-8 -*- |
|
|
3 |
import matplotlib.pyplot as plt |
|
|
4 |
from .statistic import StatisticHistogram |
|
|
5 |
import singlecellmultiomics.pyutils as pyutils |
|
|
6 |
import collections |
|
|
7 |
import pandas as pd |
|
|
8 |
|
|
|
9 |
import matplotlib |
|
|
10 |
matplotlib.rcParams['figure.dpi'] = 160 |
|
|
11 |
matplotlib.use('Agg') |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
class RejectionReasonHistogram(StatisticHistogram): |
|
|
15 |
def __init__(self, args): |
|
|
16 |
StatisticHistogram.__init__(self, args) |
|
|
17 |
self.histogram = collections.Counter() |
|
|
18 |
|
|
|
19 |
def processRead(self, R1,R2): |
|
|
20 |
|
|
|
21 |
for read in [R1,R2]: |
|
|
22 |
if read is None: |
|
|
23 |
continue |
|
|
24 |
if read.has_tag('RR'): |
|
|
25 |
self.histogram[read.get_tag('RR')] += 1 |
|
|
26 |
break |
|
|
27 |
|
|
|
28 |
def __repr__(self): |
|
|
29 |
rt = 'Rejection reasons:' |
|
|
30 |
for reason, obs in self.histogram.most_common(): |
|
|
31 |
rt += f'{reason}\t:\t{obs}\n' |
|
|
32 |
return rt |
|
|
33 |
|
|
|
34 |
def __iter__(self): |
|
|
35 |
return iter(self.histogram.most_common()) |
|
|
36 |
|
|
|
37 |
def plot(self, target_path, title=None): |
|
|
38 |
if len(self.histogram) == 0: |
|
|
39 |
print('Not plotting rejection reasons, rejection tag RR was never seen') |
|
|
40 |
return |
|
|
41 |
|
|
|
42 |
df = pd.DataFrame.from_dict({'Reason': dict(self)}).T |
|
|
43 |
|
|
|
44 |
df.plot.bar(figsize=(10, 4)).legend(bbox_to_anchor=(1, 0.98)) |
|
|
45 |
if title is not None: |
|
|
46 |
plt.title(title) |
|
|
47 |
|
|
|
48 |
plt.tight_layout() |
|
|
49 |
plt.subplots_adjust(right=0.6) |
|
|
50 |
plt.savefig(target_path) |
|
|
51 |
|
|
|
52 |
ax = plt.gca() |
|
|
53 |
ax.set_yscale('log') |
|
|
54 |
plt.savefig(target_path.replace('.png', '.log.png')) |
|
|
55 |
plt.close() |