-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsampletab.c
149 lines (107 loc) · 3.36 KB
/
sampletab.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
#include <string.h>
#include <stdio.h>
#include <hdf5.h>
#include <hdf5_hl.h>
#include "memutil.h"
#include "sampletab.h"
#include "err.h"
#include "util.h"
/**
* Allocates memory for and initializes data structure
* that describes the HDF5 table used to store Samples data.
*
*/
SampleTab *sample_tab_new(hid_t h5file, const char *chrom_name,
size_t n_record) {
herr_t status;
SampleTab *tab;
Sample sample_desc;
const char *field_names[] = {"name"};
tab = my_malloc(sizeof(SampleTab));
tab->h5file = h5file;
/* set datatypes for each field */
tab->name_type = H5Tcopy(H5T_C_S1);
H5Tset_size(tab->name_type, SAMPLE_MAX_NAME);
tab->field_type[0] = tab->name_type; /* name */
/* sizes of record and each field */
tab->record_size = sizeof(Sample);
tab->field_size[0] = sizeof(sample_desc.name);
/* offsets of each field */
tab->field_offset[0] = HOFFSET(Sample, name);
/* title and name of table */
tab->title = util_str_concat(chrom_name, " samples", NULL);
tab->name = util_str_concat("samples_", chrom_name, NULL);
/* set chunk size and compression */
tab->chunk_size = SAMPLETAB_CHUNK_SIZE;
tab->compress = 1;
tab->n_record = 0;
status = H5TBmake_table(tab->title, tab->h5file, tab->name,
SAMPLETAB_N_FIELDS, n_record,
tab->record_size, field_names,
tab->field_offset, tab->field_type,
tab->chunk_size, NULL, tab->compress, NULL);
if(status < 0) {
my_err("%s:%d: could not create samples table "
"for chromosome %s\n", chrom_name,
__FILE__, __LINE__);
}
return tab;
}
void sample_tab_free(SampleTab *tab) {
H5Tclose(tab->name_type);
my_free(tab->title);
my_free(tab->name);
my_free(tab);
}
/**
* Creates a new samples table HDF5 file pointed to by h5file handle
* and populates it using provided array of samples
*/
SampleTab *sample_tab_create(hid_t h5file, const char *chrom_name,
Sample *samples, size_t n_sample) {
SampleTab *tab;
int i;
tab = sample_tab_new(h5file, chrom_name, n_sample);
for(i = 0; i < n_sample; i++) {
sample_tab_append_row(tab, &samples[i]);
}
return tab;
}
SampleTab *sample_tab_from_names(hid_t h5file, const char *chrom_name,
char **sample_names, size_t n_sample) {
Sample *samples;
SampleTab *samp_tab;
int i;
samples = my_malloc(sizeof(Sample) * n_sample);
for(i = 0; i < n_sample; i++) {
util_strncpy(samples[i].name, sample_names[i],
sizeof(samples[i].name));
}
samp_tab = sample_tab_create(h5file, chrom_name, samples, n_sample);
my_free(samples);
return samp_tab;
}
/**
* Appends row to table described by provided SampleTab datastructure.
*/
void sample_tab_append_row(SampleTab *tab, Sample *data) {
herr_t status;
hsize_t n_to_write;
n_to_write = 1;
/* fprintf(stderr, "writing sample record:\n" */
/* "data: %s\n" */
/* "n_record: %d\n" */
/* "record_size: %d\n" */
/* "field_offset[0]: %d\n" */
/* "field_size[0]: %d\n", data->name, tab->n_record, tab->record_size, */
/* tab->field_offset[0], tab->field_size[0]); */
status = H5TBwrite_records(tab->h5file, tab->name,
tab->n_record, n_to_write,
tab->record_size, tab->field_offset,
tab->field_size, data);
tab->n_record += 1;
if(status < 0) {
my_err("%s:%d: failed to write record to Sample table\n",
__FILE__, __LINE__);
}
}