gromologist 0.330__tar.gz → 0.332__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 (30) hide show
  1. {gromologist-0.330 → gromologist-0.332}/PKG-INFO +1 -1
  2. {gromologist-0.330 → gromologist-0.332}/gromologist/Entries.py +23 -8
  3. {gromologist-0.330 → gromologist-0.332}/gromologist/Gmx.py +202 -49
  4. {gromologist-0.330 → gromologist-0.332}/gromologist/Parser.py +40 -10
  5. {gromologist-0.330 → gromologist-0.332}/gromologist/Pdb.py +49 -14
  6. {gromologist-0.330 → gromologist-0.332}/gromologist/Section.py +87 -24
  7. {gromologist-0.330 → gromologist-0.332}/gromologist/Subsection.py +3 -2
  8. {gromologist-0.330 → gromologist-0.332}/gromologist/Topology.py +21 -11
  9. {gromologist-0.330 → gromologist-0.332}/gromologist/Utils.py +306 -7
  10. gromologist-0.332/gromologist/gromologist_gui.py +866 -0
  11. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/PKG-INFO +1 -1
  12. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/SOURCES.txt +1 -0
  13. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/entry_points.txt +2 -0
  14. {gromologist-0.330 → gromologist-0.332}/setup.py +3 -1
  15. {gromologist-0.330 → gromologist-0.332}/tests/test_pdb.py +41 -1
  16. {gromologist-0.330 → gromologist-0.332}/LICENSE +0 -0
  17. {gromologist-0.330 → gromologist-0.332}/README.md +0 -0
  18. {gromologist-0.330 → gromologist-0.332}/gromologist/Crooks.py +0 -0
  19. {gromologist-0.330 → gromologist-0.332}/gromologist/DihOpt.py +0 -0
  20. {gromologist-0.330 → gromologist-0.332}/gromologist/Mutant.py +0 -0
  21. {gromologist-0.330 → gromologist-0.332}/gromologist/ThDiff.py +0 -0
  22. {gromologist-0.330 → gromologist-0.332}/gromologist/__init__.py +0 -0
  23. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/dependency_links.txt +0 -0
  24. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/not-zip-safe +0 -0
  25. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/requires.txt +0 -0
  26. {gromologist-0.330 → gromologist-0.332}/gromologist.egg-info/top_level.txt +0 -0
  27. {gromologist-0.330 → gromologist-0.332}/setup.cfg +0 -0
  28. {gromologist-0.330 → gromologist-0.332}/tests/test_pdb_extended.py +0 -0
  29. {gromologist-0.330 → gromologist-0.332}/tests/test_top.py +0 -0
  30. {gromologist-0.330 → gromologist-0.332}/tests/test_top_section_entry.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: gromologist
3
- Version: 0.330
3
+ Version: 0.332
4
4
  Summary: Library to handle various GROMACS-related stuff
5
5
  Author: Milosz Wieczor
6
6
  Author-email: milafternoon@gmail.com
@@ -322,15 +322,22 @@ class EntryParam(Entry):
322
322
  """
323
323
  def __init__(self, content: str, subsection: "gml.Subsection", processed: bool = False, perres=False):
324
324
  super().__init__(content, subsection)
325
+ # Preserve keyword-like parameters (e.g. #define symbols) instead of forcing float conversion
326
+ def _coerce(token):
327
+ try:
328
+ return float(token)
329
+ except ValueError:
330
+ return token
331
+
325
332
  self.atoms_per_entry = type(self.subsection).n_atoms[self.subsection.header]
326
333
  self.types = tuple(self.content[:self.atoms_per_entry])
327
334
  if self.subsection.header == 'cmaptypes' and processed:
328
335
  if perres:
329
336
  self.modifiers = self.content[self.atoms_per_entry + 1:self.atoms_per_entry + 4]
330
- self.params = [float(x) for x in self.content[self.atoms_per_entry + 4:]]
337
+ self.params = [_coerce(x) for x in self.content[self.atoms_per_entry + 4:]]
331
338
  else:
332
339
  self.modifiers = self.content[self.atoms_per_entry + 1:self.atoms_per_entry + 3]
333
- self.params = [float(x) for x in self.content[self.atoms_per_entry + 3:]]
340
+ self.params = [_coerce(x) for x in self.content[self.atoms_per_entry + 3:]]
334
341
  self.interaction_type = self.content[self.atoms_per_entry]
335
342
  elif self.subsection.header == 'cmaptypes' and not processed:
336
343
  self.modifiers = []
@@ -343,24 +350,27 @@ class EntryParam(Entry):
343
350
  elif self.subsection.header == 'atomtypes':
344
351
  if self.content[4] in 'ASVD':
345
352
  self.modifiers = self.content[self.atoms_per_entry:self.atoms_per_entry + 4]
346
- self.params = [float(x) for x in self.content[self.atoms_per_entry + 4:]]
353
+ self.params = [_coerce(x) for x in self.content[self.atoms_per_entry + 4:]]
347
354
  elif self.content[3] in 'ASVD':
348
355
  self.modifiers = [''] + self.content[self.atoms_per_entry:self.atoms_per_entry + 3]
349
- self.params = [float(x) for x in self.content[self.atoms_per_entry + 3:]]
356
+ self.params = [_coerce(x) for x in self.content[self.atoms_per_entry + 3:]]
350
357
  else:
351
358
  raise RuntimeError(f"Can't determine format of atomtype {self.content}")
352
359
  self.interaction_type = ''
353
360
  else:
354
- self.params = [float(x) for x in self.content[self.atoms_per_entry + 1:]]
361
+ self.params = [_coerce(x) for x in self.content[self.atoms_per_entry + 1:]]
355
362
  self.modifiers = []
356
363
  self.interaction_type = self.content[self.atoms_per_entry]
357
364
  if self.subsection.header == 'dihedraltypes':
358
365
  if any([self.infer_type(x) == float for x in self.types]):
359
366
  self.types = self.content[:2]
360
367
  self.interaction_type = self.content[2]
361
- self.params = [float(x) for x in self.content[3:]]
368
+ self.params = [_coerce(x) for x in self.content[3:]]
362
369
  if self.subsection.header == 'dihedraltypes' and self.interaction_type in ('9', '4', '1'):
363
- self.params[-1] = int(self.params[-1])
370
+ try:
371
+ self.params[-1] = int(self.params[-1])
372
+ except (ValueError, TypeError):
373
+ pass
364
374
  self.identifier = self.subsection.header + '-' + '-'.join(self.types) + '-' + self.interaction_type
365
375
 
366
376
  def format(self) -> str:
@@ -379,6 +389,7 @@ class EntryParam(Entry):
379
389
  ('dihedraltypes', '1'): "{:>8s} {:>8s} {:>8s} {:>8s} {:>6s} {:>13.6f} {:>13.6f} {:>6d} ",
380
390
  ('atomtypes', ''): "{:>6s} {} {:>6s} {:>13s} {:>9s} {:>3s} {:>16.12f} {:>9.5f} ",
381
391
  ('pairtypes', '1'): "{:>8s} {:>8s} {:>3s} {:>16.12f} {:>16.12f} ",
392
+ ('constrainttypes', '2'): "{:>8s} {:>8s} {:>3s} {:>16.12f} ",
382
393
  ('nonbond_params', '1'): "{:>8s} {:>8s} {:>3s} {:>20.16f} {:>20.16f} ",
383
394
  ('implicit_genborn_params', ''): " {:8s} {:8.4f} {:8.4f} {:8.4f} {:8.4f} {:8.4f} "}
384
395
  if (self.subsection.header, self.interaction_type) in fmt.keys():
@@ -437,6 +448,10 @@ class EntryParam(Entry):
437
448
  last = last + '\\\n' + \
438
449
  (((npar-10*int(npar/10)) * "{} ")[:-1]).format(*self.params[10*int(npar/10):]) + '\n'
439
450
  return first + last + '\n'
451
+ # If parameters contain string keywords (e.g. #define tokens), fall back to a simple join
452
+ if any(isinstance(p, str) for p in self.params):
453
+ fields = [*self.types, self.interaction_type, *self.modifiers, *[str(p) for p in self.params]]
454
+ return ' '.join(fields) + ' ' + self.comment + '\n'
440
455
  elif self.format():
441
456
  try:
442
457
  return self.format().format(*self.types, self.interaction_type, *self.modifiers, *self.params) +\
@@ -474,7 +489,7 @@ class EntryAtom(Entry):
474
489
  self.type_b, self.charge_b, self.mass_b = self.content[8], float(self.content[9]), float(self.content[10])
475
490
  else:
476
491
  self.type_b, self.charge_b, self.mass_b = None, None, None
477
- self.fstring = "{:>6d} {:>11s} {:>7d}{:>7s}{:>7s}{:>7d}"
492
+ self.fstring = "{:>6d} {:>11s} {:>7d} {:>7s} {:>7s} {:>7d} "
478
493
 
479
494
  def __eq__(self, other) -> bool:
480
495
  if not isinstance(other, gml.EntryAtom):
@@ -44,6 +44,7 @@ Notes:
44
44
 
45
45
 
46
46
  from copy import deepcopy
47
+ from collections import Counter
47
48
  from subprocess import run, PIPE
48
49
  import os, sys
49
50
  import numpy as np
@@ -315,7 +316,10 @@ def calc_gmx_energy(struct: str, topfile: str, gmx: str = '', quiet: bool = Fals
315
316
  if set_omp is None:
316
317
  gmx_command(gmx, 'mdrun', quiet=quiet, deffnm='rerun', rerun=struct if traj is None else traj)
317
318
  else:
318
- gmx_command(gmx, 'mdrun', quiet=quiet, deffnm='rerun', rerun=struct if traj is None else traj, ntomp=1)
319
+ try:
320
+ gmx_command(gmx, 'mdrun', quiet=quiet, deffnm='rerun', rerun=struct if traj is None else traj, ntomp=1, ntmpi=1)
321
+ except:
322
+ gmx_command(gmx, 'mdrun', quiet=quiet, deffnm='rerun', rerun=struct if traj is None else traj, ntomp=1)
319
323
  legend = get_legend(gmx, 'rerun.edr')
320
324
  if terms == 'all':
321
325
  terms = list(legend.keys())
@@ -430,7 +434,8 @@ def prepare_system(struct: str, ff: Optional[str] = None, water: Optional[str] =
430
434
  cation: Optional[str] = 'K', anion: Optional[str] = 'CL', ion_conc: Optional[float] = 0.15,
431
435
  maxsol: Optional[int] = None, resize_box: bool = True, box_margin: Optional[float] = 1.5,
432
436
  explicit_box_size: Optional[list] = None, quiet: bool = True, topology: Optional[str] = None,
433
- maxwarn: int = 1, minimize: bool = True, solvent_from: Optional[str] = None, **kwargs):
437
+ maxwarn: int = 1, minimize: bool = True, solvent_from: Optional[str] = None,
438
+ basename: Optional[str] = None, **kwargs):
434
439
  """
435
440
  Implements a full system preparation workflow (parsing the structure with pdb2gmx,
436
441
  setting the box size, adding solvent and ions, minimizing, generating a final
@@ -452,8 +457,10 @@ def prepare_system(struct: str, ff: Optional[str] = None, water: Optional[str] =
452
457
  :param minimize: bool, whether to do energy minimization at the end
453
458
  :param maxwarn: int, how many warnings to tolerate in grompp
454
459
  :param solvent_from: str, reproduce solvent data from a chosen topology file
460
+ :param basename: str, optional prefix for all files created by this protocol
455
461
  :return: None
456
462
  """
463
+ bname = '' if basename is None else str(basename) + '-'
457
464
  gmx = gml.find_gmx_dir()
458
465
  if not topology:
459
466
  rtpnum = None
@@ -480,25 +487,27 @@ def prepare_system(struct: str, ff: Optional[str] = None, water: Optional[str] =
480
487
  if rtpnum is not None:
481
488
  pathtoff = found[rtpnum - 1].replace(' (local)', '')
482
489
  else:
483
- pathtoff = [x for x in found if ff in x][0]
490
+ pathtoff = [x.replace(' (local)', '') for x in found if ff in x][0]
484
491
  water = [line.split()[0] for line in open(pathtoff + os.sep + 'watermodels.dat') if 'recommended' in line][
485
492
  0]
486
493
  gmx_command(gmx[1], 'pdb2gmx', quiet=quiet, f=struct, ff=ff, water=water,
487
- answer=True, fail_on_error=True, **kwargs)
494
+ answer=True, fail_on_error=True, o=f'{bname}conf.gro', p=f'{bname}topol.top', **kwargs)
488
495
  else:
489
- if topology != 'topol.top':
490
- copy2(topology, 'topol.top')
491
- gml.Pdb(struct).save_gro('conf.gro')
496
+ if topology != f'{bname}topol.top':
497
+ copy2(topology, f'{bname}topol.top')
498
+ gml.Pdb(struct).save_gro(f'{bname}conf.gro')
492
499
  water = 'TIP' + str([mol for mol in gml.Top(topology, suppress=True).molecules if mol.is_water][0].natoms)
500
+ print(f"Assuming {water} water model for solvating the box based on the topology provided")
501
+ gml.Top(f'{bname}topol.top', suppress=True).save_top(f'{bname}topol.top')
493
502
  if resize_box:
494
503
  if explicit_box_size is None:
495
- gmx_command(gmx[1], 'editconf', f='conf.gro', o='box.gro', d=box_margin, bt=box, quiet=quiet,
504
+ gmx_command(gmx[1], 'editconf', f=f'{bname}conf.gro', o=f'{bname}box.gro', d=box_margin, bt=box, quiet=quiet,
496
505
  answer=True, fail_on_error=True)
497
506
  else:
498
- gmx_command(gmx[1], 'editconf', f='conf.gro', o='box.gro', bt=box, quiet=quiet, answer=True,
507
+ gmx_command(gmx[1], 'editconf', f=f'{bname}conf.gro', o=f'{bname}box.gro', bt=box, quiet=quiet, answer=True,
499
508
  box=' '.join([str(x) for x in explicit_box_size]), fail_on_error=True)
500
509
  else:
501
- copy2('conf.gro', 'box.gro')
510
+ copy2(f'{bname}conf.gro', f'{bname}box.gro')
502
511
  waterbox = 'spc216.gro' if ('3' in water or 'spc' in water) else 'tip5p.gro' if '5' in water else 'tip4p.gro'
503
512
  if solvent_from is not None:
504
513
  nwater = 0
@@ -525,19 +534,21 @@ def prepare_system(struct: str, ff: Optional[str] = None, water: Optional[str] =
525
534
  anion = list(ions.keys())[1]
526
535
  nanion = ions[anion]
527
536
  if maxsol is None:
528
- gmx_command(gmx[1], 'solvate', cp='box.gro', p='topol.top', o='water.gro', cs=waterbox, quiet=quiet,
537
+ gmx_command(gmx[1], 'solvate', cp=f'{bname}box.gro', p=f'{bname}topol.top', o=f'{bname}water.gro', cs=waterbox, quiet=quiet,
529
538
  answer=True, fail_on_error=True)
539
+ elif maxsol == 0:
540
+ copy2(f'{bname}box.gro', f'{bname}water.gro')
530
541
  else:
531
- gmx_command(gmx[1], 'solvate', cp='box.gro', p='topol.top', o='water.gro', maxsol=maxsol, cs=waterbox,
542
+ gmx_command(gmx[1], 'solvate', cp=f'{bname}box.gro', p=f'{bname}topol.top', o=f'{bname}water.gro', maxsol=maxsol, cs=waterbox,
532
543
  quiet=quiet, answer=True, fail_on_error=True)
533
544
  gml.gen_mdp('do_minimization.mdp', runtype='mini')
534
545
  if ion_conc == 0 and (nanion is None or nanion == 0) and (ncation is None or ncation == 0):
535
- copy2('water.gro', 'ions.gro')
546
+ copy2(f'{bname}water.gro', f'{bname}ions.gro')
536
547
  else:
537
- gmx_command(gmx[1], 'grompp', f='do_minimization.mdp', p='topol.top', c='water.gro', o='ions',
548
+ gmx_command(gmx[1], 'grompp', f='do_minimization.mdp', p=f'{bname}topol.top', c=f'{bname}water.gro', o=f'{bname}ions',
538
549
  maxwarn=maxwarn, quiet=quiet, answer=True, fail_on_error=True)
539
- answer = gmx_command(gmx[1], 'genion', pass_values=['a'], s='ions', pname=cation, nname=anion, conc=ion_conc,
540
- quiet=quiet, neutral=True, p="topol", o='test', answer=True)
550
+ answer = gmx_command(gmx[1], 'genion', pass_values=['a'], s=f'{bname}ions', pname=cation, nname=anion, conc=ion_conc,
551
+ quiet=quiet, neutral=True, p=f'{bname}topol', o='test', answer=True)
541
552
  sol = int([line.split()[1] for line in answer.split('\n') if 'SOL' in line][0])
542
553
  if nanion is not None or ncation is not None:
543
554
  newkwargs = {}
@@ -548,48 +559,48 @@ def prepare_system(struct: str, ff: Optional[str] = None, water: Optional[str] =
548
559
  if nanion is not None and ncation is not None:
549
560
  print(f"Adding {ncation} cations ({cation}) and {nanion} anions ({anion}) as specified - "
550
561
  f"make sure your system is neutral!")
551
- gmx_command(gmx[1], 'genion', pass_values=[sol], s='ions', quiet=quiet, p="topol",
552
- o='ions', answer=True, fail_on_error=True, **newkwargs)
562
+ gmx_command(gmx[1], 'genion', pass_values=[sol], s=f'{bname}ions', quiet=quiet, p=f'{bname}topol',
563
+ o=f'{bname}ions', answer=True, fail_on_error=True, **newkwargs)
553
564
  else:
554
- gmx_command(gmx[1], 'genion', pass_values=[sol], s='ions', quiet=quiet, neutral=True, p="topol",
555
- o='ions', answer=True, fail_on_error=True, **newkwargs)
565
+ gmx_command(gmx[1], 'genion', pass_values=[sol], s=f'{bname}ions', quiet=quiet, neutral=True, p=f'{bname}topol',
566
+ o=f'{bname}ions', answer=True, fail_on_error=True, **newkwargs)
556
567
  else:
557
- gmx_command(gmx[1], 'genion', pass_values=[sol], s='ions', pname=cation, nname=anion, conc=ion_conc,
558
- quiet=quiet, neutral=True, p="topol", o='ions', answer=True, fail_on_error=True)
559
- output = gmx_command(gmx[1], 'grompp', f='do_minimization.mdp', p='topol.top', c='ions.gro', o='do_mini',
568
+ gmx_command(gmx[1], 'genion', pass_values=[sol], s=f'{bname}ions', pname=cation, nname=anion, conc=ion_conc,
569
+ quiet=quiet, neutral=True, p=f'{bname}topol', o=f'{bname}ions', answer=True, fail_on_error=True)
570
+ output = gmx_command(gmx[1], 'grompp', f='do_minimization.mdp', p=f'{bname}topol.top', c=f'{bname}ions.gro', o=f'{bname}do_mini',
560
571
  quiet=quiet, answer=True, maxwarn=maxwarn, fail_on_error=True)
561
572
  if extract_warnings(output):
562
573
  print(extract_warnings(output))
563
574
  if minimize:
564
- gmx_command(gmx[1], 'mdrun', deffnm='do_mini', v=True,
575
+ gmx_command(gmx[1], 'mdrun', deffnm=f'{bname}do_mini', v=True,
565
576
  quiet=quiet, answer=True, fail_on_error=True)
566
577
  final_str_name = 'minimized_structure.pdb'
567
578
  else:
568
- copy2('ions.gro', 'do_mini.gro')
569
- final_str_name = 'pre-minimized_structure.pdb'
570
- ndx(gml.Pdb('do_mini.gro'), selections=['all', 'not solvent'])
571
- gmx_command(gmx[1], 'trjconv', s='do_mini.tpr', f='do_mini.gro', o='whole.gro', pbc='cluster',
579
+ copy2(f'{bname}ions.gro', f'{bname}do_mini.gro')
580
+ final_str_name = f'pre-minimized_structure.pdb'
581
+ ndx(gml.Pdb(f'{bname}do_mini.gro'), selections=['all', 'not solvent'], fname=f'{bname}gml.ndx')
582
+ gmx_command(gmx[1], 'trjconv', s=f'{bname}do_mini.tpr', f=f'{bname}do_mini.gro', o=f'{bname}whole.gro', pbc='cluster',
572
583
  pass_values=[1, 0], quiet=quiet,
573
- n='gml.ndx', answer=True, fail_on_error=True)
584
+ n=f'{bname}gml.ndx', answer=True, fail_on_error=True)
574
585
 
575
- t = gml.Top('topol.top')
586
+ t = gml.Top(f'{bname}topol.top')
576
587
  t.clear_sections()
577
588
  try:
578
- os.mkdir('ready_system')
589
+ os.mkdir(f'{bname}ready_system')
579
590
  except:
580
591
  pass
581
- t.save_top('ready_system/merged_topology.top')
582
- p = gml.Pdb('whole.gro', top=t)
592
+ t.save_top(f'{bname}ready_system/merged_topology.top')
593
+ p = gml.Pdb(f'{bname}whole.gro', top=t)
583
594
  p.add_chains()
584
- p.save_pdb('ready_system/' + final_str_name)
585
- gml.gen_mdp('ready_system/eq1_nvt.mdp', runtype='md', integrator="md", nstxout__compressed=50000,
595
+ p.save_pdb(f'{bname}ready_system/' + final_str_name)
596
+ gml.gen_mdp(f'{bname}ready_system/eq1_nvt.mdp', runtype='md', integrator="md", nstxout__compressed=50000,
586
597
  compressed__x__precision=2500, dt=0.001, coulombtype="PME", ref__t=298, nsteps=100000,
587
598
  gen__temp=100, pcoupl="no", tcoupl="v-rescale", tau__t=25.0, define="-DPOSRES",
588
599
  verlet__buffer__tolerance='2e-04')
589
- gml.gen_mdp('ready_system/eq2_npt.mdp', runtype='md', integrator="md", nstxout__compressed=50000,
600
+ gml.gen_mdp(f'{bname}ready_system/eq2_npt.mdp', runtype='md', integrator="md", nstxout__compressed=50000,
590
601
  compressed__x__precision=2500, dt=0.002, coulombtype="PME", ref__t=298, nsteps=100000, gen__vel="no",
591
602
  pcoupl="C-rescale", tcoupl="v-rescale")
592
- with open('ready_system/equilibrate.sh', 'w') as outf:
603
+ with open(f'{bname}ready_system/equilibrate.sh', 'w') as outf:
593
604
  outf.write(f'gmx grompp -f eq1_nvt.mdp -c {final_str_name} -r {final_str_name} -p merged_topology.top -o eq1\n')
594
605
  outf.write(f'gmx mdrun -deffnm eq1 -cpi -v\n')
595
606
  outf.write(f'gmx grompp -f eq2_npt.mdp -c {final_str_name} -p merged_topology.top -t eq1.cpt -o eq2\n')
@@ -729,6 +740,80 @@ def process_trajectories(mask: str, tpr: str, group_cluster: str = 'Protein', gr
729
740
  gmx_command(gmx[1], 'trjconv', f='gml_tmp0.xtc', o='processed_traj.xtc', skip=stride)
730
741
  os.remove('gml_tmp0.xtc')
731
742
 
743
+ def compare_two_rtp_sets(dir1, dir2):
744
+ # TODO for now we ignore consistency in bondedtype/improper type number
745
+ typedict1, chargedict1, dihedrals1, impropers1, bonds1 = {}, {}, {}, {}, {}
746
+ typedict2, chargedict2, dihedrals2, impropers2, bonds2 = {}, {}, {}, {}, {}
747
+ for rtp in sorted(glob(f"{dir1}/*rtp")):
748
+ tdt, cdt, dhl, imp, _, bnd = gml.SectionMol.parse_rtp('', rtp, static=True)
749
+ typedict1.update(tdt)
750
+ chargedict1.update(cdt)
751
+ dihedrals1.update(dhl)
752
+ impropers1.update(imp)
753
+ bonds1.update(bnd)
754
+ for rtp in sorted(glob(f"{dir2}/*rtp")):
755
+ tdt, cdt, dhl, imp, _, bnd = gml.SectionMol.parse_rtp('', rtp, static=True)
756
+ typedict2.update(tdt)
757
+ chargedict2.update(cdt)
758
+ dihedrals2.update(dhl)
759
+ impropers2.update(imp)
760
+ bonds2.update(bnd)
761
+ def _print_atomlevel_diff(dict1, dict2, label):
762
+ only1 = sorted(set(dict1.keys()).difference(dict2.keys()))
763
+ only2 = sorted(set(dict2.keys()).difference(dict1.keys()))
764
+ changed = sorted(k for k in set(dict1.keys()).intersection(dict2.keys()) if dict1[k] != dict2[k])
765
+ if not only1 and not only2 and not changed:
766
+ return False
767
+ print(f"{label} differ:")
768
+ for resname, atomname in only1:
769
+ print(f" only in {dir1}: {resname}/{atomname} = {dict1[(resname, atomname)]}")
770
+ for resname, atomname in only2:
771
+ print(f" only in {dir2}: {resname}/{atomname} = {dict2[(resname, atomname)]}")
772
+ for resname, atomname in changed:
773
+ print(f" changed in {resname}/{atomname}: {dir1} -> {dict1[(resname, atomname)]}, "
774
+ f"{dir2} -> {dict2[(resname, atomname)]}")
775
+ return True
776
+
777
+ def _canonical_entry(entry, section_name):
778
+ if section_name == 'bonds':
779
+ return tuple(sorted(entry[:2]))
780
+ return tuple(entry)
781
+
782
+ def _print_residue_list_diff(dict1, dict2, label, section_name):
783
+ any_diff = False
784
+ for resname in sorted(set(dict1.keys()).union(dict2.keys())):
785
+ entries1 = Counter(_canonical_entry(x, section_name) for x in dict1.get(resname, []))
786
+ entries2 = Counter(_canonical_entry(x, section_name) for x in dict2.get(resname, []))
787
+ only1 = sorted((entries1 - entries2).elements())
788
+ only2 = sorted((entries2 - entries1).elements())
789
+ if not only1 and not only2:
790
+ continue
791
+ if not any_diff:
792
+ print(f"{label} differ:")
793
+ any_diff = True
794
+ print(f" residue {resname}:")
795
+ for entry in only1:
796
+ print(f" only in {dir1}: {' '.join(entry)}")
797
+ for entry in only2:
798
+ print(f" only in {dir2}: {' '.join(entry)}")
799
+ return any_diff
800
+
801
+ found_diffs = False
802
+ found_diffs = _print_atomlevel_diff(typedict1, typedict2, "Type definitions") or found_diffs
803
+ found_diffs = _print_atomlevel_diff(chargedict1, chargedict2, "Charges") or found_diffs
804
+ found_diffs = _print_residue_list_diff(dihedrals1, dihedrals2, "Dihedrals", 'dihedrals') or found_diffs
805
+ found_diffs = _print_residue_list_diff(impropers1, impropers2, "Impropers", 'impropers') or found_diffs
806
+ found_diffs = _print_residue_list_diff(bonds1, bonds2, "Bonds", 'bonds') or found_diffs
807
+ if not found_diffs:
808
+ print(f"No RTP differences found between {dir1} and {dir2}")
809
+
810
+
811
+ def compare_two_ffdirs(dir1, dir2):
812
+ t1 = gml.Top(f'{dir1}/forcefield.itp')
813
+ t2 = gml.Top(f'{dir2}/forcefield.itp')
814
+ t1.compare_two_ffparamsets(t2, cleaned=False)
815
+ gml.compare_two_rtp_sets(dir1, dir2)
816
+
732
817
 
733
818
  def analytical_forces(struct: str, topfile: str, gmx: str = '', quiet: bool = True, cleanup: bool = True,
734
819
  traj: Optional[str] = None, selection: Optional[str] = None, savetxt: Optional[str] = None,
@@ -823,6 +908,16 @@ def numerical_forces(struct: str, topfile: str, gmx: str = '', quiet: bool = Tru
823
908
  return forces
824
909
 
825
910
 
911
+ def save_with_water(struct: str, traj: str, gmx: str = '', quiet=False):
912
+ if not gmx:
913
+ gmx = gml.find_gmx_dir()[1]
914
+ gmx_command(gmx, 'trjconv', f=traj, s=struct, o='centered_temp.xtc', pass_values='0 0', pbc='mol', center=True, ur='compact',
915
+ quiet=quiet)
916
+ gmx_command(gmx, 'trjorder', f='centered_temp.xtc', s=struct, o='ordered_temp.xtc', pass_values='0 0', quiet=quiet)
917
+ gmx_command(gmx, 'trjconv', f='ordered_temp.xtc', s=struct, o='with_water_layer.xtc', pass_values='0 0', quiet=quiet)
918
+ gmx_command(gmx, 'trjconv', f=traj, s=struct, o='centered_temp.xtc', pass_values='0 0', quiet=quiet)
919
+
920
+
826
921
  def read_log(logfile: str) -> (dict, float):
827
922
  """
828
923
  Parse a .log file.
@@ -834,8 +929,9 @@ def read_log(logfile: str) -> (dict, float):
834
929
  Each value is a 1-D numpy array aligned by block (same length).
835
930
  mean_ps_per_hour : float
836
931
  Average simulation speed in ps/hour, computed from checkpoint intervals.
837
- Returns np.nan if fewer than two checkpoints are found or interpolation
838
- is not possible.
932
+ If no usable checkpoints are present, falls back to using the mdrun
933
+ start timestamp and the latest reported simulation time.
934
+ Returns np.nan if neither timing route is possible.
839
935
  """
840
936
 
841
937
  def chunks15(s):
@@ -848,6 +944,9 @@ def read_log(logfile: str) -> (dict, float):
848
944
  checkpoint_re = re.compile(
849
945
  r'Writing checkpoint,\s*step\s*(\d+)\s*at\s*([A-Za-z]{3}\s+[A-Za-z]{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4})'
850
946
  )
947
+ start_re = re.compile(
948
+ r'Started mdrun on rank \d+ ([A-Za-z]{3}\s+[A-Za-z]{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2}\s+\d{4})'
949
+ )
851
950
 
852
951
  # Storage
853
952
  cols = {} # name -> list of values
@@ -855,6 +954,7 @@ def read_log(logfile: str) -> (dict, float):
855
954
  steps = [] # for time interpolation later
856
955
  times = [] # simulation times (ps)
857
956
  checkpoints = [] # list of (step_int, datetime)
957
+ start_time = None
858
958
 
859
959
  # Read all lines (we need lookahead)
860
960
  with open(logfile, "r", errors="ignore") as f:
@@ -898,6 +998,15 @@ def read_log(logfile: str) -> (dict, float):
898
998
  i += 1
899
999
  continue
900
1000
 
1001
+ m = start_re.search(line)
1002
+ if m:
1003
+ try:
1004
+ start_time = datetime.strptime(m.group(1), "%a %b %d %H:%M:%S %Y")
1005
+ except ValueError:
1006
+ start_time = None
1007
+ i += 1
1008
+ continue
1009
+
901
1010
  # Step / Time block
902
1011
  if step_time_header_re.match(line):
903
1012
  # Next non-empty line should be the numbers line in fixed width
@@ -1009,19 +1118,50 @@ def read_log(logfile: str) -> (dict, float):
1009
1118
  else:
1010
1119
  mean_ps_per_hour = np.nan
1011
1120
 
1012
- return arrays, mean_ps_per_hour
1013
-
1014
-
1015
- def report_timings():
1016
-
1017
- def newest_per_subdir(pattern="**/*.log"):
1121
+ if np.isnan(mean_ps_per_hour) and start_time is not None and len(times) > 0:
1122
+ elapsed_hours = (datetime.now() - start_time).total_seconds() / 3600.0
1123
+ first_time_ps = float(times[0])
1124
+ latest_time_ps = float(times[-1])
1125
+ simulated_ps = latest_time_ps - first_time_ps
1126
+ if elapsed_hours > 0 and np.isfinite(simulated_ps) and simulated_ps >= 0:
1127
+ mean_ps_per_hour = simulated_ps / elapsed_hours
1128
+
1129
+ return arrays, round(mean_ps_per_hour, 1)
1130
+
1131
+
1132
+ def report_timings(*filters):
1133
+ if not filters:
1134
+ filters = sys.argv[1:]
1135
+ if filters and filters[0] in ['-h', '--help']:
1136
+ print("usage: gml-report-timings [include_filter ...] [-exclude_filter ...]\n"
1137
+ "\n"
1138
+ "Reports the newest GROMACS log found in each subdirectory and prints\n"
1139
+ "the latest simulated time together with the estimated performance.\n"
1140
+ "\n"
1141
+ "Positional filters without a leading dash include only matching paths.\n"
1142
+ "Filters starting with '-' exclude matching paths.\n"
1143
+ "\n"
1144
+ "Examples:\n"
1145
+ " gml-report-timings\n"
1146
+ " gml-report-timings production\n"
1147
+ " gml-report-timings production -equil")
1148
+ return
1149
+
1150
+ def newest_per_subdir(pattern="**/*log"):
1151
+ positive_filters = [f for f in filters if not f.startswith('-')]
1152
+ negative_filters = [f[1:] for f in filters if f.startswith('-') and len(f) > 1]
1018
1153
  newest_files = {}
1019
1154
  for path in Path(".").rglob(pattern):
1155
+ path_str = str(path)
1156
+ if positive_filters and not any(f in path_str for f in positive_filters):
1157
+ continue
1158
+ if any(f in path_str for f in negative_filters):
1159
+ continue
1020
1160
  subdir = path.parent
1021
1161
  # stat().st_mtime gives modification time (float, seconds since epoch)
1022
1162
  if subdir not in newest_files or path.stat().st_mtime > newest_files[subdir].stat().st_mtime:
1023
1163
  newest_files[subdir] = path
1024
- return [str(p.resolve()) for p in newest_files.values()]
1164
+ return [str(p) for p in newest_files.values()]
1025
1165
 
1026
1166
  for logfile in newest_per_subdir():
1027
1167
  try:
@@ -1035,6 +1175,19 @@ def report_timings():
1035
1175
 
1036
1176
 
1037
1177
  def make_index(argv=sys.argv[1:]):
1038
- if len(argv) != 2:
1039
- print("syntax: gml-make-index structure.pdb 'selection for the group'")
1040
- ndx(argv[0], selections=argv[1])
1178
+ if argv and argv[0] in ['-h', '--help']:
1179
+ print("usage: gml-make-index structure.pdb 'selection for the group [filename]'\n"
1180
+ "\n"
1181
+ "Creates a GROMACS index group from a single Gromologist selection.\n"
1182
+ "\n"
1183
+ "Example:\n"
1184
+ " gml-make-index structure.pdb 'protein and name CA'\n"
1185
+ " gml-make-index structure.pdb 'nucleic or resname LIG' dna_ligand.ndx\n")
1186
+ return
1187
+ if len(argv) < 2 or len(argv) > 3:
1188
+ print("syntax: gml-make-index structure.pdb 'selection for the group' [filename]")
1189
+ return
1190
+ if len(argv) == 2:
1191
+ ndx(argv[0], selections=argv[1])
1192
+ else:
1193
+ ndx(argv[0], selections=argv[1], fname=argv[2])
@@ -22,6 +22,7 @@ Notes:
22
22
 
23
23
  from .Entries import EntryAtom
24
24
  from .Pdb import Pdb
25
+ import re
25
26
 
26
27
 
27
28
  class SelectionParser:
@@ -51,18 +52,28 @@ class SelectionParser:
51
52
  solvent_selection = "resname HOH TIP3 SOL OPC SPC WAT K CL NA POT K+ NA+ CLA CL-"
52
53
  backbone_selection = ("name P O1P O2P OP1 OP2 O5' H5T C5' H5'1 H5'2 H5' H5'' C4' H4' O4' C1' H1' C3' H3' C2' "
53
54
  "H2'1 H2'2 H2' H2'' O2' HO2' O3' H3T 1H5' 2H5' 1H2' 2H2' N HN CA HA C O HA1 HA2 H HO'2")
55
+ ion_selection = "resname K CL NA POT K+ NA+ CLA CL- MG MGA ZN"
54
56
  all_selection = "serial < 10000000"
55
57
  noh_selection = "not element H"
56
- selection_string = selection_string.replace('solvent', solvent_selection)
57
- selection_string = selection_string.replace('water', 'resname HOH TIP3 SOL OPC SPC WAT')
58
- selection_string = selection_string.replace('protein', protein_selection)
59
- selection_string = selection_string.replace('backbone', backbone_selection)
60
- selection_string = selection_string.replace('nucleic', '(dna or rna)')
61
- selection_string = selection_string.replace('noh', noh_selection)
62
- selection_string = selection_string.replace('dna', dna_selection)
63
- selection_string = selection_string.replace('rna', rna_selection)
64
- selection_string = selection_string.replace('all', all_selection)
65
- return sorted(list(self._select_set_atoms(selection_string))) if 'dihedral' not in selection_string else list(self._select_set_atoms(selection_string))
58
+ self.keyword_selections = {
59
+ "solvent": solvent_selection,
60
+ "water": "resname HOH TIP3 SOL OPC SPC WAT",
61
+ "protein": protein_selection,
62
+ "backbone": backbone_selection,
63
+ "nucleic": f"({dna_selection} or {rna_selection})",
64
+ "noh": noh_selection,
65
+ "ions": ion_selection,
66
+ "dna": dna_selection,
67
+ "rna": rna_selection,
68
+ "all": all_selection,
69
+ }
70
+ for key in ("solvent", "water", "protein", "backbone", "nucleic", "noh", "ions", "dna", "rna", "all"):
71
+ # Expand plain keywords, but keep keyword#N for handling in _select_set_atoms.
72
+ selection_string = re.sub(rf"\b{re.escape(key)}\b(?!\s*#)", self.keyword_selections[key], selection_string)
73
+ if 'dihedral' not in selection_string:
74
+ return sorted(list(self._select_set_atoms(selection_string)))
75
+ else:
76
+ return list(self._select_set_atoms(selection_string))
66
77
 
67
78
  def _select_set_atoms(self, selection_string: str) -> set:
68
79
  """
@@ -79,6 +90,25 @@ class SelectionParser:
79
90
  parenth_ranges, operators = self._parse_sel_string(selection_string)
80
91
  # simple phrase with no parenthesis and no operators, except for "not":
81
92
  if not parenth_ranges and not operators:
93
+ keyword_limited = re.fullmatch(r"(?:not\s+)?([A-Za-z_]+)\s*#\s*(\d+)", selection_string.strip())
94
+ if keyword_limited:
95
+ keyword = keyword_limited.group(1)
96
+ limit = int(keyword_limited.group(2))
97
+ reverse = selection_string.strip().startswith("not ")
98
+ base_sel = getattr(self, "keyword_selections", {}).get(keyword)
99
+ if base_sel is not None:
100
+ base_parenth, base_ops = self._parse_sel_string(base_sel)
101
+ if not base_parenth and not base_ops:
102
+ if base_sel.startswith("not "):
103
+ selected = sorted(list(self._find_atoms(base_sel[4:], rev=True)))
104
+ else:
105
+ selected = sorted(list(self._find_atoms(base_sel)))
106
+ else:
107
+ selected = sorted(list(self._select_set_atoms(base_sel)))
108
+ if reverse:
109
+ set_all = {n for n in range(self.nat)}
110
+ selected = sorted(list(set_all.difference(set(selected))))
111
+ return set(selected[:limit])
82
112
  if selection_string.strip().startswith("not "):
83
113
  return self._find_atoms(selection_string.lstrip()[4:], rev=True)
84
114
  else: