-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmartinize2
executable file
·1161 lines (1082 loc) · 39.6 KB
/
martinize2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2018 University of Groningen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
High level API for Martinize2
"""
import argparse
import functools
import logging
import itertools
import textwrap
from pathlib import Path
import sys
import networkx as nx
import vermouth
import vermouth.forcefield
from vermouth.file_writer import deferred_open, DeferredFileWriter
from vermouth import DATA_PATH
from vermouth.dssp import dssp
from vermouth.dssp.dssp import (
AnnotateDSSP,
AnnotateMartiniSecondaryStructures,
AnnotateResidues,
)
from vermouth.log_helpers import (
StyleAdapter,
BipolarFormatter,
CountingHandler,
TypeAdapter,
ignore_warnings_and_count,
)
from vermouth import selectors
from vermouth.map_input import (
read_mapping_directory,
generate_all_self_mappings,
combine_mappings,
)
from vermouth.rcsu.contact_map import read_go_map
from vermouth.rcsu.go_pipeline import GoPipeline
from vermouth.citation_parser import citation_formatter
from vermouth.gmx.topology import write_gmx_topology
# TODO Since vermouth's __init__.py does some logging (KDTree), this may or may
# not work as intended. Investigation required.
LOGGER = TypeAdapter(logging.getLogger("vermouth"))
PRETTY_FORMATTER = logging.Formatter(
fmt="{levelname:>8} - {type} - {message}", style="{"
)
DETAILED_FORMATTER = logging.Formatter(
fmt="{levelname:>8} - {type} - {name} - {message}", style="{"
)
COUNTER = CountingHandler()
# Control above what level message we want to count
COUNTER.setLevel(logging.WARNING)
CONSOLE_HANDLER = logging.StreamHandler()
FORMATTER = BipolarFormatter(
DETAILED_FORMATTER, PRETTY_FORMATTER, logging.DEBUG, logger=LOGGER
)
CONSOLE_HANDLER.setFormatter(FORMATTER)
LOGGER.addHandler(CONSOLE_HANDLER)
LOGGER.addHandler(COUNTER)
LOGGER = StyleAdapter(LOGGER)
VERSION = "martinize with vermouth {}".format(vermouth.__version__)
def read_system(path, ignore_resnames=(), ignh=None, modelidx=None):
"""
Read a system from a PDB or GRO file.
This function guesses the file type based on the file extension.
The resulting system does not have a force field and may not have edges.
"""
system = vermouth.System()
file_extension = path.suffix.upper()[1:] # We do not keep the dot
if file_extension in ["PDB", "ENT"]:
vermouth.PDBInput(
str(path), exclude=ignore_resnames, ignh=ignh, modelidx=modelidx
).run_system(system)
elif file_extension in ["GRO"]:
vermouth.GROInput(str(path), exclude=ignore_resnames, ignh=ignh).run_system(
system
)
else:
raise ValueError('Unknown file extension "{}".'.format(file_extension))
return system
def pdb_to_universal(
system,
delete_unknown=False,
force_field=None,
modifications=None,
mutations=None,
bonds_from_name=True,
bonds_from_dist=True,
bonds_fudge=1,
write_graph=None,
write_repair=None,
write_canon=None,
):
"""
Convert a system read from the PDB to a clean canonical atomistic system.
"""
if force_field is None:
force_field = vermouth.forcefield.get_native_force_field("charmm")
if modifications is None:
modifications = []
if mutations is None:
mutations = []
canonicalized = system.copy()
canonicalized.force_field = force_field
LOGGER.info("Guessing the bonds.", type="step")
vermouth.MakeBonds(
allow_name=bonds_from_name, allow_dist=bonds_from_dist, 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, 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,
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,
defer_writing=False,
)
vermouth.AttachMass(attribute="mass").run_system(canonicalized)
return canonicalized
def martinize(system, mappings, to_ff, delete_unknown=False, idrs=False, disordered_regions=None):
"""
Convert a system from one force field to an other at lower resolution.
"""
LOGGER.info("Creating the graph at the target resolution.", type="step")
vermouth.DoMapping(
mappings=mappings,
to_ff=to_ff,
delete_unknown=delete_unknown,
attribute_keep=("cgsecstruct", "chain"),
attribute_must=("resname",),
attribute_stash=("resid",),
).run_system(system)
LOGGER.info("Averaging the coordinates.", type="step")
vermouth.DoAverageBead(ignore_missing_graphs=True).run_system(system)
if idrs:
LOGGER.info("Annotating IDRs.", type="step")
vermouth.AnnotateIDRs(id_regions=[(int(start), int(stop)) for start, stop in disordered_regions]).run_system(system)
LOGGER.info("Applying the links.", type="step")
vermouth.DoLinks().run_system(system)
LOGGER.info("Placing the charge dummies.", type="step")
vermouth.LocateChargeDummies().run_system(system)
return system
def _cys_argument(value):
"""
Convert and validate the value of the cys option for argparse.
Parameters
----------
value: str
The value given to the command line.
Return
------
str or float
A value understood by the main function. This value can be either
'auto' to detect cystein bridges automatically based on a default
distance, 'none' to not have cystein bridges, or a float value to set
cystein bridges based on a distance threshold.
Raises
------
argparse.ArgumentError
Raised when the value cannot be converted.
"""
try:
result = float(value)
except ValueError:
lowered = value.lower()
if lowered in ("auto", "none"):
return lowered
raise argparse.ArgumentError(
argument="-cys",
message=(
'The value of the "cys" option must be "auto", "none", '
"or a distance in nanometers."
),
)
else:
return result
def maxwarn(value):
"""
Given a maxwarn specification, split it in a warning type, and the number
to ignore.
>>> maxwarn('3')
(None, 3)
>>> maxwarn('general:15')
('general', 15)
>>> maxwarn('inconsistent-data')
('inconsistent-data, None)
Parameters
----------
value: str
A warning type and a count, separated by a colon.
Returns
-------
tuple[str, int]
A warning type and the associated count to ignore. Either element can be
None if not specified.
Raises
------
argparse.ArgumentTypeError
"""
msg = (
"Values for the -maxwarn option must be the name of a "
"warning type, a number, or following the format "
"'<warning-type>:<count>' where <warning-type> is the name "
"of the warning type to ignore, and <count> is the number of "
"warning of that type to ignore. "
"'{value}' is not a valid value.".format(value=value)
)
splitted = value.split(":")
if len(splitted) == 1:
try:
count = int(value)
except ValueError:
# The value is not an int, so a warning type to ignore an
# an unspecified number of
return (value, None)
else:
return (None, count)
elif len(splitted) == 2:
try:
count = int(splitted[1])
except ValueError:
pass # The exception will be raised at the end of the function
else:
return (splitted[0], count)
raise argparse.ArgumentTypeError(msg)
def entry():
"""
Parses commandline arguments and performs the logic.
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("-V", "--version", action="version", version=VERSION)
file_group = parser.add_argument_group("Input and output files")
file_group.add_argument(
"-f", dest="inpath", required=False, type=Path, help="Input file (PDB|GRO)"
)
file_group.add_argument(
"-x",
dest="outpath",
required=False,
type=Path,
help="Output coarse grained structure (PDB)",
)
file_group.add_argument(
"-o", dest="top_path", type=Path, help="Output topology (TOP)"
)
file_group.add_argument(
"-sep",
dest="keep_duplicate_itp",
action="store_true",
default=False,
help="Write separate topologies for identical chains",
)
chain_merging = file_group.add_argument(
"-merge",
dest="merge_chains",
type=str,
action="append",
help="Merge chains: either a comma separated list of chains to merge e.g. -merge A,B,C (+), or -merge all\n"
"if instead all chains in the input file should be merged.\n"
"Can be given multiple times for different groups of chains to merge.",
)
file_group.add_argument(
"-resid",
dest="resid_handling",
type=str,
default="mol",
help="How to handle resid. Choice of mol or input.\n"
"mol: resids are numbered from 1 to n for each molecule\n"
"input: resids are the same as in the input pdb",
)
file_group.add_argument(
"-ignore",
dest="ignore_res",
nargs="+",
default=[],
action="append",
type=lambda x: x.split(","),
help="Ignore residues with that name: e.g. " "-ignore HOH,LIG (+)",
)
file_group.add_argument(
"-ignh",
dest="ignore_h",
default=False,
action="store_true",
help="Ignore all Hydrogen atoms in the input file",
)
file_group.add_argument(
"-model",
dest="modelidx",
default=None,
type=int,
help="Which MODEL to select. Only meaningful for" " PDB files.",
)
file_group.add_argument(
"-bonds-from",
dest="bonds_from",
choices=["name", "distance", "none", "both"],
default="both",
help="How to determine connectivity in the input. "
"If 'none', only bonds from the input file (CONECT)"
" will be used.",
)
file_group.add_argument(
"-bonds-fudge",
dest="bonds_fudge",
type=float,
default=1.2,
help="Factor with which Van der Waals"
" radii should be scaled when determining bonds "
"based on distances.",
)
ff_group = parser.add_argument_group("Force field selection")
ff_group.add_argument(
"-ff", dest="to_ff", default="martini3001", help="Which forcefield to use"
)
ff_group.add_argument(
"-from",
dest="from_ff",
default="charmm",
help="Force field of the original structure.",
)
ff_group.add_argument(
"-ff-dir",
dest="extra_ff_dir",
action="append",
type=Path,
default=[],
help="Additional repository for custom force fields.",
)
ff_group.add_argument(
"-map-dir",
dest="extra_map_dir",
action="append",
type=Path,
default=[],
help="Additional repository for mapping files.",
)
ff_group.add_argument(
"-list-ff",
action="store_true",
dest="list_ff",
help="List all known force fields, and exit.",
)
ff_group.add_argument(
"-list-blocks",
action="store_true",
dest="list_blocks",
help="List all Blocks and Modifications known to the" " force field, and exit.",
)
posres_group = parser.add_argument_group("Position restraints")
posres_group.add_argument(
"-p",
dest="posres",
type=str.lower,
choices=("none", "all", "backbone"),
default="none",
help="Output position restraints (none/all/backbone)",
)
posres_group.add_argument(
"-pf",
dest="posres_fc",
type=float,
default=1000,
help="Position restraints force constant in kJ/mol/nm^2",
)
secstruct_group = parser.add_argument_group("Secondary structure handling")
secstruct_exclusion = secstruct_group.add_mutually_exclusive_group()
secstruct_exclusion.add_argument(
"-dssp",
nargs="?",
const=True,
help="DSSP executable for determining structure. If this flag is given"
"but no executable is specified, the mdtraj library will be used"
"to compute the secondary structure, if it can be imported.",
)
secstruct_exclusion.add_argument(
"-ss",
dest="ss",
type=str.upper,
metavar="SEQUENCE",
help=("Manually set the secondary structure of the proteins. "
"Either give a dssp sequence for each residue in your input file "
"or a single letter to be repeated for the entire sequence, e.g. -ss C"),
)
secstruct_exclusion.add_argument(
"-collagen", action="store_true", default=False, help="Use collagen parameters"
)
secstruct_group.add_argument(
"-ed",
dest="extdih",
action="store_true",
default=False,
help=("Use dihedrals for extended regions " "rather than elastic bonds"),
)
rb_group = parser.add_argument_group("Protein elastic network")
rb_group.add_argument(
"-elastic", action="store_true", default=False, help="Write elastic bonds"
)
rb_group.add_argument(
"-ef",
dest="rb_force_constant",
type=float,
default=500,
help="Elastic bond force constant Fc in kJ/mol/nm^2",
)
rb_group.add_argument(
"-el",
dest="rb_lower_bound",
type=float,
default=0,
help="Elastic bond lower cutoff: F = Fc if rij < lo",
)
rb_group.add_argument(
"-eu",
dest="rb_upper_bound",
type=float,
default=0.9,
help="Elastic bond upper cutoff: F = 0 if rij > up",
)
rb_group.add_argument(
"-ermd",
dest="res_min_dist",
type=int,
help=(
"The minimum separation between two residues to have an RB "
"the default value is set by the force-field."
),
default=None,
)
rb_group.add_argument(
"-ea",
dest="rb_decay_factor",
type=float,
default=0,
help="Elastic bond decay factor a",
)
rb_group.add_argument(
"-ep",
dest="rb_decay_power",
type=float,
default=1,
help="Elastic bond decay power p",
)
rb_group.add_argument(
"-em",
dest="rb_minimum_force",
type=float,
default=0,
help="Remove elastic bonds with force constant lower than this",
)
rb_group.add_argument(
"-eb",
dest="rb_selection",
type=lambda x: x.split(","),
default=None,
help="Comma separated list of bead names for elastic bonds",
)
rb_group.add_argument(
"-eunit",
dest="rb_unit",
default="molecule",
help=(
"Establish what is the structural unit for the "
"elastic network. Bonds are only created within"
" a unit. Options are molecule, chain, all, or a"
"specified region defined by resids, with following"
"format: <start_resid_1>:<end_resid_1>, <start_resid_2>:<end_resid_2>..."
),
)
rb_group.add_argument(
"-ew",
dest="rb_write",
default=False,
action="store_true",
help="Write the elastic network to an external .itp file",
)
go_group = parser.add_argument_group("Virtual site based GoMartini")
go_group.add_argument(
"-go",
dest="go",
required=False,
type=Path,
default=None,
help="Contact map to be used for the Martini Go model."
"Currently, only one format is supported. See docs.",
)
go_group.add_argument(
"-go-eps",
dest="go_eps",
type=float,
default=9.414,
help=("The strength of the Go model structural bias in kJ/mol."),
)
go_group.add_argument(
"-go-moltype",
dest="govs_moltype",
default="molecule_0",
help=("Set the name of the molecule when using " "Virtual Sites GoMartini."),
)
go_group.add_argument(
"-go-low",
dest="go_low",
type=float,
default=0.3,
help=("Minimum distance (nm) below which contacts are removed."),
)
go_group.add_argument(
"-go-up",
dest="go_up",
type=float,
default=1.1,
help=("Maximum distance (nm) above which contacts are removed."),
)
go_group.add_argument(
"-go-res-dist",
dest="go_res_dist",
type=int,
default=3,
help=("Minimum graph distance (similar sequence distance) below which"
"contacts are removed. "),
)
water_group = parser.add_argument_group("Apply water bias.")
water_group.add_argument(
"-water-bias",
dest="water_bias",
action="store_true",
default=False,
help=("Automatically apply water bias to different secondary structure elements."),
)
water_group.add_argument(
"-water-bias-eps",
dest="water_bias_eps",
nargs='+',
type=lambda s: s.split(":"),
default=[],
help=("Define the strength of the water bias by secondary structure type. "
"For example, use `H:3.6 C:2.1` to bias helixes and coils. "
"Using the idr option (e.g. idr:2.1) intrinsically disordered regions "
"are biased seperately."),
)
water_group.add_argument(
"-id-regions",
dest="water_idrs",
type=lambda s: s.split(":"),
default=[],
nargs='+',
help=("Intrinsically disordered regions specified by resid."
"These parts are biased differently when applying a water bias."
"format: <start_resid_1>:<end_resid_1> <start_resid_2>:<end_resid_2>..."
),
)
idr_tuning = water_group.add_argument(
"-idr-tune",
dest="idr_tune",
action="store_true",
default=False,
help=("Tune the idr regions with specific bonded potentials."),
)
prot_group = parser.add_argument_group("Protein description")
prot_group.add_argument(
"-scfix",
dest="scfix",
action="store_true",
default=False,
help="Apply side chain corrections.",
)
prot_group.add_argument(
"-cys",
dest="cystein_bridge",
type=_cys_argument,
default="none",
help="Cystein bonds",
)
prot_group.add_argument(
"-mutate",
dest="mutations",
action="append",
type=lambda s: s.split(":"),
default=[],
help="Mutate a residue. Desired mutation is "
"specified as, e.g. A-PHE45:ALA. The format is "
"<chain>-<resname><resid>:<new resname>. Elements "
"of the specification can be omitted as required."
"e.g. PHE45:ALA will mutate all PHE with resid 45 to ALA, "
"A-PHE:ALA will mutate all PHE on chain A to ALA",
)
prot_group.add_argument(
"-modify",
dest="modifications",
action="append",
type=lambda s: s.split(":"),
default=[],
help="Add a modification to a residue. Desired "
"modification is specified as, e.g. A-ASP45:ASP0. "
"The format is <chain>-<resname><resid>:<modification>."
" Elements of the specification can be omitted as "
"required. e.g. ASP45:ASP0 will modify all ASP with "
"resid 45 to ASP0, A-ASP:ASP0 will modify all ASP on "
"chain A to ASP0",
)
prot_group.add_argument(
"-nter",
dest="modifications",
action="append",
type=lambda s: ["nter", s],
default=[],
help="Shorthand for patching N-termini. An "
"N-terminus is defined as a residue which is "
"connected to 1 other residue, and has the highest "
"resid.",
)
prot_group.add_argument(
"-cter",
dest="modifications",
action="append",
type=lambda s: ["cter", s],
default=[],
help="Shorthand for patching C-termini. A "
"C-terminus is defined as a residue which is "
"connected to 1 other residue, and has the lowest "
"resid.",
)
# Unfortunately there's no action=extend_const. append_const *almost* does
# what we need, but it makes the resulting list too deep:
# [[['cter', 'COOH-ter'], ['nter', 'NH2-ter']], ['ASP3', 'ASP0']]
prot_group.add_argument(
"-nt",
dest="neutral_termini",
action="store_true",
default=False,
help="Set neutral termini (charged is default). "
'Alias for "-nter NH2-ter -cter COOH-ter"',
)
debug_group = parser.add_argument_group("Debugging options")
debug_group.add_argument(
"-write-graph",
type=Path,
default=None,
help="Write the graph as PDB after the MakeBonds step.",
)
debug_group.add_argument(
"-write-repair",
type=Path,
default=None,
help=(
"Write the graph as PDB after the "
"RepairGraph step. The resulting file may "
'contain "nan" coordinates making it '
"unreadable by most softwares."
),
)
debug_group.add_argument(
"-write-canon",
type=Path,
default=None,
help=(
"Write the graph as PDB after the "
"CanonicalizeModifications step. The "
'resulting file may contain "nan" '
"coordinates making it unreadable by most "
"software."
),
)
debug_group.add_argument(
"-v",
dest="verbosity",
action="count",
help="Enable debug logging output. Can be given " "multiple times.",
default=0,
)
debug_group.add_argument(
"-maxwarn",
dest="maxwarn",
type=maxwarn,
action="append",
nargs="+",
default=[],
help="The maximum number of allowed warnings. If "
"more warnings are encountered no output files are"
" written.",
)
args = parser.parse_args()
loglevels = {0: logging.INFO, 1: logging.DEBUG, 2: 5}
LOGGER.setLevel(loglevels[args.verbosity])
known_force_fields = vermouth.forcefield.find_force_fields(
Path(DATA_PATH) / "force_fields"
)
known_mappings = read_mapping_directory(
Path(DATA_PATH) / "mappings", known_force_fields
)
# Add user force fields and mappings
for directory in args.extra_ff_dir:
try:
vermouth.forcefield.find_force_fields(directory, known_force_fields)
except FileNotFoundError:
msg = '"{}" given to the -ff-dir option should be a directory.'
raise ValueError(msg.format(directory))
for directory in args.extra_map_dir:
try:
partial_mapping = read_mapping_directory(directory, known_force_fields)
except NotADirectoryError:
msg = '"{}" given to the -map-dir option should be a directory.'
raise ValueError(msg.format(directory))
combine_mappings(known_mappings, partial_mapping)
if args.list_ff:
print("The following force fields are known:")
for idx, ff_name in enumerate(reversed(list(known_force_fields)), 1):
print("{:3d}. {}".format(idx, ff_name))
parser.exit()
# Build self mappings
partial_mapping = generate_all_self_mappings(known_force_fields.values())
combine_mappings(known_mappings, partial_mapping)
from_ff = args.from_ff
if args.to_ff not in known_force_fields:
raise ValueError('Unknown force field "{}".'.format(args.to_ff))
if args.from_ff not in known_force_fields:
raise ValueError('Unknown force field "{}".'.format(args.from_ff))
# if from_ff not in known_mappings or args.to_ff not in known_mappings[from_ff]:
# raise ValueError('No mapping known to go from "{}" to "{}".'
# .format(from_ff, args.to_ff))
if args.list_blocks:
print("The following Blocks are known to force field {}:".format(args.from_ff))
print(", ".join(sorted(known_force_fields[args.from_ff].blocks)))
print(
"The following Modifications are known to force field {}:".format(
args.from_ff
)
)
print(", ".join(sorted(known_force_fields[args.from_ff].modifications)))
print()
print("The following Blocks are known to force field {}:".format(args.to_ff))
print(", ".join(sorted(known_force_fields[args.to_ff].blocks)))
print(
"The following Modifications are known to force field {}:".format(
args.to_ff
)
)
print(", ".join(sorted(known_force_fields[args.to_ff].modifications)))
parser.exit()
if args.elastic and args.go:
parser.error(
"A rubber band elastic network and GoMartini are not "
"compatible. The -elastic and -govs-include flags cannot "
"be used together."
)
if args.to_ff.startswith("elnedyn"):
# FIXME: This type of thing should be added to the FF itself.
LOGGER.info(
"The forcefield {} must always be used with an elastic "
"network. Enabling it now.",
args.to_ff,
)
args.elastic = True
file_extension = args.inpath.suffix.upper()[1:] # We do not keep the dot
if file_extension in ["GRO"] and args.modelidx is not None:
parser.error("GRO files don't know the concept of models.")
if args.modelidx is None:
# Set a sane default value. Can't do this using argparse machinery,
# since we need to be able to check whether the flag was given.
args.modelidx = 1
bonds_from_name = args.bonds_from in ("name", "both")
bonds_from_dist = args.bonds_from in ("distance", "both")
# args.ignore_res is a pretty deep list: given "-ignore HOH CU,LIG -ignore LIG2"
# it'll contain [[['HOH'], ['CU', 'LIG']], [['LIG2']]]
ignore_res = set()
for grp in args.ignore_res:
ignore_res.update(*grp)
if args.neutral_termini:
args.modifications.append(["cter", "COOH-ter"])
args.modifications.append(["nter", "NH2-ter"])
else:
if args.modifications:
resspecs, mods = zip(*args.modifications)
else:
resspecs, mods = [], []
if not any("cter" in resspec for resspec in resspecs):
args.modifications.append(["cter", "C-ter"])
if not any("nter" in resspec for resspec in resspecs):
args.modifications.append(["nter", "N-ter"])
# Reading the input structure.
# So far, we assume we only go from atomistic to martini. We want the
# input structure to be a clean universal system.
# For now at least, we silently delete molecules with unknown blocks.
system = read_system(
args.inpath,
ignore_resnames=ignore_res,
ignh=args.ignore_h,
modelidx=args.modelidx,
)
system = pdb_to_universal(
system,
delete_unknown=True,
force_field=known_force_fields[from_ff],
bonds_from_name=bonds_from_name,
bonds_from_dist=bonds_from_dist,
bonds_fudge=args.bonds_fudge,
modifications=args.modifications,
mutations=args.mutations,
write_graph=args.write_graph,
write_repair=args.write_repair,
write_canon=args.write_canon,
)
LOGGER.info("Read input.", type="step")
for molecule in system.molecules:
LOGGER.debug("Read molecule {}.", molecule, type="step")
target_ff = known_force_fields[args.to_ff]
if args.dssp:
if not isinstance(args.dssp, str):
args.dssp = None
AnnotateDSSP(executable=args.dssp, savedir='.').run_system(system)
AnnotateMartiniSecondaryStructures().run_system(system)
elif args.ss is not None:
AnnotateResidues(
attribute="secstruct",
sequence=args.ss,
molecule_selector=selectors.is_protein,
).run_system(system)
AnnotateMartiniSecondaryStructures().run_system(system)
elif args.collagen:
if not target_ff.has_feature("collagen"):
LOGGER.warning(
'The force field "{}" does not have specific '
"parameters for collagen (-collagen).",
target_ff.name,
type="missing-feature",
)
AnnotateResidues(
attribute="cgsecstruct",
sequence="F",
molecule_selector=selectors.is_protein,
).run_system(system)
if args.extdih and not target_ff.has_feature("extdih"):
LOGGER.warning(
'The force field "{}" does not define dihedral '
"angles for extended regions of proteins (-extdih).",
target_ff.name,
type="missing-feature",
)
vermouth.SetMoleculeMeta(extdih=args.extdih).run_system(system)
if args.scfix and not target_ff.has_feature("scfix"):
LOGGER.warning(
'The force field "{}" does not define angle and '
"torsion for the side chain corrections (-scfix).",
target_ff.name,
type="missing-feature",
)
vermouth.SetMoleculeMeta(scfix=args.scfix).run_system(system)
vermouth.SetMoleculeMeta(idr=args.idr_tune).run_system(system)
if args.idr_tune:
if not target_ff.has_feature("idr"):
LOGGER.warning('Improved IDR potentials are not implemented '
'for this force field',
type="missing-feature")
ss_sequence = list(
itertools.chain(
*(
dssp.sequence_from_residues(molecule, "secstruct")
for molecule in system.molecules
if selectors.is_protein(molecule)
)
)
)
if args.cystein_bridge == "none":
vermouth.RemoveCysteinBridgeEdges().run_system(system)
elif args.cystein_bridge != "auto":
vermouth.AddCysteinBridgesThreshold(args.cystein_bridge).run_system(system)
# Run martinize on the system.
system = martinize(
system,
mappings=known_mappings,
to_ff=known_force_fields[args.to_ff],
delete_unknown=True,
idrs=args.idr_tune,
disordered_regions=args.water_idrs
)
# Apply position restraints if required.
if args.posres != "none":
LOGGER.info("Applying position restraints.", type="step")
node_selectors = {
"all": selectors.select_all,
"backbone": selectors.select_backbone,
}
node_selector = node_selectors[args.posres]
vermouth.ApplyPosres(node_selector, args.posres_fc).run_system(system)
# Generate the Go model if required
if args.go:
LOGGER.info("Reading Go model contact map.", type="step")
go_map = read_go_map(args.go)
LOGGER.info("Generating the Go model.", type="step")
GoPipeline.run_system(system,
moltype=args.govs_moltype,
contact_map=go_map,
cutoff_short=args.go_low,
cutoff_long=args.go_up,
go_eps=args.go_eps,
res_dist=args.go_res_dist,)
defines = ("GO_VIRT",)
itp_paths = {"atomtypes": "go_atomtypes.itp",
"nonbond_params": "go_nbparams.itp"}
else: