molutils 0.2.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. {molutils-0.2.0 → molutils-0.4.0}/PKG-INFO +2 -2
  2. {molutils-0.2.0 → molutils-0.4.0}/molutils/__init__.py +4 -1
  3. {molutils-0.2.0 → molutils-0.4.0}/molutils/__main__.py +1 -1
  4. {molutils-0.2.0 → molutils-0.4.0}/molutils/_core/count.py +19 -1
  5. molutils-0.4.0/molutils/_core/extract.py +123 -0
  6. molutils-0.4.0/molutils/_core/list_.py +49 -0
  7. molutils-0.4.0/molutils/_parsers/chain_resid.py +34 -0
  8. {molutils-0.2.0 → molutils-0.4.0}/molutils/_parsers/parser_pdb.py +8 -0
  9. molutils-0.4.0/molutils/_ui/app_main.py +54 -0
  10. molutils-0.4.0/molutils/_ui/app_subcommand.py +23 -0
  11. {molutils-0.2.0 → molutils-0.4.0}/molutils/_ui/fy_help.fyh +20 -4
  12. molutils-0.4.0/molutils/_ui/fy_rules.fyr +47 -0
  13. molutils-0.4.0/molutils/_version.py +1 -0
  14. {molutils-0.2.0 → molutils-0.4.0}/molutils.egg-info/PKG-INFO +2 -2
  15. {molutils-0.2.0 → molutils-0.4.0}/molutils.egg-info/SOURCES.txt +3 -1
  16. molutils-0.4.0/molutils.egg-info/requires.txt +1 -0
  17. {molutils-0.2.0 → molutils-0.4.0}/setup.py +1 -1
  18. molutils-0.2.0/molutils/_core/extract.py +0 -44
  19. molutils-0.2.0/molutils/_core/list_.py +0 -16
  20. molutils-0.2.0/molutils/_ui/app.py +0 -123
  21. molutils-0.2.0/molutils/_ui/fy_rules.fyr +0 -29
  22. molutils-0.2.0/molutils/_version.py +0 -1
  23. molutils-0.2.0/molutils.egg-info/requires.txt +0 -1
  24. {molutils-0.2.0 → molutils-0.4.0}/LICENSE.md +0 -0
  25. {molutils-0.2.0 → molutils-0.4.0}/README.md +0 -0
  26. {molutils-0.2.0 → molutils-0.4.0}/molutils/_core/__init__.py +0 -0
  27. {molutils-0.2.0 → molutils-0.4.0}/molutils/_parsers/__init__.py +0 -0
  28. {molutils-0.2.0 → molutils-0.4.0}/molutils/_ui/__init__.py +0 -0
  29. {molutils-0.2.0 → molutils-0.4.0}/molutils.egg-info/dependency_links.txt +0 -0
  30. {molutils-0.2.0 → molutils-0.4.0}/molutils.egg-info/entry_points.txt +0 -0
  31. {molutils-0.2.0 → molutils-0.4.0}/molutils.egg-info/top_level.txt +0 -0
  32. {molutils-0.2.0 → molutils-0.4.0}/pyproject.toml +0 -0
  33. {molutils-0.2.0 → molutils-0.4.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: molutils
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Collection of tools for preprocessing or summarizing details from biomolecular structure files.
5
5
  Home-page: https://github.com/diegobarmor/molutils
6
6
  Author: DiegoBarMor
@@ -12,7 +12,7 @@ Classifier: Operating System :: OS Independent
12
12
  Requires-Python: >=3.10
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE.md
15
- Requires-Dist: freyacli==0.3.1
15
+ Requires-Dist: freyacli==0.4.0
16
16
  Dynamic: author
17
17
  Dynamic: author-email
18
18
  Dynamic: classifier
@@ -1,9 +1,12 @@
1
1
  from ._version import __version__
2
2
 
3
3
  from ._parsers.parser_pdb import ParserPDB
4
+ from ._parsers.chain_resid import ChainResid
5
+
6
+ from ._ui.app_subcommand import AppSubcommand
4
7
 
5
8
  from ._core.list_ import List
6
9
  from ._core.count import Count
7
10
  from ._core.extract import Extract
8
11
 
9
- from ._ui.app import App
12
+ from ._ui.app_main import AppMain
@@ -9,7 +9,7 @@ except ImportError:
9
9
 
10
10
  # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
11
  def main():
12
- mu.App(sys.argv).run()
12
+ mu.AppMain(sys.argv).run()
13
13
 
14
14
 
15
15
  ################################################################################
@@ -1,8 +1,26 @@
1
1
  from pathlib import Path
2
2
 
3
+ import freyacli as fy
4
+ import molutils as mu
5
+
3
6
  # //////////////////////////////////////////////////////////////////////////////
4
- class Count:
7
+ class Count(mu.AppSubcommand):
8
+ # -------------------------------------------------------------------------- UI SECTION
9
+ def run(self):
10
+ command = self.main.subcommands.pop(0)
11
+
12
+ if command == "models": return self.app_count_models()
13
+
14
+ raise ValueError(f"Unknown command: {command}")
15
+
16
+
5
17
  # --------------------------------------------------------------------------
18
+ def app_count_models(self):
19
+ path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
20
+ print(mu.Count.models(path_in))
21
+
22
+
23
+ # -------------------------------------------------------------------------- LOGIC SECTION
6
24
  @classmethod
7
25
  def models(cls, path_pdb: Path) -> int:
8
26
  data = path_pdb.read_text()
@@ -0,0 +1,123 @@
1
+ from collections import defaultdict
2
+
3
+ import freyacli as fy
4
+ import molutils as mu
5
+
6
+ # //////////////////////////////////////////////////////////////////////////////
7
+ class Extract(mu.AppSubcommand):
8
+ # -------------------------------------------------------------------------- UI SECTION
9
+ def run(self):
10
+ command = self.main.subcommands.pop(0)
11
+
12
+ if command == "models" : return self.app_extract_models()
13
+ if command == "chains" : return self.app_extract_chains()
14
+ if command == "residue": return self.app_extract_residue()
15
+
16
+ raise ValueError(f"Unknown command: {command}")
17
+
18
+ # --------------------------------------------------------------------------
19
+ def app_extract_models(self):
20
+ path_in, folder_out = self._io_filein_dirout()
21
+ data_pdb = path_in.read_text()
22
+
23
+ if self.main.get_arg_bool("first_only"):
24
+ _, model = mu.Extract.next_model(data_pdb)
25
+ path_out = folder_out / f"{path_in.stem}.m0.pdb"
26
+ path_out.write_text(model)
27
+ return
28
+
29
+ for i, model in enumerate(mu.Extract.iter_models(data_pdb)):
30
+ path_out = folder_out / f"{path_in.stem}.m{i:03}.pdb"
31
+ path_out.write_text(model)
32
+
33
+
34
+ # --------------------------------------------------------------------------
35
+ def app_extract_chains(self):
36
+ path_in, folder_out = self._io_filein_dirout()
37
+ data_pdb = path_in.read_text()
38
+
39
+ chains = mu.Extract.split_chains(data_pdb)
40
+
41
+ chain_ids = mu.List.chains(path_in, first_only = True) \
42
+ if self.main.get_arg_bool("first_only") else chains.keys()
43
+
44
+ for chain_id in chain_ids:
45
+ path_out = folder_out / f"{path_in.stem}.{chain_id}.pdb"
46
+ path_out.write_text(chains[chain_id])
47
+
48
+
49
+ # --------------------------------------------------------------------------
50
+ def app_extract_residue(self):
51
+ path_in, folder_out = self._io_filein_dirout()
52
+ data_pdb = path_in.read_text()
53
+
54
+ residue_dotstr = self.main.get_arg_str("residue")
55
+ chres = mu.ChainResid.from_dotstr(residue_dotstr)
56
+ extracted = mu.Extract.residue(data_pdb, chres.resid, chres.chain)
57
+
58
+ path_out = folder_out / f"{path_in.stem}.{residue_dotstr}.pdb"
59
+ path_out.write_text(extracted)
60
+
61
+
62
+ # -------------------------------------------------------------------------- LOGIC SECTION
63
+ @classmethod
64
+ def next_model(cls, data: str, start: int = 0) -> tuple[int, str]:
65
+ idx_0 = data.find("\nMODEL", start)
66
+ if idx_0 == -1: return -1, data
67
+
68
+ idx_1 = data.find("\nENDMDL", idx_0)
69
+ if idx_1 == -1: return -1, data[idx_0:]
70
+
71
+ idx_1 += len("\nENDMDL")
72
+ return idx_1, data[idx_0:idx_1] + "\nEND"
73
+
74
+
75
+ # --------------------------------------------------------------------------
76
+ @classmethod
77
+ def iter_models(cls, data: str):
78
+ while data:
79
+ idx, model = cls.next_model(data)
80
+ yield model
81
+ if idx == -1: break
82
+ data = data[idx:]
83
+
84
+
85
+ # --------------------------------------------------------------------------
86
+ @classmethod
87
+ def split_chains(cls, data: str) -> dict[str, str]:
88
+ pdb = mu.ParserPDB(data)
89
+ chains = defaultdict(list)
90
+ for line in pdb.iter_atoms():
91
+ chain_id = mu.ParserPDB.get_chainid(line)
92
+ chains[chain_id].append(line)
93
+ return {
94
+ chain_id: mu.ParserPDB.join_lines(lines)
95
+ for chain_id, lines in chains.items()
96
+ }
97
+
98
+
99
+ # --------------------------------------------------------------------------
100
+ @classmethod
101
+ def residue(cls, data: str, resid: str, chain: str = None):
102
+ pdb = mu.ParserPDB(data)
103
+ gen_residue = (
104
+ line for line in pdb.iter_atoms()
105
+ if mu.ParserPDB.get_resid(line) == resid
106
+ )
107
+ if chain is not None: gen_residue = (
108
+ line for line in gen_residue
109
+ if mu.ParserPDB.get_chainid(line) == chain
110
+ )
111
+ return mu.ParserPDB.join_lines(gen_residue)
112
+
113
+
114
+ # --------------------------------------------------------------------------
115
+ def _io_filein_dirout(self):
116
+ path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
117
+ folder_out = self.main.get_arg_path("folder_out",
118
+ default = path_in.parent, assertion = fy.PathAssertion.DIR_OUT
119
+ )
120
+ return path_in, folder_out
121
+
122
+
123
+ # //////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,49 @@
1
+ from pathlib import Path
2
+
3
+ import freyacli as fy
4
+ import molutils as mu
5
+
6
+ # //////////////////////////////////////////////////////////////////////////////
7
+ class List(mu.AppSubcommand):
8
+ # -------------------------------------------------------------------------- UI SECTION
9
+ def run(self):
10
+ command = self.main.subcommands.pop(0)
11
+
12
+ if command == "chains": return self.app_list_chains()
13
+ if command == "residues": return self.app_list_residues()
14
+
15
+ raise ValueError(f"Unknown command: {command}")
16
+
17
+
18
+ # --------------------------------------------------------------------------
19
+ def app_list_chains(self):
20
+ path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
21
+ first_only = self.main.get_arg_bool("first_only")
22
+ print(*mu.List.chains(path_in, first_only))
23
+
24
+
25
+ # --------------------------------------------------------------------------
26
+ def app_list_residues(self):
27
+ path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
28
+ print(*mu.List.residues(path_in))
29
+
30
+
31
+ # -------------------------------------------------------------------------- LOGIC SECTION
32
+ @classmethod
33
+ def chains(cls, path_pdb: Path, first_only: bool = False) -> list[str]:
34
+ pdb = mu.ParserPDB.from_file(path_pdb)
35
+ gen_chain_ids = (mu.ParserPDB.get_chainid(line) for line in pdb.iter_atoms())
36
+ if first_only: return mu.ParserPDB.safe_next(gen_chain_ids)
37
+ return sorted(set(gen_chain_ids))
38
+
39
+
40
+ # --------------------------------------------------------------------------
41
+ @classmethod
42
+ def residues(cls, path_pdb: Path) -> list[str]:
43
+ """Returns list of unique residue identifiers in the format "chainid.resid"."""
44
+ pdb = mu.ParserPDB.from_file(path_pdb)
45
+ gen_residues = (mu.ChainResid.from_pdb(line).get_dotstr() for line in pdb.iter_atoms())
46
+ return sorted(set(gen_residues))
47
+
48
+
49
+ # //////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,34 @@
1
+ import molutils as mu
2
+
3
+ # //////////////////////////////////////////////////////////////////////////////
4
+ class ChainResid:
5
+ def __init__(self, chain: str, resid: str):
6
+ self.chain = chain
7
+ self.resid = resid
8
+
9
+
10
+ # --------------------------------------------------------------------------
11
+ @classmethod
12
+ def from_pdb(cls, line: str) -> "ChainResid":
13
+ """Parses an ATOM/HETATM line (PDB format) and returns a ChainResid instance."""
14
+ return cls(mu.ParserPDB.get_chainid(line), mu.ParserPDB.get_resid(line))
15
+
16
+
17
+ # --------------------------------------------------------------------------
18
+ @classmethod
19
+ def from_dotstr(cls, dotstr: str) -> "ChainResid":
20
+ """Parses a string in the format `chainid.resid`."""
21
+ parts = dotstr.split(".")
22
+ if len(parts) != 2:
23
+ raise ValueError(f"Invalid format for ChainResid: {dotstr}. Expected format: `chainid.resid`.")
24
+ chain, resid = parts
25
+ return cls(chain, resid)
26
+
27
+
28
+ # --------------------------------------------------------------------------
29
+ def get_dotstr(self) -> str:
30
+ """Returns a string in the format `chainid.resid`."""
31
+ return f"{self.chain}.{self.resid}"
32
+
33
+
34
+ # //////////////////////////////////////////////////////////////////////////////
@@ -4,6 +4,8 @@ from pathlib import Path
4
4
  class ParserPDB:
5
5
  LENGTH_RECORD = 80
6
6
  IDX_CHAIN_ID = 21
7
+ IDX0_RESID = 22
8
+ IDX1_RESID = 26
7
9
 
8
10
  # --------------------------------------------------------------------------
9
11
  def __init__(self, data_pdb: str):
@@ -44,4 +46,10 @@ class ParserPDB:
44
46
  except IndexError: raise ValueError(f"Unexpected format of ATOM records in PDB file")
45
47
 
46
48
 
49
+ # --------------------------------------------------------------------------
50
+ @classmethod
51
+ def get_resid(cls, line: str) -> str:
52
+ return line[cls.IDX0_RESID:cls.IDX1_RESID].strip()
53
+
54
+
47
55
  # //////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,54 @@
1
+ from pathlib import Path
2
+
3
+ import freyacli as fy
4
+ import molutils as mu
5
+
6
+ # //////////////////////////////////////////////////////////////////////////////
7
+ class AppMain(fy.App):
8
+ _APP_NAME = "molutils"
9
+ _VERSION = mu.__version__
10
+
11
+ # --------------------------------------------------------------------------
12
+ def __init__(self, argv: list[str]):
13
+ dir_ui = Path(__file__).parent
14
+ super().__init__(
15
+ args = argv,
16
+ path_fyr = dir_ui / "fy_rules.fyr",
17
+ path_fyh = dir_ui / "fy_help.fyh",
18
+ )
19
+ self.subcommands = self.get_path_to_root()
20
+
21
+
22
+ # --------------------------------------------------------------------------
23
+ def run(self):
24
+ command = self.subcommands.pop(0)
25
+ if command == "list" : return mu.List(self).run()
26
+ if command == "count" : return mu.Count(self).run()
27
+ if command == "extract": return mu.Extract(self).run()
28
+ if command == "select" : return self._run_select()
29
+ if command == "merge" : return self._run_merge()
30
+ raise ValueError(f"Unknown command: {command}")
31
+
32
+
33
+ # --------------------------------------------------------------------------
34
+ def _run_select(self):
35
+ query = self.get_arg_str("query")
36
+ path_in = self.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
37
+ path_out = self.get_arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
38
+
39
+ import MDAnalysis as mda
40
+ mda.Universe(path_in).select_atoms(query).write(path_out)
41
+
42
+
43
+ # --------------------------------------------------------------------------
44
+ def _run_merge(self):
45
+ paths_in = self.get_arg_path("paths_in", assertion = fy.PathAssertion.FILE_IN, is_list = True)
46
+ path_out = self.get_arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
47
+ data = mu.ParserPDB.join_lines(
48
+ line for path_in in paths_in
49
+ for line in mu.ParserPDB.from_file(path_in).iter_atoms()
50
+ )
51
+ path_out.write_text(data)
52
+
53
+
54
+ # //////////////////////////////////////////////////////////////////////////////
@@ -0,0 +1,23 @@
1
+ from abc import ABC, abstractmethod
2
+
3
+ import molutils as mu
4
+
5
+ # //////////////////////////////////////////////////////////////////////////////
6
+ class AppSubcommand(ABC):
7
+ """
8
+ Base class for all subcommands of the molutils suite of applications.
9
+ They hold a reference to the unique `AppMain` instance, which they can use to access the CLI arguments.
10
+ """
11
+
12
+ # --------------------------------------------------------------------------
13
+ def __init__(self, app_main: "mu.AppMain"):
14
+ self.main = app_main
15
+
16
+
17
+ # --------------------------------------------------------------------------
18
+ @abstractmethod
19
+ def run(self):
20
+ raise NotImplementedError()
21
+
22
+
23
+ # //////////////////////////////////////////////////////////////////////////////
@@ -7,6 +7,10 @@
7
7
  !fyh:path_in Path of a PDB file.
8
8
  !fyh:first_only Use this flag for displaying only the first chain of the structure.
9
9
  !@
10
+ @residues
11
+ !fyh:_ Lists the residues in a PDB file in the format "chainid.resid".
12
+ !fyh:path_in Path of a PDB file.
13
+ !@
10
14
  !@
11
15
 
12
16
  @count
@@ -21,14 +25,19 @@
21
25
  !fyh:_ Extract specific sections of data from biomolecular structures.
22
26
  @models
23
27
  !fyh:_ Extracts the models in a PDB file into separate files.
24
- !use:_extract
28
+ !use:_io_filein_dirout
25
29
  !fyh:first_only Use this flag for extracting only the first model of the structure and disregard the rest.
26
30
  !@
27
31
  @chains
28
32
  !fyh:_ Extracts the chains in a PDB file into separate files.
29
- !use:_extract
33
+ !use:_io_filein_dirout
30
34
  !fyh:first_only Use this flag for extracting only the first chain of the structure and disregard the rest.
31
35
  !@
36
+ @residue
37
+ !fyh:_ Extract a specific residue from a PDB file into a separate file.
38
+ !use:_io_filein_dirout
39
+ !fyh:residue Residue to extract, in the format "chainid.resid".
40
+ !@
32
41
  !@
33
42
 
34
43
  @select
@@ -38,8 +47,15 @@
38
47
  !fyh:path_out Path for the output selected atoms. Can be anything supported by MDanalysis.
39
48
  !@
40
49
 
41
- !def _extract
50
+ @merge
51
+ !fyh:_ Merge structures from multiple PDB files into a single PDB file.
52
+ !fyh:paths_in List of PDB file paths with the input structures to be merged.
53
+ At least one PDB file must be provided.
54
+ !fyh:path_out Path where to save the merged PDB structure.
55
+ !@
56
+
57
+ !def _io_filein_dirout
42
58
  !fyh:path_in Path of a PDB file.
43
- !fyh:folder_out Path of the directory where to place the unpacked structures.
59
+ !fyh:folder_out Path of the directory where to place the unpacked structure(s).
44
60
  If not provided, the parent directory of the input PDB file will be used.
45
61
  !fed
@@ -0,0 +1,47 @@
1
+ @list
2
+ @chains
3
+ path_in[]1.PATH
4
+ ~first_only[f,first-only]
5
+ !@
6
+ @residues
7
+ path_in[]1.PATH
8
+ !@
9
+ !@
10
+
11
+ @count
12
+ @models
13
+ path_in[]1.PATH
14
+ !@
15
+ !@
16
+
17
+ @extract
18
+ @models
19
+ !use:_io_filein_dirout
20
+ ~first_only[f,first-only]
21
+ !@
22
+ @chains
23
+ !use:_io_filein_dirout
24
+ ~first_only[f,first-only]
25
+ !@
26
+ @residue
27
+ path_in[]1.PATH
28
+ residue[]1.STR
29
+ ~folder_out[o,out]1.PATH
30
+ !@
31
+ !@
32
+
33
+ @select
34
+ query[]1.STR
35
+ path_in[]1.PATH
36
+ path_out[]1.PATH
37
+ !@
38
+
39
+ @merge
40
+ paths_in[]1+.PATH
41
+ path_out[o,out]1.PATH
42
+ !@
43
+
44
+ !def _io_filein_dirout
45
+ path_in[]1.PATH
46
+ ~folder_out[]1.PATH
47
+ !fed
@@ -0,0 +1 @@
1
+ __version__ = "0.4.0"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: molutils
3
- Version: 0.2.0
3
+ Version: 0.4.0
4
4
  Summary: Collection of tools for preprocessing or summarizing details from biomolecular structure files.
5
5
  Home-page: https://github.com/diegobarmor/molutils
6
6
  Author: DiegoBarMor
@@ -12,7 +12,7 @@ Classifier: Operating System :: OS Independent
12
12
  Requires-Python: >=3.10
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE.md
15
- Requires-Dist: freyacli==0.3.1
15
+ Requires-Dist: freyacli==0.4.0
16
16
  Dynamic: author
17
17
  Dynamic: author-email
18
18
  Dynamic: classifier
@@ -16,8 +16,10 @@ molutils/_core/count.py
16
16
  molutils/_core/extract.py
17
17
  molutils/_core/list_.py
18
18
  molutils/_parsers/__init__.py
19
+ molutils/_parsers/chain_resid.py
19
20
  molutils/_parsers/parser_pdb.py
20
21
  molutils/_ui/__init__.py
21
- molutils/_ui/app.py
22
+ molutils/_ui/app_main.py
23
+ molutils/_ui/app_subcommand.py
22
24
  molutils/_ui/fy_help.fyh
23
25
  molutils/_ui/fy_rules.fyr
@@ -0,0 +1 @@
1
+ freyacli==0.4.0
@@ -21,7 +21,7 @@ setup(
21
21
  "_ui/fy_rules.fyr", "_ui/fy_help.fyh",
22
22
  ],
23
23
  },
24
- install_requires=["freyacli==0.3.1"],
24
+ install_requires=["freyacli==0.4.0"],
25
25
  entry_points={
26
26
  "console_scripts": [
27
27
  "molutils=molutils.__main__:main",
@@ -1,44 +0,0 @@
1
- from collections import defaultdict
2
-
3
- import molutils as mu
4
-
5
- # //////////////////////////////////////////////////////////////////////////////
6
- class Extract:
7
- # --------------------------------------------------------------------------
8
- @classmethod
9
- def next_model(cls, data: str, start: int = 0) -> tuple[int, str]:
10
- idx_0 = data.find("\nMODEL", start)
11
- if idx_0 == -1: return -1, data
12
-
13
- idx_1 = data.find("\nENDMDL", idx_0)
14
- if idx_1 == -1: return -1, data[idx_0:]
15
-
16
- idx_1 += len("\nENDMDL")
17
- return idx_1, data[idx_0:idx_1] + "\nEND"
18
-
19
-
20
- # --------------------------------------------------------------------------
21
- @classmethod
22
- def iter_models(cls, data: str):
23
- while data:
24
- idx, model = cls.next_model(data)
25
- yield model
26
- if idx == -1: break
27
- data = data[idx:]
28
-
29
-
30
- # --------------------------------------------------------------------------
31
- @classmethod
32
- def split_chains(cls, data: str) -> dict[str, str]:
33
- pdb = mu.ParserPDB(data)
34
- chains = defaultdict(list)
35
- for line in pdb.iter_atoms():
36
- chain_id = mu.ParserPDB.get_chainid(line)
37
- chains[chain_id].append(line)
38
- return {
39
- chain_id: mu.ParserPDB.join_lines(lines)
40
- for chain_id, lines in chains.items()
41
- }
42
-
43
-
44
- # //////////////////////////////////////////////////////////////////////////////
@@ -1,16 +0,0 @@
1
- from pathlib import Path
2
-
3
- import molutils as mu
4
-
5
- # //////////////////////////////////////////////////////////////////////////////
6
- class List:
7
- # --------------------------------------------------------------------------
8
- @classmethod
9
- def chains(cls, path_pdb: Path, first_only: bool = False) -> list[str]:
10
- pdb = mu.ParserPDB.from_file(path_pdb)
11
- gen_chain_ids = (mu.ParserPDB.get_chainid(line) for line in pdb.iter_atoms())
12
- if first_only: return mu.ParserPDB.safe_next(gen_chain_ids)
13
- return sorted(set(gen_chain_ids))
14
-
15
-
16
- # //////////////////////////////////////////////////////////////////////////////
@@ -1,123 +0,0 @@
1
- from pathlib import Path
2
-
3
- import molutils as mu
4
-
5
- import freyacli as fy
6
-
7
-
8
- # //////////////////////////////////////////////////////////////////////////////
9
- class App(fy.App):
10
- _APP_NAME = "molutils"
11
- _VERSION = mu.__version__
12
-
13
- # --------------------------------------------------------------------------
14
- def __init__(self, argv: list[str]):
15
- dir_ui = Path(__file__).parent
16
- super().__init__(
17
- args = argv,
18
- path_fyr = dir_ui / "fy_rules.fyr",
19
- path_fyh = dir_ui / "fy_help.fyh",
20
- )
21
- self.subcommands = self.get_path_to_root()
22
-
23
-
24
- # --------------------------------------------------------------------------
25
- def run(self):
26
- command = self.subcommands.pop(0)
27
- if command == "list" : return self._run_list()
28
- if command == "count" : return self._run_count()
29
- if command == "extract": return self._run_extract()
30
- if command == "select" : return self._run_select()
31
- raise ValueError(f"Unknown command: {command}")
32
-
33
-
34
- # --------------------------------------------------------------------------
35
- def _run_list(self):
36
- def list_chains():
37
- path_in = self.get_arg_path("path_in")
38
- first_only = self.get_arg_bool("first_only")
39
- self.assert_file_in(path_in)
40
- print(*mu.List.chains(path_in, first_only))
41
-
42
- command = self.subcommands.pop(0)
43
-
44
- if command == "chains": return list_chains()
45
-
46
- raise ValueError(f"Unknown command: {command}")
47
-
48
-
49
- # --------------------------------------------------------------------------
50
- def _run_count(self):
51
- def count_models():
52
- path_in = self.get_arg_path("path_in")
53
- self.assert_file_in(path_in)
54
- print(mu.Count.models(path_in))
55
-
56
-
57
- command = self.subcommands.pop(0)
58
-
59
- if command == "models": return count_models()
60
-
61
- raise ValueError(f"Unknown command: {command}")
62
-
63
-
64
- # --------------------------------------------------------------------------
65
- def _run_extract(self):
66
- def extract_models():
67
- path_in = self.get_arg_path("path_in")
68
- self.assert_file_in(path_in)
69
-
70
- folder_out = self.get_arg_path("folder_out", default = path_in.parent)
71
- self.assert_dir_out(folder_out)
72
-
73
- data_pdb = path_in.read_text()
74
-
75
- if self.get_arg_bool("first_only"):
76
- _, model = mu.Extract.next_model(data_pdb)
77
- path_out = folder_out / f"{path_in.stem}.m0.pdb"
78
- path_out.write_text(model)
79
- return
80
-
81
- for i, model in enumerate(mu.Extract.iter_models(data_pdb)):
82
- path_out = folder_out / f"{path_in.stem}.m{i:03}.pdb"
83
- path_out.write_text(model)
84
-
85
- def extract_chains():
86
- path_in = self.get_arg_path("path_in")
87
- self.assert_file_in(path_in)
88
-
89
- folder_out = self.get_arg_path("folder_out", default = path_in.parent)
90
- self.assert_dir_out(folder_out)
91
-
92
- data_pdb = path_in.read_text()
93
-
94
- chains = mu.Extract.split_chains(data_pdb)
95
-
96
- chain_ids = mu.List.chains(path_in, first_only = True) \
97
- if self.get_arg_bool("first_only") else chains.keys()
98
-
99
- for chain_id in chain_ids:
100
- path_out = folder_out / f"{path_in.stem}.{chain_id}.pdb"
101
- path_out.write_text(chains[chain_id])
102
-
103
-
104
- command = self.subcommands.pop(0)
105
-
106
- if command == "models": return extract_models()
107
- if command == "chains": return extract_chains()
108
-
109
- raise ValueError(f"Unknown command: {command}")
110
-
111
-
112
- # --------------------------------------------------------------------------
113
- def _run_select(self):
114
- query = self.get_arg_str("query")
115
- path_in = self.get_arg_path("path_in")
116
- path_out = self.get_arg_path("path_out")
117
- self.assert_file_in(path_in)
118
-
119
- import MDAnalysis as mda
120
- mda.Universe(path_in).select_atoms(query).write(path_out)
121
-
122
-
123
- # //////////////////////////////////////////////////////////////////////////////
@@ -1,29 +0,0 @@
1
- @list
2
- @chains
3
- path_in[]1.PATH
4
- ~first_only[f,first-only]
5
- !@
6
- !@
7
-
8
- @count
9
- @models
10
- path_in[]1.PATH
11
- !@
12
- !@
13
-
14
- @extract
15
- @models !use:_extract !@
16
- @chains !use:_extract !@
17
- !@
18
-
19
- @select
20
- query[]1.STR
21
- path_in[]1.PATH
22
- path_out[]1.PATH
23
- !@
24
-
25
- !def _extract
26
- path_in[]1.PATH
27
- ~folder_out[]1.PATH
28
- ~first_only[f,first-only]
29
- !fed
@@ -1 +0,0 @@
1
- __version__ = "0.2.0"
@@ -1 +0,0 @@
1
- freyacli==0.3.1
File without changes
File without changes
File without changes
File without changes