From 94a02fa887e6eecb3c3925ad237fb2440b2bc43c Mon Sep 17 00:00:00 2001 From: Diego Prada Date: Wed, 20 Sep 2023 15:43:55 -0600 Subject: [PATCH] In process to refactor set chain_id to atoms --- .../_private/digestion/argument/chain_id.py | 6 +- molsysmt/_private/digestion/digest.py | 4 +- molsysmt/basic/set.py | 1 - molsysmt/build/__init__.py | 1 + molsysmt/build/define_chain.py | 20 ++ molsysmt/form/molsysmt_Topology/set.py | 125 ++++++++--- sandbox/Test.ipynb | 199 ++++++++++++++++++ sandbox/Test_flip.ipynb | 127 ----------- 8 files changed, 329 insertions(+), 154 deletions(-) create mode 100644 molsysmt/build/define_chain.py create mode 100644 sandbox/Test.ipynb delete mode 100644 sandbox/Test_flip.ipynb diff --git a/molsysmt/_private/digestion/argument/chain_id.py b/molsysmt/_private/digestion/argument/chain_id.py index bd9c59244..fd268f746 100644 --- a/molsysmt/_private/digestion/argument/chain_id.py +++ b/molsysmt/_private/digestion/argument/chain_id.py @@ -1,6 +1,10 @@ from ...exceptions import ArgumentError from ...variables import is_all +set_functions = ( + 'set.set', + 'set_chain_id_to_atom') + def digest_chain_id(chain_id, caller=None): """Checks if `chain_id` has the expected type and value. @@ -28,7 +32,7 @@ def digest_chain_id(chain_id, caller=None): if caller=='molsysmt.basic.get.get': if isinstance(chain_id, bool): return chain_id - elif caller=='molsysmt.basic.set.set': + elif caller.endswith(set_functions): if isinstance(chain_id, str): return chain_id elif caller.startswith('molsysmt.form.') and caller.count('.to_')==2: diff --git a/molsysmt/_private/digestion/digest.py b/molsysmt/_private/digestion/digest.py index 08ac87b1c..a6b5da90b 100644 --- a/molsysmt/_private/digestion/digest.py +++ b/molsysmt/_private/digestion/digest.py @@ -77,7 +77,6 @@ def wrapper(*args, **kwargs): all_args = argument_names_standardization(caller, all_args) - # Digestions: digested_args = {} @@ -102,13 +101,16 @@ def gut(arg_name): not_digested_args[arg_name] = all_args[arg_name] pass + for arg_name in all_args: gut(arg_name) + for arg_name in not_digested_args: if arg_name not in ['self']: warnings.warn(arg_name+' from '+caller, NotDigestedArgumentWarning, stacklevel=2) + final_args = digested_args if 'self' in all_args: diff --git a/molsysmt/basic/set.py b/molsysmt/basic/set.py index 8d32c2930..440a98deb 100644 --- a/molsysmt/basic/set.py +++ b/molsysmt/basic/set.py @@ -154,7 +154,6 @@ def set(molecular_system, indices = select(molecular_system, element=element, selection=selection, syntax=syntax) # doing the work here - for in_attribute in in_attributes: dict_indices = {} diff --git a/molsysmt/build/__init__.py b/molsysmt/build/__init__.py index dd9ee4d43..6299d4223 100644 --- a/molsysmt/build/__init__.py +++ b/molsysmt/build/__init__.py @@ -14,4 +14,5 @@ from .solvate import solvate from .model_loop import model_loop from .mutate import mutate +from .define_chain import define_chain diff --git a/molsysmt/build/define_chain.py b/molsysmt/build/define_chain.py new file mode 100644 index 000000000..92460b8cd --- /dev/null +++ b/molsysmt/build/define_chain.py @@ -0,0 +1,20 @@ +from molsysmt._private.digestion import digest +import numpy as np + +@digest() +def define_chain(molecular_system, selection=None, id=None, name=None, syntax='MolSysMT'): + """ + To be written soon... + """ + + from molsysmt.basic import get, set + + former_id, former_name = get(molecular_system, element='chain', chain_id=True) + + if id in former_id: + raise ValueError() + + set(molecular_system, element='atom', selection=selection, syntax=syntax, + chain_id=id, chain_name=name) + + pass diff --git a/molsysmt/form/molsysmt_Topology/set.py b/molsysmt/form/molsysmt_Topology/set.py index 786b1de5e..0fb323c54 100644 --- a/molsysmt/form/molsysmt_Topology/set.py +++ b/molsysmt/form/molsysmt_Topology/set.py @@ -1,4 +1,5 @@ from molsysmt._private.digestion import digest +from molsysmt._private.variables import is_all from molsysmt import pyunitwizard as puw import numpy as np @@ -11,168 +12,244 @@ @digest(form=form) def set_atom_index_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'atom_index']=value + if is_all(indices): + item.atoms_dataframe['atom_index']=value + else: + item.atoms_dataframe.loc[indices, 'atom_index']=value pass @digest(form=form) def set_atom_name_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'atom_name']=value + if is_all(indices): + item.atoms_dataframe['atom_name']=value + else: + item.atoms_dataframe.loc[indices, 'atom_name']=value pass @digest(form=form) def set_atom_id_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'atom_id']=value + if is_all(indices): + item.atoms_dataframe['atom_id']=value + else: + item.atoms_dataframe.loc[indices, 'atom_id']=value pass @digest(form=form) def set_atom_type_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'atom_type']=value + if is_all(indices): + item.atoms_dataframe['atom_type']=value + else: + item.atoms_dataframe.loc[indices, 'atom_type']=value pass @digest(form=form) def set_group_index_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'group_index']=value + if is_all(indices): + item.atoms_dataframe['group_index']=value + else: + item.atoms_dataframe.loc[indices, 'group_index']=value pass @digest(form=form) def set_group_name_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'group_name']=value + if is_all(indices): + item.atoms_dataframe['group_name']=value + else: + item.atoms_dataframe.loc[indices, 'group_name']=value pass @digest(form=form) def set_group_id_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'group_id']=value + if is_all(indices): + item.atoms_dataframe['group_id']=value + else: + item.atoms_dataframe.loc[indices, 'group_id']=value pass @digest(form=form) def set_group_type_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'group_type']=value + if is_all(indices): + item.atoms_dataframe['group_type']=value + else: + item.atoms_dataframe.loc[indices, 'group_type']=value pass @digest(form=form) def set_component_index_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'component_index']=value + if is_all(indices): + item.atoms_dataframe['component_index']=value + else: + item.atoms_dataframe.loc[indices, 'component_index']=value pass @digest(form=form) def set_component_name_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'component_name']=value + if is_all(indices): + item.atoms_dataframe['component_name']=value + else: + item.atoms_dataframe.loc[indices, 'component_name']=value pass @digest(form=form) def set_component_id_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'component_id']=value + if is_all(indices): + item.atoms_dataframe['component_id']=value + else: + item.atoms_dataframe.loc[indices, 'component_id']=value pass @digest(form=form) def set_component_type_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'component_type']=value + if is_all(indices): + item.atoms_dataframe['component_type']=value + else: + item.atoms_dataframe.loc[indices, 'component_type']=value pass @digest(form=form) def set_molecule_index_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'molecule_index']=value + if is_all(indices): + item.atoms_dataframe['molecule_index']=value + else: + item.atoms_dataframe.loc[indices, 'molecule_index']=value pass @digest(form=form) def set_molecule_name_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'molecule_name']=value + if is_all(indices): + item.atoms_dataframe['molecule_name']=value + else: + item.atoms_dataframe.loc[indices, 'molecule_name']=value pass @digest(form=form) def set_molecule_id_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'molecule_id']=value + if is_all(indices): + item.atoms_dataframe['molecule_id']=value + else: + item.atoms_dataframe.loc[indices, 'molecule_id']=value pass @digest(form=form) def set_molecule_type_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'molecule_type']=value + if is_all(indices): + item.atoms_dataframe['molecule_type']=value + else: + item.atoms_dataframe.loc[indices, 'molecule_type']=value pass @digest(form=form) def set_chain_index_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'chain_index']=value + if is_all(indices): + item.atoms_dataframe['chain_index']=value + else: + item.atoms_dataframe.loc[indices, 'chain_index']=value pass @digest(form=form) def set_chain_name_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'chain_name']=value + if is_all(indices): + item.atoms_dataframe['chain_name']=value + else: + item.atoms_dataframe.loc[indices, 'chain_name']=value pass @digest(form=form) def set_chain_id_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'chain_id']=value + if is_all(indices): + item.atoms_dataframe['chain_id']=value + item.atoms_dataframe['chain_index']=0 + else: + item.atoms_dataframe.loc[indices, 'chain_id']=value + aux_list = item.atoms_dataframe['chain_id'].unique() + for new_index, id for enumerate(aux_list): + item[item['chain_id']==id]=new_index pass @digest(form=form) def set_chain_type_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'chain_type']=value + if is_all(indices): + item.atoms_dataframe['chain_type']=value + else: + item.atoms_dataframe.loc[indices, 'chain_type']=value pass @digest(form=form) def set_entity_index_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'entity_index']=value + if is_all(indices): + item.atoms_dataframe['entity_index']=value + else: + item.atoms_dataframe.loc[indices, 'entity_index']=value pass @digest(form=form) def set_entity_name_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'entity_name']=value + if is_all(indices): + item.atoms_dataframe['entity_name']=value + else: + item.atoms_dataframe.loc[indices, 'entity_name']=value pass @digest(form=form) def set_entity_id_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'entity_id']=value + if is_all(indices): + item.atoms_dataframe['entity_id']=value + else: + item.atoms_dataframe.loc[indices, 'entity_id']=value pass @digest(form=form) def set_entity_type_to_atom(item, indices='all', value=None): - item.atoms_dataframe.loc[indices, 'entity_type']=value + if is_all(indices): + item.atoms_dataframe['entity_type']=value + else: + item.atoms_dataframe.loc[indices, 'entity_type']=value pass diff --git a/sandbox/Test.ipynb b/sandbox/Test.ipynb new file mode 100644 index 000000000..ef5446ab0 --- /dev/null +++ b/sandbox/Test.ipynb @@ -0,0 +1,199 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "85d75849-5640-43b7-8b93-87cdbf6b8792", + "metadata": {}, + "source": [ + "# Test" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cc406dee-f551-4e0a-a80c-0917a334b0d6", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "5ee221e5bfd941d8a9a226c8d46b16fc", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import molsysmt as msm" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5b9602f9-f45d-4be0-bd00-8c1302b9c53b", + "metadata": {}, + "outputs": [], + "source": [ + "molsys = msm.systems.demo['TcTIM']['1tcd.mmtf']" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "d4041918-a6de-4ccd-a980-3050c2b558a0", + "metadata": {}, + "outputs": [], + "source": [ + "molsys = msm.convert(molsys)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6ef1b26d-c66e-4af4-a99d-7b9e0e62ea82", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['A', 'A', 'A', ..., 'D', 'D', 'D'], dtype=object)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "msm.get(molsys, element='atom', chain_id=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4dd69a1a-a055-4424-b253-c728a8868291", + "metadata": {}, + "outputs": [], + "source": [ + "msm.set(molsys, element='atom', chain_id='X')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b277b085-aebd-4608-b0db-0ac6441d866b", + "metadata": {}, + "outputs": [], + "source": [ + "msm.get(molsys, element='atom', chain_id=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ed66999f-e475-47e3-af87-c93a67cb855c", + "metadata": {}, + "outputs": [], + "source": [ + "msm.info(molsys, element='chain')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1e6dabfa-6fd2-46b8-8b58-bbec76b80e4b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['A', 'B', 'C', 'D'], dtype=object)" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "molsys.topology.atoms_dataframe['chain_id'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "9a97d171-7f8e-4e66-ab74-5b2c8dba6fc2", + "metadata": {}, + "outputs": [], + "source": [ + "molsys.topology.atoms_dataframe.loc[100, 'chain_id']='X'" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "4212252d-a97d-4e97-bc70-d4f7ae682b0e", + "metadata": {}, + "outputs": [], + "source": [ + "aux = molsys.topology.atoms_dataframe['chain_id'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "44ef4628-032b-4f6a-bcd4-ceb7eac05082", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 A\n", + "1 X\n", + "2 B\n", + "3 C\n", + "4 D\n" + ] + } + ], + "source": [ + "for ii,jj in enumerate(aux):\n", + " print(ii,jj)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8aae1876-4f18-4a82-87c0-278502e385d3", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/sandbox/Test_flip.ipynb b/sandbox/Test_flip.ipynb deleted file mode 100644 index afbdbc63a..000000000 --- a/sandbox/Test_flip.ipynb +++ /dev/null @@ -1,127 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "85d75849-5640-43b7-8b93-87cdbf6b8792", - "metadata": {}, - "source": [ - "# Test" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "cc406dee-f551-4e0a-a80c-0917a334b0d6", - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "5efd6e6ba9bd4d16819d63e5e088170e", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "import molsysmt as msm" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "5b9602f9-f45d-4be0-bd00-8c1302b9c53b", - "metadata": {}, - "outputs": [], - "source": [ - "molsys = msm.systems.demo['TcTIM']['1tcd.mmtf']" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "6ef1b26d-c66e-4af4-a99d-7b9e0e62ea82", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array(['A', 'A', 'A', ..., 'D', 'D', 'D'], dtype=' 1\u001b[0m \u001b[43mmsm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mset\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmolsys\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43melement\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43matom\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mchain_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mX\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/projects@uibcdf/MolSysMT/molsysmt/_private/digestion/digest.py:117\u001b[0m, in \u001b[0;36mdigest..digestor..wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(all_args[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mself\u001b[39m\u001b[38;5;124m'\u001b[39m], \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mfinal_args)\n\u001b[1;32m 116\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 117\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mfinal_args\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/projects@uibcdf/MolSysMT/molsysmt/basic/set.py:168\u001b[0m, in \u001b[0;36mset\u001b[0;34m(molecular_system, element, selection, structure_indices, syntax, **kwargs)\u001b[0m\n\u001b[1;32m 166\u001b[0m item, form \u001b[38;5;241m=\u001b[39m where_is_attribute(molecular_system, in_attribute, check_if_None\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 167\u001b[0m in_value \u001b[38;5;241m=\u001b[39m value_of_in_attribute[in_attribute]\n\u001b[0;32m--> 168\u001b[0m set_function \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mgetattr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m_dict_modules\u001b[49m\u001b[43m[\u001b[49m\u001b[43mform\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mset_\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43min_attribute\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m_to_\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[43melement\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 169\u001b[0m set_function(item, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mdict_indices, value\u001b[38;5;241m=\u001b[39min_value)\n\u001b[1;32m 171\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n", - "\u001b[0;31mAttributeError\u001b[0m: module 'molsysmt.form.file_mmtf' has no attribute 'set_chain_id_to_atom'" - ] - } - ], - "source": [ - "msm.set(molsys, element='atom', chain_id='X')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b277b085-aebd-4608-b0db-0ac6441d866b", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "65532330-ce76-4593-819a-bafcae238479", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.10.12" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}