-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgro87.cpp
More file actions
2208 lines (1856 loc) · 68.7 KB
/
gro87.cpp
File metadata and controls
2208 lines (1856 loc) · 68.7 KB
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
/********************************************\
*
* Sire - Molecular Simulation Framework
*
* Copyright (C) 2017 Christopher Woods
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For full details of the license please see the COPYING file
* that should have come with this distribution.
*
* You can contact the authors at https://sire.openbiosim.org
*
\*********************************************/
#include "SireIO/gro87.h"
#include "SireIO/amberformat.h"
#include "SireSystem/system.h"
#include "SireMol/atomcoords.h"
#include "SireMol/atomvelocities.h"
#include "SireMol/core.h"
#include "SireMol/mgname.h"
#include "SireMol/molecule.h"
#include "SireMol/moleculeinfo.h"
#include "SireMol/moleditor.h"
#include "SireMol/molidx.h"
#include "SireMol/trajectory.h"
#include "SireUnits/units.h"
#include "SireVol/periodicbox.h"
#include "SireVol/triclinicbox.h"
#include "SireBase/generalunitproperty.h"
#include "SireBase/numberproperty.h"
#include "SireBase/parallel.h"
#include "SireBase/stringproperty.h"
#include "SireBase/timeproperty.h"
#include "SireError/errors.h"
#include "SireIO/errors.h"
#include "SireMol/errors.h"
#include "SireStream/datastream.h"
#include "SireStream/shareddatastream.h"
#include <QDebug>
#include <QElapsedTimer>
#include <QRegularExpression>
using namespace SireIO;
using namespace SireMol;
using namespace SireBase;
using namespace SireSystem;
using namespace SireVol;
using namespace SireUnits;
using namespace SireUnits::Dimension;
using namespace SireStream;
const RegisterParser<Gro87> register_gro87;
static const RegisterMetaType<Gro87> r_gro87;
QDataStream &operator<<(QDataStream &ds, const Gro87 &gro87)
{
writeHeader(ds, r_gro87, 1);
SharedDataStream sds(ds);
sds << gro87.ttle << gro87.current_time << gro87.coords << gro87.vels << gro87.box_v1 << gro87.box_v2
<< gro87.box_v3 << gro87.resnums << gro87.resnams << gro87.atmnums << gro87.atmnams << gro87.parse_warnings
<< static_cast<const MoleculeParser &>(gro87);
return ds;
}
QDataStream &operator>>(QDataStream &ds, Gro87 &gro87)
{
VersionID v = readHeader(ds, r_gro87);
if (v == 1)
{
SharedDataStream sds(ds);
sds >> gro87.ttle >> gro87.current_time >> gro87.coords >> gro87.vels >> gro87.box_v1 >> gro87.box_v2 >>
gro87.box_v3 >> gro87.resnums >> gro87.resnams >> gro87.atmnums >> gro87.atmnams >> gro87.parse_warnings >>
static_cast<MoleculeParser &>(gro87);
}
else
throw version_error(v, "1", r_gro87, CODELOC);
return ds;
}
/** Constructor */
Gro87::Gro87() : ConcreteProperty<Gro87, MoleculeParser>()
{
}
/** Construct to read in the data from the file called 'filename'. The
passed property map can be used to pass extra parameters to control
the parsing */
Gro87::Gro87(const QString &filename, const PropertyMap &map) : ConcreteProperty<Gro87, MoleculeParser>(filename, map)
{
// parse the data in the parse function
this->parseLines(map);
// now make sure that everything is correct with this object
this->assertSane();
}
/** Construct to read in the data from the passed text lines. The
passed property map can be used to pass extra parameters to control
the parsing */
Gro87::Gro87(const QStringList &lines, const PropertyMap &map) : ConcreteProperty<Gro87, MoleculeParser>(lines, map)
{
// parse the data in the parse function
this->parseLines(map);
// now make sure that everything is correct with this object
this->assertSane();
}
static QVector<QString> toLines(const QVector<QString> &atmnams, const QVector<QString> &resnams,
const QVector<qint64> &resnums, const QVector<Vector> &coords,
const QVector<Vector> &vels, int precision, bool uses_parallel, QStringList *errors)
{
// do any of the molecules have velocities?
const bool has_velocities = not vels.isEmpty();
// calculate the total number of atoms
const int nats = coords.count();
if (nats == 0)
{
// there are no atoms!
return QVector<QString>();
}
// reserve space for all of the lines
QVector<QString> lines(nats);
auto lines_data = lines.data();
auto write_line = [&](int iatm)
{
// the atom number is iatm+1
int atmnum = iatm + 1;
// however, it cannot be larger than 99999, so it should be capped at this value
if (atmnum > 99999)
atmnum = 99999;
int resnum = resnums.constData()[iatm];
// similarly, the residue number cannot be greater than 99999
if (resnum > 99999)
resnum = 99999;
const auto resnam = resnams.constData()[iatm];
const auto atmnam = atmnams.constData()[iatm];
Vector coord = 0.1 * coords.constData()[iatm]; // convert to nanometers
if (has_velocities)
{
Vector vel = vels.constData()[iatm];
lines_data[iatm] = QString("%1%2%3%4%5%6%7%8%9%10")
.arg(resnum, 5)
.arg(resnam.left(5), -5)
.arg(atmnam.left(5), 5)
.arg(atmnum, 5)
.arg(coord.x(), precision + 5, 'f', precision)
.arg(coord.y(), precision + 5, 'f', precision)
.arg(coord.z(), precision + 5, 'f', precision)
.arg(vel.x(), precision + 5, 'f', precision + 1)
.arg(vel.y(), precision + 5, 'f', precision + 1)
.arg(vel.z(), precision + 5, 'f', precision + 1);
}
else
{
lines_data[iatm] = QString("%1%2%3%4%5%6%7")
.arg(resnum, 5)
.arg(resnam.left(5), -5)
.arg(atmnam.left(5), 5)
.arg(atmnum, 5)
.arg(coord.x(), precision + 5, 'f', precision)
.arg(coord.y(), precision + 5, 'f', precision)
.arg(coord.z(), precision + 5, 'f', precision);
}
};
if (uses_parallel)
{
tbb::parallel_for(tbb::blocked_range<int>(0, nats), [&](const tbb::blocked_range<int> &r)
{
for (int i = r.begin(); i < r.end(); ++i)
{
write_line(i);
} });
}
else
{
for (int i = 0; i < nats; ++i)
{
write_line(i);
}
}
return lines;
}
static std::tuple<QVector<QString>, QVector<qint64>, QVector<QString>> getIDs(const Selector<Atom> &atoms)
{
const int nats = atoms.count();
// get the atoms out in AtomIdx order
if (nats == 0)
{
return std::tuple<QVector<QString>, QVector<qint64>, QVector<QString>>();
}
QVector<QString> resnams(nats);
QVector<qint64> resnums(nats);
QVector<QString> atmnams(nats);
QString *resnams_data = resnams.data();
QString *atmnams_data = atmnams.data();
qint64 *resnums_data = resnums.data();
for (int i = 0; i < nats; ++i)
{
const auto &atom = atoms(i);
const auto residue = atom.residue();
atmnams_data[i] = atom.name().value();
resnams_data[i] = residue.name().value();
resnums_data[i] = residue.number().value();
}
return std::make_tuple(resnams, resnums, atmnams);
}
static QVector<Vector> getCoordinates(const Selector<Atom> &atoms, const PropertyName &coords_property)
{
if (not atoms.data().hasProperty(coords_property))
{
return QVector<Vector>();
}
const int nats = atoms.count();
QVector<Vector> coords(nats);
Vector *coords_data = coords.data();
for (int i = 0; i < nats; ++i)
{
coords_data[i] = atoms(i).property<Vector>(coords_property);
}
return coords;
}
static QVector<Vector> getVelocities(const Selector<Atom> &atoms, const PropertyName &vels_property)
{
if (not atoms.data().hasProperty(vels_property))
{
return QVector<Vector>();
}
try
{
const int nats = atoms.count();
QVector<Vector> vels(nats);
Vector *vels_data = vels.data();
const auto units = nanometer / picosecond;
for (int i = 0; i < nats; ++i)
{
const auto atomvels = atoms(i).property<Velocity3D>(vels_property);
// need to convert the velocities into units of nanometers / picoseconds
vels_data[i] = Vector(atomvels.x().to(units), atomvels.y().to(units), atomvels.z().to(units));
}
return vels;
}
catch (...)
{
return QVector<Vector>();
}
}
/** Construct this parser by extracting all necessary information from the
passed SireSystem::System, looking for the properties that are specified
in the passed property map */
Gro87::Gro87(const SireSystem::System &system, const PropertyMap &map) : ConcreteProperty<Gro87, MoleculeParser>(system, map)
{
// get the MolNums of each molecule in the System - this returns the
// numbers in MolIdx order
const QVector<MolNum> molnums = system.getMoleculeNumbers().toVector();
if (molnums.isEmpty())
{
// no molecules in the system
this->operator=(Gro87());
return;
}
// get the names, numbers coordinates (and velocities if available)
// for each molecule in the system
QVector<QVector<Vector>> all_coords(molnums.count());
QVector<QVector<Vector>> all_vels(molnums.count());
QVector<QVector<QString>> all_resnams(molnums.count());
QVector<QVector<qint64>> all_resnums(molnums.count());
QVector<QVector<QString>> all_atmnams(molnums.count());
const auto vels_property = map["velocity"];
if (usesParallel())
{
tbb::parallel_for(tbb::blocked_range<int>(0, molnums.count()), [&](const tbb::blocked_range<int> r)
{
for (int i = r.begin(); i < r.end(); ++i)
{
const auto atoms = system[molnums[i]].atoms();
auto coords_property = map["coordinates"];
bool is_perturbable = false;
try
{
is_perturbable = atoms.data().property(map["is_perturbable"]).asABoolean();
}
catch (...)
{
}
if (is_perturbable)
{
// Allow the user to override the default.
if ((map["coordinates"] == map["coordinates0"]) or (map["coordinates"] == map["coordinates1"]))
{
coords_property = map["coordinates"];
}
else
{
// Default to lambda = 0.
if (atoms.data().hasProperty(map["coordinates0"]))
coords_property = map["coordinates0"];
else if (atoms.data().hasProperty(map["coordinates1"]))
coords_property = map["coordinates1"];
else
// use the default coordinates property
coords_property = map["coordinates"];
}
}
tbb::parallel_invoke(
[&]() {
const auto ids = ::getIDs(atoms);
all_resnams[i] = std::get<0>(ids);
all_resnums[i] = std::get<1>(ids);
all_atmnams[i] = std::get<2>(ids);
},
[&]() { all_coords[i] = ::getCoordinates(atoms, coords_property); },
[&]() { all_vels[i] = ::getVelocities(atoms, vels_property); });
} });
}
else
{
for (int i = 0; i < molnums.count(); ++i)
{
const auto atoms = system[molnums[i]].atoms();
auto coords_property = map["coordinates"];
bool is_perturbable = false;
try
{
is_perturbable = atoms.data().property(map["is_perturbable"]).asABoolean();
}
catch (...)
{
}
if (is_perturbable)
{
// Allow the user to override the default.
if ((map["coordinates"] == map["coordinates0"]) or (map["coordinates"] == map["coordinates1"]))
{
coords_property = map["coordinates"];
}
else
{
// Default to lambda = 0.
if (atoms.data().hasProperty(map["coordinates0"]))
coords_property = map["coordinates0"];
else if (atoms.data().hasProperty(map["coordinates1"]))
coords_property = map["coordinates1"];
else
// use the default coordinates property
coords_property = map["coordinates"];
}
}
const auto ids = ::getIDs(atoms);
all_resnams[i] = std::get<0>(ids);
all_resnums[i] = std::get<1>(ids);
all_atmnams[i] = std::get<2>(ids);
all_coords[i] = ::getCoordinates(atoms, coords_property);
all_vels[i] = ::getVelocities(atoms, vels_property);
}
}
QStringList errors;
// extract the space of the system
SpacePtr space;
try
{
space = system.property(map["space"]).asA<Space>();
}
catch (...)
{
}
// extract the current time for the system
Time time(-1);
try
{
const Property &prop = system.property(map["time"]);
Time time;
if (prop.isA<TimeProperty>())
time = prop.asA<TimeProperty>().value();
else
time = prop.asA<GeneralUnitProperty>();
}
catch (...)
{
}
// what precision should be used - this can be set by the user
int precision = 6;
try
{
precision = map["precision"].value().asA<NumberProperty>().value();
if (precision < 1)
{
precision = 1;
}
else if (precision > 16)
{
precision = 16;
}
}
catch (...)
{
}
// check the edge case that some molecules had velocities defined, but some didn't
bool none_have_velocities = true;
bool all_have_velocities = true;
bool some_have_velocities = false;
for (const auto &vels : all_vels)
{
if (vels.isEmpty())
{
all_have_velocities = false;
}
else
{
none_have_velocities = false;
}
if (all_have_velocities == false and none_have_velocities == false)
{
// ok, only some have velocities...
some_have_velocities = true;
break;
}
}
if (some_have_velocities)
{
// we need to populate the empty velocities with values of 0
for (int i = 0; i < molnums.count(); ++i)
{
auto &vels = all_vels[i];
if (vels.isEmpty())
{
const int natoms = system[molnums[i]].atoms().count();
vels = QVector<Vector>(natoms, Vector(0));
}
}
}
// now convert these into text lines that can be written as the file
QVector<QString> lines = ::toLines(SireIO::detail::collapse(all_atmnams), SireIO::detail::collapse(all_resnams),
SireIO::detail::collapse(all_resnums), SireIO::detail::collapse(all_coords),
SireIO::detail::collapse(all_vels), precision, this->usesParallel(), &errors);
if (not errors.isEmpty())
{
throw SireIO::parse_error(
QObject::tr("Errors converting the system to a Gromacs Gro87 format...\n%1").arg(errors.join("\n")),
CODELOC);
}
// we don't need the coords and vels data any more, so free the memory
all_coords.clear();
all_vels.clear();
all_resnams.clear();
all_resnums.clear();
all_atmnams.clear();
// add the title, number of atoms and time to the top of the lines
//(the number of atoms is the current number of lines)
const int natoms = lines.count();
lines.prepend(QString("%1").arg(natoms, 5));
if (time.value() >= 0)
{
lines.prepend(QString("%1, t= %2").arg(system.name().value()).arg(time.to(picosecond), 8, 'f', 7));
}
else
{
lines.prepend(system.name().value());
}
// finally add on the box dimensions
if (space.read().isA<PeriodicBox>())
{
Vector dims = space.read().asA<PeriodicBox>().dimensions();
lines += QString(" %1 %2 %3")
.arg(0.1 * dims.x(), 9, 'f', 5)
.arg(0.1 * dims.y(), 9, 'f', 5)
.arg(0.1 * dims.z(), 9, 'f', 5);
}
else if (space.read().isA<TriclinicBox>())
{
Matrix cell = space.read().asA<TriclinicBox>().cellMatrix();
lines += QString(" %1 %2 %3 %4 %5 %6 %7 %8 %9")
.arg(0.1 * cell.column0().x(), 9, 'f', 5) // XX
.arg(0.1 * cell.column1().y(), 9, 'f', 5) // YY
.arg(0.1 * cell.column2().z(), 9, 'f', 5) // ZZ
.arg(0.1 * cell.column0().y(), 9, 'f', 5) // XY
.arg(0.1 * cell.column0().z(), 9, 'f', 5) // XZ
.arg(0.1 * cell.column1().x(), 9, 'f', 5) // YX
.arg(0.1 * cell.column1().z(), 9, 'f', 5) // YZ
.arg(0.1 * cell.column2().x(), 9, 'f', 5) // ZX
.arg(0.1 * cell.column2().y(), 9, 'f', 5); // ZY
}
else
{
// we have to provide some space for Gro87. Supply a null vector
lines += QString(" 0.00000 0.00000 0.00000");
}
if (not errors.isEmpty())
{
throw SireIO::parse_error(
QObject::tr("Errors converting the system to a Gromacs Gro87 format...\n%1").arg(errors.join("\n")),
CODELOC);
}
// now generate this object by re-reading these lines
Gro87 parsed(lines.toList(), map);
this->operator=(parsed);
this->setParsedSystem(system, map);
}
/** Copy constructor */
Gro87::Gro87(const Gro87 &other)
: ConcreteProperty<Gro87, MoleculeParser>(other), ttle(other.ttle), current_time(other.current_time),
coords(other.coords), vels(other.vels), box_v1(other.box_v1), box_v2(other.box_v2), box_v3(other.box_v3),
resnums(other.resnums), resnams(other.resnams), atmnams(other.atmnams), atmnums(other.atmnums),
parse_warnings(other.parse_warnings)
{
}
/** Destructor */
Gro87::~Gro87()
{
}
/** Copy assignment operator */
Gro87 &Gro87::operator=(const Gro87 &other)
{
if (this != &other)
{
ttle = other.ttle;
current_time = other.current_time;
coords = other.coords;
vels = other.vels;
box_v1 = other.box_v1;
box_v2 = other.box_v2;
box_v3 = other.box_v3;
resnums = other.resnums;
resnams = other.resnams;
atmnams = other.atmnams;
atmnums = other.atmnums;
parse_warnings = other.parse_warnings;
MoleculeParser::operator=(other);
}
return *this;
}
/** Comparison operator */
bool Gro87::operator==(const Gro87 &other) const
{
return ttle == other.ttle and current_time == other.current_time and coords == other.coords and
vels == other.vels and box_v1 == other.box_v1 and box_v2 == other.box_v2 and box_v3 == other.box_v3 and
resnums == other.resnums and resnams == other.resnams and atmnams == other.atmnams and
atmnums == other.atmnums and parse_warnings == other.parse_warnings and MoleculeParser::operator==(other);
}
/** Comparison operator */
bool Gro87::operator!=(const Gro87 &other) const
{
return not operator==(other);
}
/** Return the C++ name for this class */
const char *Gro87::typeName()
{
return QMetaType::typeName(qMetaTypeId<Gro87>());
}
/** Return the C++ name for this class */
const char *Gro87::what() const
{
return Gro87::typeName();
}
/** Return the number of frames in the file */
int Gro87::count() const
{
return this->nFrames();
}
/** Return the number of frames in the file */
int Gro87::size() const
{
return this->nFrames();
}
bool Gro87::isTopology() const
{
return true;
}
bool Gro87::isFrame() const
{
return true;
}
void Gro87::reorderLoadedFrame()
{
throw SireError::incompatible_error(QObject::tr("Cannot reorder a Gro87 file!"));
}
Frame Gro87::getFrame(int i) const
{
i = SireID::Index(i).map(this->nFrames());
QVector<Vector> frame_coords;
if (not this->coords.isEmpty())
{
frame_coords = this->coords[i];
}
QVector<Velocity3D> frame_vels;
if (not this->vels.isEmpty())
{
const auto velocities = this->vels[i];
const int nats = frame_vels.count();
frame_vels = QVector<Velocity3D>(nats);
auto velocities_data = frame_vels.data();
const auto vels_data = velocities.constData();
// velocity is Angstroms per 1/20.455 ps
const auto vel_unit = (1.0 / 20.455) * angstrom / picosecond;
for (int i = 0; i < nats; ++i)
{
const Vector &vel = vels_data[i];
velocities_data[i] = Velocity3D(vel.x() * vel_unit, vel.y() * vel_unit, vel.z() * vel_unit);
}
}
// Convert the box vectors into a Space object.
SpacePtr space;
if (not this->box_v1.isEmpty() and not this->box_v2.isEmpty() and not this->box_v3.isEmpty())
{
Vector v1 = this->box_v1[i];
Vector v2 = this->box_v2[i];
Vector v3 = this->box_v3[i];
if (v1.y() == 0 and v1.z() == 0 and v2.x() == 0 and v2.z() == 0 and v3.x() == 0 and v3.y() == 0)
{
// PeridicBox
space = SpacePtr(new PeriodicBox(Vector(v1.x(), v2.y(), v3.z())));
}
else
{
// TriclinicBox
space = SpacePtr(new TriclinicBox(v1, v2, v3));
}
}
else
{
// no box information
space = SpacePtr(new Cartesian());
}
Time frame_time;
if (not this->current_time.isEmpty())
{
frame_time = this->current_time[i] * SireUnits::picosecond;
}
return SireMol::Frame(frame_coords, frame_vels, space.read(), frame_time);
}
/** Return the Gro87 object that contains only the information for the ith
frame. This allows you to extract and create a system for the ith frame
from a trajectory */
Gro87 Gro87::operator[](int i) const
{
throw SireError::unsupported(
QObject::tr("Gro87::operator[] is not yet implemented!"), CODELOC);
}
/** Return the parser that has been constructed by reading in the passed
file using the passed properties */
MoleculeParserPtr Gro87::construct(const QString &filename, const PropertyMap &map) const
{
return Gro87(filename, map);
}
/** Return the parser that has been constructed by reading in the passed
text lines using the passed properties */
MoleculeParserPtr Gro87::construct(const QStringList &lines, const PropertyMap &map) const
{
return Gro87(lines, map);
}
/** Return the parser that has been constructed by extract all necessary
data from the passed SireSystem::System using the specified properties */
MoleculeParserPtr Gro87::construct(const SireSystem::System &system, const PropertyMap &map) const
{
return Gro87(system, map);
}
/** Return a string representation of this parser */
QString Gro87::toString() const
{
if (nAtoms() == 0)
{
return QObject::tr("Gro87( nAtoms() = 0 )");
}
else
{
return QObject::tr("Gro87( title() = %1, nAtoms() = %2, nResidues() = %6, nFrames() = %5, "
"hasCoordinates() = %3, hasVelocities() = %4 )")
.arg(title())
.arg(nAtoms())
.arg(hasCoordinates())
.arg(hasVelocities())
.arg(nFrames())
.arg(nResidues());
}
}
/** Return the format name that is used to identify this file format within Sire */
QString Gro87::formatName() const
{
return "GRO87";
}
/** Return a description of the file format */
QString Gro87::formatDescription() const
{
return QObject::tr("Gromacs Gro87 structure format files.");
}
/** Return the suffixes that these files are normally associated with */
QStringList Gro87::formatSuffix() const
{
static const QStringList suffixes = {"gro", "g87"};
return suffixes;
}
/** Function that is called to assert that this object is sane. This
should raise an exception if the parser is in an invalid state */
void Gro87::assertSane() const
{
// the number of coordinate and velocity frames should be identical
QStringList errors;
if (coords.count() != vels.count())
{
if (not(coords.isEmpty() or vels.isEmpty()))
{
errors.append(QObject::tr("Error in Gro87 file as the number of "
"coordinates frames (%1) read does not equal the number of "
"velocity frames (2) read!")
.arg(coords.count())
.arg(vels.count()));
}
}
// make sure that the number of atoms is consistent
int nats = this->nAtoms();
for (int i = 0; i < coords.count(); ++i)
{
if (coords.at(i).count() != nats)
{
errors.append(QObject::tr("Error: The number of atoms for coordinate "
"frame %1 (%2) is not consistent with the number of atoms (%3).")
.arg(i)
.arg(coords.at(i).count())
.arg(nats));
}
}
for (int i = 0; i < vels.count(); ++i)
{
if (vels.at(i).count() != nats)
{
errors.append(QObject::tr("Error: The number of atoms for velocity "
"frame %1 (%2) is not consistent with the number of atoms (%3).")
.arg(i)
.arg(vels.at(i).count())
.arg(nats));
}
}
if (atmnams.count() != nats)
{
errors.append(QObject::tr("Error: The number of atom names (%1) does not "
"equal the number of atoms (%2)!")
.arg(atmnams.count())
.arg(nats));
}
if (atmnums.count() != nats)
{
errors.append(QObject::tr("Error: The number of atom numbers (%1) does not "
"equal the number of atoms (%2)!")
.arg(atmnums.count())
.arg(nats));
}
if (resnams.count() != nats)
{
errors.append(QObject::tr("Error: The number of residue names (%1) does not "
"equal the number of atoms (%2)!")
.arg(resnams.count())
.arg(nats));
}
if (resnums.count() != nats)
{
errors.append(QObject::tr("Error: The number of residue numbers (%1) does not "
"equal the number of atoms (%2)!")
.arg(resnums.count())
.arg(nats));
}
if (box_v1.count() != box_v2.count() or box_v1.count() != box_v3.count())
{
errors.append(QObject::tr("Error: The number of frames of box dimension "
"information is not consistent: %1 vs %2 vs %3.")
.arg(box_v1.count())
.arg(box_v2.count())
.arg(box_v3.count()));
}
if (not box_v1.isEmpty())
{
if (box_v1.count() != this->nFrames())
{
errors.append(QObject::tr("Error: The number of frames of box dimension "
"information (%1) is not equal to the number of frames of trajectory (%2).")
.arg(box_v1.count())
.arg(this->nFrames()));
}
}
if (not current_time.isEmpty())
{
if (current_time.count() != this->nFrames())
{
errors.append(QObject::tr("Error: The number of times from the trajectory "
"(%1) does not equal the number of frames of trajectory (%2).")
.arg(current_time.count())
.arg(this->nFrames()));
}
}
// make sure that every atom with the same residue number has the same residue name
if (not resnams.isEmpty())
{
QHash<qint64, QString> resnum_to_nam;
resnum_to_nam.reserve(resnams.count());
for (int i = 0; i < resnams.count(); ++i)
{
const auto resnum = resnums[i];
const auto resnam = resnams[i];
if (resnum_to_nam.contains(resnum))
{
if (resnum_to_nam[resnum] != resnam)
{
errors.append(QObject::tr("Error: Disagreement of the residue name "
"for residue number %1 for atom at index %2. It should be %3, "
"but for this atom it is %4.")
.arg(resnum)
.arg(i)
.arg(resnum_to_nam[resnum])
.arg(resnam));
}
else
{
resnum_to_nam.insert(resnum, resnam);
}
}
}
}
if (not errors.isEmpty())
{
throw SireIO::parse_error(QObject::tr("There were errors reading the Gro87 format "
"file:\n%1")
.arg(errors.join("\n\n")),
CODELOC);
}
}
/** Return the title of the file */
QString Gro87::title() const
{
return ttle;
}
/** Return the current time of the simulation from which this coordinate
file was written. Returns 0 if there is no time set. If there are
multiple frames, then the time of the first frame is returned */
double Gro87::time() const
{
if (current_time.isEmpty())
{
return 0.0;
}
else
{
return current_time[0];
}
}
/** Return the time for the structure at the specified frame */
double Gro87::time(int frame) const
{
return current_time[Index(frame).map(current_time.count())];