Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiraka committed Nov 24, 2024
1 parent a6cb230 commit 7cfa992
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 197 deletions.
18 changes: 18 additions & 0 deletions examples/runtime/configs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
IsFirstColumnId = Yes
SaveFull = No
SaveRuntime = No
SequencesDir = data/pydnaepbd_things/id_seqs.txt
OutputsDir = no_outputs/
Flanks = None
Temperature = 310
Iterations = 100
PreheatingSteps = 50000
PostPreheatingSteps = 80000
ComputingNodes = 256
BubbleMonitor = Off
CoordinateMonitor = On
FlippingMonitorVerbose = On
FlippingMonitor = Off
EnergyMonitor = Off
MeltingAndFractionMonitor = Off
MeltingAndFractionManyMonitor = Off
12 changes: 3 additions & 9 deletions pydna_epbd/input_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
from pydna_epbd.configs import InputConfigs


def read_sequences_from_a_file(
input_seqs_dir, filename_with_ext, is_first_col_id, flanks, outputs_dir
):
def read_sequences_from_a_file(input_seqs_dir, filename_with_ext, is_first_col_id, flanks, outputs_dir):
"""Read DNA sequences from one input file. This also creates output directories for saving simulation outputs.
Args:
Expand Down Expand Up @@ -56,9 +54,7 @@ def read_all_sequences(input_seqs_dir, is_first_col_id, flanks, outputs_dir):
all_seqs = []
total_num_of_bps = 0
for i, filename_with_ext in enumerate(os.listdir(input_seqs_dir)):
seqs = read_sequences_from_a_file(
input_seqs_dir, filename_with_ext, is_first_col_id, flanks, outputs_dir
)
seqs = read_sequences_from_a_file(input_seqs_dir, filename_with_ext, is_first_col_id, flanks, outputs_dir)
all_seqs += seqs

n_bps = len(seqs[0][2]) - (2 * len(flanks))
Expand Down Expand Up @@ -110,9 +106,7 @@ def read_configurations(configuration_filepath):
os.environ["FLIPPING_MONITOR"] = configs["FlippingMonitor"]
os.environ["ENERGY_MONITOR"] = configs["EnergyMonitor"]
os.environ["MELTING_AND_FRACTION_MONITOR"] = configs["MeltingAndFractionMonitor"]
os.environ["MELTING_AND_FRACTION_MANY_MONITOR"] = configs[
"MeltingAndFractionManyMonitor"
]
os.environ["MELTING_AND_FRACTION_MANY_MONITOR"] = configs["MeltingAndFractionManyMonitor"]

# reading sequences and creating outputs directory
sequences = read_all_sequences(input_seqs_dir, is_first_col_id, flanks, outputs_dir)
Expand Down
8 changes: 2 additions & 6 deletions pydna_epbd/monitors/all_monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ def __init__(self, dna, n_preheating_steps, n_steps_after_preheating) -> None:
self.monitors.append(self.energy_monitor)

if os.environ["MELTING_AND_FRACTION_MONITOR"] == "On":
self.melting_and_fraction_monitor = MeltingAndFractionMonitor(
dna, n_steps_after_preheating
)
self.melting_and_fraction_monitor = MeltingAndFractionMonitor(dna, n_steps_after_preheating)
self.monitors.append(self.melting_and_fraction_monitor)

if os.environ["MELTING_AND_FRACTION_MANY_MONITOR"] == "On":
self.melting_and_fraction_many_monitor = MeltingAndFractionManyMonitor(
dna, n_preheating_steps
)
self.melting_and_fraction_many_monitor = MeltingAndFractionManyMonitor(dna, n_preheating_steps)
self.monitors.append(self.melting_and_fraction_many_monitor)

# if os.environ['COORD_VERBOSE_MONITOR'] == 'True':
Expand Down
3 changes: 1 addition & 2 deletions pydna_epbd/monitors/bubble_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def __init__(self, dna) -> None:
"""
super(BubbleMonitor, self).__init__(dna)
self.bubbles = [
[[0] * self.TRESHOLD_SIZE for _ in range(self.MAX_BUB_ELEM)]
for _ in range(self.dna.n_nt_bases)
[[0] * self.TRESHOLD_SIZE for _ in range(self.MAX_BUB_ELEM)] for _ in range(self.dna.n_nt_bases)
] # shape=(n_nt_bases, MAX_BUB_ELEM, TRESHOLD_SIZE)

def collect_at_step(self, step_no):
Expand Down
4 changes: 2 additions & 2 deletions pydna_epbd/monitors/coord_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, dna) -> None:
"""
super(CoordMonitor, self).__init__(dna)
self.coord = [0.0] * self.dna.n_nt_bases
self.coord_square = [0.0] * self.dna.n_nt_bases
# self.coord_square = [0.0] * self.dna.n_nt_bases

def collect_at_step(self, step_no):
"""Collect bps distance and squared-distance at every post-preheating step.
Expand All @@ -22,4 +22,4 @@ def collect_at_step(self, step_no):
"""
for i in range(self.dna.n_nt_bases):
self.coord[i] += self.dna.coords_dist[i]
self.coord_square[i] += self.dna.coords_dist[i] ** 2
# self.coord_square[i] += self.dna.coords_dist[i] ** 2
7 changes: 3 additions & 4 deletions pydna_epbd/monitors/coord_monitor_verbose.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# this is deprecated, this information is already in the coord-monitor.

# import sys
Expand All @@ -16,10 +15,10 @@
# """
# def __init__(self, dna:DNA, input_configs:InputConfigs) -> None:
# super(CoordMonitorVerbose, self).__init__(dna, input_configs)

# def collect_at_step(self, step_no):
# return super().collect_at_step(step_no)

# def output_iter(self):
# output_file = f"outputs/coord_monitor_verbose/seqidx_{self.seq_idx}_iter_{self.iter_no}.txt"
# super()._write_file(output_file, self.coord)
# super()._write_file(output_file, self.coord)
4 changes: 1 addition & 3 deletions pydna_epbd/monitors/energy_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def collect_at_step(self, step_no):
Args:
step_no (int): Step number.
"""
temp = self.dna.total_energy / (
self.KB * self.dna.n_nt_bases
) # computing temperature from energy
temp = self.dna.total_energy / (self.KB * self.dna.n_nt_bases) # computing temperature from energy
self.energy[step_no] += temp

def collect_at_step_preheat(self, step_no):
Expand Down
4 changes: 1 addition & 3 deletions pydna_epbd/monitors/flipping_monitor_verbose.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ def __init__(self, dna) -> None:
dna (DNA): A DNA object.
"""
super(FlippingMonitorVerbose, self).__init__(dna)
self.flip = [
[0.0] * self.FLIP_SIZES for i in range(self.dna.n_nt_bases)
] # shape=(n_nt_bases, FLIP_SIZE)
self.flip = [[0.0] * self.FLIP_SIZES for i in range(self.dna.n_nt_bases)] # shape=(n_nt_bases, FLIP_SIZE)

def collect_at_step(self, step_no):
"""Collects flipping characteristics considering different thresholds.
Expand Down
25 changes: 6 additions & 19 deletions pydna_epbd/monitors/melting_and_fraction_many_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ class MeltingAndFractionManyMonitor(Monitor):
in between [0.5, 2.5) Angstrom with step size 0.1.
"""

MELT_FRACTION_TRESHOLDS = [
i / 10 for i in range(5, 25)
] # start=0.5, end=2.5, step=0.1
MELT_FRACTION_TRESHOLDS = [i / 10 for i in range(5, 25)] # start=0.5, end=2.5, step=0.1
MELT_FRACTION_SIZES = len(MELT_FRACTION_TRESHOLDS)

# MELT_FRACTION_TIME_STEP = 800
# MELT_FRACTION_MAX_STEPS = 80000
MELT_FRACTION_TIME_STEPS = (
100 # int(MELT_FRACTION_MAX_STEPS/MELT_FRACTION_TIME_STEP) # 100
)
MELT_FRACTION_TIME_STEPS = 100 # int(MELT_FRACTION_MAX_STEPS/MELT_FRACTION_TIME_STEP) # 100

def __init__(self, dna, n_preheating_steps) -> None:
"""Initialize MeltingAndFractionManyMonitor object.
Expand All @@ -33,12 +29,10 @@ def __init__(self, dna, n_preheating_steps) -> None:

self.melting_fraction_many = [0.0] * self.dna.n_nt_bases
self.melting_many = [
[0.0] * self.MELT_FRACTION_SIZES
for i in range(self.MELT_FRACTION_TIME_STEPS)
[0.0] * self.MELT_FRACTION_SIZES for i in range(self.MELT_FRACTION_TIME_STEPS)
] # shape=(MELT_FRACTION_TIME_STEPS, MELT_FRACTION_SIZES)
self.fraction_many = [
[0.0] * self.MELT_FRACTION_SIZES
for i in range(self.MELT_FRACTION_TIME_STEPS)
[0.0] * self.MELT_FRACTION_SIZES for i in range(self.MELT_FRACTION_TIME_STEPS)
] # shape=(MELT_FRACTION_TIME_STEPS, MELT_FRACTION_SIZES)

def collect_at_step(self, step_no):
Expand All @@ -61,18 +55,11 @@ def __check_melting_and_fraction_many(self, step, time_step):
melting, fraction = 1.0, 0.0

for base_idx in range(self.dna.n_nt_bases):
if (
melting == 1.0
and self.melting_fraction_many[base_idx] / (step + 1)
< self.MELT_FRACTION_TRESHOLDS[thresh_idx]
):
if melting == 1.0 and self.melting_fraction_many[base_idx] / (step + 1) < self.MELT_FRACTION_TRESHOLDS[thresh_idx]:
melting = 0.0
break

if (
self.melting_fraction_many[base_idx] / (step + 1)
> self.MELT_FRACTION_TRESHOLDS[thresh_idx]
):
if self.melting_fraction_many[base_idx] / (step + 1) > self.MELT_FRACTION_TRESHOLDS[thresh_idx]:
fraction += 1 # computing total fraction length for this threshold

self.melting_many[time_step][thresh_idx] += melting
Expand Down
14 changes: 3 additions & 11 deletions pydna_epbd/monitors/melting_and_fraction_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,11 @@ def collect_at_iter(self):
"""Melting and fraction characteristics are collected at the end of the iteration."""
melting, fraction = 1.0, 0.0
for i in range(self.dna.n_nt_bases):
if (
melting == 1.0
and (self.melting_fraction[i] / self.n_steps_after_preheating)
< self.MELT_FRACTION_TRESHOLD
):
if melting == 1.0 and (self.melting_fraction[i] / self.n_steps_after_preheating) < self.MELT_FRACTION_TRESHOLD:
melting = 0.0 # 0.0 means did not melt the whole DNA

if (
self.melting_fraction[i] / self.n_steps_after_preheating
) > self.MELT_FRACTION_TRESHOLD:
if (self.melting_fraction[i] / self.n_steps_after_preheating) > self.MELT_FRACTION_TRESHOLD:
fraction += 1 # total number of base pairs whose distance is >MELT_FRACTION_TRESHOLD

self.melting = melting # if melting is 1.0, then the fraction should be 1.0, because the whole DNA melted.
self.fraction = (
fraction / self.dna.n_nt_bases
) # fraction of the bps got melted.
self.fraction = fraction / self.dna.n_nt_bases # fraction of the bps got melted.
5 changes: 1 addition & 4 deletions pydna_epbd/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ def parse_args():

# dividing the input sequences to the nodes based on job-idx
chunk_size = math.ceil(len(input_configs.sequences) / input_configs.n_nodes)
sequence_chunks = [
input_configs.sequences[x : x + chunk_size]
for x in range(0, len(input_configs.sequences), chunk_size)
]
sequence_chunks = [input_configs.sequences[x : x + chunk_size] for x in range(0, len(input_configs.sequences), chunk_size)]
sequences = sequence_chunks[job_idx]
print(f"job_idx:{job_idx}, n_seqs:{len(sequences)}")

Expand Down
74 changes: 17 additions & 57 deletions pydna_epbd/simulation/aggregate_outputs_and_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def aggregate_outputs_for_single_temp(list_of_monitors, input_configs, out_filep

if os.environ["COORD_MONITOR"] == "On":
coord_iter_list.append(monitors.coord_monitor.coord)
coord_squared_iter_list.append(monitors.coord_monitor.coord_square)
# coord_squared_iter_list.append(monitors.coord_monitor.coord_square)

if os.environ["FLIPPING_MONITOR"] == "On":
flip_iter_list.append(monitors.flipping_monitor.flip)
Expand All @@ -46,91 +46,51 @@ def aggregate_outputs_for_single_temp(list_of_monitors, input_configs, out_filep
fraction_iter_list.append(monitors.melting_and_fraction_monitor.fraction)

if os.environ["MELTING_AND_FRACTION_MANY_MONITOR"] == "True":
melting_many_iter_list.append(
monitors.melting_and_fraction_many_monitor.melting_many
)
fraction_many_iter_list.append(
monitors.melting_and_fraction_many_monitor.fraction_many
)
melting_many_iter_list.append(monitors.melting_and_fraction_many_monitor.melting_many)
fraction_many_iter_list.append(monitors.melting_and_fraction_many_monitor.fraction_many)

# formating outputs as dictionary, all features are averaged over the number of iterations
outputs = {}
if os.environ["BUBBLE_MONITOR"] == "On":
# (n_iters, seq_len, max_bubble_elem=20, thr_sizes=20)
outputs["bubbles"] = (
np.array(bubble_iter_list)
if input_configs.save_full
else np.array(bubble_iter_list).mean(0)
)
outputs["bubbles"] = np.array(bubble_iter_list) if input_configs.save_full else np.array(bubble_iter_list).mean(0)

if os.environ["COORD_MONITOR"] == "On":
# (n_iters, seq_len)
outputs["coord"] = (
np.array(coord_iter_list)
if input_configs.save_full
else np.array(coord_iter_list).mean(0)
)
outputs["coord"] = np.array(coord_iter_list) if input_configs.save_full else np.array(coord_iter_list).mean(0)

# (n_iters, seq_len)
outputs["coord_squared"] = (
np.array(coord_squared_iter_list)
if input_configs.save_full
else np.array(coord_squared_iter_list).mean(0)
)
# outputs["coord_squared"] = (
# np.array(coord_squared_iter_list)
# if input_configs.save_full
# else np.array(coord_squared_iter_list).mean(0)
# )

if os.environ["FLIPPING_MONITOR"] == "On":
# (n_iters, seq_len)
outputs["flip"] = (
np.array(flip_iter_list).mean(0)
if input_configs.save_full
else np.array(flip_iter_list).mean(0)
)
outputs["flip"] = np.array(flip_iter_list).mean(0) if input_configs.save_full else np.array(flip_iter_list).mean(0)

if os.environ["FLIPPING_MONITOR_VERBOSE"] == "On":
# (n_iters, seq_len, flip_sizes=10)
outputs["flip_verbose"] = (
np.array(flip_verbose_iter_list)
if input_configs.save_full
else np.array(flip_verbose_iter_list).mean(0)
)
outputs["flip_verbose"] = np.array(flip_verbose_iter_list) if input_configs.save_full else np.array(flip_verbose_iter_list).mean(0)

if os.environ["ENERGY_MONITOR"] == "On":
# (n_iters, total_steps)
outputs["energy"] = (
np.array(energy_iter_list)
if input_configs.save_full
else np.array(energy_iter_list).mean(0)
)
outputs["energy"] = np.array(energy_iter_list) if input_configs.save_full else np.array(energy_iter_list).mean(0)

if os.environ["MELTING_AND_FRACTION_MONITOR"] == "On":
# (n_iters)
outputs["melting"] = (
np.array(melting_iter_list)
if input_configs.save_full
else np.array(melting_iter_list).mean(0)
)
outputs["melting"] = np.array(melting_iter_list) if input_configs.save_full else np.array(melting_iter_list).mean(0)

# (n_iters)
outputs["fraction"] = (
np.array(fraction_iter_list)
if input_configs.save_full
else np.array(fraction_iter_list).mean(0)
)
outputs["fraction"] = np.array(fraction_iter_list) if input_configs.save_full else np.array(fraction_iter_list).mean(0)

if os.environ["MELTING_AND_FRACTION_MANY_MONITOR"] == "On":
# (n_iters, n_time_steps=100, melt_faction_sizes=20)
outputs["melting_many"] = (
np.array(melting_many_iter_list)
if input_configs.save_full
else np.array(melting_many_iter_list).mean(0)
)
outputs["melting_many"] = np.array(melting_many_iter_list) if input_configs.save_full else np.array(melting_many_iter_list).mean(0)

# (n_iters, n_time_steps=100, melt_faction_sizes=20)
outputs["fraction_many"] = (
np.array(fraction_many_iter_list)
if input_configs.save_full
else np.array(fraction_many_iter_list).mean(0)
)
outputs["fraction_many"] = np.array(fraction_many_iter_list) if input_configs.save_full else np.array(fraction_many_iter_list).mean(0)

utils.save_as_pickle(outputs, out_filepath)

Expand Down
4 changes: 1 addition & 3 deletions pydna_epbd/simulation/dna.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def reset(self):

self.DA, self.kn = self.__init_stacking_terms()
self.kn_div_four = [i * 0.25 for i in self.kn]
self.DA_div_sqrt_two = [
[j * one_div_sqrt2 for j in self.DA[i]] for i in range(self.n_nt_bases)
]
self.DA_div_sqrt_two = [[j * one_div_sqrt2 for j in self.DA[i]] for i in range(self.n_nt_bases)]

def __init_coords(self):
"""Private method for initializing coordinates (u (left) and v (right) bases).
Expand Down
Loading

0 comments on commit 7cfa992

Please sign in to comment.