def test_import_warning(self): """Tests an error is thrown when the version is set wrong""" graph = BELGraph() # Much with stuff that would normally be set graph.graph[GRAPH_PYBEL_VERSION] = '0.0.0' graph_bytes = to_bytes(graph) with self.assertRaises(ImportVersionWarning) as cm: from_bytes(graph_bytes) self.assertEqual( import_version_message_fmt.format( '0.0.0', PYBEL_MINIMUM_IMPORT_VERSION), str(cm.exception))
def get_pathways_with_node(): """Return all pathways having a given node""" bel_nodes = request.args.getlist('node_selection[]') if not bel_nodes: abort(500, '"{}" is not a valid input for this input'.format(request.args)) pathways = set() for pathway in current_app.pathme_manager.get_all_pathways(): # Load networkX graph graph = from_bytes(pathway.blob) # Check if node is in the pathway for node in graph: if node.as_bel() not in bel_nodes: continue pathways.add(pathway) return render_template('pathway_table.html', pathways=pathways, DATABASE_URL_DICT=DATABASE_URL_DICT, DATABASE_STYLE_DICT=DATABASE_STYLE_DICT)
def get_all_pathway_graphs(self): """Get all pathway graphs. :rtype: list[pybel.BELGraph] """ return [ from_bytes(pathway.blob) for pathway in self.get_all_pathways() ]
def view_summary(graph_id): """Renders a page with the parsing errors for a given BEL script""" try: network = manager.get_network_by_id(graph_id) graph = from_bytes( network.blob, check_version=app.config.get('PYBEL_DS_CHECK_VERSION')) except: flask.flash("Problem getting graph {}".format(graph_id), category='error') return redirect(url_for('view_summary')) return render_graph_summary(graph_id, graph, api)
def view_upload(): """An upload form for a BEL script""" form = UploadForm() if not form.validate_on_submit(): return render_template('upload.html', form=form, current_user=current_user) log.info('uploading %s', form.file.data.filename) try: graph_bytes = form.file.data.read() graph = pybel.from_bytes(graph_bytes) except Exception as e: log.exception('gpickle error') flash('The given file was not able to be unpickled [{}]'.format( str(e)), category='error') return redirect(url_for('view_upload')) try: network = manager.insert_graph(graph) except IntegrityError: log.exception('integrity error') flash(integrity_message.format(graph.name, graph.version), category='error') manager.rollback() return redirect(url_for('view_upload')) except Exception as e: log.exception('upload error') flash("Error storing in database [{}]".format(e), category='error') return redirect(url_for('view_upload')) log.info('done uploading %s [%d]', form.file.data.filename, network.id) try: add_network_reporting(manager, network, current_user, graph.number_of_nodes(), graph.number_of_edges(), len(graph.warnings), precompiled=True, public=form.public.data) except IntegrityError: log.exception('integrity error') flash('problem with reporting service', category='warning') manager.rollback() return redirect(url_for('view_summary', graph_id=network.id))
def load_networks(self, check_version=True, force_reload=False, eager=False): """This function needs to get all networks from the graph cache manager and make a dictionary :param check_version: Should the version of the BELGraphs be checked from the database? Defaults to :code`True`. :type check_version: bool :param force_reload: Should all graphs be reloaded even if they have already been cached? :type force_reload: bool """ query = self.manager.session.query(Network.id, Network.blob) for network_id, network_blob in query.filter(Network.id not in self.networks).all(): try: log.debug('getting bytes from [%s]', network_id) graph = from_bytes(network_blob, check_version=check_version) except: log.exception("couldn't load from bytes [%s]", network_id) continue self.add_network(network_id, graph, force_reload=force_reload, eager=eager) log.info('universe has (%s nodes/%s edges)', self.universe.number_of_nodes(), self.universe.number_of_edges())
def get_network(self, network_id=None): """Gets a network by its ID or super network if identifier is not specified :param network_id: The internal ID of the network to get :type network_id: int :return: A BEL Graph :rtype: pybel.BELGraph """ if network_id is None: log.debug('got universe (%s nodes/%s edges)', self.universe.number_of_nodes(), self.universe.number_of_edges()) return self.universe if network_id not in self.networks: network = self.manager.session.query(Network).get(network_id) log.debug('getting bytes from [%s]', network_id) self.add_network(network_id, from_bytes(network.blob)) result = self.networks[network_id] log.debug('got network [%s] (%s nodes/%s edges)', network_id, result.number_of_nodes(), result.number_of_edges()) return result
def as_bel(self): """Get this network and loads it into a :class:`BELGraph`. :rtype: pybel.BELGraph """ return from_bytes(self.blob)
def test_slushy_bytes(self): graph_bytes = to_bytes(self.slushy_graph) graph = from_bytes(graph_bytes) self.bel_slushy_reconstituted(graph)
def test_thorough_bytes(self): graph_bytes = to_bytes(self.thorough_graph) graph = from_bytes(graph_bytes) self.bel_thorough_reconstituted(graph)
def test_example_bytes(self): graph_bytes = to_bytes(sialic_acid_graph) graph = from_bytes(graph_bytes) self.help_test_equal(graph)
def test_example_bytes(self): """Test the round-trip through bytes.""" graph_bytes = to_bytes(sialic_acid_graph) graph = from_bytes(graph_bytes) self._help_test_equal(graph)
def view_analysis_uploader(network_id): """Views the results of analysis on a given graph""" form = DifferentialGeneExpressionForm() if not form.validate_on_submit(): name, = manager.session.query( Network.name).filter(Network.id == network_id).one() return render_template('analyze_dgx.html', form=form, network_name=name) log.info('analyzing %s: %s with CMPA (%d trials)', form.file.data.filename, form.description.data, form.permutations.data) t = time.time() df = pandas.read_csv(form.file.data) gene_column = form.gene_symbol_column.data data_column = form.log_fold_change_column.data if gene_column not in df.columns: raise ValueError('{} not a column in document'.format(gene_column)) if data_column not in df.columns: raise ValueError('{} not a column in document'.format(data_column)) df = df.loc[df[gene_column].notnull(), [gene_column, data_column]] data = {k: v for _, k, v in df.itertuples()} network = manager.get_network_by_id(network_id) graph = pybel.from_bytes(network.blob) remove_nodes_by_namespace(graph, {'MGI', 'RGD'}) collapse_by_central_dogma_to_genes(graph) rewire_variants_to_genes(graph) overlay_type_data(graph, data, LABEL, GENE, 'HGNC', overwrite=False, impute=0) candidate_mechanisms = generation.generate_bioprocess_mechanisms( graph, LABEL) scores = npa.calculate_average_npa_on_subgraphs( candidate_mechanisms, LABEL, runs=form.permutations.data) log.info('done running CMPA in %.2fs', time.time() - t) experiment = Experiment( description=form.description.data, source_name=form.file.data.filename, source=pickle.dumps(df), result=pickle.dumps(scores), permutations=form.permutations.data, user=current_user, ) experiment.network = network manager.session.add(experiment) manager.session.commit() return redirect( url_for('view_analysis_results', analysis_id=experiment.id))