def generate_project_similarity_plots(self): plots = [] identifier = "Similarities" title = "Similarities to other Projects" plots.append( viz.get_table(self.similar_projects, identifier + ' table', title + ' table')) plots.append( viz.get_sankey_plot(self.similar_projects, identifier, args={ 'source': 'current', 'target': 'other', 'weight': 'similarity_pearson', 'orientation': 'h', 'valueformat': '.0f', 'width': 800, 'height': 800, 'font': 12, 'title': title })) plots.append(self.get_similarity_network()) return plots
def get_project_information(driver, project_id): query_name = 'project_graph' res = pd.DataFrame() try: query = '' parameters = {'project_id': project_id} data_upload_cypher = get_data_upload_queries() queries = data_upload_cypher[query_name]['query'].split(';')[:-1] for query in queries: res = connector.getCursorData(driver, query + ';', parameters=parameters) except Exception as err: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logger.error( "Error: {}. Creating analytical samples: Query name ({}) - Query ({}), error info: {}, file: {},line: {}" .format(err, query_name, query, sys.exc_info(), fname, exc_tb.tb_lineno)) if not res.empty: res = viz.get_table( res, identifier='new_project', title='Data Uploaded for Project {}'.format(project_id)) return res
def generate_project_attributes_plot(self): project_df = self.to_dataframe() project_df = project_df.drop(['similar_projects', 'overlap'], axis=1) identifier = "Project info" title = "Project: {} information".format(self.name) plot = [viz.get_table(project_df, identifier, title)] return plot
def get_knowledge_graph_plot(self): if self.graph is None: self.generate_knowledge_graph() title = 'Project {} Knowledge Graph'.format(self.identifier) if self.data is not None: if 'name' in self.data: title = 'Project {} Knowledge Graph'.format(self.data['name']) args = {'title': title, 'node_properties': {}, 'width': 2600, 'height': 2600, 'maxLinkWidth': 7, 'maxRadius': 20} color_selector = "{'selector': '[name = \"KEY\"]', 'style': {'font-size': 10, 'background-color':'VALUE','width': 50,'height': 50,'background-image':'/assets/graph_icons/ENTITY.png','background-fit': 'cover','opacity':OPACITY}}" stylesheet = [{'selector': 'node', 'style': {'label': 'data(name)', 'z-index': 9999}}, {'selector': 'edge', 'style': {'label': 'data(type)', 'curve-style': 'unbundled-bezier', 'control-point-distance': '20px', 'control-point-weight': '0.7', 'z-index': 5000, 'line-color': '#bdbdbd', 'opacity': 0.2, 'font-size': '7px'}}] layout = {'name': 'circle'} #stylesheet.extend([{'selector':'[weight < 0]', 'style':{'line-color':'#3288bd'}},{'selector':'[width > 0]', 'style':{'line-color':'#d73027'}}]) for n in self.nodes: color = self.nodes[n]['color'] image = self.nodes[n]['type'] opacity = 0.3 if image == 'Module' or image == 'Group' else 1 stylesheet.append(ast.literal_eval(color_selector.replace("KEY", n.replace("'", "")).replace("VALUE", color).replace("ENTITY", image).replace("OPACITY", str(opacity)))) stylesheet.extend([{'selector': '[weight < 0]', 'style': {'line-color': '#4add1'}}, {'selector': '[weight > 0]', 'style': {'line-color': '#d6604d'}}]) args['stylesheet'] = stylesheet args['layout'] = layout nodes_table, edges_table = viz.network_to_tables(self.graph) nodes_fig_table = viz.get_table(nodes_table, identifier=self.identifier+"_nodes_table", title="Nodes table") edges_fig_table = viz.get_table(edges_table, identifier=self.identifier+"_edges_table", title="Edges table") cy_elements, mouseover_node = utils.networkx_to_cytoscape(self.graph) #args['mouseover_node'] = mouseover_node net = {"notebook": [cy_elements, stylesheet, layout], "app": viz.get_cytoscape_network(cy_elements, self.identifier, args), "net_tables": (nodes_fig_table, edges_fig_table), "net_json": json_graph.node_link_data(self.graph)} return net
def get_project_information(driver, project_id): query_name = 'project_graph' queries = [] data = [] res = [] try: query = '' data_upload_cypher = get_data_upload_queries() for section in data_upload_cypher[query_name]: code = section['query'] queries.extend( code.replace("PROJECTID", project_id).split(';')[0:-1]) for query in queries: result = connector.sendQuery(driver, query + ";")[0] data.append(result) except Exception as err: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] logger.error( "Error: {}. Creating analytical samples: Query name ({}) - Query ({}), error info: {}, file: {},line: {}" .format(err, query_name, query, sys.exc_info(), fname, exc_tb.tb_lineno)) if data: for i, j in enumerate(data): df = pd.DataFrame([data[i]], columns=data[i].keys()) header = '_'.join(df.columns[0].split('_', 1)[1:]).capitalize() df.rename(columns={df.columns[0]: 'project'}, inplace=True) res.append( viz.get_table(df, identifier='new_project_{}'.format(header), args={ 'title': '{} data uploaded for project {}'.format( header, project_id) })) else: res = None logger.error( "Error: No data was uploaded for project: {}. Review your experimental design and data files and the logs for errors." .format(project_id)) return res
def generate_overlap_plots(self): plots = [] identifier = "Overlap" title = "Protein Identification Overlap" plots.append( viz.get_table(self.overlap, identifier + ' table', title + ' table')) if self.overlap is not None: for i, row in self.overlap.iterrows(): ntitle = title + ":\n" + row['project1_name'] + " - " + row[ 'project2_name'] + "(overlap similarity: " + str( row['similarity']) + ")" plot = viz.plot_2_venn_diagram(row['from'], row['to'], row['project1_unique'], row['project2_unique'], row['intersection'], identifier=identifier + str(i), args={'title': ntitle}) plots.append(plot) return plots
def get_plot(self, name, identifier): plot = [] if len(self.result) >= 1: if name == "basicTable": colors = ('#C2D4FF', '#F5F8FF') columns = None rows = None figure_title = 'Basic table' if "colors" in self.args: colors = self.args["colors"] if "cols" in self.args: columns = self.args["cols"] if "rows" in self.args: rows = self.args["rows"] if "title" in self.args: figure_title = self.args["title"] for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args["title"] + id[ 0] + " vs " + id[1] plot.append( viz.get_table(self.result[id], identifier, args={ 'title': figure_title, 'colors': colors, 'cols': columns, 'rows': rows, 'width': 800, 'height': 1500, 'font': 12 })) if name == "multiTable": for id in self.result: plot.append( viz.get_multi_table(self.result[id], identifier, self.args["title"])) elif name == "barplot": x_title = "x" y_title = "y" if "x_title" in self.args: x_title = self.args["x_title"] if "y_title" in self.args: y_title = self.args["y_title"] for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args["title"] = figure_title plot.append( viz.get_barplot(self.result[id], identifier, self.args)) elif name == "facetplot": x_title = "x" y_title = "y" plot_type = "bar" if "x_title" not in self.args: self.args["x_title"] = x_title if "y_title" not in self.args: self.args["y_title"] = y_title if "plot_type" not in self.args: self.args["plot_type"] = plot_type for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args['title'] = figure_title plot.append( viz.get_facet_grid_plot(self.result[id], identifier, self.args)) elif name == "scatterplot": x_title = "x" y_title = "y" if "x_title" in self.args: x_title = self.args["x_title"] if "y_title" in self.args: y_title = self.args["y_title"] for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args['title'] = figure_title plot.append( viz.get_scatterplot(self.result[id], identifier, self.args)) elif name == 'pca': x_title = "x" y_title = "y" if "x_title" in self.args: x_title = self.args["x_title"] if "y_title" in self.args: y_title = self.args["y_title"] for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args['title'] = figure_title plot.append( viz.get_pca_plot(self.result[id], identifier, self.args)) elif name == "volcanoplot": alpha = 0.05 lfc = 1.0 if "alpha" not in self.args: self.args["alpha"] = alpha if "lfc" not in self.args: self.args["lfc"] = lfc for pair in self.result: signature = self.result[pair] self.args["title"] = self.args['title'] + " " + pair[ 0] + " vs " + pair[1] p = viz.run_volcano( signature, identifier + "_" + pair[0] + "_vs_" + pair[1], self.args) plot.extend(p) elif name == 'network': source = 'source' target = 'target' if "source" not in self.args: self.args["source"] = source if "target" not in self.args: self.args["target"] = target for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args["title"] + id[ 0] + " vs " + id[1] else: figure_title = self.args["title"] self.args["title"] = figure_title plot.append( viz.get_network(self.result[id], identifier, self.args)) elif name == "heatmap": for id in self.result: if not self.result[id].empty: if isinstance(id, tuple): identifier = identifier + "_" + id[ 0] + "_vs_" + id[1] figure_title = self.args["title"] + id[ 0] + " vs " + id[1] else: figure_title = self.args["title"] self.args["title"] = figure_title plot.append( viz.get_complex_heatmapplot( self.result[id], identifier, self.args)) elif name == "mapper": for id in self.result: labels = {} if "labels" not in self.args: self.args["labels"] = labels if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] plot.append( viz.getMapperFigure(self.result[id], identifier, title=figure_title, labels=self.args["labels"])) elif name == "scatterplot_matrix": for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args["title"] = figure_title plot.append( viz.get_scatterplot_matrix(self.result[id], identifier, self.args)) elif name == "distplot": for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args["title"] = figure_title plot.extend( viz.get_distplot(self.result[id], identifier, self.args)) elif name == "violinplot": for id in self.result: if isinstance(id, tuple): identifier = identifier + "_" + id[0] + "_vs_" + id[1] figure_title = self.args['title'] + id[ 0] + " vs " + id[1] else: figure_title = self.args['title'] self.args["title"] = figure_title plot.extend( viz.get_violinplot(self.result[id], identifier, self.args)) elif name == "polar": for id in self.result: figure_title = self.args['title'] plot.append( viz.get_polar_plot(self.result[id], identifier, self.args)) elif name == "km": for id in self.result: plot.append( viz.get_km_plot(self.result[id], identifier, self.args)) elif name == "wgcnaplots": start = time.time() data = {} wgcna_data = self.result if 'drop_cols_exp' in self.args and 'drop_cols_cli' in self.args: if 'wgcna' in wgcna_data and wgcna_data[ 'wgcna'] is not None: for dtype in wgcna_data['wgcna']: data = wgcna_data['wgcna'][dtype] plot.extend( viz.get_WGCNAPlots(data, identifier + "-" + dtype)) print('WGCNA-plot', time.time() - start) elif name == 'ranking': for id in self.result: plot.append( viz.get_ranking_plot(self.result[id], identifier, self.args)) elif name == 'qcmarkers_boxplot': for id in self.result: plot.append( viz.get_boxplot_grid(self.result[id], identifier, self.args)) elif name == 'clustergrammer': for id in self.result: plot.append( viz.get_clustergrammer_plot(self.result[id], identifier, self.args)) elif name == 'cytonet': for id in self.result: plot.append( viz.get_cytoscape_network(self.result[id], identifier, self.args)) elif name == 'wordcloud': for id in self.result: plot.append( viz.get_wordcloud(self.result[id], identifier, self.args)) self.update_plots({identifier: plot}) return plot
def get_knowledge_graph_plot(self, summarize=True): if self.graph is None: self.generate_knowledge_graph() selected_nodes = [] if summarize and len(self.graph.nodes()) > 1: centrality = nx.betweenness_centrality(self.graph, k=None, weight='weight', normalized=False) #centrality = nx.pagerank(G, alpha=0.95, weight='weight') nx.set_node_attributes(self.graph, centrality, 'centrality') sorted_centrality = sorted(centrality.items(), key=itemgetter(1), reverse=True) for node_type in self.entities: nodes = [ x for x, y in self.graph.nodes(data=True) if 'type' in y and y['type'] == node_type and x not in self.keep_nodes ] selected_nodes.extend( [n for n, c in sorted_centrality if n in nodes][15:]) if len(selected_nodes) > 0: self.graph.remove_nodes_from(selected_nodes) title = 'Project {} Knowledge Graph'.format(self.identifier) if self.data is not None: if 'name' in self.data: title = 'Project {} Knowledge Graph'.format(self.data['name']) args = { 'title': title, 'node_properties': {}, 'width': 2000, 'height': 2000, 'maxLinkWidth': 7, 'maxRadius': 20 } color_selector = "{'selector': '[name = \"KEY\"]', 'style': {'font-size': '7px', 'text-opacity': 0.8, 'background-color':'VALUE','width': 50,'height': 50,'background-image':'/assets/graph_icons/ENTITY.png','background-fit': 'cover','opacity':OPACITY}}" stylesheet = [{ 'selector': 'node', 'style': { 'label': 'data(name)', 'opacity': 0.7 } }, { 'selector': 'edge', 'style': { 'label': 'data(type)', 'curve-style': 'unbundled-bezier', 'control-point-distance': '30px', 'control-point-weight': '0.7', 'z-index': 5000, 'line-color': '#bdbdbd', 'opacity': 0.2, 'font-size': '2.5px', 'text-opacity': 1, 'font-style': "normal", 'font-weight': "normal" } }] layout = { 'name': 'cose', 'idealEdgeLength': 100, 'nodeOverlap': 20, 'refresh': 20, 'randomize': False, 'componentSpacing': 100, 'nodeRepulsion': 400000, 'edgeElasticity': 100, 'nestingFactor': 5, 'gravity': 80, 'numIter': 1000, 'initialTemp': 200, 'coolingFactor': 0.95, 'minTemp': 1.0 } stylesheet.extend([{ 'selector': '[weight < 0]', 'style': { 'line-color': '#3288bd' } }, { 'selector': '[weight > 0]', 'style': { 'line-color': '#d73027' } }]) for n, attr in self.graph.nodes(data=True): color = self.default_color image = '' if 'color' in attr: color = attr['color'] if 'type' in attr: image = attr['type'] opacity = 0.3 if image == 'Module' or image == 'Group' else 1 stylesheet.append( ast.literal_eval( color_selector.replace("KEY", n.replace("'", "")).replace( "VALUE", color).replace("ENTITY", image).replace("OPACITY", str(opacity)))) stylesheet.extend([{ 'selector': 'node', 'style': { 'width': 'mapData(centrality, 0, 1, 15, 30)', 'height': 'mapData(centrality, 0, 1, 15, 30)' } }]) args['stylesheet'] = stylesheet args['layout'] = layout G = self.graph.copy() if G.has_node('Regulated'): G.remove_node('Regulated') nodes_table, edges_table = viz.network_to_tables(G, source='node1', target='node2') nodes_fig_table = viz.get_table(nodes_table, identifier=self.identifier + "_nodes_table", args={'title': "Nodes table"}) edges_fig_table = viz.get_table(edges_table, identifier=self.identifier + "_edges_table", args={'title': "Edges table"}) cy_elements, mouseover_node = utils.networkx_to_cytoscape(G) #args['mouseover_node'] = mouseover_node net = { "notebook": [cy_elements, stylesheet, layout], "app": viz.get_cytoscape_network(cy_elements, self.identifier, args), "net_tables": (nodes_table, edges_table), "net_tables_viz": (nodes_fig_table, edges_fig_table), "net_json": json_graph.node_link_data(G) } return net