molutils 0.5.0__tar.gz → 0.6.1__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 (32) hide show
  1. {molutils-0.5.0 → molutils-0.6.1}/PKG-INFO +3 -3
  2. {molutils-0.5.0 → molutils-0.6.1}/molutils/__init__.py +0 -3
  3. molutils-0.6.1/molutils/_core/count.py +119 -0
  4. {molutils-0.5.0 → molutils-0.6.1}/molutils/_core/extract.py +20 -81
  5. molutils-0.6.1/molutils/_core/list_.py +48 -0
  6. {molutils-0.5.0 → molutils-0.6.1}/molutils/_core/remove.py +19 -16
  7. {molutils-0.5.0 → molutils-0.6.1}/molutils/_ui/app_main.py +15 -10
  8. {molutils-0.5.0 → molutils-0.6.1}/molutils/_ui/fy_help.fyh +15 -3
  9. {molutils-0.5.0 → molutils-0.6.1}/molutils/_ui/fy_rules.fyr +4 -1
  10. molutils-0.6.1/molutils/_version.py +1 -0
  11. {molutils-0.5.0 → molutils-0.6.1}/molutils.egg-info/PKG-INFO +3 -3
  12. {molutils-0.5.0 → molutils-0.6.1}/molutils.egg-info/SOURCES.txt +0 -3
  13. molutils-0.6.1/molutils.egg-info/requires.txt +2 -0
  14. {molutils-0.5.0 → molutils-0.6.1}/setup.py +4 -1
  15. molutils-0.5.0/molutils/_core/count.py +0 -104
  16. molutils-0.5.0/molutils/_core/list_.py +0 -51
  17. molutils-0.5.0/molutils/_parsers/chain_resid.py +0 -34
  18. molutils-0.5.0/molutils/_parsers/parser_pdb.py +0 -80
  19. molutils-0.5.0/molutils/_ui/__init__.py +0 -0
  20. molutils-0.5.0/molutils/_version.py +0 -1
  21. molutils-0.5.0/molutils.egg-info/requires.txt +0 -1
  22. {molutils-0.5.0 → molutils-0.6.1}/LICENSE.md +0 -0
  23. {molutils-0.5.0 → molutils-0.6.1}/README.md +0 -0
  24. {molutils-0.5.0 → molutils-0.6.1}/molutils/__main__.py +0 -0
  25. {molutils-0.5.0 → molutils-0.6.1}/molutils/_core/__init__.py +0 -0
  26. {molutils-0.5.0/molutils/_parsers → molutils-0.6.1/molutils/_ui}/__init__.py +0 -0
  27. {molutils-0.5.0 → molutils-0.6.1}/molutils/_ui/app_subcommand.py +0 -0
  28. {molutils-0.5.0 → molutils-0.6.1}/molutils.egg-info/dependency_links.txt +0 -0
  29. {molutils-0.5.0 → molutils-0.6.1}/molutils.egg-info/entry_points.txt +0 -0
  30. {molutils-0.5.0 → molutils-0.6.1}/molutils.egg-info/top_level.txt +0 -0
  31. {molutils-0.5.0 → molutils-0.6.1}/pyproject.toml +0 -0
  32. {molutils-0.5.0 → molutils-0.6.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: molutils
3
- Version: 0.5.0
3
+ Version: 0.6.1
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,14 +12,14 @@ 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.4.1
15
+ Requires-Dist: molsimple==0.1.2
16
+ Requires-Dist: freyacli==0.5.0
16
17
  Dynamic: author
17
18
  Dynamic: author-email
18
19
  Dynamic: classifier
19
20
  Dynamic: description
20
21
  Dynamic: description-content-type
21
22
  Dynamic: home-page
22
- Dynamic: keywords
23
23
  Dynamic: license
24
24
  Dynamic: license-file
25
25
  Dynamic: requires-dist
@@ -1,8 +1,5 @@
1
1
  from ._version import __version__
2
2
 
3
- from ._parsers.parser_pdb import ParserPDB
4
- from ._parsers.chain_resid import ChainResid
5
-
6
3
  from ._ui.app_subcommand import AppSubcommand
7
4
 
8
5
  from ._core.list_ import List
@@ -0,0 +1,119 @@
1
+ import molsimple as ms
2
+ import freyacli as fy
3
+ import molutils as mu
4
+
5
+ # //////////////////////////////////////////////////////////////////////////////
6
+ class Count(mu.AppSubcommand):
7
+ # -------------------------------------------------------------------------- UI SECTION
8
+ def run(self):
9
+ command = self.main.subcommands.pop(0)
10
+
11
+ if command == "models" : return self.app_count_models()
12
+ if command == "chains" : return self.app_count_chains()
13
+ if command == "residues" : return self.app_count_residues()
14
+ if command == "particles": return self.app_count_particles()
15
+ if command == "frames" : return self.app_count_frames()
16
+ if command == "altlocs" : return self.app_count_altlocs()
17
+
18
+ raise ValueError(f"Unknown command: {command}")
19
+
20
+
21
+ # --------------------------------------------------------------------------
22
+ def app_count_models(self):
23
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
24
+ print(mu.Count.models(ms.System.read_pdb(path_in)))
25
+
26
+
27
+ # --------------------------------------------------------------------------
28
+ def app_count_chains(self):
29
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
30
+ print(mu.Count.chains(ms.System.read_pdb(path_in)))
31
+
32
+
33
+ # --------------------------------------------------------------------------
34
+ def app_count_residues(self):
35
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
36
+ print(mu.Count.residues(ms.System.read_pdb(path_in)))
37
+
38
+
39
+ # --------------------------------------------------------------------------
40
+ def app_count_particles(self):
41
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
42
+ print(mu.Count.particles(ms.System.read_pdb(path_in)))
43
+
44
+
45
+ # --------------------------------------------------------------------------
46
+ def app_count_frames(self):
47
+ path_struct = self.main.arg_path("path_struct", assertion = fy.PathAssertion.FILE_IN)
48
+ path_traj = self.main.arg_path("path_traj", assertion = fy.PathAssertion.FILE_IN)
49
+
50
+ import MDAnalysis as mda
51
+ args_traj = [str(path_traj)] if path_traj is not None else []
52
+ u = mda.Universe(str(path_struct), *args_traj)
53
+
54
+ nframes_valid, _ = mu.Count.frames(u)
55
+ print(f"{nframes_valid}")
56
+
57
+
58
+
59
+ # --------------------------------------------------------------------------
60
+ def app_count_altlocs(self):
61
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
62
+ print(mu.Count.altlocs(ms.System.read_pdb(path_in)))
63
+
64
+
65
+ # -------------------------------------------------------------------------- LOGIC SECTION
66
+ @classmethod
67
+ def models(cls, system: ms.System) -> int:
68
+ return len(system.models)
69
+
70
+
71
+ # --------------------------------------------------------------------------
72
+ @classmethod
73
+ def chains(cls, system: ms.System) -> int:
74
+ return len(mu.List.chains(system, do_sort = False))
75
+
76
+
77
+ # --------------------------------------------------------------------------
78
+ @classmethod
79
+ def residues(cls, system: ms.System) -> int:
80
+ return len(mu.List.residues(system, do_sort = False))
81
+
82
+
83
+ # --------------------------------------------------------------------------
84
+ @classmethod
85
+ def particles(cls, system: ms.System) -> int:
86
+ return len(system.particles)
87
+
88
+
89
+ # --------------------------------------------------------------------------
90
+ @classmethod
91
+ def frames(cls, universe) -> tuple[int, int]:
92
+ """
93
+ Requires MDAnalysis (`universe` should be an instance of `MDAnalysis.Universe`).
94
+ Returns a tuple of `(nframes_valid, nframes_expected)`, where:
95
+ - `nframes_valid`: The number of frames that could be successfully read from the trajectory.
96
+ - `nframes_expected`: The total number of frames expected based on the trajectory metadata.
97
+ """
98
+ nframes_expected = universe.trajectory.n_frames
99
+ nframes_valid = sum(1 for _ in universe.trajectory)
100
+ return nframes_valid, nframes_expected
101
+
102
+
103
+ # --------------------------------------------------------------------------
104
+ @classmethod
105
+ def altlocs(cls, system: ms.System) -> int:
106
+ parts_with_altloc: set[tuple[str, str]] = set()
107
+
108
+ for particle in system.particles:
109
+ if not particle.altloc: continue
110
+
111
+ key = (particle.get_chain_resid().get_dotstr(), particle.name)
112
+ if key in parts_with_altloc: continue
113
+
114
+ parts_with_altloc.add(key)
115
+
116
+ return len(parts_with_altloc)
117
+
118
+
119
+ # //////////////////////////////////////////////////////////////////////////////
@@ -1,6 +1,7 @@
1
1
  from pathlib import Path
2
2
  from collections import defaultdict
3
3
 
4
+ import molsimple as ms
4
5
  import freyacli as fy
5
6
  import molutils as mu
6
7
 
@@ -20,110 +21,48 @@ class Extract(mu.AppSubcommand):
20
21
  # --------------------------------------------------------------------------
21
22
  def app_extract_models(self):
22
23
  path_in, folder_out = self._io_filein_dirout()
23
- data_pdb = path_in.read_text()
24
+ first_only = self.main.arg_toggle("first_only")
24
25
 
25
- if self.main.get_arg_bool("first_only"):
26
- _, model = mu.Extract.next_model(data_pdb)
27
- path_out = folder_out / f"{path_in.stem}.m0.pdb"
28
- path_out.write_text(model)
29
- return
30
-
31
- for i, model in enumerate(mu.Extract.iter_models(data_pdb)):
26
+ for i, model in enumerate(ms.System.read_pdb(path_in).models):
32
27
  path_out = folder_out / f"{path_in.stem}.m{i:03}.pdb"
33
- path_out.write_text(model)
28
+ model.save(path_out)
29
+ if first_only: break
34
30
 
35
31
 
36
32
  # --------------------------------------------------------------------------
37
33
  def app_extract_chains(self):
38
34
  path_in, folder_out = self._io_filein_dirout()
39
- data_pdb = path_in.read_text()
40
-
41
- chains = mu.Extract.split_chains(data_pdb)
35
+ first_only = self.main.arg_toggle("first_only")
42
36
 
43
- chain_ids = mu.List.chains(path_in, first_only = True) \
44
- if self.main.get_arg_bool("first_only") else chains.keys()
37
+ system = ms.System.read_pdb(path_in)
38
+ chain_ids = mu.List.chains(system, do_sort = False)
45
39
 
46
40
  for chain_id in chain_ids:
47
41
  path_out = folder_out / f"{path_in.stem}.{chain_id}.pdb"
48
- path_out.write_text(chains[chain_id])
42
+ system.particles.select_chainid(chain_id).save(path_out)
43
+ if first_only: break
49
44
 
50
45
 
51
46
  # --------------------------------------------------------------------------
52
47
  def app_extract_residue(self):
53
48
  path_in, folder_out = self._io_filein_dirout()
54
- data_pdb = path_in.read_text()
49
+ residue_dotstr = self.main.arg_str("residue")
55
50
 
56
- residue_dotstr = self.main.get_arg_str("residue")
57
- chres = mu.ChainResid.from_dotstr(residue_dotstr)
58
- extracted = mu.Extract.residue(data_pdb, chres.resid, chres.chain)
59
-
60
- path_out = folder_out / f"{path_in.stem}.{residue_dotstr}.pdb"
61
- path_out.write_text(extracted)
51
+ pdb = ms.System.read_pdb(path_in)
52
+ chres = ms.ChainResid.from_dotstr(residue_dotstr)
53
+ extracted = pdb.particles.select_chain_resid(chres)
54
+ extracted.save(folder_out / f"{path_in.stem}.{residue_dotstr}.pdb")
62
55
 
63
56
 
64
57
  # --------------------------------------------------------------------------
65
58
  def app_extract_frames(self):
66
59
  path_struct, folder_out = self._io_filein_dirout(key_in = "path_struct")
67
- path_traj = self.main.get_arg_path("path_traj",
68
- assertion = fy.PathAssertion.FILE_IN, allow_none = True
69
- )
70
- frames = self.main.get_arg_int("frames", is_list = True)
71
- unpack = self.main.get_arg_bool("unpack")
60
+ path_traj = self.main.arg_path("path_traj", assertion = fy.PathAssertion.FILE_IN)
61
+ frames = self.main.arg_int_list("frames")
62
+ unpack = self.main.arg_toggle("unpack")
72
63
  mu.Extract.frames(path_struct, path_traj, folder_out, frames, unpack)
73
64
 
74
65
 
75
- # -------------------------------------------------------------------------- LOGIC SECTION
76
- @classmethod
77
- def next_model(cls, data: str, start: int = 0) -> tuple[int, str]:
78
- idx_0 = data.find("\nMODEL", start)
79
- if idx_0 == -1: return -1, data
80
-
81
- idx_1 = data.find("\nENDMDL", idx_0)
82
- if idx_1 == -1: return -1, data[idx_0:]
83
-
84
- idx_1 += len("\nENDMDL")
85
- return idx_1, data[idx_0:idx_1] + "\nEND"
86
-
87
-
88
- # --------------------------------------------------------------------------
89
- @classmethod
90
- def iter_models(cls, data: str):
91
- while data:
92
- idx, model = cls.next_model(data)
93
- yield model
94
- if idx == -1: break
95
- data = data[idx:]
96
-
97
-
98
- # --------------------------------------------------------------------------
99
- @classmethod
100
- def split_chains(cls, data: str) -> dict[str, str]:
101
- pdb = mu.ParserPDB(data)
102
- chains = defaultdict(list)
103
- for line in pdb.iter_atoms():
104
- chain_id = mu.ParserPDB.get_chainid(line)
105
- chains[chain_id].append(line)
106
- return {
107
- chain_id: mu.ParserPDB.join_lines(lines)
108
- for chain_id, lines in chains.items()
109
- }
110
-
111
-
112
- # --------------------------------------------------------------------------
113
- @classmethod
114
- def residue(cls, data: str, resid: str, chain: str = None) -> str:
115
- pdb = mu.ParserPDB(data)
116
- gen_residue = (
117
- line for line in pdb.iter_atoms()
118
- if mu.ParserPDB.get_resid(line) == resid
119
- )
120
- if chain is not None: gen_residue = (
121
- line for line in gen_residue
122
- if mu.ParserPDB.get_chainid(line) == chain
123
- )
124
- return mu.ParserPDB.join_lines(gen_residue)
125
-
126
-
127
66
  # --------------------------------------------------------------------------
128
67
  @classmethod
129
68
  def frames(cls,
@@ -160,8 +99,8 @@ class Extract(mu.AppSubcommand):
160
99
 
161
100
  # --------------------------------------------------------------------------
162
101
  def _io_filein_dirout(self, key_in = "path_in") -> tuple[Path, Path]:
163
- path_in = self.main.get_arg_path(key_in, assertion = fy.PathAssertion.FILE_IN)
164
- folder_out = self.main.get_arg_path("folder_out",
102
+ path_in = self.main.arg_path(key_in, assertion = fy.PathAssertion.FILE_IN)
103
+ folder_out = self.main.arg_path("folder_out",
165
104
  default = path_in.parent, assertion = fy.PathAssertion.DIR_OUT
166
105
  )
167
106
  return path_in, folder_out
@@ -0,0 +1,48 @@
1
+ from pathlib import Path
2
+
3
+ import molsimple as ms
4
+ import freyacli as fy
5
+ import molutils as mu
6
+
7
+ # //////////////////////////////////////////////////////////////////////////////
8
+ class List(mu.AppSubcommand):
9
+ # -------------------------------------------------------------------------- UI SECTION
10
+ def run(self):
11
+ command = self.main.subcommands.pop(0)
12
+
13
+ if command == "chains": return self.app_list_chains()
14
+ if command == "residues": return self.app_list_residues()
15
+
16
+ raise ValueError(f"Unknown command: {command}")
17
+
18
+
19
+ # --------------------------------------------------------------------------
20
+ def app_list_chains(self):
21
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
22
+ print(*mu.List.chains(ms.System.read_pdb(path_in), do_sort = True))
23
+
24
+
25
+ # --------------------------------------------------------------------------
26
+ def app_list_residues(self):
27
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
28
+ print(*mu.List.residues(ms.System.read_pdb(path_in), do_sort = True))
29
+
30
+
31
+ # -------------------------------------------------------------------------- LOGIC SECTION
32
+ @classmethod
33
+ def chains(cls, system: ms.System, do_sort: bool = True) -> list[str]:
34
+ unique_chains = set(system.particles.get_chainids())
35
+ if do_sort: return sorted(unique_chains)
36
+ return list(unique_chains)
37
+
38
+
39
+ # --------------------------------------------------------------------------
40
+ @classmethod
41
+ def residues(cls, system: ms.System, do_sort: bool = True) -> list[str]:
42
+ """Returns a list of unique residue identifiers in the format "chainid.resid"."""
43
+ unique_residues = set(part.get_chain_resid().get_dotstr() for part in system)
44
+ if do_sort: return sorted(unique_residues)
45
+ return list(unique_residues)
46
+
47
+
48
+ # //////////////////////////////////////////////////////////////////////////////
@@ -1,3 +1,6 @@
1
+ from pathlib import Path
2
+
3
+ import molsimple as ms
1
4
  import freyacli as fy
2
5
  import molutils as mu
3
6
 
@@ -14,34 +17,34 @@ class Remove(mu.AppSubcommand):
14
17
 
15
18
  # --------------------------------------------------------------------------
16
19
  def app_remove_altlocs(self):
17
- path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
18
- path_out = self.main.get_arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
19
- out = mu.Remove.altlocs(path_in.read_text())
20
- path_out.write_text(out)
20
+ path_in = self.main.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
21
+ path_out = self.main.arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
22
+ mu.Remove.altlocs(path_in).save(path_out)
21
23
 
22
24
 
23
25
  # --------------------------------------------------------------------------
24
26
  @classmethod
25
- def altlocs(cls, data: str) -> str:
26
- pdb = mu.ParserPDB(data)
27
+ def altlocs(cls, path_pdb: Path) -> ms.ParticleGroup:
28
+ pdb = ms.System.read_pdb(path_pdb)
27
29
 
28
- out = []
30
+ out: list[ms.Particle] = []
29
31
  seen_altlocs = set()
30
- for line in pdb.iter_atoms():
31
- altloc = mu.ParserPDB.get_altloc(line)
32
- if altloc == " ":
33
- out.append(line)
32
+ for particle in pdb.particles:
33
+ if particle.altloc == "":
34
+ out.append(particle)
34
35
  continue
35
36
 
36
- resid = mu.ChainResid.from_pdb(line).get_dotstr()
37
- name = mu.ParserPDB.get_atomname(line)
38
- key = (resid, name)
37
+ chres = particle.get_chain_resid().get_dotstr()
38
+ name = particle.name
39
+
40
+ key = (chres, name)
39
41
  if key in seen_altlocs: continue
40
42
 
41
43
  seen_altlocs.add(key)
42
- out.append(mu.ParserPDB.set_altloc(line, " "))
44
+ particle.altloc = ""
45
+ out.append(particle)
43
46
 
44
- return mu.ParserPDB.join_lines(out)
47
+ return ms.ParticleGroup(out)
45
48
 
46
49
 
47
50
  # //////////////////////////////////////////////////////////////////////////////
@@ -1,5 +1,6 @@
1
1
  from pathlib import Path
2
2
 
3
+ import molsimple as ms
3
4
  import freyacli as fy
4
5
  import molutils as mu
5
6
 
@@ -33,23 +34,27 @@ class AppMain(fy.App):
33
34
 
34
35
  # --------------------------------------------------------------------------
35
36
  def _run_select(self):
36
- query = self.get_arg_str("query")
37
- path_in = self.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
38
- path_out = self.get_arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
37
+ query = self.arg_str("query")
38
+ path_in = self.arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
39
+ path_out = self.arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
40
+ paths_traj = self.arg_path_list("paths_traj", assertion = fy.PathAssertion.FILE_OUT)
39
41
 
40
42
  import MDAnalysis as mda
41
43
  mda.Universe(path_in).select_atoms(query).write(path_out)
42
44
 
45
+ if not paths_traj: return
46
+
47
+ path_traj_in, path_traj_out = paths_traj
48
+ u = mda.Universe(path_in, path_traj_in)
49
+ u.select_atoms(query).write(str(path_traj_out), frames = "all")
50
+
43
51
 
44
52
  # --------------------------------------------------------------------------
45
53
  def _run_merge(self):
46
- paths_in = self.get_arg_path("paths_in", assertion = fy.PathAssertion.FILE_IN, is_list = True)
47
- path_out = self.get_arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
48
- data = mu.ParserPDB.join_lines(
49
- line for path_in in paths_in
50
- for line in mu.ParserPDB.from_file(path_in).iter_atoms()
51
- )
52
- path_out.write_text(data)
54
+ paths_in = self.arg_path_list("paths_in", assertion = fy.PathAssertion.FILE_IN)
55
+ path_out = self.arg_path("path_out", assertion = fy.PathAssertion.FILE_OUT)
56
+ pgroups = (ms.System.read_pdb(path_in).particles for path_in in paths_in)
57
+ ms.ParticleGroup((p for pgroup in pgroups for p in pgroup)).save(path_out)
53
58
 
54
59
 
55
60
  # //////////////////////////////////////////////////////////////////////////////
@@ -5,7 +5,6 @@
5
5
  @chains
6
6
  !fyh:_ Lists the chains in a PDB file.
7
7
  !fyh:path_in Path of a PDB file.
8
- !fyh:first_only Use this flag for displaying only the first chain of the structure.
9
8
  !@
10
9
  @residues
11
10
  !fyh:_ Lists the residues in a PDB file in the format "chainid.resid".
@@ -21,10 +20,17 @@
21
20
  !@
22
21
  @chains
23
22
  !fyh:_ Counts the number of chains in a PDB file.
23
+ Considers only the first model of the molecular system.
24
24
  !fyh:path_in Path of a PDB file.
25
25
  !@
26
26
  @residues
27
27
  !fyh:_ Counts the number of residues in a PDB file in the format "chainid.resid".
28
+ Considers only the first model of the molecular system.
29
+ !fyh:path_in Path of a PDB file.
30
+ !@
31
+ @particles
32
+ !fyh:_ Counts the number of particles in a PDB file in the format "chainid.resid".
33
+ Considers only the first model of the molecular system.
28
34
  !fyh:path_in Path of a PDB file.
29
35
  !@
30
36
  @frames
@@ -46,12 +52,12 @@
46
52
  @extract
47
53
  !fyh:_ Extract specific sections of data from biomolecular structures.
48
54
  @models
49
- !fyh:_ Extracts the models in a PDB file into separate files.
55
+ !fyh:_ Extract the models in a PDB file into separate files.
50
56
  !use:_io_filein_dirout
51
57
  !fyh:first_only Use this flag for extracting only the first model of the structure and disregard the rest.
52
58
  !@
53
59
  @chains
54
- !fyh:_ Extracts the chains in a PDB file into separate files.
60
+ !fyh:_ Extract the chains in a PDB file into separate files.
55
61
  !use:_io_filein_dirout
56
62
  !fyh:first_only Use this flag for extracting only the first chain of the structure and disregard the rest.
57
63
  !@
@@ -76,11 +82,17 @@
76
82
  !@
77
83
  !@
78
84
 
85
+ @remove
86
+ !fyh:_ Remove undesired data from a PDB file.
87
+ !@
88
+
79
89
  @select
80
90
  !fyh:_ Select specific atoms with a custom query (MDAnalysis format) and save them into a file. Requires MDAnalysis.
81
91
  !fyh:query Query for selecting the atoms of interest. Must follow MDAnalysis format
82
92
  !fyh:path_in Path of the structure file whose atoms will be selected. Can be anything supported by MDanalysis.
83
93
  !fyh:path_out Path for the output selected atoms. Can be anything supported by MDanalysis.
94
+ !fyh:paths_traj Input and output paths for an optional trajectory.
95
+ The selection will be applied for all frames of the selection accordingly.
84
96
  !@
85
97
 
86
98
  @merge
@@ -1,7 +1,6 @@
1
1
  @list
2
2
  @chains
3
3
  path_in[]1.PATH
4
- ~first_only[f,first-only]
5
4
  !@
6
5
  @residues
7
6
  path_in[]1.PATH
@@ -18,6 +17,9 @@
18
17
  @residues
19
18
  path_in[]1.PATH
20
19
  !@
20
+ @particles
21
+ path_in[]1.PATH
22
+ !@
21
23
  @frames
22
24
  path_struct[]1.PATH
23
25
  ~path_traj[]1.PATH
@@ -61,6 +63,7 @@
61
63
  query[]1.STR
62
64
  path_in[]1.PATH
63
65
  path_out[]1.PATH
66
+ ~paths_traj[t,traj]2.PATH
64
67
  !@
65
68
 
66
69
  @merge
@@ -0,0 +1 @@
1
+ __version__ = "0.6.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: molutils
3
- Version: 0.5.0
3
+ Version: 0.6.1
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,14 +12,14 @@ 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.4.1
15
+ Requires-Dist: molsimple==0.1.2
16
+ Requires-Dist: freyacli==0.5.0
16
17
  Dynamic: author
17
18
  Dynamic: author-email
18
19
  Dynamic: classifier
19
20
  Dynamic: description
20
21
  Dynamic: description-content-type
21
22
  Dynamic: home-page
22
- Dynamic: keywords
23
23
  Dynamic: license
24
24
  Dynamic: license-file
25
25
  Dynamic: requires-dist
@@ -16,9 +16,6 @@ molutils/_core/count.py
16
16
  molutils/_core/extract.py
17
17
  molutils/_core/list_.py
18
18
  molutils/_core/remove.py
19
- molutils/_parsers/__init__.py
20
- molutils/_parsers/chain_resid.py
21
- molutils/_parsers/parser_pdb.py
22
19
  molutils/_ui/__init__.py
23
20
  molutils/_ui/app_main.py
24
21
  molutils/_ui/app_subcommand.py
@@ -0,0 +1,2 @@
1
+ molsimple==0.1.2
2
+ freyacli==0.5.0
@@ -21,7 +21,10 @@ setup(
21
21
  "_ui/fy_rules.fyr", "_ui/fy_help.fyh",
22
22
  ],
23
23
  },
24
- install_requires=["freyacli==0.4.1"],
24
+ install_requires=[
25
+ "molsimple==0.1.2",
26
+ "freyacli==0.5.0",
27
+ ],
25
28
  entry_points={
26
29
  "console_scripts": [
27
30
  "molutils=molutils.__main__:main",
@@ -1,104 +0,0 @@
1
- from pathlib import Path
2
-
3
- import freyacli as fy
4
- import molutils as mu
5
-
6
- # //////////////////////////////////////////////////////////////////////////////
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
- if command == "chains" : return self.app_count_chains()
14
- if command == "residues": return self.app_count_residues()
15
- if command == "frames" : return self.app_count_frames()
16
- if command == "altlocs" : return self.app_count_altlocs()
17
-
18
- raise ValueError(f"Unknown command: {command}")
19
-
20
-
21
- # --------------------------------------------------------------------------
22
- def app_count_models(self):
23
- path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
24
- print(mu.Count.models(path_in))
25
-
26
-
27
- # --------------------------------------------------------------------------
28
- def app_count_chains(self):
29
- path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
30
- print(len(mu.List.chains(path_in, do_sort = False)))
31
-
32
-
33
- # --------------------------------------------------------------------------
34
- def app_count_residues(self):
35
- path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
36
- print(len(mu.List.residues(path_in, do_sort = False)))
37
-
38
-
39
- # --------------------------------------------------------------------------
40
- def app_count_frames(self):
41
- path_struct = self.main.get_arg_path("path_struct", assertion = fy.PathAssertion.FILE_IN)
42
- path_traj = self.main.get_arg_path("path_traj",
43
- assertion = fy.PathAssertion.FILE_IN, allow_none = True
44
- )
45
- nframes_valid, nframes_expected = mu.Count.frames(path_struct, path_traj)
46
- print(f"{nframes_valid}/{nframes_expected}")
47
-
48
-
49
- # --------------------------------------------------------------------------
50
- def app_count_altlocs(self):
51
- path_in = self.main.get_arg_path("path_in", assertion = fy.PathAssertion.FILE_IN)
52
- print(mu.Count.altlocs(path_in))
53
-
54
-
55
- # -------------------------------------------------------------------------- LOGIC SECTION
56
- @classmethod
57
- def models(cls, path_pdb: Path) -> int:
58
- data = path_pdb.read_text()
59
- return max(1, cls._count_substring('\n'+data, "\nMODEL"))
60
-
61
-
62
- # --------------------------------------------------------------------------
63
- @classmethod
64
- def frames(cls, path_struct: Path, path_traj: Path | None) -> tuple[int, int]:
65
- import MDAnalysis as mda
66
- args_traj = [str(path_traj)] if path_traj is not None else []
67
- u = mda.Universe(str(path_struct), *args_traj)
68
-
69
- nframes_expected = u.trajectory.n_frames
70
- nframes_valid = sum(1 for _ in u.trajectory)
71
- return nframes_valid, nframes_expected
72
-
73
-
74
- # --------------------------------------------------------------------------
75
- @classmethod
76
- def altlocs(cls, path_pdb: Path) -> int:
77
- atoms: dict[str, str] = {}
78
- pdb = mu.ParserPDB.from_file(path_pdb)
79
-
80
- ### [TODO] adapt for extracting first altloc
81
- for line in pdb.iter_atoms():
82
- altloc = mu.ParserPDB.get_altloc(line)
83
- if altloc == " ": continue
84
-
85
- resid = mu.ChainResid.from_pdb(line).get_dotstr()
86
- name = mu.ParserPDB.get_atomname(line)
87
-
88
- key = f"{resid}.{name}"
89
- if key in atoms: continue
90
-
91
- atoms[key] = line
92
-
93
- return len(atoms)
94
-
95
-
96
- # --------------------------------------------------------------------------
97
- @staticmethod
98
- def _count_substring(string: str, substring: str) -> int:
99
- """Counts the number of occurrences of `substring` in `string`."""
100
- replaced = string.replace(substring, "")
101
- return (len(string) - len(replaced)) // len(substring)
102
-
103
-
104
- # //////////////////////////////////////////////////////////////////////////////
@@ -1,51 +0,0 @@
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 = 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, do_sort: bool = True, 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
- if do_sort: return sorted(set(gen_chain_ids))
38
- return list(set(gen_chain_ids))
39
-
40
-
41
- # --------------------------------------------------------------------------
42
- @classmethod
43
- def residues(cls, path_pdb: Path, do_sort: bool = True) -> list[str]:
44
- """Returns list of unique residue identifiers in the format "chainid.resid"."""
45
- pdb = mu.ParserPDB.from_file(path_pdb)
46
- gen_residues = (mu.ChainResid.from_pdb(line).get_dotstr() for line in pdb.iter_atoms())
47
- if do_sort: return sorted(set(gen_residues))
48
- return list(set(gen_residues))
49
-
50
-
51
- # //////////////////////////////////////////////////////////////////////////////
@@ -1,34 +0,0 @@
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
- # //////////////////////////////////////////////////////////////////////////////
@@ -1,80 +0,0 @@
1
- from pathlib import Path
2
-
3
- # //////////////////////////////////////////////////////////////////////////////
4
- class ParserPDB:
5
- LENGTH_RECORD = 80
6
- IDX0_ATOMNAME = 12
7
- IDX1_ATOMNAME = 16
8
- IDX_ALTLOC = 16
9
- IDX_CHAIN_ID = 21
10
- IDX0_RESID = 22
11
- IDX1_RESID = 26
12
-
13
- # --------------------------------------------------------------------------
14
- def __init__(self, data_pdb: str):
15
- self.data = data_pdb.splitlines()
16
-
17
-
18
- # --------------------------------------------------------------------------
19
- @classmethod
20
- def from_file(cls, path_pdb: Path):
21
- return cls(path_pdb.read_text())
22
-
23
-
24
- # --------------------------------------------------------------------------
25
- @classmethod
26
- def join_lines(cls, lines: list[str]) -> str:
27
- return '\n'.join(lines) + "\nEND"
28
-
29
-
30
- # --------------------------------------------------------------------------
31
- def iter_atoms(self):
32
- return (
33
- line for line in self.data
34
- if line.startswith("ATOM") or line.startswith("HETATM")
35
- )
36
-
37
-
38
- # --------------------------------------------------------------------------
39
- @classmethod
40
- def safe_next(cls, gen):
41
- try: return next(gen)
42
- except StopIteration: raise ValueError(f"No ATOM records found in PDB file")
43
-
44
-
45
- # --------------------------------------------------------------------------
46
- @classmethod
47
- def get_atomname(cls, line: str) -> str:
48
- try: return line[cls.IDX0_ATOMNAME:cls.IDX1_ATOMNAME].strip()
49
- except IndexError: raise ValueError(f"Unexpected format of ATOM records in PDB file")
50
-
51
-
52
- # --------------------------------------------------------------------------
53
- @classmethod
54
- def get_chainid(cls, line: str) -> str:
55
- try: return line[cls.IDX_CHAIN_ID]
56
- except IndexError: raise ValueError(f"Unexpected format of ATOM records in PDB file")
57
-
58
-
59
- # --------------------------------------------------------------------------
60
- @classmethod
61
- def get_resid(cls, line: str) -> str:
62
- return line[cls.IDX0_RESID:cls.IDX1_RESID].strip()
63
-
64
-
65
- # --------------------------------------------------------------------------
66
- @classmethod
67
- def get_altloc(cls, line: str) -> str:
68
- try: return line[cls.IDX_ALTLOC]
69
- except IndexError: raise ValueError(f"Unexpected format of ATOM records in PDB file")
70
-
71
-
72
- # --------------------------------------------------------------------------
73
- @classmethod
74
- def set_altloc(cls, line: str, altloc: str) -> str:
75
- if len(altloc) != 1: raise ValueError(f"altloc must be a single character")
76
- try: return line[:cls.IDX_ALTLOC] + altloc + line[cls.IDX_ALTLOC+1:]
77
- except IndexError: raise ValueError(f"Unexpected format of ATOM records in PDB file")
78
-
79
-
80
- # //////////////////////////////////////////////////////////////////////////////
File without changes
@@ -1 +0,0 @@
1
- __version__ = "0.5.0"
@@ -1 +0,0 @@
1
- freyacli==0.4.1
File without changes
File without changes
File without changes
File without changes
File without changes