Example #1
0
    def __call__(self, **kw):
        features = track(kw.get('features'))
        signal = [track(sig, chrmeta=features.chrmeta) for sig in kw.get('signal', [])]
        labels = None
        data = None
        for chrom in features.chrmeta:
            _l, _d = feature_matrix([s.read(chrom) for s in signal],
                                    features.read(chrom), segment=True)

            if _d.size == 0:
                continue
            if data is None:
                labels = _l
                data = _d
            else:
                labels = concatenate((labels, _l))
                data = vstack((data, _d))
        pdf = self.temporary_path(fname='plot_features', ext='.pdf')
        if data is None:
            raise ValueError("No data")
        kw['mode'] = int(kw.get('mode', 0))
        if kw['mode'] == 0:
            new = True
            for n in range(data.shape[-1] - 1):
                heatmap(data[:, :, n], output=pdf, new=new, last=False,
                        rows=labels, orderRows=True, orderCols=False)
                new = False
            heatmap(data[:, :, -1], output=pdf, new=new, last=True,
                    rows=labels, orderRows=True, orderCols=False)
        elif kw['mode'] == 1:
            X = range(data.shape[1])
            Y = data.mean(axis=0)
            lineplot(X, [Y[:, n] for n in range(data.shape[-1])],
                     output=pdf, new=True, last=True)
        elif kw['mode'] == 2:
            X = range(data.shape[1])
            new = True
            mfrow = [4, 3]
            nplot = min(data.shape[0], max_pages * mfrow[0] * mfrow[1])
            for reg in range(nplot - 1):
                lineplot(X, [data[reg, :, n] for n in range(data.shape[-1])],
                         output=pdf, new=new, last=False, mfrow=mfrow)
                new = False
                mfrow = []
            lineplot(X, [data[nplot - 1, :, n] for n in range(data.shape[-1])],
                     output=pdf, new=new, last=True)
        else:
            raise ValueError("Mode not implemented: %s" % kw['mode'])
        self.new_file(pdf, 'plot_features')
        return 1
Example #2
0
 def __call__(self, **kw):
     chrmeta = "guess"
     features = track(kw.get('features'), chrmeta=chrmeta)
     signals = kw.get('signals', [])
     if not isinstance(signals, list): signals = [signals]
     snames = [os.path.splitext(os.path.basename(sig))[0] 
               for sig in signals]
     signals = [track(sig) for sig in signals]
     labels = None
     data = None
     for chrom in features.chrmeta:
         _l, _d = feature_matrix([s.read(chrom) for s in signals],
                                 features.read(chrom), segment=True, 
                                 nbins=nbins, upstream=upstr, downstream=downstr)
         if _d.size == 0:
             continue
         if data is None:
             labels = _l
             data = _d
         else:
             labels = concatenate((labels, _l))
             data = vstack((data, _d))
     pdf = self.temporary_path(fname='plot_features.pdf')
     if data is None:
         raise ValueError("No data")
     kw['mode'] = int(kw.get('mode', 0))
     X = array(range(-upstr[1]+1,nbins+downstr[1]+1))/(1.0*nbins)
     if kw['mode'] == 0: #heatmap
         new = True
         for n in range(data.shape[-1]-1):
             heatmap(data[:, :, n], output=pdf, new=new, last=False,
                     rows=labels, columns=X, main=snames[n],
                     orderRows=True, orderCols=False)
             new = False
         heatmap(data[:, :, -1], output=pdf, new=new, last=True,
                 rows=labels,  columns=X, main=snames[-1],
                 orderRows=True, orderCols=False)
     elif kw['mode'] == 1: #average lineplot
         Y = data.mean(axis=0)
         ymin = min([x.min() for x in Y]+[0])
         ymax = max([x.max() for x in Y])
         lineplot(X, [Y[:, n] for n in range(data.shape[-1])],
                  output=pdf, new=True, last=True, legend=snames, ylim=(ymin,ymax))
     elif kw['mode'] == 2: #mosaic
         new = True
         mfrow = [4, 3]
         nplot = min(data.shape[0], max_pages*mfrow[0]*mfrow[1])
         ymin = min([data.min(),0])
         ymax = data.max()
         for reg in range(nplot-1):
             lineplot(X, [data[reg, :, n] for n in range(data.shape[-1])],
                      output=pdf, new=new, last=False, mfrow=mfrow, 
                      main=labels[reg], ylim=(ymin,ymax))
             new = False
             mfrow = []
         lineplot(X, [data[nplot-1, :, n] for n in range(data.shape[-1])],
                  output=pdf, new=new, last=True, main=labels[-1], 
                  legend=snames, ylim=(ymin,ymax))
     else:
         raise ValueError("Mode not implemented: %s" % kw['mode'])
     self.new_file(pdf, 'plot_features')
     return self.display_time()