def generateDensityPlots(self, labtekId, resD):
     
     labeledPosD = {'s': self.lc.negCtrPosL,
                    '-': self.lc.emptyL}
 
     idL = filter(lambda x: x.split('--')[0] == labtekId, resD.keys())
     idL.sort()
     
     pattern = [(0,0,0), (0,0,0.5), (0,1,1), (1,1,1)]
     colVecL = colors.make_colors(pattern, 500)
     settingL = [
                 {'min': 15, 'max': 800, 'colVecL': None},
                 {'min': 15, 'max': 800, 'colVecL': None},
                 {'min': 0.0, 'max': 4.0, 'colVecL': colVecL}
                 ]
                 
     # init cell count
     for pheno, setting in zip(['initCellCount', 'endCellCount', 'proliferation'], settingL):
         plotDir = os.path.join(self.html_dir, 'plots', labtekId)
         if not os.path.isdir(plotDir):
             os.makedirs(plotDir)
         filename = '%s--%s' % (pheno, labtekId)
         data = self.ap.prepareData(resD, self.ap.getPhenoVal(pheno), idL)
         self.ap.plotArray(data, filename, plotDir=plotDir, colVecL=setting['colVecL'],
             min_val=setting['min'], max_val=setting['max'], 
             label_colors = False, labeledPosD=labeledPosD,
             legend_plot = True, legend_filename='legend_%s' % filename)
     
     return
    def plotArray(self, hitDataParam, filename, plotDir=None, labeledPosD=None,
                  title='', type='png',
                  label_colors = True, colVecL=None, legend_plot = False, legend_filename=None, 
                  control_color='black', numeric_as_int = False,
                  grid=True, max_val=0.3, min_val=0.0, cut_data = True):
        if plotDir is None:
            plotDir = self.plotDir
        full_filename = os.path.join(plotDir, filename)
    
        #hitData = copy.deepcopy(hitDataParam)                
        hitData = hitDataParam
        #nrow = len(hitData)
        #ncol = len(hitData[0])
        nrow = self.nb_row
        ncol = self.nb_col
        
        xL = range(1, self.nb_col + 1)
        yL = range(1, self.nb_row + 1)
        
        rdev = plot_utilities.RDevice(name = full_filename, title=title, plotType=type, 
                                      width=640, height=250)

        if label_colors:
            # in this case, the data is considered to consist of labels
            labelL = []
            for i in range(nrow):
                for j in range(ncol):
                    if hitData[i][j] not in labelL:
                        labelL.append(hitData[i][j]) 
            
            if colVecL is None:
                colVecL = r.rainbow(len(labelL))
            colBreaksL = range(len(colVecL) + 1)

            if legend_plot:
                self.plotLabelLegend(colVecL, plotType=type, filename=legend_filename)
        else:
            # in this case, the data is numeric.
            if cut_data:
                max_rel_cell_count = max_val
                min_rel_cell_count = 0.0
                for i in range(nrow):
                    for j in range(ncol):
                        hitData[i][j] = max(min_rel_cell_count, min(max_rel_cell_count, hitData[i][j])) 
            else:
                max_rel_cell_count = max([max(x) for x in hitData.tolist() ]) 
                min_rel_cell_count = min([min(x) for x in hitData.tolist() ])
            
            if numeric_as_int:
                nb_colors = max_rel_cell_count
            else:
                nb_colors = 500

            if colVecL is None:                    
                pattern = [(0,0,0),(0.7,0,0),(1,1,0),(1,1,1)]
                colVecL = colors.make_colors(pattern, nb_colors)
            colBreaksL = [1.0/ (len(colVecL) - 1) * x * (max_rel_cell_count - min_rel_cell_count) + min_rel_cell_count  for x in range(len(colVecL) + 1)]

            if legend_plot:
                self.plotNumLegend(colVecL, colBreaksL, 16, filename=legend_filename, type=type, int_labels=numeric_as_int, legendDir = plotDir)

        axisSize = .8
        r("par(mar=c(1.6,1.6,0.1,0.1))")
        r.image(xL, yL, r.t(hitData), axes = False, ann=False, cex=1, col=colVecL, breaks=colBreaksL)
        r.box()
        if not labeledPosD is None:
            for label in labeledPosD.keys():
                posL = labeledPosD[label]
                if len(posL) > 0:
                    xlL = [(int(x)-1) % self.nb_col + 1 for x in posL]
                    ylL = [(int(x)-1) / self.nb_col + 1 for x in posL]
                    r.points(xlL, ylL, pch=label, col=control_color, cex=axisSize)
                    print
                    print xlL
                    print ylL
                    print
        # grid
        if grid:
            for i in range(self.nb_col):
                r.abline(h=i+.5, lty=3, lwd=1, col='grey')
            for i in range(self.nb_row):
                r.abline(v=i+.5, lty=3, lwd=1, col='grey')
            
        r.axis(1, at=xL, labels=[str(x) for x in xL], tick=False, line=-1.0, cex_axis=axisSize)
        r.axis(2, at=yL, labels=[str(y) for y in yL], tick=False, line=-1.0, cex_axis=axisSize)
        rdev.close()
        
        return
    def generateDensityPlots(self, labtekId, resD):
        
        labeledPosD = {'s': ['015', '026', '063', '074', '304', '315', '352'],
                       'b' : ['363'],
                       'e' : ['290', '301', '338', '349'],
                       'i' : ['004', '049', '052'],
                       'm' : ['001']
                       }      
    
        idL = filter(lambda x: x.split('--')[0] == labtekId, resD.keys())
        idL.sort()
        
        #pattern = [(0,0,0), (0,0,1), (0,0.5,1), (1,1,1)]
#        pattern = [(0.00, 0.10, 0.30)
#                   (0.00, 0.22, 0.61),
#                   (0.1, 0.31, 0.74), 
#                   (0.3, 0.48, 0.84),
#                   (0.6, 0.64, 0.91),
#                   (0.94, 0.95, 1.00)]
        pattern = get_color_brewer_pattern("YlGnBu")
        col2 = colors.make_colors(pattern, 500)
        
#        pattern = [(0, 0, 0), 
#                   (0.0, 0.26, 0.1), 
#                   (0.0, 0.43, 0.17), 
#                   (0.1, 0.54, 0.27), 
#                   (0.2, 0.67, 0.36), 
#                   (0.3, 0.77, 0.46), 
#                   (0.4, 0.85, 0.60), 
#                   (0.6, 0.91, 0.75), 
#                   (0.8, 0.96, 0.88), 
#                   (0.97, 0.98, 1.0)]        
#        pattern = [(0,0,0), (0,0.5,0), (0.3,1,0), (1,1,1)]
        pattern = get_color_brewer_pattern("YlGn")
        col1 = colors.make_colors(pattern, 500)

        #paletteName = 'RdPu'
        #colRampFunc = r.colorRampPalette(brewer.pal(9, paletteName))
        #colVec = colRampFunc(384)

        settingL = [
                    {'min': 0.0, 'max': 80.0, 'colVecL': col1},
                    {'min': 0.0, 'max': 100.0, 'colVecL': col2},
                    {'min': 0, 'max': 800, 'colVecL': None},
                    {'min': 0, 'max': 384, 'colVecL': None},
                    {'min': 0.0, 'max': 100.0, 'colVecL': col2},                   
                    ]
                    
        # init cell count
        for pheno, setting in zip(['ratio', 'fractionIndex', 'number', 'pos', 'fractionIndexPlateThresh'], settingL):
            plotDir = os.path.join(self.html_dir, 'plots', labtekId)
            if not os.path.isdir(plotDir):
                os.makedirs(plotDir)
            filename = '%s--%s' % (pheno, labtekId)
            data = self.ap.prepareData(resD, self.ap.getPhenoVal(pheno), idL)
            self.ap.plotArray(data, filename, plotDir=plotDir, colVecL=setting['colVecL'],
                min_val=setting['min'], max_val=setting['max'], 
                label_colors = False, labeledPosD=labeledPosD,
                legend_plot = True, legend_filename='legend_%s' % filename)

            filename = 'Rotate--%s--%s' % (pheno, labtekId)
            data = self.ap.prepareData(resD, self.ap.getPhenoVal(pheno), idL, flip_axis=True)
            self.ap.plotArray(data, filename, plotDir=plotDir, colVecL=setting['colVecL'],
                min_val=setting['min'], max_val=setting['max'], 
                label_colors = False, labeledPosD={},
                legend_plot = False, flip_axis=True, axis=False)
        
        return