comparison hetbox.py @ 2:57c5ea9c3c5c draft

Uploaded
author boris
date Fri, 21 Jun 2013 16:05:46 -0400
parents 153d1a6e8c5e
children 1c21e9f3104b
comparison
equal deleted inserted replaced
1:b4f9a4f2f65d 2:57c5ea9c3c5c
6 # New in this version: 6 # New in this version:
7 # - Add in proper header line if not present 7 # - Add in proper header line if not present
8 8
9 import os 9 import os
10 import sys 10 import sys
11 import array
11 import numpy 12 import numpy
12 from rpy2.robjects import Formula 13 from rpy2.robjects import Formula
13 from rpy2.robjects.packages import importr 14 from rpy2.robjects.packages import importr
14 from rpy2 import robjects 15 from rpy2 import robjects
15 16
47 if 'SAMPLE' not in labels or labels[11] != 'MINOR.FREQ.PERC.': 48 if 'SAMPLE' not in labels or labels[11] != 'MINOR.FREQ.PERC.':
48 sys.stderr.write("Error: Input file does not seem to have a proper header " 49 sys.stderr.write("Error: Input file does not seem to have a proper header "
49 +"line.\nAdding an artificial header..") 50 +"line.\nAdding an artificial header..")
50 add_header = True 51 add_header = True
51 52
53 r = robjects.r
52 base = importr('base') 54 base = importr('base')
53 utils = importr('utils') 55 utils = importr('utils')
54 stats = importr('stats') 56 stats = importr('stats')
55 rprint = robjects.globalenv.get("print") 57 rprint = robjects.globalenv.get("print")
56 graphics = importr('graphics') 58 graphics = importr('graphics')
84 # Formula() creates a Python object representing the R object returned by x ~ y 86 # Formula() creates a Python object representing the R object returned by x ~ y
85 formula = Formula('minor_freq ~ samples') 87 formula = Formula('minor_freq ~ samples')
86 # The "environment" in .getenvironment() is the entire R workspace in which the 88 # The "environment" in .getenvironment() is the entire R workspace in which the
87 # Formula object exists. The R workspace meaning all the defined variables. 89 # Formula object exists. The R workspace meaning all the defined variables.
88 # Here, the .getenvironment() method is being used to set some variables in the 90 # Here, the .getenvironment() method is being used to set some variables in the
91 # R workspace
89 92
90 # R workspace
91 formula.getenvironment()['minor_freq'] = minor_freq 93 formula.getenvironment()['minor_freq'] = minor_freq
92 formula.getenvironment()['samples'] = samples 94 formula.getenvironment()['samples'] = samples
95
96
97 r.par(oma=array.array('i', [0,0,0,0]))
98 r.par(mar=array.array('i', [10,4,4,2]))
99 ylimit = array.array('i',[-5,50])
93 100
94 # create boxplot - fill kwargs1 with the options for the boxplot function 101 # create boxplot - fill kwargs1 with the options for the boxplot function
95 kwargs1 = {'ylab':"Minor allele frequency (%)", 'col':"gray", 'xaxt':"n", 102 kwargs1 = {'ylab':"Minor allele frequency (%)", 'col':"gray", 'xaxt':"n",
96 'outpch':"*",'main':"Distribution of minor allele frequencies", 103 'outpch':"*",'main':"Distribution of minor allele frequencies",
97 'cex.lab':"1.5"} 104 'cex.lab':"1.5"}
98 p = graphics.boxplot(formula, **kwargs1) 105 p = graphics.boxplot(formula, axes=0,ylim=ylimit, lty=1,**kwargs1)
99 table = base.table(DATA.rx2('SAMPLE')) 106
100 graphics.text(0.5, 1, 'N:', font=2) 107 table = base.table(DATA.rx2('SAMPLE'))
108 #graphics.text(-1, 1, 'N:', font=2)
101 for i in range(1, base.length(table)[0]+1, 1): 109 for i in range(1, base.length(table)[0]+1, 1):
102 graphics.text(i, 1, table[i-1], font=2) 110 graphics.text(i, -1, table[i-1], font=2)
103 111
104 graphlabels = base.names(table) 112 graphlabels = base.names(table)
105 kwargs3 = {'pos':"0", 'las':"2", 'cex.axis':"1"} 113 kwargs3 = {'pos':"-2", 'las':"2", 'cex.axis':"1"}
106 graphics.axis(1, at=range(1, len(graphlabels)+1, 1), labels=graphlabels, **kwargs3) 114 graphics.axis(1, at=range(1, len(graphlabels)+1, 1),labels=graphlabels, **kwargs3)
115 graphics.axis(2,at=(range(0,60,10)),pos=0,font=2)
107 grdevices.dev_off() 116 grdevices.dev_off()
108 117
109 if not report: 118 if not report:
110 sys.exit(0) 119 sys.exit(0)
111 120