-
Notifications
You must be signed in to change notification settings - Fork 2
/
MPI_packing.c
181 lines (159 loc) · 5.44 KB
/
MPI_packing.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
#include "bison.h"
/******************************************************************************
*
* Take a BAM header and pack it into a single contiguous memory block. Store
* the resulting block and its size in an MPI_Header structure.
*
* THE RESULT MUST BE free()d
*
* bam_hdr_t *header: The header to store
*
*******************************************************************************/
MPI_Header * pack_header(bam_hdr_t *header) {
size_t size = sizeof(int32_t); //n_targets
int32_t *pint32_t;
uint32_t *puint32_t;
char *pchar;
int *pint;
int i;
void *p;
MPI_Header *output = malloc(sizeof(MPI_Header));
assert(output);
//target_name
for(i=0; i<header->n_targets; i++) {
size += (sizeof(char) * (1+strlen(header->target_name[i])));
}
//target_len
size += sizeof(uint32_t) * header->n_targets;
//l_text
size += sizeof(int);
//text
size += sizeof(char) * (1 + header->l_text);
//Start copying, layout is n_targets,target_name[s],target_len[s],l_text,text
output->size = (int) size;
output->packed = malloc(size);
assert(output->packed);
p = output->packed;
//n_targets
memcpy(p, (void *) &(header->n_targets), sizeof(int32_t));
pint32_t = (int32_t *) p;
p = (void *) (++pint32_t);
//target_name
for(i=0; i<header->n_targets; i++) {
memcpy(p, (void *) header->target_name[i], sizeof(char) * (1 + strlen(header->target_name[i])));
pchar = (char *) p;
p = (void *) (pchar+1+strlen(header->target_name[i]));
}
//target_len
memcpy(p, (void *) header->target_len, sizeof(uint32_t)*(header->n_targets));
puint32_t = (uint32_t *) p;
p = (void *) (puint32_t + header->n_targets);
//l_text
memcpy(p, (void *) &(header->l_text), sizeof(int));
pint = (int *) p;
p = (void *) ++pint;
//text
memcpy(p, (void *) (header->text), sizeof(char) * (header->l_text));
return output;
}
/******************************************************************************
*
* Unpack a header packed into an initialized bam_hdr_t
*
* bam_hdr_t *header: The header to unpack into
* void *packed: The packed header
*
*******************************************************************************/
void unpack_header(bam_hdr_t *header, void *packed) {
void *p = packed;
int i;
int *pint;
int32_t *pint32_t;
uint32_t *puint32_t;
char *pchar;
size_t strlength;
//n_targets
header->n_targets = *((int32_t *) packed);
pint32_t = (int32_t *) p;
p = (void *) (++pint32_t);
//**target_name
header->target_name = (char **) malloc(sizeof(char *) * (header->n_targets));
assert(header->target_name);
for(i=0; i<header->n_targets; i++) {
strlength = strlen((char *) p)+1;
header->target_name[i] = malloc(sizeof(char) * strlength);
assert(header->target_name[i]);
memcpy((void *) (header->target_name[i]), p, sizeof(char)*strlength);
pchar = (char *) p;
p = (void *) (pchar+strlength);
}
//target_len
header->target_len = malloc(sizeof(uint32_t) * (header->n_targets));
assert(header->target_len);
for(i=0; i<header->n_targets; i++) {
header->target_len[i] = *((uint32_t *) p);
puint32_t = (uint32_t *) p;
p = (void *) ++puint32_t;
}
//l_text
header->l_text = *((int *) p);
pint = (int *) p;
p = (void *) ++pint;
//text
header->text = (char *) malloc(sizeof(char) * (header->l_text+1));
assert(header->text);
memcpy((void *) (header->text), p, sizeof(char) * (header->l_text + 1));
}
/******************************************************************************
*
* Take a BAM read and pack it into a single contiguous memory block. Store
* the resulting block and its size in an MPI_Read structure.
*
* THE RESULT MUST BE free()d
*
* bam1_t *read: The read to store
*
*******************************************************************************/
MPI_read * pack_read(bam1_t *read, MPI_read *output) {
bam1_t *pbam1_t;
int needed_size, m_data = read->m_data;
needed_size = (int) (sizeof(bam1_t) + m_data);
if(output->size == 0) {
output->packed = malloc((size_t) needed_size);
assert(output->packed);
output->size = needed_size;
} else if(needed_size > output->size) {
output->packed = realloc(output->packed, (size_t) needed_size);
assert(output->packed);
output->size = needed_size;
}
memcpy((void *) output->packed, (void *) read, sizeof(bam1_t));
pbam1_t = output->packed;
pbam1_t++;
memcpy((void *) pbam1_t, (void *) read->data, m_data);
return output;
}
/******************************************************************************
*
* Unpack a packed read into an initialized bam1_t read.
*
* bam1_t *read: The read to unpack into
* void *packed: The packed read
*
*******************************************************************************/
bam1_t *unpack_read(bam1_t *read, void *packed) {
bam1_t *pbam1_t = packed;
uint8_t *pdata = (uint8_t *) (pbam1_t+1);
uint8_t *newdata;
pbam1_t->data = pdata;
if(read != NULL) bam_destroy1(read);
read = bam_init1();
read->core = pbam1_t->core;
read->l_data= pbam1_t->l_data;
read->m_data = pbam1_t->m_data;
newdata = (uint8_t *) malloc(read->m_data);
assert(newdata);
memcpy((void *) newdata, (void *) pdata, read->m_data);
read->data = newdata;
return read;
}