Switch to unified view

a b/singlecellmultiomics/statistic/allele.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
8
import matplotlib
9
matplotlib.rcParams['figure.dpi'] = 160
10
matplotlib.use('Agg')
11
12
13
class AlleleHistogram(StatisticHistogram):
14
    def __init__(self, args):
15
        StatisticHistogram.__init__(self, args)
16
        self.histogram = collections.Counter()
17
18
    def processRead(self, R1,R2):
19
20
        for read in [R1,R2]:
21
            if read is None:
22
                continue
23
            if read.has_tag('DA'):
24
                self.histogram[read.get_tag('DA')] += 1
25
26
    def __repr__(self):
27
        rt = 'Allele observations:'
28
        for allele, obs in self.histogram.most_common():
29
            rt += f'{allele}\t:\t{obs}\n'
30
        return rt
31
32
    def __iter__(self):
33
        return iter(self.histogram.most_common())
34
35
    def plot(self, target_path, title=None):
36
        d = dict(self)
37
        fig, ax = plt.subplots()
38
        ax.scatter(list(d.keys()), list(d.values()))
39
        plt.subplots_adjust(hspace=1)
40
        ax.set_yscale('log')
41
        ax.set_ylabel('# Molecules')
42
        ax.set_xlabel('Times oversequenced')
43
        ax.set_xlim(0, 20.5)
44
        ax.set_ylim((1, None))
45
46
        if title is not None:
47
            plt.title(title)
48
        plt.tight_layout()
49
        plt.savefig(target_path)
50
        plt.close()