Skip to content

Instantly share code, notes, and snippets.

@Midnighter
Created April 5, 2013 21:14
Show Gist options
  • Save Midnighter/5322677 to your computer and use it in GitHub Desktop.
Save Midnighter/5322677 to your computer and use it in GitHub Desktop.
Simple example of parsing a RegulonDB dataset into a network.
{
"metadata": {
"name": "simple_tf_gene_network"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import networkx as nx"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"with open(\"network_tf_gene.txt\", \"r\") as file_h:\n",
" content = file_h.readlines()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The data set provided by RegulonDB is a tab-delimited file with four \"columns\". The first is the regulating transcription factor (TF), the second the regulated gene, the third is the effect of the regulatory action, and the fourth is the experimental evidence which we ignore for now."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"trn = nx.DiGraph()\n",
"for line in content:\n",
" # ignore empty lines\n",
" if not line:\n",
" continue\n",
" # ignore comments\n",
" if line.startswith(\"#\"):\n",
" continue\n",
" tmp = line.split(\"\\t\")\n",
" if len(tmp) != 4:\n",
" continue\n",
" trn.add_edge(tmp[0], tmp[1], effect=tmp[2])"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The TF-gene network has"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(trn)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 6,
"text": [
"1858"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"nodes and"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"trn.size()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 7,
"text": [
"3922"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"links."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"components = nx.connected_components(trn.to_undirected())"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The network consists of"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(components)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 10,
"text": [
"23"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"components with the largest connected component of"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"len(components[0])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 11,
"text": [
"1755"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"nodes."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment