import networkx as nx
import itertools
import matplotlib.pyplot as plt
import scipy
# Define the possible nucleotides
= ['A', 'C', 'G', 'T']
nucleotides
# Generate all possible DNA sequences of length 3
= [''.join(seq) for seq in itertools.product(nucleotides, repeat=3)]
sequences
# Initialize an empty graph
= nx.Graph()
G
# Add nodes for each possible sequence
G.add_nodes_from(sequences)
# Function to compute Hamming distance
def hamming_distance(seq1, seq2):
return sum(c1 != c2 for c1, c2 in zip(seq1, seq2))
# Add edges between nodes with Hamming distance of 1
for i in range(len(sequences)):
for j in range(i + 1, len(sequences)):
if hamming_distance(sequences[i], sequences[j]) == 1:
G.add_edge(sequences[i], sequences[j])
# Draw the graph
= nx.kamada_kawai_layout(G) # positions for all nodes
pos =True, node_color='black', edge_color='gray', node_size=400, font_size=10)
nx.draw(G, pos, with_labels
# Display the plot
"graph.svg") plt.savefig(
# Define the possible nucleotides
= ['A', 'C', 'G', 'T']
nucleotides
# Generate all possible DNA sequences of length 3
= [''.join(seq) for seq in itertools.product(nucleotides, repeat=2)]
sequences
# Initialize an empty graph
= nx.Graph()
G
# Add nodes for each possible sequence
G.add_nodes_from(sequences)
# Function to compute Hamming distance
def hamming_distance(seq1, seq2):
return sum(c1 != c2 for c1, c2 in zip(seq1, seq2))
# Add edges between nodes with Hamming distance of 1
for i in range(len(sequences)):
for j in range(i + 1, len(sequences)):
if hamming_distance(sequences[i], sequences[j]) == 1:
G.add_edge(sequences[i], sequences[j])
# Draw the graph
= nx.spring_layout(G) # positions for all nodes
pos =True, node_color='grey', edge_color='gray', node_size=400, font_size=10)
nx.draw(G, pos, with_labels
# Display the plot
"graph2.svg") plt.savefig(
=True, node_color='black', edge_color='gray', node_size=800, font_size=11)
nx.draw(G, pos, with_labels"graph2.svg") plt.savefig(