0
|
1 '''
|
|
2 NAME
|
|
3
|
|
4 pieplots_macs.py
|
|
5
|
|
6 SYNOPSIS
|
|
7
|
|
8 python pieplots_macs.py --genefile MACSoutfile_genes.txt --peakfile MACSoutfile_peaks.bed --outfile MACSdirectory/piechart.pdf
|
|
9
|
|
10
|
|
11 DESCRIPTION
|
|
12
|
|
13 Peaks are assigned to the closest gene and then categorized according to their location at different genomic regions (promoter, intron, exon, or after the gene). Sites >10kb away from any gene are considered intergenic. (from Pamela)
|
|
14
|
|
15 '''
|
|
16
|
|
17 __author__='Renan Escalante'
|
|
18 __email__='renanec@mit.edu'
|
|
19
|
2
|
20 import sys
|
0
|
21 import matplotlib
|
|
22 matplotlib.use('pdf')
|
|
23 from matplotlib import pyplot as plt
|
|
24 matplotlib.rcParams['pdf.fonttype']=42
|
|
25 matplotlib.rcParams['font.size']=14
|
9
|
26 import pandas as pd
|
0
|
27 from argparse import ArgumentParser
|
|
28
|
15
|
29 def map_peaks(gene, peak, outfile, macsFlag):
|
0
|
30 genefile = open(gene, 'r')
|
|
31 peakfile = open(peak, 'r')
|
|
32
|
15
|
33 types = {'promoter':0, 'after':0, 'intron':0, 'exon':0}
|
0
|
34
|
|
35 #read mapped gene file, store closest map for each peak
|
|
36 peaks={} #{chrom:{peakStart:[dist, type]}}
|
|
37 for line in genefile:
|
|
38 words = line.strip().split('\t')
|
|
39 #ignore first line
|
|
40 if words[0] == 'knownGeneID': continue
|
|
41 chrom = words[2]
|
|
42
|
6
|
43 if macsFlag:
|
|
44 start = int(words[3])-1
|
|
45 dist = abs(int(words[12]))
|
|
46 maptype = words[14]
|
|
47 if maptype == 'gene':
|
|
48 maptype = words[15]
|
|
49 else:
|
0
|
50 try:
|
|
51 start = int(words[3])
|
|
52 dist = abs(int(words[15]))
|
|
53 maptype = words[16]
|
|
54 if maptype == 'gene':
|
|
55 maptype = words[17]
|
|
56 except:
|
9
|
57 print 'there probably were not enough columns in the genefile'
|
0
|
58 pass
|
|
59
|
|
60 if chrom not in peaks:
|
|
61 #new chrom
|
|
62 peaks[chrom] = {start:[dist,maptype]}
|
|
63 else:
|
|
64 if start in peaks[chrom].keys():
|
|
65 #account for duplicate entries - choose closest gene and store type
|
|
66 if dist < peaks[chrom][start][0]:
|
|
67 #closer gene
|
|
68 peaks[chrom][start] = [dist, maptype]
|
|
69 else: peaks[chrom][start] = [dist, maptype]
|
|
70
|
|
71 #count types - 1 per peak in peak file
|
|
72 types = {'promoter':0, 'after':0, 'intron':0, 'exon': 0, 'inter': 0}
|
|
73 totalpks = 0
|
|
74 #Read peak file in bed format
|
|
75 for line in peakfile:
|
|
76 words = line.strip().split('\t')
|
|
77 chrom = words[0]
|
|
78 start = int(words[1])
|
|
79 if chrom in peaks:
|
|
80 if start in peaks[chrom]:
|
|
81 types[peaks[chrom][start][1]] += 1
|
|
82 else:
|
|
83 types['inter'] += 1
|
|
84 else:
|
|
85 types['inter'] += 1
|
|
86 totalpks += 1
|
|
87
|
|
88
|
|
89 #--------------------------------------------
|
|
90 # make a square figure and axes
|
|
91 #--------------------------------------------
|
|
92
|
|
93 fig = plt.figure(figsize=(6,6))
|
|
94 pie_ax = fig.add_axes((0.3,0.2,0.4,0.4))
|
|
95
|
|
96 # The slices will be ordered and plotted counter-clockwise.
|
|
97 labels = ['exon: %i'%types['exon'],'intron: %i'%types['intron'],'promoter: %i'%types['promoter'],'intergenic: %i'%types['inter'], 'after: %i'%types['after']]
|
|
98 fracs = [types['exon'], types['intron'], types['promoter'], types['inter'], types['after']]
|
|
99
|
|
100 plt.pie(fracs, labels=labels) #, autopct='%1.1f%%')
|
|
101
|
|
102 #Export data frame with all the counts
|
15
|
103 # indexDataFrame = ['exon','intron','promoter','intergenic','after']
|
|
104 # df = pd.DataFrame(data=fracs, index=indexDataFrame)
|
|
105 # dfFileName = outfile.replace("pdf","csv")
|
|
106 # df.to_csv(dfFileName, sep=',')
|
0
|
107 #plt.title('MACS peaks in %s'%(name))
|
|
108 plt.figtext(.5, .1, 'Total: %i'%totalpks, ha='center')
|
|
109 fig.savefig(outfile)
|
|
110
|
|
111 def main():
|
|
112 usage = "usage: %prog --genefile MACSoutfile_genes.txt --peakfile MACSoutfile_peaks.bed --outfile MACSdirectory/piechart.pdf"
|
|
113 parser = ArgumentParser(usage)
|
|
114 parser.add_argument("--genefile", dest="genefile", help="Path to file MACS_mfold10,30_pval1e-5_genes.txt")
|
|
115 parser.add_argument("--peakfile", dest="peakfile", help="Path to file MACS_mfold10,30_pval1e-5_peaks.bed")
|
|
116 parser.add_argument("--outfile", dest="outfile", default="MACS_piechart.pdf", help="Path to pdf file where you want to store the piechart")
|
|
117 parser.add_argument('--MACS',action='store_true',default=False,help='Set this value if you have MACS peaks')
|
|
118
|
|
119 args=parser.parse_args()
|
|
120
|
|
121 map_peaks(args.genefile, args.peakfile, args.outfile, args.MACS)
|
|
122
|
|
123
|
|
124 if __name__=='__main__':
|
|
125 main()
|