freec_CNVPlot.py 4.31 KB
Newer Older
Peter van 't Hof's avatar
Peter van 't Hof committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#
# Biopet is built on top of GATK Queue for building bioinformatic
# pipelines. It is mainly intended to support LUMC SHARK cluster which is running
# SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
# should also be able to execute Biopet tools and pipelines.
#
# Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
#
# Contact us at: sasc@lumc.nl
#
# A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
# license; For commercial users or users who do not want to follow the AGPL
# license, please contact us to obtain a separate license.
#

Sander Bollen's avatar
Sander Bollen committed
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
import argparse
from os.path import join

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt


class BamRatioReader(object):
    """
    Reader object for bam_ratio.txt files
    """

    def __init__(self, filename):
        self.filename = filename
        self.__handle = open(filename)


    def __reset_handle(self):
        self.__handle.close()
        self.__handle = open(self.filename)


    def get_chromosome(self, chromosome):
        self.__reset_handle()
        lines = []
        for line in self.__handle:
            if line.split("\t")[0] == chromosome:
                lines.append(line)
        return lines

    @property
    def chromosomes(self):
        self.__reset_handle()
        chrs = []
        for x in self.__handle:
            if "Chromosome" not in x:
                chrs.append(x.split("\t")[0])
        return list(set(chrs))



def get_cn_lines(lines, cn_type="diploid" ,ploidy=2):
    """
    For a list if lines, return those lines belonging to a certain cn_typle
    :param lines: list of lines
    :param cn_type: either "normal" (cn= ploidy), "loss" (cn < ploidy) or "gain" (cn > ploidy)
    :return: list of lines
    """
    n_lines = []
    for line in lines:
        cn = line.strip().split("\t")[-1]
        if cn_type == "normal" and int(cn) == ploidy:
            n_lines.append(line)
        elif cn_type == "loss" and int(cn) < ploidy:
            n_lines.append(line)
        elif cn_type == "gain" and int(cn) > ploidy:
            n_lines.append(line)
    return n_lines


def lines_to_array(lines, ploidy=2):
    """
    Convert list of lines to numpy array of [start, ratio]
    """
    tmp = []
    for x in lines:
        start = x.split("\t")[1]
        ratio = x.split("\t")[2]
        tmp.append([int(start), float(ratio)*ploidy])
    return np.array(tmp)


def plot_chromosome(lines, chromosome, output_file, ploidy):
    """
    Plot lines belonging to a chromosome
    green = where CN = ploidy
    red = where CN > ploidy
    blue = where CN < ploidy
    """
    fig = plt.figure(figsize=(8,6))
    ax = fig.add_subplot(111)
    normals = lines_to_array(get_cn_lines(lines, "normal", ploidy), ploidy)
    losses = lines_to_array(get_cn_lines(lines, "loss", ploidy), ploidy)
    gains = lines_to_array(get_cn_lines(lines, "gain", ploidy), ploidy)
    print("Plotting chromosome {0}".format(chromosome))
    all_x = []

    if len(normals) > 0:
        ax.scatter(normals[:, 0], normals[:,1], color="g")
        for x in normals[:, 0]:
            all_x += [x]
    if len(losses) > 0:
        ax.scatter(losses[:, 0], losses[:,1], color="b")
        for x in losses[:, 0]:
            all_x += [x]
    if len(gains) > 0:
        for x in gains[:, 0]:
            all_x += [x]
        ax.scatter(gains[:,0], gains[:,1], color="r")

    ax.set_ylim(0, ploidy*3)
    ax.set_xlim(int(0-(max(all_x)*0.1)), int(max(all_x)+(max(all_x)*0.1)))

    ax.set_xlabel("chromosome position")
    ax.set_ylabel("CN")
    ax.set_title("Chromosome {0}".format(chromosome))

    plt.savefig(output_file)
    plt.close()



if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-I', "--input", required=True, help="Input bam_ratio.txt file")
Sander Bollen's avatar
Sander Bollen committed
132
    parser.add_argument('-O', '--output-prefix', required=True, help="Path to output prefix")
Sander Bollen's avatar
Sander Bollen committed
133
134
135
136
137
138
    parser.add_argument("-p", "--ploidy", type=int, default=2, help="Ploidy of sample")

    args = parser.parse_args()

    reader = BamRatioReader(args.input)
    for chromosome in reader.chromosomes:
Sander Bollen's avatar
Sander Bollen committed
139
        ofile = args.output_prefix + "." + "chr{0}.png".format(chromosome)
Sander Bollen's avatar
Sander Bollen committed
140
141
        lines = reader.get_chromosome(chromosome)
        plot_chromosome(lines, chromosome, ofile, args.ploidy)