Block Model

class pygna.block_model.BlockModel(object)[source]
create_graph() → None[source]

Create a graph from the parameters passed in the constructor of the class

Example

>>> bm = BlockModel(np.array(config["BlockModel"]["matrix"]), n_nodes=config["BlockModel"]["n_nodes"], nodes_percentage=config["BlockModel"]["nodes_percentage"])
>>> bm.create_graph()
plot_graph(output_folder: str) → None[source]

Plot the block model graph

Parameters:output_folder – the folder where to save the result

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> bm.plot_graph("block_model_path.pdf")
set_bm(block_model_matrix: pandas.core.frame.DataFrame) → None[source]

Change block model matrix used in the class

Parameters:block_model_matrix – the block model matrix

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> bmm = pd.DataFrame(mydata_matrix)
>>> bm.set_bm(bmm)
set_nodes(nodes_names: list) → None[source]

Set the nodes name of the block model

Parameters:nodes_names – the names list

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> nodes = list("A", "B", "C")
>>> bm.set_nodes(nodes)
set_nodes_in_block(nodes_in_block: int) → None[source]

Set the nodes number in the block model

Parameters:nodes_in_block – the number of nodes in the block list

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> bm.set_nodes_in_block(1000)
set_nodes_in_block_percentage(nodes_percentage: list) → None[source]

Pass the percentage of nodes in each block as a list, for example [0.5, 0.5]

Parameters:nodes_percentage – percentage of the nodes

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> bm.set_nodes_in_block_percentage([0.5, 0.5])
write_cluster_genelist(output_file: str) → None[source]

Save the gene list to a GMT file

Parameters:output_file – the output path where to save the results

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> bm.write_cluster_genelist("genes.gmt")
write_network(output_file: str) → None[source]

Save the network on a given file

Parameters:output_file – the output path where to save the results

Example

>>> p = 0.5
>>> n_nodes = 1000
>>> matrix = np.array([[1, 2], [3, 4]])
>>> bm = BlockModel(matrix, n_nodes=n_nodes, nodes_percentage=[p, 1 - p])
>>> bm.write_network("network.tsv")
pygna.block_model.generate_graph_from_sm(n_nodes: int, block_model: pandas.core.frame.DataFrame, nodes_in_block: list = False, node_names: list = None, nodes_percentage: list = None) → networkx.classes.graph.Graph[source]

This function creates a graph with n_nodes number of vertices and a matrix block_model that describes the intra e inter-block connectivity. The nodes_in_block is parameter, list, to control the number of nodes in each cluster

Parameters:
  • n_nodes – the number of nodes in the block model
  • block_model – the block model to elaborate
  • nodes_in_block – the list of nodes in the block model
  • node_names – the list of names in the block model
  • nodes_percentage – the percentage of nodes to use for the calculations, passed through a list for example [0.5, 0.5]

Example

>>> bm = pd.DataFrame(mydata_matrix)
>>> nodes = list("A","B","C")
>>> graph = generate_graph_from_sm(n_nodes, bm, nodes_in_block, nodes, nodes_percentage)
pygna.block_model.plot_bm_graph(graph: networkx.classes.graph.Graph, block_model: pandas.core.frame.DataFrame, output_folder: str = None) → None[source]

Save the graph on a file

Parameters:
  • graph – the graph with name of the nodes
  • block_model – the block model
  • output_folder – the folder where to save the file

Example

>>> bm = pd.DataFrame(mydata_matrix)
>>> graph = nx.complete_graph(100)
>>> plot_bm_graph(graph, bm, output_folder="./results/")
pygna.block_model.generate_sbm_network(input_file: yaml configuration file) → None[source]

This function generates a simulated network, using the block model matrix given as input and saves both the network and the cluster nodes. All parameters must be specified in a yaml file. This function allows to create network and geneset for any type of SBM