Skip to content

Commit

Permalink
fix: remove conformation only, and stick to conformation range
Browse files Browse the repository at this point in the history
  • Loading branch information
radifar committed Jan 13, 2024
1 parent f40ba1b commit c84f8f7
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/deemian/chem/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class InteractionData:
subject_2_mol: Chem.rdchem.Mol
subject_1_df: pd.DataFrame
subject_2_df: pd.DataFrame
conformation: list
conformation_range: range
dataframe: pd.DataFrame = field(default_factory=lambda: pd.DataFrame())

def calculate_electrostatic(self, positive: bool, negative: bool):
Expand All @@ -33,7 +33,7 @@ def calculate_electrostatic(self, positive: bool, negative: bool):
cation_2_df = df2[df2.index.isin(cation_2_ids)]
anion_2_df = df2[df2.index.isin(anion_2_ids) & ~df2["atom_symbol"].isin(exclude_atom)]

for conf_num in self.conformation:
for conf_num in self.conformation_range:
conformation_column = "conf_" + str(conf_num)

if (not cation_1_df.empty) and (not anion_2_df.empty):
Expand Down
14 changes: 6 additions & 8 deletions src/deemian/engine/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ class Measurement:
interactions: list = field(default_factory=lambda: [])
ionizable: dict = field(default_factory=lambda: {"positive": False, "negative": False})
interacting_subjects: dict = field(default_factory=lambda: {})
conformation: list = field(default_factory=lambda: [])
conformation_range: list = field(default_factory=lambda: [])
conformation_range: range = field(default_factory=lambda: range(1, 2))
calculation_results: dict = field(default_factory=lambda: {})

def set_conformation_range(self, start, end):
self.conformation = list(range(int(start), int(end) + 1))
self.conformation_range = [start, end]
self.conformation_range = range(int(start), int(end) + 1)

def set_ionizable(self, charge: str, boolean: str):
if boolean == "true":
Expand Down Expand Up @@ -106,11 +104,11 @@ def calculate_interactions(self, id):
subject_2_mol = self.molecule[subject_2.mol_parent].rdkit_mol
subject_1_df = subject_1.mol_dataframe
subject_2_df = subject_2.mol_dataframe
conformation = measurement.conformation
if not any(conformation):
conformation = [1]
conformation_range = measurement.conformation_range

interaction_data = InteractionData(subject_1_mol, subject_2_mol, subject_1_df, subject_2_df, conformation)
interaction_data = InteractionData(
subject_1_mol, subject_2_mol, subject_1_df, subject_2_df, conformation_range
)

interaction_type = measurement.interactions

Expand Down
3 changes: 0 additions & 3 deletions src/deemian/engine/director.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def director(steps: Tree, data: DeemianData):
elif inst.type == "interacting_subject":
measurement.interacting_subjects[inst.name] = (inst.subject_1, inst.subject_2)

elif inst.type == "conformation":
measurement.conformation.extend(inst.number)

elif inst.type == "conformation_range":
measurement.set_conformation_range(inst.start, inst.end)

Expand Down
1 change: 0 additions & 1 deletion src/deemian/engine/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ measurement_instruction: "interactions" INTERACTION("," INTERACTION)*
| "ionizable" CHARGE BOOLEAN -> include_ionizable
| PREPOSITION IDENTIFIER (PREPOSITION IDENTIFIER)? -> interacting_subject
| PREPOSITION IDENTIFIER (PREPOSITION IDENTIFIER)? "as" IDENTIFIER -> interacting_subject_with_alias
| "conformation" INTEGER("," INTEGER)* -> conformation
| "conformation" INTEGER "to" INTEGER -> conformation_range

presentation_instruction: "interactions" INTERACTION_FORMAT? FILE_NAME -> interaction_output
Expand Down
5 changes: 0 additions & 5 deletions src/deemian/engine/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class InstructionTransformer(Transformer):
DeemianData = namedtuple("DeemianData", "out_file type", defaults=["deemian_data"])
InteractionList = namedtuple("InteractionList", "interactions type", defaults=["interaction_list"])
IncludeIonizable = namedtuple("IncludeIonizable", "charge boolean type", defaults=["include_ionizable"])
Conformation = namedtuple("Conformation", "number type", defaults=["conformation"])
ConformationRange = namedtuple("ConformationRange", "start end type", defaults=["conformation_range"])
InteractingSubject = namedtuple(
"InteractingSubject", "subject_1 subject_2 name type", defaults=["interacting_subject"]
Expand Down Expand Up @@ -79,10 +78,6 @@ def interacting_subject_with_alias(self, args):

return self.InteractingSubject(subject_1, subject_2, name)

def conformation(self, args):
conformation_list = [int(arg.value) for arg in args]
return self.Conformation(conformation_list)

def conformation_range(self, args):
return self.ConformationRange(args[0].value, args[1].value)

Expand Down
9 changes: 2 additions & 7 deletions src/deemian/writer/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ def write_metadata(metadata, measurement_data):
interacting_subject = dict(name=name, subjects=subjects, results=parquet_result)
metadata_copy.measurement["interacting_subjects"].append(interacting_subject)

if measurement_data.conformation_range:
metadata_copy.measurement["conformation_range"] = measurement_data.conformation_range
else:
if measurement_data.conformation:
metadata_copy.measurement["conformation"] = measurement_data.conformation
else:
metadata_copy.measurement["conformation"] = [1]
conf_range = measurement_data.conformation_range
metadata_copy.measurement["conformation_range"] = [conf_range.start, conf_range.stop - 1]

# setup the original metadata, because the running time is used by main script
metadata.end_time = time.time()
Expand Down
1 change: 0 additions & 1 deletion tests/data/n1-oseltamivir-5nzn.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ measure protein_ligand [
ionizable positive true
ionizable negative true
between oseltamivir and protein_A
conformation 1
]

present protein_ligand [
Expand Down
2 changes: 1 addition & 1 deletion tests/test_chem_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@pytest.fixture
def interaction_data_n1(oseltamivir_corrected, n1, oseltamivir_corrected_df, n1_protein_A):
interaction_data = InteractionData(oseltamivir_corrected, n1, oseltamivir_corrected_df, n1_protein_A, [1])
interaction_data = InteractionData(oseltamivir_corrected, n1, oseltamivir_corrected_df, n1_protein_A, range(1, 2))

return interaction_data

Expand Down
8 changes: 3 additions & 5 deletions tests/test_engine_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ def test_deemian_data_measurement():
measurement.set_ionizable("positive", "true")
measurement.set_ionizable("negative", "false")
measurement.interacting_subjects["oseltamivir:protein_A"] = ("oseltamivir", "protein_A")
measurement.set_conformation_range("1", "4")
measurement.conformation.extend([5])
measurement.set_conformation_range("1", "5")

with patch.object(data, "calculate_interactions", return_value=1):
result = data.calculate_interactions("protein_ligand")
Expand All @@ -65,7 +64,7 @@ def test_deemian_data_measurement():
== """Measurement(interactions=['all'], \
ionizable={'positive': True, 'negative': False}, \
interacting_subjects={'oseltamivir:protein_A': ('oseltamivir', 'protein_A')}, \
conformation=[1, 2, 3, 4, 5], conformation_range=['1', '4'], \
conformation_range=range(1, 6), \
calculation_results={})"""
)
assert result == 1
Expand Down Expand Up @@ -94,7 +93,6 @@ def test_deemian_data_calculate_interactions():
measurement.set_ionizable("positive", "true")
measurement.set_ionizable("negative", "true")
measurement.interacting_subjects["oseltamivir:protein_A"] = ("oseltamivir", "protein_A")
measurement.conformation.extend([1])

with patch("deemian.engine.builder.InteractionData") as int_data:
data.calculate_interactions("protein_ligand")
Expand All @@ -105,7 +103,7 @@ def test_deemian_data_calculate_interactions():
"neuraminidase:rdkitmol",
"oseltamivir:pd.DataFrame",
"protein_A:pd.DataFrame",
[1],
range(1, 2),
),
call().calculate_electrostatic(positive=True, negative=True),
]
Expand Down
3 changes: 0 additions & 3 deletions tests/test_engine_director.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
DeemianData = namedtuple("DeemianData", "out_file type", defaults=["deemian_data"])
InteractionList = namedtuple("InteractionList", "interactions type", defaults=["interaction_list"])
IncludeIonizable = namedtuple("IncludeIonizable", "charge boolean type", defaults=["include_ionizable"])
Conformation = namedtuple("Conformation", "number type", defaults=["conformation"])
ConformationRange = namedtuple("ConformationRange", "start end type", defaults=["conformation_range"])
InteractingSubject = namedtuple(
"InteractingSubject", "subject_1 subject_2 name type", defaults=["interacting_subject"]
Expand Down Expand Up @@ -53,7 +52,6 @@ def steps_simple() -> Tree:
name="oseltamivir:protein_A",
type="interacting_subject",
),
Conformation(number=[1], type="conformation"),
],
),
Tree(
Expand Down Expand Up @@ -197,7 +195,6 @@ def test_engine_director_simple(steps_simple):
call.add_measurement().interacting_subjects.__setitem__(
"oseltamivir:protein_A", ("oseltamivir", "protein_A")
),
call.add_measurement().conformation.extend([1]),
call.calculate_interactions("protein_ligand"),
call.write_readable_output("protein_ligand", "protein_ligand.txt", "detailed_conf_first"),
call.write_deemian_data("protein_ligand", "protein_ligand.dd"),
Expand Down
3 changes: 0 additions & 3 deletions tests/test_engine_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def test_measure_transform(n1_transformed, spike_transformed, vps_transformed):
n1_ionizable1 = n1_measure.children[2]
n1_ionizable2 = n1_measure.children[3]
n1_interacting_subject = n1_measure.children[4]
n1_conformation = n1_measure.children[5]

spike_measure = spike_transformed.children[1]
spike_interacting_subject_as = spike_measure.children[4]
Expand All @@ -156,8 +155,6 @@ def test_measure_transform(n1_transformed, spike_transformed, vps_transformed):
assert n1_interacting_subject.subject_2 == "protein_A"
assert n1_interacting_subject.name == "oseltamivir:protein_A"
assert n1_interacting_subject.type == "interacting_subject"
assert n1_conformation.number == [1]
assert n1_conformation.type == "conformation"

assert spike_interacting_subject_as.subject_1 == "spike_rbm"
assert spike_interacting_subject_as.subject_2 == "ace2_hotspot31"
Expand Down
17 changes: 2 additions & 15 deletions tests/test_writer_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,18 @@ def test_writer_bundle_dict_factory(metadata_obj):
assert metadata_keys == expected_keys


def test_writer_bundle_write_metadata(metadata_obj, measurement_data):
measurement_data.conformation = [1]

metadata_file = write_metadata(metadata_obj, measurement_data)
metadata_file_name = metadata_file[0]
metadata_io = metadata_file[1]
metadata_json = json.loads(metadata_io.read())

assert metadata_file_name == "deemian.json"
assert metadata_json["measurement"]["interacting_subjects"][0]["results"] == "oseltamivir_protein_A.parquet"
assert metadata_json["measurement"]["conformation"] == [1]


def test_writer_bundle_write_metadata_no_conformation(metadata_obj, measurement_data):
metadata_file = write_metadata(metadata_obj, measurement_data)
metadata_file_name = metadata_file[0]
metadata_io = metadata_file[1]
metadata_json = json.loads(metadata_io.read())

assert metadata_file_name == "deemian.json"
assert metadata_json["measurement"]["conformation"] == [1]
assert metadata_json["measurement"]["conformation_range"] == [1, 1]


def test_writer_bundle_write_metadata_conformation_range(metadata_obj, measurement_data):
measurement_data.conformation_range = [1, 20]
measurement_data.conformation_range = range(1, 21)

metadata_file = write_metadata(metadata_obj, measurement_data)
metadata_file_name = metadata_file[0]
Expand Down

0 comments on commit c84f8f7

Please sign in to comment.