2.2. Computational Method
Python Code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import networkx as nx
from scipy.spatial import distance
import seaborn as sns
from scipy.integrate import odeint
class TopologicalGeneNetwork:
def __init__(self, n_genes=5):
self.n_genes = n_genes
self.network = self._create_network()
def _create_network(self):
# Create a directed graph representing gene regulatory network
G = nx.DiGraph()
# Add nodes (genes)
for i in range(self.n_genes):
G.add_node(i, expression=np.random.random())
# Add edges (regulatory interactions) with weights
for i in range(self.n_genes):
for j in range(self.n_genes):
if i != j and np.random.random() < 0.3: # 30% chance of interaction
G.add_edge(i, j, weight=np.random.normal())
return G
def visualize_network(self):
plt.figure(figsize=(10, 8))
pos = nx.spring_layout(self.network)
# Draw nodes
nx.draw_networkx_nodes(self.network, pos,
node_color=[self.network.nodes[n]['expression'] for n in self.network.nodes],
node_size=1000,
cmap=plt.cm.viridis)
# Draw edges with weights determining thickness
edges = self.network.edges()
weights = [self.network[u][v]['weight'] for u, v in edges]
nx.draw_networkx_edges(self.network, pos,
edge_color=weights,
edge_cmap=plt.cm.RdBu,
width=2,
edge_vmin=-1,
edge_vmax=1,
arrows=True,
arrowsize=20)
plt.title("Gene Regulatory Network Topology")
plt.colorbar(plt.cm.ScalarMappable(cmap=plt.cm.viridis),
label="Gene Expression Level")
plt.axis('off')
return plt.gcf()
class ExpressionLandscape:
def __init__(self, resolution=50):
self.resolution = resolution
def potential_function(self, X, Y):
"""
Implementation of equation (3) from methodology: E(x)
Simulates a complex expression landscape with multiple stable states
"""
return (1 - X)**2 * np.exp(-X**2 - (Y + 1)**2) - \
(X/5 - X**3 - Y**5) * np.exp(-X**2 - Y**2) + \
0.5 * (X**2 + Y**2)
def visualize_landscape(self):
x = np.linspace(-3, 3, self.resolution)
y = np.linspace(-3, 3, self.resolution)
X, Y = np.meshgrid(x, y)
Z = self.potential_function(X, Y)
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
surface = ax.plot_surface(X, Y, Z, cmap='viridis',
linewidth=0, antialiased=True)
ax.set_xlabel('Gene 1 Expression')
ax.set_ylabel('Gene 2 Expression')
ax.set_zlabel('Potential Energy')
ax.set_title('Gene Expression Landscape')
fig.colorbar(surface, label='Energy')
return fig
class TADStructure:
def __init__(self, size=50):
self.size = size
self.interaction_matrix = self._generate_tad_matrix()
def _generate_tad_matrix(self):
""" Implementation of equation (5) from methodology: I(i,j)
"""matrix = np.zeros((self.size, self.size))
# Generate two TADs
tad_positions = [(0, 25), (25, 50)]
for start, end in tad_positions:
for i in range(start, end):
for j in range(start, end):
# Higher interaction frequency within TADs
distance = abs(i - j)
matrix[i, j] = np.exp(-distance/10) + 0.1 * np.random.random()
return matrix
def visualize_tads(self):
plt.figure(figsize=(10, 8))
sns.heatmap(self.interaction_matrix,
cmap='YlOrRd',
xticklabels=False,
yticklabels=False)
plt.title('Topologically Associating Domains (TADs)')
plt.xlabel('Genomic Position')
plt.ylabel('Genomic Position')
return plt.gcf()
def main():
# Create and visualize gene regulatory network
grn = TopologicalGeneNetwork(n_genes=8)
fig1 = grn.visualize_network()
fig1.savefig('gene_network.png')
# Create and visualize expression landscape
landscape = ExpressionLandscape()
fig2 = landscape.visualize_landscape()
fig2.savefig('expression_landscape.png')
# Create and visualize TAD structure
tads = TADStructure()
fig3 = tads.visualize_tads()
fig3.savefig('tad_structure.png')
plt.show()
if __name__ == "__main__":
main()