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
|
5
|
26 # import pandas as pd
|
0
|
27 from argparse import ArgumentParser
|
|
28
|
|
29 def map_peaks(gene,peak,outfile,macsFlag):
|
|
30 genefile = open(gene, 'r')
|
|
31 peakfile = open(peak, 'r')
|
|
32
|
|
33 types = {'promoter':0, 'after':0, 'intron':0, 'exon': 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
|
|
43
|
6
|
44 if macsFlag:
|
|
45 print "MacsFlag off"
|
|
46 start = int(words[3])-1
|
|
47 dist = abs(int(words[12]))
|
|
48 maptype = words[14]
|
|
49 if maptype == 'gene':
|
|
50 maptype = words[15]
|
|
51
|
|
52 else:
|
|
53 print "macsFlag on"
|
0
|
54 try:
|
|
55 start = int(words[3])
|
|
56 dist = abs(int(words[15]))
|
|
57 maptype = words[16]
|
|
58 if maptype == 'gene':
|
|
59 maptype = words[17]
|
|
60 except:
|
|
61 pass
|
|
62
|
6
|
63 print "dist:"
|
|
64 print dist
|
0
|
65
|
|
66 if chrom not in peaks:
|
|
67 #new chrom
|
|
68 peaks[chrom] = {start:[dist,maptype]}
|
|
69 else:
|
|
70 if start in peaks[chrom].keys():
|
|
71 #account for duplicate entries - choose closest gene and store type
|
|
72 if dist < peaks[chrom][start][0]:
|
|
73 #closer gene
|
|
74 peaks[chrom][start] = [dist, maptype]
|
|
75 else: peaks[chrom][start] = [dist, maptype]
|
|
76
|
|
77 #count types - 1 per peak in peak file
|
|
78 types = {'promoter':0, 'after':0, 'intron':0, 'exon': 0, 'inter': 0}
|
|
79 totalpks = 0
|
|
80 #Read peak file in bed format
|
|
81 for line in peakfile:
|
|
82 words = line.strip().split('\t')
|
|
83 chrom = words[0]
|
|
84 start = int(words[1])
|
|
85 if chrom in peaks:
|
|
86 if start in peaks[chrom]:
|
|
87 types[peaks[chrom][start][1]] += 1
|
|
88 else:
|
|
89 types['inter'] += 1
|
|
90 else:
|
|
91 types['inter'] += 1
|
|
92 totalpks += 1
|
|
93
|
|
94
|
|
95 #--------------------------------------------
|
|
96 # make a square figure and axes
|
|
97 #--------------------------------------------
|
|
98
|
|
99 fig = plt.figure(figsize=(6,6))
|
|
100 pie_ax = fig.add_axes((0.3,0.2,0.4,0.4))
|
|
101
|
|
102 # The slices will be ordered and plotted counter-clockwise.
|
|
103 labels = ['exon: %i'%types['exon'],'intron: %i'%types['intron'],'promoter: %i'%types['promoter'],'intergenic: %i'%types['inter'], 'after: %i'%types['after']]
|
|
104 fracs = [types['exon'], types['intron'], types['promoter'], types['inter'], types['after']]
|
|
105
|
|
106 plt.pie(fracs, labels=labels) #, autopct='%1.1f%%')
|
|
107
|
|
108 #Export data frame with all the counts
|
|
109 indexDataFrame = ['exon','intron','promoter','intergenic','after']
|
|
110 df = pd.DataFrame(data=fracs, index=indexDataFrame)
|
|
111 dfFileName = outfile.replace("pdf","csv")
|
|
112 df.to_csv(dfFileName, sep=',')
|
|
113 #plt.title('MACS peaks in %s'%(name))
|
|
114 plt.figtext(.5, .1, 'Total: %i'%totalpks, ha='center')
|
|
115 fig.savefig(outfile)
|
|
116
|
|
117 def main():
|
|
118 usage = "usage: %prog --genefile MACSoutfile_genes.txt --peakfile MACSoutfile_peaks.bed --outfile MACSdirectory/piechart.pdf"
|
|
119 parser = ArgumentParser(usage)
|
|
120 parser.add_argument("--genefile", dest="genefile", help="Path to file MACS_mfold10,30_pval1e-5_genes.txt")
|
|
121 parser.add_argument("--peakfile", dest="peakfile", help="Path to file MACS_mfold10,30_pval1e-5_peaks.bed")
|
|
122 parser.add_argument("--outfile", dest="outfile", default="MACS_piechart.pdf", help="Path to pdf file where you want to store the piechart")
|
|
123 parser.add_argument('--MACS',action='store_true',default=False,help='Set this value if you have MACS peaks')
|
|
124
|
|
125 args=parser.parse_args()
|
|
126
|
6
|
127 print args.MACS
|
|
128
|
0
|
129 map_peaks(args.genefile, args.peakfile, args.outfile, args.MACS)
|
|
130
|
|
131
|
|
132 if __name__=='__main__':
|
|
133 main()
|