Exemple #1
0
    def generate(self, input=None, **kwargs):
        dsi = input
        opener = register_openers()
        url = 'http://www.kegg.jp/kegg-bin/mcolor_pathway'

        data = io.StringIO()

        node_colors = {}
        if dsi:
            mini, maxi = min(abs(np.median(dsi.data)), 0), max(abs(np.median(dsi.data)), 0)
            mini, maxi = -2.0, +2.0  # Fudge; need an intelligent way to determine (2*median? 2*mean?)
            scale = utils.calculate_scale([mini, 0, maxi], [9, 1], out=np.around)  # rdbu9 scale

            #for n, m in enumerate(dsi.entities[1]):
            #    xref = self.get_xref( m )

            for n, m in enumerate(dsi.entities[1]):
                if m:

                    if 'LIGAND-CPD' in m.databases:
                        kegg_id = m.databases['LIGAND-CPD']
                        ecol = utils.calculate_rdbu9_color(scale, dsi.data[0, n])
                        if kegg_id is not None and ecol is not None:
                            node_colors[kegg_id] = ecol

                    elif 'NCBI-GENE' in m.databases:
                        kegg_id = m.databases['NCBI-GENE']
                        ecol = utils.calculate_rdbu9_color(scale, dsi.data[0, n])
                        if kegg_id is not None and ecol is not None:
                            node_colors[kegg_id] = ecol

        tmp = open(os.path.join(QDir.tempPath(), 'kegg-pathway-data.txt'), 'w')
        tmp.write('#hsa\tData\n')
        for k, c in list(node_colors.items()):
            tmp.write('%s\t%s\n' % (k, c[0]))
        tmp = open(os.path.join(QDir.tempPath(), 'kegg-pathway-data.txt'), 'r')

        values = {
                  'map': self.kegg_pathway_t.text(),
                  'mapping_list': tmp,
                  'mode': 'color',
                  'submit': 'Exec',
                 }

        self.status.emit('waiting')

        datagen, headers = multipart_encode(values)
        # Create the Request object
        # Actually do the request, and get the response
        request = Request(url, datagen, headers)

        try:
            response = urlopen(request)
        except urllib.error.HTTPError as e:
            print(e)
            return

        return {'html': response.read()}
Exemple #2
0
    def prerender(self,
                  compound_data=None,
                  gene_data=None,
                  protein_data=None,
                  gpml=None):

        node_colors = {}

        for dso in compound_data, gene_data, protein_data:
            if dso == None:
                continue

            mini, maxi = min(abs(np.median(dso.data)),
                             0), max(abs(np.median(dso.data)), 0)
            mini, maxi = -1.0, +1.0  # Fudge; need an intelligent way to determine (2*median? 2*mean?)
            scale = utils.calculate_scale([mini, 0, maxi], [9, 1],
                                          out=np.around)  # rdbu9 scale

            for n, m in enumerate(dso.entities[1]):
                xref = self.get_xref(m)
                ecol = utils.calculate_rdbu9_color(scale, dso.data[0, n])
                #print xref, ecol
                if xref is not None and ecol is not None:
                    node_colors[xref] = ecol

        print(node_colors)

        return {'View': {'gpml': gpml, 'node_colors': node_colors}}
Exemple #3
0
    def prerender(self, compound_data=None, gene_data=None, protein_data=None, gpml=None):

        node_colors = {}

        for dso in compound_data, gene_data, protein_data:
            if dso == None:
                continue

            mini, maxi = min(abs(np.median(dso.data)), 0), max(abs(np.median(dso.data)), 0)
            mini, maxi = -1.0, +1.0  # Fudge; need an intelligent way to determine (2*median? 2*mean?)
            scale = utils.calculate_scale([mini, 0, maxi], [9, 1], out=np.around)  # rdbu9 scale

            for n, m in enumerate(dso.entities[1]):
                xref = self.get_xref(m)
                ecol = utils.calculate_rdbu9_color(scale, dso.data[0, n])
                #print xref, ecol
                if xref is not None and ecol is not None:
                    node_colors[xref] = ecol
        #logging.debug("Calculated node colors: %s" % (','.join(node_colors)) )

        return {'View': {'gpml': gpml, 'node_colors': node_colors}}
Exemple #4
0
    def generateGraph(self, filename, suggested_pathways=[], compound_data=None, gene_data=None, protein_data=None, format='svg'):
        # Build options-like structure for generation of graph
        # (compatibility with command line version, we need to fake it)
        options = Values()

        options._update_loose({
            'file': None,
            #'pathways': self.config.Read('/Pathways/Show'),
            #'not_pathways':'',
            'show_all': False,  # self.config.ReadBool('/Pathways/ShowAll'),
            'search': '',
            'cluster_by': self.config.get('/App/ClusterBy'),
            'show_enzymes': self.config.get('/App/ShowEnzymes'),  # self.config.ReadBool('/App/ShowEnzymes'),
            'show_secondary': self.config.get('/App/Show2nd'),
            'show_molecular': self.config.get('/App/ShowMolecular'),
            'show_network_analysis': self.config.get('/App/ShowAnalysis'),
            'show_gibbs': self.config.get('/App/ShowGibbs'),

            'highlightpathways': self.config.get('/App/HighlightPathways'),
            'highlightregions': self.config.get('/App/HighlightRegions'),

            'splines': 'true',
            'focus': False,
            'show_pathway_links': self.config.get('/Pathways/ShowLinks'),
            # Always except when saving the file
            'output': format,

        })

        #pathway_ids = self.config.value('/Pathways/Show').split(',')
        if suggested_pathways:
            pathway_ids = [p.id for p in suggested_pathways.entities[1]]
        else:
            pathway_ids = []

        # Add the manually Shown pathways
        pathway_ids_show = self.config.get('/Pathways/Show')
        pathway_ids.extend(pathway_ids_show)

        # Now remove the Hide pathways
        pathway_ids_hide = self.config.get('/Pathways/Hide')
        pathway_ids = [p for p in pathway_ids if p not in pathway_ids_hide]

        # Convert pathways_ids to pathways
        pathways = [self.m.db.pathway(pid) for pid in pathway_ids if self.m.db.pathway(pid) is not None]

        
        if pathway_ids == []:
            return None

        
        if compound_data or gene_data or protein_data:

        # Generate independent scales
            node_colors = {}

            for dsi in compound_data, gene_data, protein_data:
                if dsi == None:
                    continue
                #if self.m.data.analysis_timecourse:
                #    # Generate the multiple views
                #    tps = sorted( self.m.data.analysis_timecourse.keys(), key=int )
                #    # Insert counter variable into the filename
                #    filename = self.get_filename_with_counter(filename)
                #    print "Generate timecourse..."
                #    for tp in tps:
                #        print "%s" % tp
                #        graph = generator( pathways, options, self.m.db, analysis=self.m.data.analysis_timecourse[ tp ]) #, layout=self.layout)
                #        graph.write(filename % tp, format=options.output, prog='neato')
                #    return tps
                #else:
                print("Generate map for single control:test...")
                # Build analysis lookup dict; we want a single color for each metabolite
                mini, maxi = min(abs(np.median(dsi.data)), 0), max(abs(np.median(dsi.data)), 0)
                mini, maxi = -1.0, +1.0  # Fudge; need an intelligent way to determine (2*median? 2*mean?)
                scale = utils.calculate_scale([mini, 0, maxi], [9, 1], out=np.around)  # rdbu9 scale

                for n, m in enumerate(dsi.entities[1]):
                    if m is not None:
                        ecol = utils.calculate_rdbu9_color(scale, dsi.data[0, n])
                        #print xref, ecol
                        if ecol is not None:
                            node_colors[m.id] = ecol

            graph = generator(pathways, options, self.m.db, analysis=node_colors)  # , layout=self.layout)
            self.status.emit('waiting')
            self.progress.emit(0.5)
            graph.write(filename, format=options.output, prog='neato')
            return None
        else:
            graph = generator(pathways, options, self.m.db)  # , layout=self.layout)
            self.status.emit('waiting')
            self.progress.emit(0.5)
            graph.write(filename, format=options.output, prog='neato')
            return None
Exemple #5
0
    def generate(self, input=None):
        dsi = input
        opener = register_openers()
        url = 'http://www.kegg.jp/kegg-bin/mcolor_pathway'

        data = io.StringIO()

        node_colors = {}
        if dsi:
            mini, maxi = min(abs(np.median(dsi.data)),
                             0), max(abs(np.median(dsi.data)), 0)
            mini, maxi = -2.0, +2.0  # Fudge; need an intelligent way to determine (2*median? 2*mean?)
            scale = utils.calculate_scale([mini, 0, maxi], [9, 1],
                                          out=np.around)  # rdbu9 scale

            #for n, m in enumerate(dsi.entities[1]):
            #    xref = self.get_xref( m )

            for n, m in enumerate(dsi.entities[1]):
                if m:

                    if 'LIGAND-CPD' in m.databases:
                        kegg_id = m.databases['LIGAND-CPD']
                        ecol = utils.calculate_rdbu9_color(
                            scale, dsi.data[0, n])
                        if kegg_id is not None and ecol is not None:
                            node_colors[kegg_id] = ecol

                    elif 'NCBI-GENE' in m.databases:
                        kegg_id = m.databases['NCBI-GENE']
                        ecol = utils.calculate_rdbu9_color(
                            scale, dsi.data[0, n])
                        if kegg_id is not None and ecol is not None:
                            node_colors[kegg_id] = ecol

        tmp = open(os.path.join(QDir.tempPath(), 'kegg-pathway-data.txt'), 'w')
        tmp.write('#hsa\tData\n')
        for k, c in list(node_colors.items()):
            tmp.write('%s\t%s\n' % (k, c[0]))
        tmp = open(os.path.join(QDir.tempPath(), 'kegg-pathway-data.txt'), 'r')

        values = {
            'map': self.kegg_pathway_t.text(),
            'mapping_list': tmp,
            'mode': 'color',
            'submit': 'Exec',
        }

        self.status.emit('waiting')

        datagen, headers = multipart_encode(values)
        # Create the Request object
        # Actually do the request, and get the response
        request = Request(url, datagen, headers)

        try:
            response = urlopen(request)
        except urllib.error.HTTPError as e:
            print(e)
            return

        return {'html': response.read()}
Exemple #6
0
    def generateGraph(self,
                      filename,
                      suggested_pathways=[],
                      compound_data=None,
                      gene_data=None,
                      protein_data=None,
                      format='svg'):
        # Build options-like structure for generation of graph
        # (compatibility with command line version, we need to fake it)
        options = Values()

        options._update_loose({
            'file':
            None,
            #'pathways': self.config.Read('/Pathways/Show'),
            #'not_pathways':'',
            'show_all':
            False,  # self.config.ReadBool('/Pathways/ShowAll'),
            'search':
            '',
            'cluster_by':
            self.config.get('/App/ClusterBy'),
            'show_enzymes':
            self.config.get('/App/ShowEnzymes'
                            ),  # self.config.ReadBool('/App/ShowEnzymes'),
            'show_secondary':
            self.config.get('/App/Show2nd'),
            'show_molecular':
            self.config.get('/App/ShowMolecular'),
            'show_network_analysis':
            self.config.get('/App/ShowAnalysis'),
            'highlightpathways':
            self.config.get('/App/HighlightPathways'),
            'highlightregions':
            self.config.get('/App/HighlightRegions'),
            'splines':
            'true',
            'focus':
            False,
            'show_pathway_links':
            self.config.get('/Pathways/ShowLinks'),
            # Always except when saving the file
            'output':
            format,
        })

        #pathway_ids = self.config.value('/Pathways/Show').split(',')
        if suggested_pathways:
            pathway_ids = [p.id for p in suggested_pathways.entities[1]]
        else:
            pathway_ids = []

        print(self.config.get('/Pathways/Show'))

        # Add the manually Shown pathways
        pathway_ids_show = self.config.get('/Pathways/Show')
        pathway_ids.extend(pathway_ids_show)

        # Now remove the Hide pathways
        pathway_ids_hide = self.config.get('/Pathways/Hide')
        pathway_ids = [p for p in pathway_ids if p not in pathway_ids_hide]

        # Convert pathways_ids to pathways
        pathways = [
            self.m.db.pathways[pid] for pid in pathway_ids
            if pid in list(self.m.db.pathways.keys())
        ]

        if pathway_ids == []:
            return None

        if compound_data or gene_data or protein_data:

            # Generate independent scales
            node_colors = {}

            for dsi in compound_data, gene_data, protein_data:
                if dsi == None:
                    continue
                #if self.m.data.analysis_timecourse:
                #    # Generate the multiple views
                #    tps = sorted( self.m.data.analysis_timecourse.keys(), key=int )
                #    # Insert counter variable into the filename
                #    filename = self.get_filename_with_counter(filename)
                #    print "Generate timecourse..."
                #    for tp in tps:
                #        print "%s" % tp
                #        graph = generator( pathways, options, self.m.db, analysis=self.m.data.analysis_timecourse[ tp ]) #, layout=self.layout)
                #        graph.write(filename % tp, format=options.output, prog='neato')
                #    return tps
                #else:
                print("Generate map for single control:test...")
                # Build analysis lookup dict; we want a single color for each metabolite
                mini, maxi = min(abs(np.median(dsi.data)),
                                 0), max(abs(np.median(dsi.data)), 0)
                mini, maxi = -1.0, +1.0  # Fudge; need an intelligent way to determine (2*median? 2*mean?)
                scale = utils.calculate_scale([mini, 0, maxi], [9, 1],
                                              out=np.around)  # rdbu9 scale

                for n, m in enumerate(dsi.entities[1]):
                    if m is not None:
                        ecol = utils.calculate_rdbu9_color(
                            scale, dsi.data[0, n])
                        #print xref, ecol
                        if ecol is not None:
                            node_colors[m.id] = ecol

            graph = generator(pathways,
                              options,
                              self.m.db,
                              analysis=node_colors)  # , layout=self.layout)
            self.status.emit('waiting')
            self.progress.emit(0.5)
            graph.write(filename, format=options.output, prog='neato')
            return None
        else:
            graph = generator(pathways, options,
                              self.m.db)  # , layout=self.layout)
            self.status.emit('waiting')
            self.progress.emit(0.5)
            graph.write(filename, format=options.output, prog='neato')
            return None