-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
288 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from typing import List, NamedTuple | ||
from pathlib import Path | ||
|
||
from napalib.system.universe import NapAUniverse | ||
import MDAnalysis as mda | ||
|
||
from . import Database | ||
|
||
|
||
def get_NapA_universe_by_simID(db: Database, simID: int) -> NapAUniverse: | ||
row = db.get_table("Simulations").get_row(simID) | ||
topology = row.topology | ||
trajectory = row.trajectory | ||
u = NapAUniverse(topology) | ||
u.load_new(trajectory) | ||
|
||
return u | ||
|
||
|
||
def get_universe_by_simID(db: Database, simID: int) -> mda.Universe: | ||
row = db.get_table("Simulations").get_row(simID) | ||
topology = row.topology | ||
trajectory = row.trajectory | ||
|
||
return mda.Universe(topology, trajectory) | ||
|
||
|
||
class DBAnalysisRunner: | ||
|
||
def __init__(self, db: Database, Analysis): | ||
|
||
self.db = db | ||
self.Analysis = Analysis | ||
self.name = self.Analysis.name | ||
self._analysis = None | ||
|
||
try: | ||
self.observables = self.db.get_table("Observables") | ||
except ValueError: | ||
self.observables = self.db.create_table( | ||
"Observables (name TEXT, progenitor TEXT)" | ||
) | ||
finally: | ||
self.observables.insert_array([ | ||
(self.name, self.Analysis._path), | ||
]) | ||
|
||
def __enter__(self): | ||
self.db.open() | ||
return self | ||
|
||
def __exit__(self, *args): | ||
self.db.close() | ||
|
||
@property | ||
def results(self): | ||
if self._analysis is not None: | ||
return self._analysis.results | ||
|
||
def run_for_simID(self, simID: int, **kwargs) -> None: | ||
"""""" | ||
universe = get_NapA_universe_by_simID(self.db, simID) | ||
self._analysis = self.Analysis(universe, **kwargs) | ||
self._analysis._simID = simID | ||
self._analysis.run() | ||
|
||
def save(self) -> None: | ||
if not self.results: | ||
raise ValueError("no results") | ||
|
||
if self.name not in self.db._get_table_names(): | ||
self.db.create_table(self.Analysis.schema) | ||
|
||
rows = self.results[self.Analysis.results_key] | ||
|
||
table = self.db.get_table(self.name) | ||
table.insert_array(rows) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from collections import namedtuple | ||
import pathlib | ||
|
||
import numpy as np | ||
from MDAnalysis.analysis.base import AnalysisBase | ||
from MDAnalysis.analysis.distances import distance_array | ||
|
||
from mdaadb.analysis import DBAnalysisRunner | ||
from mdaadb import Database | ||
|
||
|
||
class K305_D156(AnalysisBase): | ||
|
||
name = "K305D156" | ||
results_key = "distance" | ||
Row = namedtuple("Row", ["simID", "frame", "time", "dA", "dB"]) | ||
schema = f"{name} (simID INT, frame INT, time REAL, dA REAL, dB REAL)" | ||
_path = str(pathlib.Path(__file__).resolve()) | ||
|
||
def __init__(self, universe): | ||
self._simID = None | ||
|
||
super().__init__(universe.trajectory) | ||
self.u = universe | ||
self.n_frames = len(self.u.trajectory) | ||
|
||
self.OD_A = self.u.select_atoms( | ||
"resid 156 and name OD1 OD2 and segid A" | ||
) | ||
self.OD_B = self.u.select_atoms( | ||
"resid 156 and name OD1 OD2 and segid B" | ||
) | ||
self.NZ_A = self.u.select_atoms( | ||
"resid 305 and name NZ and segid A" | ||
) | ||
self.NZ_B = self.u.select_atoms( | ||
"resid 305 and name NZ and segid B" | ||
) | ||
|
||
def _prepare(self): | ||
self.results[self.results_key] = [] | ||
|
||
def _single_frame(self): | ||
simID = self._simID | ||
ts = self.u.trajectory.ts | ||
frame = ts.frame | ||
time = ts.time | ||
|
||
d_A = np.min( | ||
distance_array( | ||
self.OD_A.positions, | ||
self.NZ_A.positions, | ||
box=self.u.dimensions | ||
) | ||
) | ||
d_B = np.min( | ||
distance_array( | ||
self.OD_B.positions, | ||
self.NZ_B.positions, | ||
box=self.u.dimensions | ||
) | ||
) | ||
|
||
row = self.Row(simID, frame, time, d_A, d_B) | ||
|
||
self.results[self.results_key].append(row) | ||
|
||
|
||
def main(): | ||
|
||
napa_dbfile = pathlib.Path("~/projects/napadb/napa.sqlite") | ||
napa = Database(napa_dbfile) | ||
|
||
analysis_runner = DBAnalysisRunner(napa, K305_D156) | ||
with analysis_runner as analysis: | ||
simulations = analysis.db.get_table("Simulations") | ||
simids = simulations._get_rowids() | ||
|
||
for simID in simids: | ||
analysis.run_for_simID(simID) | ||
analysis.save() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import pathlib | ||
|
||
from napalib.system.traj import trajectories | ||
from mdaadb import Database, DBAnalysisRunner | ||
|
||
|
||
def main(): | ||
|
||
def get_topology(traj): | ||
return traj.topology | ||
|
||
def get_trajectory(traj): | ||
return traj.trajectory | ||
|
||
def get_repeat(traj): | ||
return int(traj.name().split("_")[2]) | ||
|
||
def get_name(traj): | ||
repeat = get_repeat(traj) | ||
name = traj.name().split("_") | ||
name.remove(f"{repeat}") | ||
name = "_".join(name) | ||
return name | ||
|
||
def get_conformation(traj): | ||
if traj.is_inward: | ||
return "inward" | ||
if traj.is_outward: | ||
return "outward" | ||
if "_occ_" in traj.name(): | ||
return "occluded" | ||
|
||
def get_temperature(traj): | ||
if traj.is_310: | ||
return 310 | ||
if traj.is_358: | ||
return 358 | ||
|
||
def get_protonation(traj): | ||
protonation = [] | ||
if traj.has_s1: | ||
protonation.append("s1") | ||
if traj.has_s2: | ||
protonation.append("s2") | ||
if traj.has_s4: | ||
protonation.append("s4") | ||
|
||
return ",".join(protonation) | ||
|
||
|
||
def get_row(idx, traj): | ||
row = ( | ||
idx, | ||
get_topology(traj), | ||
get_trajectory(traj), | ||
get_name(traj), | ||
get_repeat(traj), | ||
get_conformation(traj), | ||
get_temperature(traj), | ||
get_protonation(traj), | ||
) | ||
return row | ||
|
||
sims_schema = ( | ||
"Simulations (simID INT PRIMARY KEY, topology TEXT, trajectory TEXT, name TEXT, repeat INT, conformation TEXT, temperature INT, protonation TEXT)" | ||
) | ||
obs_schema = ( | ||
"Observables (name TEXT, progenitor TEXT)" | ||
) | ||
|
||
rows = [get_row(idx, traj) for (idx, traj) in enumerate(trajectories)] | ||
|
||
dbfile = pathlib.Path("~/projects/napadb/napa.sqlite") | ||
with Database(dbfile) as db: | ||
db.create_table(sims_schema) | ||
db.create_table(obs_schema) | ||
|
||
db.get_table("Simulations").insert_array(rows) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |