• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GMPavanLab / SOAPify / 4607244921

pending completion
4607244921

push

github

GitHub
Adding examples (#77)

11 of 91 new or added lines in 5 files covered. (12.09%)

1 existing line in 1 file now uncovered.

830 of 929 relevant lines covered (89.34%)

2.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/SOAPify/cli.py
1
"""submodule containing the command line interfaces of SOAPify"""
NEW
2
from argparse import ArgumentParser
×
3

4

NEW
5
def getDictFromList(nameAndValues: list) -> dict:
×
6
    """generates a dictionary from a list of couples [name, value]
7

8
    Args:
9
        nameAndValues (list):
10
            the list of couples [name, value]
11

12
    Returns:
13
        dict:
14
            the dictionary, for example {name0:value0, name1:value1}
15
    """
NEW
16
    myDict = {}
×
NEW
17
    for attr in nameAndValues:
×
NEW
18
        myDict[attr[0]] = attr[1]
×
NEW
19
    return myDict
×
20

21

NEW
22
def createTrajectory():
×
23
    """Creates or overide an hdf5 file containing a given trajectory
24

25
    if you are creating ahdf5 file from a data+dump from a soap simulation
26
    remember to add `-u atom_style "id type x y z"` to the arguments
27

28
    defaults trajChunkSize=1000"""
NEW
29
    from MDAnalysis import Universe as mdaUniverse
×
30

NEW
31
    from SOAPify.HDF5er import MDA2HDF5
×
NEW
32
    from os import path
×
33

NEW
34
    parser = ArgumentParser(description=createTrajectory.__doc__)
×
NEW
35
    parser.add_argument("topology", help="the topology file")
×
NEW
36
    parser.add_argument("hdf5File", help="the file where to putput the trajectory")
×
NEW
37
    parser.add_argument(
×
38
        "-t",
39
        "--trajectory",
40
        help="the trajectory file(s)",
41
        metavar="trajectory",
42
        action="append",
43
        default=[],
44
    )
NEW
45
    parser.add_argument(
×
46
        "-n",
47
        "--name",
48
        metavar="name",
49
        help="the name of the trajectory to save",
50
    )
NEW
51
    parser.add_argument(
×
52
        "--types",
53
        metavar="atomNames",
54
        help="list of the atoms names",
55
        nargs="+",
56
        dest="atomTypes",
57
    )
NEW
58
    parser.add_argument(
×
59
        "-a",
60
        "--attribute",
61
        nargs=2,
62
        action="append",
63
        dest="extraAttributes",
64
        metavar=("name", "value"),
65
        help="extra attributes to store in the trajectory, saved as strings",
66
    )
NEW
67
    parser.add_argument(
×
68
        "-u",
69
        "--universe-options",
70
        nargs=2,
71
        action="append",
72
        dest="universeOPTs",
73
        metavar=("name", "value"),
74
        help="extra option to pass to the MDA universe, compatible only with string"
75
        " values, use the python script if you need to pass more other settings",
76
    )
NEW
77
    parser.add_argument(
×
78
        "-d",
79
        "--dry-run",
80
        action="store_true",
81
        help="just print the output without making any action",
82
    )
NEW
83
    args = parser.parse_args()
×
84
    # print(args)
85
    # arguments
NEW
86
    trajectoryFiles = args.trajectory
×
NEW
87
    filename = args.hdf5File
×
NEW
88
    topo = args.topology
×
NEW
89
    name = args.name
×
NEW
90
    if name is None:
×
NEW
91
        name = path.basename(topo)
×
NEW
92
        name = name.split(".")[0]
×
93

NEW
94
    print(
×
95
        f'from topology "{topo}"',
96
        f'and trajectory "{trajectoryFiles}":',
97
        "and creating a new trajectory in",
98
    )
99

NEW
100
    print(f'"{filename}/Trajectories/{name}"')
×
NEW
101
    extraAttrs = None
×
NEW
102
    if args.extraAttributes:
×
NEW
103
        extraAttrs = getDictFromList(args.extraAttributes)
×
NEW
104
        print("extra attributes:", extraAttrs)
×
NEW
105
    universeOptions = {}
×
NEW
106
    if args.universeOPTs:
×
NEW
107
        universeOptions = getDictFromList(args.universeOPTs)
×
108

NEW
109
    if args.dry_run:
×
NEW
110
        exit()
×
111

NEW
112
    u = mdaUniverse(topo, *trajectoryFiles, **universeOptions)
×
NEW
113
    if args.atomTypes:
×
NEW
114
        ntypes = len(args.atomTypes)
×
NEW
115
        if len(u.atoms) % ntypes != 0:
×
NEW
116
            raise ValueError(
×
117
                f"The number of atom types is not compatible with the number"
118
                f" of atoms:{len(u.atoms)} % {ntypes} = {len(u.atoms) % ntypes}"
119
            )
NEW
120
        u.atoms.types = args.atomTypes * (len(u.atoms) // ntypes)
×
121

NEW
122
    MDA2HDF5(u, filename, name, trajChunkSize=1000, attrs=extraAttrs)
×
123
    # TODO: implement this:
124
    # from MDAnalysis import transformations
125
    # ref = mdaUniverse(topo, atom_style="id type x y z")
126
    # u.trajectory.add_transformations(transformations.fit_rot_trans(u, ref))
127
    # MDA2HDF5(u, name + "_fitted.hdf5", f"{name}_fitted", trajChunkSize=1000))
128

129

NEW
130
def traj2SOAP():
×
131
    """Given an hdf5 file containing trajectories calculates SOAP of the contained trajectories
132

133

134
    default SOAP engine is dscribe"""
135

NEW
136
    from SOAPify import saponifyTrajectory
×
NEW
137
    from SOAPify.HDF5er import isTrajectoryGroup
×
NEW
138
    import h5py
×
139

NEW
140
    parser = ArgumentParser(description=traj2SOAP.__doc__)
×
NEW
141
    parser.add_argument(
×
142
        "trajFile",
143
        help="the file containing the trajectories",
144
    )
NEW
145
    parser.add_argument(
×
146
        "-s",
147
        "--SOAPFile",
148
        help="The file were to save the SOAP fingerprints, if not specified"
149
        " creates a SOAP group in within the trajFile",
150
    )
NEW
151
    parser.add_argument(
×
152
        "-t",
153
        "--trajectory",
154
        default="Trajectories",
155
        help="Specify the group containing the trajectories groups or the"
156
        " trajectory group tha you wnat to calculate the SOAP fingerprints",
157
    )
NEW
158
    parser.add_argument(
×
159
        "-e",
160
        "--engine",
161
        choices=["dscribe", "quippy"],
162
        default="dscribe",
163
        help="the engine used to calculate SOAP",
164
    )
NEW
165
    parser.add_argument(
×
166
        "-l",
167
        "--lMax",
168
        type=int,
169
        default=8,
170
        help="the lmax parameter, defaults to 8",
171
    )
NEW
172
    parser.add_argument(
×
173
        "-n",
174
        "--nMax",
175
        type=int,
176
        default=8,
177
        help="the nmax parameter, defaults to 8",
178
    )
NEW
179
    parser.add_argument(
×
180
        "-r",
181
        "--rCut",
182
        type=float,
183
        default=10.0,
184
        help="the rcut parameter, defaults to 10.0 Å",
185
    )
NEW
186
    parser.add_argument(
×
187
        "-j",
188
        "--jobs",
189
        type=int,
190
        default=1,
191
        help="the number of jobs to use, defaults to 1",
192
    )
NEW
193
    parser.add_argument(
×
194
        "-d",
195
        "--dry-run",
196
        action="store_true",
197
        help="just print the output without making any action",
198
    )
NEW
199
    args = parser.parse_args()
×
200
    # print(args)
201

NEW
202
    trajGroupLocation = args.trajectory
×
203

NEW
204
    def worker(group: "h5py.Group|h5py.Dataset", soapFile: h5py.File):
×
NEW
205
        print(
×
206
            f"\"{group.name}\" is {'' if isTrajectoryGroup(group) else 'not '}a trajectory group"
207
        )
NEW
208
        if isTrajectoryGroup(group):
×
NEW
209
            if args.dry_run:
×
NEW
210
                return
×
NEW
211
            saponifyTrajectory(
×
212
                trajContainer=group,
213
                SOAPoutContainer=soapFile.require_group("SOAP"),
214
                SOAPOutputChunkDim=1000,
215
                SOAPnJobs=args.jobs,
216
                SOAPrcut=args.rCut,
217
                SOAPnmax=args.nMax,
218
                SOAPlmax=args.lMax,
219
                useSoapFrom=args.engine,
220
            )
221

NEW
222
    SOAPFile = args.SOAPFile
×
NEW
223
    if args.SOAPFile is None:
×
NEW
224
        SOAPFile = args.trajFile
×
NEW
225
    with h5py.File(
×
226
        args.trajFile, "r" if SOAPFile != args.trajFile else "a"
227
    ) as workFile, h5py.File(SOAPFile, "a") as SOAPFile:
NEW
228
        trajGroup = workFile[trajGroupLocation]
×
NEW
229
        if isTrajectoryGroup(trajGroup):
×
NEW
230
            worker(trajGroup, SOAPFile)
×
231
        else:
NEW
232
            for _, group in trajGroup.items():
×
NEW
233
                worker(group, SOAPFile)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc