Skip to main content
Became Hot Network Question
I think there was an indentation error that broke the code
Source Link
AkselA
  • 223
  • 1
  • 9
import random
import os
import csv


# Function that counts number of dissimilarities
# between two sequences
def diss(x, y):
    n = 0
    for a, b in zip(x, y):
        if a != b:
            n += 1
    return (n)


# Function that mutates a string (Dna) Gen times.
# On average 0.75 mutations per generation, as the resampling will
# pick the existing nucleotide in one out of four times.
def mutate(Dna, Gen):
    # Store a copy of the original DNA sequence that
    # the mutated sequence can be compared to.
    Dna_o = Dna[:]
    Len = len(Dna)
    Dis = []
    
    # For loop that mutates the sequence Gen times and calculates the
    # dissimilarity after each mutation.
    for _ in range(Gen):  
        # Random location for mutation
        Mut = random.randint(0, Len-1)
        # Randomly selected nucleotide
        Dna[Mut] = random.choice(['A', 'T', 'G', 'C'])
        # Calculate proportional dissimalirity
        Dis.append(diss(Dna, Dna_o)/Len)
    
    # Print proportional dissimilarities.
    return (Dis)


# Iterate (repeat) the mutate function i times
# Outputs a nested list
def mutate_i(Dna, Gen, i):
    Mat = []
    for _ in range(i):
        Mat.append(mutate(Dna, Gen))
    return (Mat)


# Function for helping export simulation result as CSV file.
def export_mutate(Mutate_mat, Filename="mutation_simulation.csv"):
    # Add a count of the generations
    Mutate_mat.insert(0, list(range(1, len(Mutate_mat[0])+1)))
    # Transpose the list (swap rows and columns)
    Mutate_mat = list(map(list, zip(*Mutate_mat)))
    
    # Add headers
    header = ["Generation"]
    for i in range(1, len(Mutate_mat[0])):
        header.append("sim_" + str(i))
    
    Mutate_mat.insert(0, header)
    
    # Export simulation data as a CSV file that can be imported to
    # Google Sheets
    with open(Filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerows(Mutate_mat)
    
    # Print the path to the CSV file
    return (os.path.join(os.getcwd(), Filename))


# # # Simulation start

# Original DNA sequence
Dna = list("ATGC" * 4)

# Set random seed so the simulation is repeatable
random.seed(1)

# Generate four simulations, each ten generations long
Mutate_mat = mutate_i(Dna, 12, 4)

# Export simulation results
export_mutate(Mutate_mat)

import random
import os
import csv


# Function that counts number of dissimilarities
# between two sequences
def diss(x, y):
    n = 0
    for a, b in zip(x, y):
        if a != b:
            n += 1
    return (n)


# Function that mutates a string (Dna) Gen times.
# On average 0.75 mutations per generation, as the resampling will
# pick the existing nucleotide in one out of four times.
def mutate(Dna, Gen):
    # Store a copy of the original DNA sequence that
    # the mutated sequence can be compared to.
    Dna_o = Dna[:]
    Len = len(Dna)
    Dis = []

    # For loop that mutates the sequence Gen times and calculates the
    # dissimilarity after each mutation.
    for _ in range(Gen):
        # Random location for mutation
        Mut = random.randint(0, Len-1)
        # Randomly selected nucleotide
        Dna[Mut] = random.choice(['A', 'T', 'G', 'C'])
        # Calculate proportional dissimalirity
        Dis.append(diss(Dna, Dna_o)/Len)
    
    # Print proportional dissimilarities.
    return (Dis)


# Iterate (repeat) the mutate function i times
# Outputs a nested list
def mutate_i(Dna, Gen, i):
    Mat = []
    for _ in range(i):
        Mat.append(mutate(Dna, Gen))
    return (Mat)


# Function for helping export simulation result as CSV file.
def export_mutate(Mutate_mat, Filename="mutation_simulation.csv"):
    # Add a count of the generations
    Mutate_mat.insert(0, list(range(1, len(Mutate_mat[0])+1)))
    # Transpose the list (swap rows and columns)
    Mutate_mat = list(map(list, zip(*Mutate_mat)))
    
    # Add headers
    header = ["Generation"]
    for i in range(1, len(Mutate_mat[0])):
        header.append("sim_" + str(i))
    
    Mutate_mat.insert(0, header)
    
    # Export simulation data as a CSV file that can be imported to
    # Google Sheets
    with open(Filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerows(Mutate_mat)
    
    # Print the path to the CSV file
    return (os.path.join(os.getcwd(), Filename))


# # # Simulation start

# Original DNA sequence
Dna = list("ATGC" * 4)

# Set random seed so the simulation is repeatable
random.seed(1)

# Generate four simulations, each ten generations long
Mutate_mat = mutate_i(Dna, 12, 4)

# Export simulation results
export_mutate(Mutate_mat)

import random
import os
import csv


# Function that counts number of dissimilarities
# between two sequences
def diss(x, y):
    n = 0
    for a, b in zip(x, y):
        if a != b:
            n += 1
    return (n)


# Function that mutates a string (Dna) Gen times.
# On average 0.75 mutations per generation, as the resampling will
# pick the existing nucleotide in one out of four times.
def mutate(Dna, Gen):
    # Store a copy of the original DNA sequence that
    # the mutated sequence can be compared to.
    Dna_o = Dna[:]
    Len = len(Dna)
    Dis = []
    
    # For loop that mutates the sequence Gen times and calculates the
    # dissimilarity after each mutation.
    for _ in range(Gen):  
        # Random location for mutation
        Mut = random.randint(0, Len-1)
        # Randomly selected nucleotide
        Dna[Mut] = random.choice(['A', 'T', 'G', 'C'])
        # Calculate proportional dissimalirity
        Dis.append(diss(Dna, Dna_o)/Len)
    
    # Print proportional dissimilarities.
    return (Dis)


# Iterate (repeat) the mutate function i times
# Outputs a nested list
def mutate_i(Dna, Gen, i):
    Mat = []
    for _ in range(i):
        Mat.append(mutate(Dna, Gen))
    return (Mat)


# Function for helping export simulation result as CSV file.
def export_mutate(Mutate_mat, Filename="mutation_simulation.csv"):
    # Add a count of the generations
    Mutate_mat.insert(0, list(range(1, len(Mutate_mat[0])+1)))
    # Transpose the list (swap rows and columns)
    Mutate_mat = list(map(list, zip(*Mutate_mat)))
    
    # Add headers
    header = ["Generation"]
    for i in range(1, len(Mutate_mat[0])):
        header.append("sim_" + str(i))
    
    Mutate_mat.insert(0, header)
    
    # Export simulation data as a CSV file that can be imported to
    # Google Sheets
    with open(Filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerows(Mutate_mat)
    
    # Print the path to the CSV file
    return (os.path.join(os.getcwd(), Filename))


# # # Simulation start

# Original DNA sequence
Dna = list("ATGC" * 4)

# Set random seed so the simulation is repeatable
random.seed(1)

# Generate four simulations, each ten generations long
Mutate_mat = mutate_i(Dna, 12, 4)

# Export simulation results
export_mutate(Mutate_mat)

Used pycodestyle to catch a few lints
Source Link
AkselA
  • 223
  • 1
  • 9
import random
import os
import csv 


# Function that counts number of dissimilarities
# between two sequences
def diss(x, y):
    n = 0
    for a, b in zip(x, y):
        if a != b:
            n += 1
    return (n) 


# Function that mutates a string (Dna) Gen times.
# On average 0.75 mutations per generation, as the resampling will 
# pick the existing nucleotide in one out of four times.
def mutate(Dna, Gen):
    # Store a copy of the original DNA sequence that
    # the mutated sequence can be compared to.
    Dna_o = Dna[:]
    Len = len(Dna)
    Dis = []
    #
    # For loop that mutates the sequence Gen times and calculates the 
    # dissimilarity after each mutation.
    for _ in range(Gen):
        # Random location for mutation
        Mut = random.randint(0, Len-1)
         # Randomly selected nucleotide
        Dna[Mut] = random.choice(['A', 'T', 'G', 'C'])
        # Calculate proportional dissimalirity
        Dis.append(diss(Dna, Dna_o)/Len) 
    #
    # Print proportional dissimilarities.
    return (Dis) 


# Iterate (repeat) the mutate function i times
# Outputs a nested list
def mutate_i(Dna, Gen, i):
    Mat = []
    for _ in range(i):
        Mat.append(mutate(Dna, Gen))
    return (Mat) 


# Function for helping export simulation result as CSV file.
def export_mutate(Mutate_mat, Filename="mutation_simulation.csv"):
    # Add a count of the generations
    Mutate_mat.insert(0, list(range(1, len(Mutate_mat[0])+1)))
    # Transpose the list (swap rows and columns)
    Mutate_mat = list(map(list, zip(*Mutate_mat)))
    #
    # Add headers
    header = ["Generation"]
    for i in range(1, len(Mutate_mat[0])):
        header.append("sim_" + str(i))
    #
    Mutate_mat.insert(0, header)
    #
    # Export simulation data as a CSV file that can be imported to
    # Google Sheets
    with open(Filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerows(Mutate_mat)
    #
    # Print the path to the CSV file
    return (os.path.join(os.getcwd(), Filename))

###
# # # Simulation start

# Original DNA sequence
Dna = list("ATGC" * 4)

# Set random seed so the simulation is repeatable
random.seed(1)

# Generate four simulations, each ten generations long
Mutate_mat = mutate_i(Dna, 12, 4)

# Export simulation results
export_mutate(Mutate_mat)

import random
import os
import csv

# Function that counts number of dissimilarities
# between two sequences
def diss(x, y):
    n = 0
    for a, b in zip(x, y):
        if a != b:
            n += 1
    return(n)

# Function that mutates a string (Dna) Gen times.
# On average 0.75 mutations per generation, as the resampling will 
# pick the existing nucleotide in one out of four times.
def mutate(Dna, Gen):
    # Store a copy of the original DNA sequence that
    # the mutated sequence can be compared to.
    Dna_o = Dna[:]
    Len = len(Dna)
    Dis = []
    #
    # For loop that mutates the sequence Gen times and calculates the 
    # dissimilarity after each mutation.
    for _ in range(Gen):
        # Random location for mutation
        Mut = random.randint(0, Len-1)
         # Randomly selected nucleotide
        Dna[Mut] = random.choice(['A', 'T', 'G', 'C'])
        # Calculate proportional dissimalirity
        Dis.append(diss(Dna, Dna_o)/Len) 
    #
    # Print proportional dissimilarities.
    return(Dis)

# Iterate (repeat) the mutate function i times
# Outputs a nested list
def mutate_i(Dna, Gen, i):
    Mat = []
    for _ in range(i):
        Mat.append(mutate(Dna, Gen))
    return(Mat)

# Function for helping export simulation result as CSV file.
def export_mutate(Mutate_mat, Filename="mutation_simulation.csv"):
    # Add a count of the generations
    Mutate_mat.insert(0, list(range(1, len(Mutate_mat[0])+1)))
    # Transpose the list (swap rows and columns)
    Mutate_mat = list(map(list, zip(*Mutate_mat)))
    #
    # Add headers
    header = ["Generation"]
    for i in range(1, len(Mutate_mat[0])):
        header.append("sim_" + str(i))
    #
    Mutate_mat.insert(0, header)
    #
    # Export simulation data as a CSV file that can be imported to
    # Google Sheets
    with open(Filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerows(Mutate_mat)
    #
    # Print the path to the CSV file
    return(os.path.join(os.getcwd(), Filename))

### Simulation start

# Original DNA sequence
Dna = list("ATGC" * 4)

# Set random seed so the simulation is repeatable
random.seed(1)

# Generate four simulations, each ten generations long
Mutate_mat = mutate_i(Dna, 12, 4)

# Export simulation results
export_mutate(Mutate_mat)

import random
import os
import csv 


# Function that counts number of dissimilarities
# between two sequences
def diss(x, y):
    n = 0
    for a, b in zip(x, y):
        if a != b:
            n += 1
    return (n) 


# Function that mutates a string (Dna) Gen times.
# On average 0.75 mutations per generation, as the resampling will
# pick the existing nucleotide in one out of four times.
def mutate(Dna, Gen):
    # Store a copy of the original DNA sequence that
    # the mutated sequence can be compared to.
    Dna_o = Dna[:]
    Len = len(Dna)
    Dis = []

    # For loop that mutates the sequence Gen times and calculates the
    # dissimilarity after each mutation.
    for _ in range(Gen):
        # Random location for mutation
        Mut = random.randint(0, Len-1)
        # Randomly selected nucleotide
        Dna[Mut] = random.choice(['A', 'T', 'G', 'C'])
        # Calculate proportional dissimalirity
        Dis.append(diss(Dna, Dna_o)/Len)
    
    # Print proportional dissimilarities.
    return (Dis) 


# Iterate (repeat) the mutate function i times
# Outputs a nested list
def mutate_i(Dna, Gen, i):
    Mat = []
    for _ in range(i):
        Mat.append(mutate(Dna, Gen))
    return (Mat) 


# Function for helping export simulation result as CSV file.
def export_mutate(Mutate_mat, Filename="mutation_simulation.csv"):
    # Add a count of the generations
    Mutate_mat.insert(0, list(range(1, len(Mutate_mat[0])+1)))
    # Transpose the list (swap rows and columns)
    Mutate_mat = list(map(list, zip(*Mutate_mat)))
    
    # Add headers
    header = ["Generation"]
    for i in range(1, len(Mutate_mat[0])):
        header.append("sim_" + str(i))
    
    Mutate_mat.insert(0, header)
    
    # Export simulation data as a CSV file that can be imported to
    # Google Sheets
    with open(Filename, 'w') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerows(Mutate_mat)
    
    # Print the path to the CSV file
    return (os.path.join(os.getcwd(), Filename))


# # # Simulation start

# Original DNA sequence
Dna = list("ATGC" * 4)

# Set random seed so the simulation is repeatable
random.seed(1)

# Generate four simulations, each ten generations long
Mutate_mat = mutate_i(Dna, 12, 4)

# Export simulation results
export_mutate(Mutate_mat)

edited tags
Link
toolic
  • 16.4k
  • 6
  • 29
  • 221
added 6 characters in body; edited tags
Source Link
toolic
  • 16.4k
  • 6
  • 29
  • 221
Loading
Source Link
AkselA
  • 223
  • 1
  • 9
Loading