Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deferring #394

Merged
merged 3 commits into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions bin/martinize2
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import sys

import vermouth
import vermouth.forcefield
from vermouth.file_writer import open, DeferredFileWriter
from vermouth.file_writer import deferred_open, DeferredFileWriter
from vermouth import DATA_PATH
from vermouth.dssp import dssp
from vermouth.dssp.dssp import (
Expand Down Expand Up @@ -118,23 +118,23 @@ def pdb_to_universal(system, delete_unknown=False, force_field=None,
fudge=bonds_fudge).run_system(canonicalized)
vermouth.MergeNucleicStrands().run_system(canonicalized)
if write_graph is not None:
vermouth.pdb.write_pdb(canonicalized, str(write_graph), omit_charges=True)
DeferredFileWriter().write()
vermouth.pdb.write_pdb(canonicalized, str(write_graph), omit_charges=True,
defer_writing=False)

LOGGER.debug('Annotating required mutations and modifications.', type='step')
vermouth.AnnotateMutMod(modifications, mutations).run_system(canonicalized)
LOGGER.info('Repairing the graph.', type='step')
vermouth.RepairGraph(delete_unknown=delete_unknown, include_graph=False).run_system(canonicalized)
if write_repair is not None:
vermouth.pdb.write_pdb(canonicalized, str(write_repair),
omit_charges=True, nan_missing_pos=True)
DeferredFileWriter().write()
omit_charges=True, nan_missing_pos=True,
defer_writing=False)
LOGGER.info('Dealing with modifications.', type='step')
vermouth.CanonicalizeModifications().run_system(canonicalized)
if write_canon is not None:
vermouth.pdb.write_pdb(canonicalized, str(write_canon),
omit_charges=True, nan_missing_pos=True)
DeferredFileWriter().write()
omit_charges=True, nan_missing_pos=True,
defer_writing=False)
vermouth.AttachMass(attribute='mass').run_system(canonicalized)
vermouth.SortMoleculeAtoms().run_system(canonicalized) # was system
return canonicalized
Expand Down Expand Up @@ -191,7 +191,7 @@ def write_gmx_topology(system, top_path, defines=(), header=()):
# A given moltype can appear more than once in the sequence of
# molecules, without being uninterupted by other moltypes. Even in
# that case, we want to write the ITP only once.
with open('{}.itp'.format(moltype), 'w') as outfile:
with deferred_open('{}.itp'.format(moltype), 'w') as outfile:
# here we format and merge all citations
header[-1] = header[-1]+"\n"
header.append("Pleas cite the following papers:")
Expand Down Expand Up @@ -232,7 +232,7 @@ def write_gmx_topology(system, top_path, defines=(), header=()):
define_string = '\n'.join(
'#define {}'.format(define) for define in defines
)
with open(str(top_path), 'w') as outfile:
with deferred_open(str(top_path), 'w') as outfile:
outfile.write(
textwrap.dedent(
template.format(
Expand Down
8 changes: 6 additions & 2 deletions vermouth/dssp/dssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import subprocess
import tempfile

from ..file_writer import open
from ..file_writer import deferred_open
from ..pdb import pdb
from ..system import System
from ..processors.processor import Processor
Expand Down Expand Up @@ -143,7 +143,7 @@ def read_dssp2(lines):
return secstructs


def run_dssp(system, executable='dssp', savefile=None):
def run_dssp(system, executable='dssp', savefile=None, defer_writing=True):
"""
Run DSSP on a system and return the assigned secondary structures.

Expand All @@ -170,6 +170,8 @@ def run_dssp(system, executable='dssp', savefile=None):
Where to find the DSSP executable.
savefile: None or str or pathlib.Path
If set to a path, the output of DSSP is written in that file.
defer_writing: bool
Whether to use :meth:`~vermouth.file_writer.DeferredFileWriter.write` for writing data

Returns
list of str
Expand Down Expand Up @@ -216,6 +218,8 @@ def run_dssp(system, executable='dssp', savefile=None):
LOGGER.debug('DSSP input file written to {}', tmpfile_name)

if savefile is not None:
if defer_writing:
open = deferred_open
with open(str(savefile), 'w') as outfile:
outfile.write(process.stdout)
return read_dssp2(process.stdout.split('\n'))
Expand Down
2 changes: 1 addition & 1 deletion vermouth/file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,4 @@ def __del__(self):
# super().__del__() # object has no __del__


open = DeferredFileWriter().open
deferred_open = DeferredFileWriter().open
1 change: 0 additions & 1 deletion vermouth/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import itertools
from glob import glob
import os
from .file_writer import open
from .gmx.rtp import read_rtp
from .ffinput import read_ff
from .citation_parser import read_bib
Expand Down
8 changes: 6 additions & 2 deletions vermouth/gmx/gro.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import numpy as np

from ..file_writer import open
from ..file_writer import deferred_open
from ..molecule import Molecule
from ..truncating_formatter import TruncFormatter
from ..utils import first_alpha
Expand Down Expand Up @@ -110,7 +110,7 @@ def read_gro(file_name, exclude=('SOL',), ignh=False):
return molecule


def write_gro(system, file_name, precision=7, title='Martinized!', box=(0, 0, 0)):
def write_gro(system, file_name, precision=7, title='Martinized!', box=(0, 0, 0), defer_writing=True):
"""
Write `system` to `file_name`, which will be a GRO96 file.

Expand All @@ -126,6 +126,8 @@ def write_gro(system, file_name, precision=7, title='Martinized!', box=(0, 0, 0)
Title for the gro file.
box: tuple[float]
Box length and optionally angles.
defer_writing: bool
Whether to use :meth:`~vermouth.file_writer.DeferredFileWriter.write` for writing data
"""
formatter = TruncFormatter()
pos_format_string = '{{:{ntx}.3ft}}'.format(ntx=precision + 1)
Expand All @@ -138,6 +140,8 @@ def write_gro(system, file_name, precision=7, title='Martinized!', box=(0, 0, 0)
vel_format_string = '{{:{ntx}.4ft}}'*3
vel_format_string = vel_format_string.format(ntx=precision+1)

if defer_writing:
open = deferred_open
with open(str(file_name), 'w') as out:
out.write(title + '\n') # Title
out.write(formatter.format('{}\n', system.num_particles)) # number of atoms
Expand Down
1 change: 0 additions & 1 deletion vermouth/map_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import itertools
from pathlib import Path

from .file_writer import open
from .log_helpers import StyleAdapter, get_logger
from .map_parser import MappingDirector, Mapping

Expand Down
1 change: 0 additions & 1 deletion vermouth/map_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from collections import defaultdict
from functools import partial

from .file_writer import open
from .ffinput import _tokenize, _parse_atom_attributes
from .graph_utils import MappingGraphMatcher
from .log_helpers import StyleAdapter, get_logger
Expand Down
8 changes: 6 additions & 2 deletions vermouth/pdb/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np
import networkx as nx

from ..file_writer import open
from ..file_writer import deferred_open
from ..molecule import Molecule
from ..utils import first_alpha, distance, format_atom_string
from ..parser_utils import LineParser
Expand Down Expand Up @@ -552,7 +552,7 @@ def write_pdb_string(system, conect=True, omit_charges=True, nan_missing_pos=Fal
return '\n'.join(out)


def write_pdb(system, path, conect=True, omit_charges=True, nan_missing_pos=False):
def write_pdb(system, path, conect=True, omit_charges=True, nan_missing_pos=False, defer_writing=True):
"""
Writes `system` to `path` as a PDB formatted string.

Expand All @@ -573,10 +573,14 @@ def write_pdb(system, path, conect=True, omit_charges=True, nan_missing_pos=Fals
with 'nan' as coordinates; this will cause the output file to be
*invalid* for most uses.
for most use.
defer_writing: bool
Whether to use :class:`~vermouth.file_writer.DeferredFileWriter` for writing data

See Also
--------
:func:write_pdb_string
"""
if defer_writing:
open = deferred_open
with open(path, 'w') as out:
out.write(write_pdb_string(system, conect, omit_charges, nan_missing_pos))
1 change: 0 additions & 1 deletion vermouth/processors/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import os.path
import random

from ..file_writer import open
from .processor import Processor
from .. import DATA_PATH
from ..log_helpers import StyleAdapter, get_logger
Expand Down