/
mmtf_parser.c
1056 lines (876 loc) · 32.4 KB
/
mmtf_parser.c
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
// *************************************************************************
// Copyright [2016] [RCSB]
//
// 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.
//
//
// This file is mmtf_parser.c, holding the source code of the MMTF parser
//
// The authors of this code are: Julien Ferte (http://www.julienferte.com/),
// Anthony Bradley, Thomas Holder.
//
//
// Other contributors: Yana Valasatava, Gazal Kalyan, Alexander Rose.
//
// *************************************************************************
#define WIN32_LEAN_AND_MEAN
#define __STDC_LIMIT_MACROS
#include "mmtf_parser.h"
//*** Standard libs
#include <stdbool.h>
//*** MsgPack lib
#ifdef MMTF_MSGPACK_USE_CPP11
#include <msgpack.hpp>
#define msgpack_object msgpack::object
#define msgpack_object_kv msgpack::object_kv
#define msgpack_object_str msgpack::object_str
#define MMTF_MSGPACK_TYPE(T) msgpack::type::T
#else
#include <msgpack.h>
#define MMTF_MSGPACK_TYPE(T) MSGPACK_OBJECT_##T
#endif
#if MSGPACK_VERSION_MAJOR < 1
#error "msgpack-c >= 1.0 required (https://github.com/msgpack/msgpack-c)"
#else
//*** For the constant NAN
#include <math.h>
//*** Standard libs
#include <stdio.h>
// byteorder functions ("ntohl" etc.)
#ifdef WIN32
#include <Winsock2.h>
#else
#include <arpa/inet.h>
#endif
// clang-format off
// typed array memory allocation
#define MALLOC_ARRAY(type, size) (type*) malloc((size) * sizeof(type))
/*
* Type aliases for code generation
*/
#define TYPEALIAS_char char
#define TYPEALIAS_int8 int8_t
#define TYPEALIAS_int32 int32_t
#define TYPEALIAS_float float
#define TYPEALIAS_string char*
#define TYPEALIAS_int int
// clang-format on
enum {
MMTF_TYPE_char,
MMTF_TYPE_int8 = MMTF_TYPE_char,
MMTF_TYPE_int16,
MMTF_TYPE_int32,
MMTF_TYPE_float,
MMTF_TYPE_string
};
/*
* Macros for null-pointer checking
*/
#define IF_NULL_PTRERROR_RETURN(ptr, returnvalue) \
if (!ptr) { \
fprintf(stderr, "Error in %s: NULL pointer.\n", __FUNCTION__); \
return returnvalue; \
}
#define IF_NULL_ALLOCERROR_RETURN(ptr, returnvalue) \
if (!ptr) { \
fprintf(stderr, "Error in %s: couldn't allocate memory.\n", __FUNCTION__); \
return returnvalue; \
}
#define IF_NOT_MULTIPLE_ERROR_RETURN(length, size, returnvalue) \
if ((length) % (size) != 0) { \
fprintf(stderr, "Error in %s: length %u is not a multiple of %u.\n", __FUNCTION__, length, size); \
return returnvalue; \
}
#define IF_NULL_ALLOCERROR_RETURN_NULL(ptr) \
IF_NULL_ALLOCERROR_RETURN(ptr, NULL)
/*
* Macros for iterating over a msgpack map
*/
#define MAP_ITERATE_BEGIN_RV(object, returnvalue) \
if (object->type != MMTF_MSGPACK_TYPE(MAP)) { \
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not a map.\n", __FUNCTION__); \
return returnvalue; \
} \
{ \
msgpack_object_kv* current_key_value = object->via.map.ptr; \
msgpack_object_kv* last_key_value = current_key_value + object->via.map.size; \
for (; current_key_value != last_key_value; ++current_key_value) { \
const msgpack_object* key = &(current_key_value->key); \
const msgpack_object* value = &(current_key_value->val); \
if (key->type != MMTF_MSGPACK_TYPE(STR)) { \
fprintf(stderr, "Warning: map key not of type str (type %d).\n", key->type); \
continue; \
}
#define MAP_ITERATE_BEGIN(object) \
MAP_ITERATE_BEGIN_RV(object, )
#define MAP_ITERATE_END() \
} \
}
/*
* Macros for inside the map iteration
*/
#define FETCH_AND_ASSIGN(this_, type, name) \
if (MMTF_parser_compare_msgpack_string_char_array(&(key->via.str), #name)) { \
this_->name = MMTF_parser_fetch_##type(value); \
continue; \
}
#define FETCH_AND_ASSIGN_DUMMYCOUNT(this_, type, name) \
if (MMTF_parser_compare_msgpack_string_char_array(&(key->via.str), #name)) { \
size_t _length; \
this_->name = MMTF_parser_fetch_##type(value, &_length); \
continue; \
}
#define FETCH_AND_ASSIGN_WITHCOUNT(this_, type, name) \
if (MMTF_parser_compare_msgpack_string_char_array(&(key->via.str), #name)) { \
this_->name = MMTF_parser_fetch_##type(value, &(this_->name##Count)); \
continue; \
}
#define FETCH_AND_ASSIGN_ARRAY(this_, type, name) \
if (MMTF_parser_compare_msgpack_string_char_array(&(key->via.str), #name)) { \
size_t _length; \
type* array = MMTF_parser_fetch_##type##_array(value, &_length); \
if (array != NULL) { \
size_t i; \
for (i = 0; i < _length; ++i) { \
this_->name[i] = array[i]; \
} \
free(array); \
} \
}
/*
* Macros for generating generic initialization and destroying functions
*/
#define CODEGEN_MMTF_parser_TYPE_init(type) \
void type##_init(type* result) { \
memset(result, 0, sizeof(type)); \
}
#define CODEGEN_MMTF_parser_TYPE_new(type) \
type* type##_new(void) { \
type* result = (type*)malloc(sizeof(type)); \
IF_NULL_ALLOCERROR_RETURN_NULL(result); \
type##_init(result); \
return result; \
}
#define CODEGEN_MMTF_parser_TYPE_clear(type) \
void type##_clear(type* result) { \
IF_NULL_PTRERROR_RETURN(result, ); \
type##_destroy(result); \
type##_init(result); \
}
#define CODEGEN_MMTF_parser_TYPE_free(type) \
void type##_free(type* thing) { \
IF_NULL_PTRERROR_RETURN(thing, ); \
type##_destroy(thing); \
free(thing); \
}
// clang-format off
#define CODEGEN_MMTF_parser_TYPE(type) \
CODEGEN_MMTF_parser_TYPE_init(type) \
CODEGEN_MMTF_parser_TYPE_new(type) \
CODEGEN_MMTF_parser_TYPE_clear(type) \
CODEGEN_MMTF_parser_TYPE_free(type)
// clang-format on
#define generic_destroy(ptr) \
free(*(ptr))
#define FREE_LIST(type_, name) \
if (name != NULL) { \
size_t i; \
for (i = 0; i < name##Count; ++i) { \
type_##_destroy(name + i); \
} \
free(name); \
}
/*
* Macros for fetching and converting msgpack array objects
*/
#define CODEGEN_BODY_fetch_OBJECT_ARRAY(type_, RESULT_I_ASSIGN) \
{ \
if (object->type != MMTF_MSGPACK_TYPE(ARRAY)) { \
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not an array.\n", __FUNCTION__); \
return NULL; \
} \
const msgpack_object* iter = object->via.array.ptr; \
(*length) = object->via.array.size; \
const msgpack_object* iter_end = iter + (*length); \
type_* result = MALLOC_ARRAY(type_, *length); \
IF_NULL_ALLOCERROR_RETURN_NULL(result); \
int i = 0; \
for (; iter != iter_end; ++iter, ++i) { \
RESULT_I_ASSIGN; \
} \
return result; \
}
// clang-format off
#define CODEGEN_MMTF_parser_fetch_array(type_, RESULT_I_ASSIGN) \
static TYPEALIAS_##type_* MMTF_parser_fetch_##type_##_array( \
const msgpack_object* object, size_t* length) { \
if (object->type == MMTF_MSGPACK_TYPE(BIN)) { \
return (TYPEALIAS_##type_*) \
MMTF_parser_fetch_typed_array(object, length, MMTF_TYPE_##type_); \
} \
CODEGEN_BODY_fetch_OBJECT_ARRAY(TYPEALIAS_##type_, RESULT_I_ASSIGN); \
}
#define CODEGEN_MMTF_parser_fetch_List(type_, suffix) \
static type_* MMTF_parser_fetch_##suffix##List( \
const msgpack_object* object, size_t* length) { \
CODEGEN_BODY_fetch_OBJECT_ARRAY(type_, { \
MMTF_parser_put_##suffix(iter, result + i); \
}) \
}
// clang-format on
/*
* Generate "initialize", "new", "empty" and "destroy" functions for MMTF struct types.
*/
// clang-format off
CODEGEN_MMTF_parser_TYPE(MMTF_container)
CODEGEN_MMTF_parser_TYPE(MMTF_BioAssembly)
CODEGEN_MMTF_parser_TYPE(MMTF_Transform)
CODEGEN_MMTF_parser_TYPE(MMTF_Entity)
CODEGEN_MMTF_parser_TYPE(MMTF_GroupType)
//*** Destroy the innner of a struct
void MMTF_container_destroy(MMTF_container* thing) {
// clang-format on
IF_NULL_PTRERROR_RETURN(thing, );
FREE_LIST(MMTF_BioAssembly, thing->bioAssemblyList);
FREE_LIST(MMTF_Entity, thing->entityList);
FREE_LIST(generic, thing->experimentalMethods);
FREE_LIST(MMTF_GroupType, thing->groupList);
FREE_LIST(generic, thing->chainIdList);
FREE_LIST(generic, thing->chainNameList);
free(thing->mmtfVersion);
free(thing->mmtfProducer);
free(thing->spaceGroup);
free(thing->structureId);
free(thing->title);
free(thing->depositionDate);
free(thing->releaseDate);
free(thing->bondAtomList);
free(thing->bondOrderList);
free(thing->xCoordList);
free(thing->yCoordList);
free(thing->zCoordList);
free(thing->bFactorList);
free(thing->atomIdList);
free(thing->altLocList);
free(thing->occupancyList);
free(thing->groupIdList);
free(thing->groupTypeList);
free(thing->secStructList);
free(thing->insCodeList);
free(thing->sequenceIndexList);
free(thing->groupsPerChain);
free(thing->chainsPerModel);
}
void MMTF_BioAssembly_destroy(MMTF_BioAssembly* bio_assembly) {
IF_NULL_PTRERROR_RETURN(bio_assembly, );
FREE_LIST(MMTF_Transform, bio_assembly->transformList);
free(bio_assembly->name);
}
void MMTF_Transform_destroy(MMTF_Transform* transform) {
IF_NULL_PTRERROR_RETURN(transform, );
free(transform->chainIndexList);
}
void MMTF_Entity_destroy(MMTF_Entity* entity) {
IF_NULL_PTRERROR_RETURN(entity, );
free(entity->chainIndexList);
free(entity->description);
free(entity->type);
free(entity->sequence);
}
void MMTF_GroupType_destroy(MMTF_GroupType* group_type) {
IF_NULL_PTRERROR_RETURN(group_type, );
FREE_LIST(generic, group_type->atomNameList);
FREE_LIST(generic, group_type->elementList);
free(group_type->formalChargeList);
free(group_type->bondAtomList);
free(group_type->bondOrderList);
free(group_type->groupName);
free(group_type->chemCompType);
}
//*** Array converters
// From bytes[] to float32[], int8[], int16[], int32[] and string
static inline
void assign_bigendian_4(void* dst, const char* src) {
*((uint32_t*)dst) = ntohl(*((uint32_t*)src));
}
static inline
void assign_bigendian_2(void* dst, const char* src) {
*((uint16_t*)dst) = ntohs(*((uint16_t*)src));
}
static
void array_copy_bigendian_4(void* dst, const char* src, size_t n) {
size_t i;
for (i = 0; i < n; i += 4) {
assign_bigendian_4(((char*)dst) + i, src + i);
}
}
static
void array_copy_bigendian_2(void* dst, const char* src, size_t n) {
size_t i;
for (i = 0; i < n; i += 2) {
assign_bigendian_2(((char*)dst) + i, src + i);
}
}
static
float* MMTF_parser_float_from_bytes(const char* input, uint32_t input_length, uint32_t* output_length) {
IF_NOT_MULTIPLE_ERROR_RETURN(input_length, 4, NULL);
(*output_length) = input_length / 4;
float* output = MALLOC_ARRAY(float, *output_length);
IF_NULL_ALLOCERROR_RETURN_NULL(output);
array_copy_bigendian_4(output, input, input_length);
return output;
}
static
int8_t* MMTF_parser_int8_from_bytes(const char* input, uint32_t input_length, uint32_t* output_length) {
(*output_length) = input_length;
int8_t* output = MALLOC_ARRAY(int8_t, *output_length);
IF_NULL_ALLOCERROR_RETURN_NULL(output);
memcpy(output, input, input_length);
return output;
}
static
int16_t* MMTF_parser_int16_from_bytes(const char* input, uint32_t input_length, uint32_t* output_length) {
IF_NOT_MULTIPLE_ERROR_RETURN(input_length, 2, NULL);
(*output_length) = input_length / 2;
int16_t* output = MALLOC_ARRAY(int16_t, (*output_length));
IF_NULL_ALLOCERROR_RETURN_NULL(output);
array_copy_bigendian_2(output, input, input_length);
return output;
}
static
int32_t* MMTF_parser_int32_from_bytes(const char* input, const uint32_t input_length, uint32_t* output_length) {
IF_NOT_MULTIPLE_ERROR_RETURN(input_length, 4, NULL);
(*output_length) = input_length / 4;
int32_t* output = MALLOC_ARRAY(int32_t, (*output_length));
IF_NULL_ALLOCERROR_RETURN_NULL(output);
array_copy_bigendian_4(output, input, input_length);
return output;
}
static
char** MMTF_parser_strings_from_bytes(const char* input, uint32_t input_length, uint32_t parameter, uint32_t* output_length) {
IF_NOT_MULTIPLE_ERROR_RETURN(input_length, parameter, NULL);
(*output_length) = input_length / parameter;
char** output = MALLOC_ARRAY(char*, (*output_length));
IF_NULL_ALLOCERROR_RETURN_NULL(output);
uint32_t i;
for (i = 0; i < *output_length; ++i) {
output[i] = MALLOC_ARRAY(char, parameter + 1);
IF_NULL_ALLOCERROR_RETURN_NULL(output[i]);
memcpy(output[i], input + (i * parameter), parameter);
output[i][parameter] = 0;
}
return output;
}
//*** Array decoders
// Run-length decode
static
int32_t* MMTF_parser_run_length_decode(const int32_t* input, uint32_t input_length, uint32_t* output_length) {
(*output_length) = 0;
IF_NOT_MULTIPLE_ERROR_RETURN(input_length, 2, NULL);
uint32_t i;
int32_t value, number;
for (i = 0; i < input_length; i += 2) {
number = input[i + 1];
(*output_length) += number;
}
int32_t* output = MALLOC_ARRAY(int32_t, (*output_length)); // The output needs to be freed by the calling process
IF_NULL_ALLOCERROR_RETURN_NULL(output);
int j = 0;
int k;
for (i = 0; i < input_length; i += 2) {
value = input[i];
number = input[i + 1];
for (k = 0; k < number; ++k) {
output[j] = value;
++j;
}
}
return output;
}
// Delta decode
static
int32_t* MMTF_parser_delta_decode(const int32_t* input, uint32_t input_length, uint32_t* output_length) {
(*output_length) = input_length;
int32_t* output = MALLOC_ARRAY(int32_t, (*output_length)); // The output needs to be freed by the calling process
IF_NULL_ALLOCERROR_RETURN_NULL(output);
if (input_length > 0) {
output[0] = input[0];
}
uint32_t i;
for (i = 1; i < input_length; ++i) {
output[i] = output[i - 1] + input[i];
}
return output;
}
// Recursive indexing decode
static
int32_t* MMTF_parser_recursive_indexing_decode_from_16(const int16_t* input, uint32_t input_length, uint32_t* output_length) {
(*output_length) = 0;
uint32_t i;
for (i = 0; i < input_length; ++i) {
if (input[i] != INT16_MAX && input[i] != INT16_MIN) {
++(*output_length);
}
}
int32_t* output = (int32_t*)MALLOC_ARRAY(int32_t, (*output_length)); // The output needs to be freed by the calling process
IF_NULL_ALLOCERROR_RETURN_NULL(output);
size_t j = 0;
if (input_length > 0) {
output[0] = 0;
}
for (i = 0; i < input_length; ++i) {
output[j] += input[i];
if (input[i] != INT16_MAX && input[i] != INT16_MIN && j + 1 < *output_length) {
++j;
output[j] = 0;
}
}
return output;
}
static
int32_t* MMTF_parser_recursive_indexing_decode_from_8(const int8_t* input, uint32_t input_length, uint32_t* output_length) {
(*output_length) = 0;
uint32_t i;
for (i = 0; i < input_length; ++i) {
if (input[i] != INT8_MAX && input[i] != INT8_MIN) {
++(*output_length);
}
}
int32_t* output = MALLOC_ARRAY(int32_t, (*output_length)); // The output needs to be freed by the calling process
IF_NULL_ALLOCERROR_RETURN_NULL(output);
size_t j = 0;
output[j] = 0;
for (i = 0; i < input_length; ++i) {
output[j] += input[i];
if (input[i] != INT8_MAX && input[i] != INT8_MIN && j + 1 < *output_length) {
++j;
output[j] = 0;
}
}
return output;
}
// Integer decoding
static
float* MMTF_parser_integer_decode_from_16(const int16_t* input, uint32_t input_length, int32_t parameter, uint32_t* output_length) {
(*output_length) = input_length;
float* output = (float*)MALLOC_ARRAY(float, (*output_length));
IF_NULL_ALLOCERROR_RETURN_NULL(output);
float parameter_float = (float)parameter;
uint32_t i;
for (i = 0; i < input_length; ++i) {
output[i] = ((float)input[i]) / parameter_float;
}
return output;
}
static
float* MMTF_parser_integer_decode_from_32(const int32_t* input, uint32_t input_length, int32_t parameter, uint32_t* output_length) {
(*output_length) = input_length;
float* output = (float*)MALLOC_ARRAY(float, (*output_length));
IF_NULL_ALLOCERROR_RETURN_NULL(output);
float parameter_float = (float)parameter;
uint32_t i;
for (i = 0; i < input_length; ++i) {
output[i] = ((float)input[i]) / parameter_float;
}
return output;
}
//*** Applying a decoding strategy for getting an array
static
void* MMTF_parser_decode_apply_strategy(const char* input,
uint32_t input_length, uint32_t* output_length, int strategy,
int32_t parameter, int* typecode) {
switch (strategy) {
case 1:
*typecode = MMTF_TYPE_float;
return MMTF_parser_float_from_bytes(input, input_length, output_length);
case 2:
*typecode = MMTF_TYPE_int8;
return MMTF_parser_int8_from_bytes(input, input_length, output_length);
case 3:
*typecode = MMTF_TYPE_int16;
return MMTF_parser_int16_from_bytes(input, input_length, output_length);
case 4:
*typecode = MMTF_TYPE_int32;
return MMTF_parser_int32_from_bytes(input, input_length, output_length);
case 5:
*typecode = MMTF_TYPE_string;
return MMTF_parser_strings_from_bytes(input, input_length, parameter, output_length);
case 6: {
// pass
}
case 7: {
uint32_t step1_length;
int32_t* step1 = MMTF_parser_int32_from_bytes(input, input_length, &step1_length);
int32_t* output = MMTF_parser_run_length_decode(step1, step1_length, output_length);
free(step1);
if (strategy == 6) {
uint32_t i = 0;
char* char_output = MALLOC_ARRAY(char, (*output_length));
IF_NULL_ALLOCERROR_RETURN_NULL(char_output);
for (; i < *output_length; ++i) {
char_output[i] = output[i];
}
free(output);
*typecode = MMTF_TYPE_int8;
return char_output;
}
*typecode = MMTF_TYPE_int32;
return output;
}
case 8: {
uint32_t step1_length;
int32_t* step1 = MMTF_parser_int32_from_bytes(input, input_length, &step1_length);
uint32_t step2_length;
int32_t* step2 = MMTF_parser_run_length_decode(step1, step1_length, &step2_length);
free(step1);
int32_t* output = MMTF_parser_delta_decode(step2, step2_length, output_length);
free(step2);
*typecode = MMTF_TYPE_int32;
return output;
}
case 9: {
uint32_t step1_length;
int32_t* step1 = MMTF_parser_int32_from_bytes(input, input_length, &step1_length);
uint32_t step2_length;
int32_t* step2 = MMTF_parser_run_length_decode(step1, step1_length, &step2_length);
free(step1);
float* output = MMTF_parser_integer_decode_from_32(step2, step2_length, parameter, output_length);
free(step2);
*typecode = MMTF_TYPE_float;
return output;
}
case 10: {
uint32_t step1_length;
int16_t* step1 = MMTF_parser_int16_from_bytes(input, input_length, &step1_length);
uint32_t step2_length;
int32_t* step2 = MMTF_parser_recursive_indexing_decode_from_16(step1, step1_length, &step2_length);
free(step1);
uint32_t step3_length;
int32_t* step3 = MMTF_parser_delta_decode(step2, step2_length, &step3_length);
free(step2);
float* output = MMTF_parser_integer_decode_from_32(step3, step3_length, parameter, output_length);
free(step3);
*typecode = MMTF_TYPE_float;
return output;
}
case 11: {
uint32_t step1_length;
int16_t* step1 = MMTF_parser_int16_from_bytes(input, input_length, &step1_length);
float* output = MMTF_parser_integer_decode_from_16(step1, step1_length, parameter, output_length);
free(step1);
*typecode = MMTF_TYPE_float;
return output;
}
case 12: {
uint32_t step1_length;
int16_t* step1 = MMTF_parser_int16_from_bytes(input, input_length, &step1_length);
uint32_t step2_length;
int32_t* step2 = MMTF_parser_recursive_indexing_decode_from_16(step1, step1_length, &step2_length);
free(step1);
float* output = MMTF_parser_integer_decode_from_32(step2, step2_length, parameter, output_length);
free(step2);
*typecode = MMTF_TYPE_float;
return output;
}
case 13: {
uint32_t step1_length;
int8_t* step1 = MMTF_parser_int8_from_bytes(input, input_length, &step1_length);
uint32_t step2_length;
int32_t* step2 = MMTF_parser_recursive_indexing_decode_from_8(step1, step1_length, &step2_length);
free(step1);
float* output = MMTF_parser_integer_decode_from_32(step2, step2_length, parameter, output_length);
free(step2);
*typecode = MMTF_TYPE_float;
return output;
}
case 14: {
uint32_t step1_length;
int16_t* step1 = MMTF_parser_int16_from_bytes(input, input_length, &step1_length);
int32_t* output = MMTF_parser_recursive_indexing_decode_from_16(step1, step1_length, output_length);
free(step1);
*typecode = MMTF_TYPE_int32;
return output;
}
case 15: {
uint32_t step1_length;
int8_t* step1 = MMTF_parser_int8_from_bytes(input, input_length, &step1_length);
int32_t* output = MMTF_parser_recursive_indexing_decode_from_8(step1, step1_length, output_length);
free(step1);
*typecode = MMTF_TYPE_int32;
return output;
}
default: {
fprintf(stderr, "Error in %s: %i does not refer to any strategy.\n", __FUNCTION__, strategy);
return NULL;
}
}
}
/*
* Copy string from 'object' to 'out'
*/
static
void MMTF_parser_put_string(const msgpack_object* object, char** out) {
size_t string_size = object->via.str.size;
char* result = (*out) = MALLOC_ARRAY(char, (string_size + 1));
IF_NULL_ALLOCERROR_RETURN(result, );
memcpy(result, object->via.str.ptr, string_size);
result[string_size] = '\0';
}
//*** Unpacking from MsgPack and applying strategy
static
char* MMTF_parser_fetch_string(const msgpack_object* object) {
if (object->type != MMTF_MSGPACK_TYPE(STR)) {
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not a string.\n", __FUNCTION__);
return NULL;
}
char* result = NULL;
MMTF_parser_put_string(object, &result);
return result;
}
static
char MMTF_parser_fetch_char(const msgpack_object* object) {
if (object->type != MMTF_MSGPACK_TYPE(STR)) {
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not a string.\n", __FUNCTION__);
return '\0';
}
return *(object->via.str.ptr);
}
static
int64_t MMTF_parser_fetch_int(const msgpack_object* object) {
int64_t result;
if (object->type == MMTF_MSGPACK_TYPE(POSITIVE_INTEGER)) {
result = object->via.u64;
} else if (object->type == MMTF_MSGPACK_TYPE(NEGATIVE_INTEGER)) {
result = object->via.i64;
} else {
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not an integer.\n", __FUNCTION__);
return 0;
}
return result;
}
static
float MMTF_parser_fetch_float(const msgpack_object* object) {
switch (object->type) {
case /* FLOAT64 */ MMTF_MSGPACK_TYPE(FLOAT):
#if MSGPACK_VERSION_MAJOR >= 2
case /* FLOAT32 */ 0x0a: // msgpack-c >= 2.1
#endif
return (float)object->via.f64;
case MMTF_MSGPACK_TYPE(POSITIVE_INTEGER):
return (float)object->via.u64;
case MMTF_MSGPACK_TYPE(NEGATIVE_INTEGER):
return (float)object->via.i64;
default:
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not a float.\n", __FUNCTION__);
return NAN;
}
}
/*
* Fetch a compressed typed array
*/
static
void* MMTF_parser_fetch_typed_array(const msgpack_object* object, size_t* length, int typecode) {
if (object->type != MMTF_MSGPACK_TYPE(BIN)) {
fprintf(stderr, "Error in %s: the entry encoded in the MMTF is not binary data.\n", __FUNCTION__);
return NULL;
}
const char* bytes = object->via.bin.ptr;
int32_t strategy, len_int32, parameter;
assign_bigendian_4(&strategy, bytes);
assign_bigendian_4(&len_int32, bytes + 4);
assign_bigendian_4(¶meter, bytes + 8);
*length = len_int32;
uint32_t out_length;
int typecheck;
void* result = MMTF_parser_decode_apply_strategy(bytes + 12,
object->via.bin.size - 12, &out_length, strategy, parameter,
&typecheck);
if (typecode != typecheck) {
fprintf(stderr, "Error in %s: typecode mismatch %d %d\n",
__FUNCTION__, typecode, typecheck);
return NULL;
}
if (out_length != *length) {
fprintf(stderr, "Error in %s: length mismatch %u %u\n",
__FUNCTION__, out_length, (unsigned)*length);
return NULL;
}
return result;
}
/*
* Fetch a typed array.
*/
// clang-format off
CODEGEN_MMTF_parser_fetch_array(char, result[i] = iter->via.u64)
CODEGEN_MMTF_parser_fetch_array(int8, result[i] = iter->via.u64)
CODEGEN_MMTF_parser_fetch_array(int32, result[i] = iter->via.u64)
CODEGEN_MMTF_parser_fetch_array(float, result[i] = MMTF_parser_fetch_float(iter))
CODEGEN_MMTF_parser_fetch_array(string, MMTF_parser_put_string(iter, result + i))
static
bool MMTF_parser_compare_msgpack_string_char_array(const msgpack_object_str* m_string, const char* string) {
// clang-format on
return (m_string->size == strlen(string) && strncmp(m_string->ptr, string, m_string->size) == 0);
}
static
void MMTF_parser_put_entity(const msgpack_object* object, MMTF_Entity* entity) {
MMTF_Entity_init(entity);
MAP_ITERATE_BEGIN(object);
FETCH_AND_ASSIGN(entity, string, description);
FETCH_AND_ASSIGN(entity, string, type);
FETCH_AND_ASSIGN_WITHCOUNT(entity, int32_array, chainIndexList);
FETCH_AND_ASSIGN(entity, string, sequence);
MAP_ITERATE_END();
}
static
void MMTF_parser_put_group(const msgpack_object* object, MMTF_GroupType* group_type) {
MMTF_GroupType_init(group_type);
MAP_ITERATE_BEGIN(object);
FETCH_AND_ASSIGN_DUMMYCOUNT(group_type, int32_array, formalChargeList);
FETCH_AND_ASSIGN_WITHCOUNT(group_type, string_array, atomNameList);
FETCH_AND_ASSIGN_WITHCOUNT(group_type, string_array, elementList);
FETCH_AND_ASSIGN_WITHCOUNT(group_type, int32_array, bondAtomList);
FETCH_AND_ASSIGN_WITHCOUNT(group_type, int8_array, bondOrderList);
FETCH_AND_ASSIGN(group_type, string, groupName);
FETCH_AND_ASSIGN(group_type, char, singleLetterCode);
FETCH_AND_ASSIGN(group_type, string, chemCompType);
MAP_ITERATE_END();
}
static
MMTF_Transform* MMTF_parser_fetch_transformList(const msgpack_object*, size_t*);
static
void MMTF_parser_put_bioAssembly(const msgpack_object* object, MMTF_BioAssembly* bio_assembly) {
MMTF_BioAssembly_init(bio_assembly);
MAP_ITERATE_BEGIN(object);
FETCH_AND_ASSIGN(bio_assembly, string, name);
FETCH_AND_ASSIGN_WITHCOUNT(bio_assembly, transformList, transformList);
MAP_ITERATE_END();
}
static
void MMTF_parser_put_transform(const msgpack_object* object, MMTF_Transform* transform) {
MAP_ITERATE_BEGIN(object);
FETCH_AND_ASSIGN_WITHCOUNT(transform, int32_array, chainIndexList);
FETCH_AND_ASSIGN_ARRAY(transform, float, matrix);
MAP_ITERATE_END();
}
// clang-format off
CODEGEN_MMTF_parser_fetch_List(MMTF_Entity, entity)
CODEGEN_MMTF_parser_fetch_List(MMTF_GroupType, group)
CODEGEN_MMTF_parser_fetch_List(MMTF_BioAssembly, bioAssembly)
CODEGEN_MMTF_parser_fetch_List(MMTF_Transform, transform)
static
bool MMTF_unpack_from_msgpack_object(const msgpack_object* object, MMTF_container* thing) {
// clang-format on
int version_major;
MAP_ITERATE_BEGIN_RV(object, false);
FETCH_AND_ASSIGN(thing, string, mmtfVersion);
MAP_ITERATE_END();
// check for MMTF major version (semantic versioning)
if (thing->mmtfVersion &&
sscanf(thing->mmtfVersion, "%d", &version_major) == 1 &&
version_major > MMTF_SPEC_VERSION_MAJOR) {
fprintf(stderr, "Error: unsupported MMTF version '%s'.\n", thing->mmtfVersion);
return false;
}
MAP_ITERATE_BEGIN_RV(object, false);
FETCH_AND_ASSIGN(thing, string, mmtfProducer);
FETCH_AND_ASSIGN(thing, string, spaceGroup);
FETCH_AND_ASSIGN(thing, string, structureId);
FETCH_AND_ASSIGN(thing, string, title);
FETCH_AND_ASSIGN(thing, string, depositionDate);
FETCH_AND_ASSIGN(thing, string, releaseDate);
FETCH_AND_ASSIGN(thing, int, numBonds);
FETCH_AND_ASSIGN(thing, int, numAtoms);
FETCH_AND_ASSIGN(thing, int, numGroups);
FETCH_AND_ASSIGN(thing, int, numChains);
FETCH_AND_ASSIGN(thing, int, numModels);
FETCH_AND_ASSIGN(thing, float, resolution);
FETCH_AND_ASSIGN(thing, float, rFree);
FETCH_AND_ASSIGN(thing, float, rWork);
FETCH_AND_ASSIGN_WITHCOUNT(thing, entityList, entityList);
FETCH_AND_ASSIGN_WITHCOUNT(thing, bioAssemblyList, bioAssemblyList);
FETCH_AND_ASSIGN_WITHCOUNT(thing, groupList, groupList);
FETCH_AND_ASSIGN_WITHCOUNT(thing, int32_array, bondAtomList);
FETCH_AND_ASSIGN_WITHCOUNT(thing, int8_array, bondOrderList);
FETCH_AND_ASSIGN_WITHCOUNT(thing, string_array, chainIdList);
FETCH_AND_ASSIGN_WITHCOUNT(thing, string_array, chainNameList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int32_array, groupTypeList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int32_array, groupIdList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int32_array, sequenceIndexList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int32_array, atomIdList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, char_array, insCodeList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, char_array, altLocList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int8_array, secStructList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, float_array, bFactorList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, float_array, xCoordList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, float_array, yCoordList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, float_array, zCoordList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, float_array, occupancyList);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int32_array, chainsPerModel);
FETCH_AND_ASSIGN_DUMMYCOUNT(thing, int32_array, groupsPerChain);
FETCH_AND_ASSIGN_WITHCOUNT(thing, string_array, experimentalMethods);
FETCH_AND_ASSIGN_ARRAY(thing, float, unitCell);
MAP_ITERATE_END();
return true;
}
/*
* Decode a MMTF_container from a string
*/
bool MMTF_unpack_from_string(const char* buffer, size_t msgsize, MMTF_container* thing) {
bool status;
#ifdef MMTF_MSGPACK_USE_CPP11
auto oh = msgpack::unpack(buffer, msgsize);
status = MMTF_unpack_from_msgpack_object(&oh.get(), thing);
#else
msgpack_zone mempool;