summaryrefslogtreecommitdiffstats
path: root/src/load/load_influenza_aa_dat.c (plain)
blob: 38263491d1b72b6c0316d5085c2dd56710710864
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
/*
 * Load the influnza_aa.dat tab delimited text file into an HDF5
 * binary table.
 *
 * todo: Handle NULL values occuring in numeric fields.
 */

#include "load_influenza_aa_dat.h"
#include "error/check_error.h"
#include "error/check_h5_error.h"
#include <hdf5_hl.h>
#include <string.h>
#include <stdlib.h>

#define NFIELDS (hsize_t) 11

void
load_influenza_aa_dat (hid_t file_id, const char* file_name)
{
  /*
   * Model the data using native types.
   */
  typedef struct
  {
    char genbank_accession_number[9];
    char host[15];
    int genome_segment_number;
    char subtype[7];
    char country[25];
    int year;
    int sequence_length;
    char virus_name[196];
    char age[17];
    char gender[6];
    char full_length_indicator[4];
  } supplementary_data;

  /*
   * Use an HDF5 Table for storage.
   * http://www.hdfgroup.org/HDF5/Tutor/h5table.html
   */

  /*
   * "Calculate the size and the offsets of our struct members in
   * memory."
   */
  size_t dst_size = sizeof (supplementary_data);
  size_t dst_offset[NFIELDS] =
    { HOFFSET (supplementary_data, genbank_accession_number),
    HOFFSET (supplementary_data, host),
    HOFFSET (supplementary_data, genome_segment_number),
    HOFFSET (supplementary_data, subtype),
    HOFFSET (supplementary_data, country),
    HOFFSET (supplementary_data, year),
    HOFFSET (supplementary_data, sequence_length),
    HOFFSET (supplementary_data, virus_name),
    HOFFSET (supplementary_data, age),
    HOFFSET (supplementary_data, gender),
    HOFFSET (supplementary_data, full_length_indicator)
  };

  supplementary_data dst_buf[1];

  size_t dst_sizes[NFIELDS] = { sizeof (dst_buf[0].genbank_accession_number),
    sizeof (dst_buf[0].host),
    sizeof (dst_buf[0].genome_segment_number),
    sizeof (dst_buf[0].subtype),
    sizeof (dst_buf[0].country),
    sizeof (dst_buf[0].year),
    sizeof (dst_buf[0].sequence_length),
    sizeof (dst_buf[0].virus_name),
    sizeof (dst_buf[0].age),
    sizeof (dst_buf[0].gender),
    sizeof (dst_buf[0].full_length_indicator)
  };

  /*
   * Map the native types to HDF5 types for each field.
   */
  hid_t field_type[NFIELDS];

  hid_t genbank_accession_number_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (genbank_accession_number_type, 9);
  field_type[0] = genbank_accession_number_type;

  hid_t host_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (host_type, 15);
  field_type[1] = host_type;

  field_type[2] = H5T_NATIVE_INT;

  hid_t subtype_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (subtype_type, 7);
  field_type[3] = subtype_type;

  hid_t country_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (country_type, 25);
  field_type[4] = country_type;

  field_type[5] = H5T_NATIVE_INT;

  field_type[6] = H5T_NATIVE_INT;

  hid_t virus_name_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (virus_name_type, 196);
  field_type[7] = virus_name_type;

  hid_t age_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (age_type, 17);
  field_type[8] = age_type;

  hid_t gender_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (gender_type, 6);
  field_type[9] = gender_type;

  hid_t full_length_indicator_type = H5Tcopy (H5T_C_S1);
  H5Tset_size (full_length_indicator_type, 4);
  field_type[10] = full_length_indicator_type;

  /*
   * Labels used for the fields in the table.
   */
  const char *field_names[NFIELDS] = { "GenBank accession number",
    "Host",
    "Genome segment number",
    "Subtype",
    "Country",
    "Year",
    "Sequence length",
    "Virus name",
    "Age",
    "Gender",
    "Full-length Indicator"
  };

  /*
   * Table storage options.
   */
  hsize_t chunk_size = 10;
  int *fill_data = NULL;
  int compress = 0;

  /*
   * Insert the records.
   */
  supplementary_data p_data;
  FILE *dat = fopen (file_name, "r");
  if (dat == NULL)
    check_error (__FILE__, __LINE__);
  char *line = NULL;
  size_t len = 0;
  int current_line = 0;

  while (getline (&line, &len, dat) != -1)
    {

      current_line++;
      char *running = strdup (line);
      char *token = NULL;

      /*
       * Parse the line, handling the case of empty fields represented
       * by sequential delimiters.
       */
      strncpy (p_data.genbank_accession_number, strsep (&running, "\t"),
	       sizeof (p_data.genbank_accession_number));

      strncpy (p_data.host, strsep (&running, "\t"), sizeof (p_data.host));

      token = strsep (&running, "\t");
      if (strcmp (token, "\0") == 0)
	p_data.genome_segment_number = 0;
      else
	p_data.genome_segment_number = atoi (token);

      strncpy (p_data.subtype, strsep (&running, "\t"),
	       sizeof (p_data.subtype));

      strncpy (p_data.country, strsep (&running, "\t"),
	       sizeof (p_data.country));

      /*
       * Convert the year field from text to numeric.  Unknown and empty
       * values are assigned a numeric value of zero.
       */
      token = strsep (&running, "\t");
      if (strcmp (token, "\0") == 0)
	p_data.year = 0;
      else if (strcmp (token, "unknown") == 0)
	p_data.year = 0;
      else if (strcmp (token, "NON") == 0)
	p_data.year = 0;
      else
	p_data.year = atoi (token);

      token = strsep (&running, "\t");
      if (strcmp (token, "\0") == 0)
	p_data.sequence_length = 0;
      else
	p_data.sequence_length = atoi (token);

      strncpy (p_data.virus_name, strsep (&running, "\t"),
	       sizeof (p_data.virus_name));

      strncpy (p_data.age, strsep (&running, "\t"), sizeof (p_data.age));

      strncpy (p_data.gender, strsep (&running, "\t"),
	       sizeof (p_data.gender));

      strncpy (p_data.full_length_indicator, strsep (&running, "\t"),
	       sizeof (p_data.full_length_indicator));

      if (current_line == 1)
	{

	  /*
	   * Dataset already exists.  Purge it.
	   */
	  if (H5LTfind_dataset (file_id, "influenza_aa.dat") == 1)
	    {
	      hsize_t nfields = 0;
	      hsize_t nrecords = 0;
	      herr_t status = H5TBget_table_info (file_id, "influenza_aa.dat",
						  &nfields, &nrecords);
	      if (status < 0)
		check_h5_error (status, __FILE__, __LINE__);

	      status = H5TBdelete_record (file_id, "influenza_aa.dat", 0, nrecords);
	      if (status < 0)
		check_h5_error (status, __FILE__, __LINE__);

	      status =
		H5TBappend_records (file_id, "influenza_aa.dat", 1, dst_size,
				    dst_offset, dst_sizes, &p_data);
	      if (status < 0)
		check_h5_error (status, __FILE__, __LINE__);
	    }

	  /*
	   * Dataset does not exist.  Create it.
	   */
	  else
	    {
	      herr_t status = H5TBmake_table ("influenza_aa.dat", file_id,
					      "influenza_aa.dat", NFIELDS, 1, dst_size,
					      field_names, dst_offset, field_type,
					      chunk_size, fill_data, compress,
					      &p_data);
	      if (status < 0)
		check_h5_error (status, __FILE__, __LINE__);
	    }
	}

      else
	{
	  herr_t status =
	    H5TBappend_records (file_id, "influenza_aa.dat", 1, dst_size,
				dst_offset, dst_sizes, &p_data);
	  if (status < 0)
	    check_h5_error (status, __FILE__, __LINE__);
	}

      if (running)
	free (running);

    }

  if (line)
    free (line);

  fclose (dat);

  H5Tclose (genbank_accession_number_type);
  H5Tclose (host_type);
  H5Tclose (subtype_type);
  H5Tclose (country_type);
  H5Tclose (virus_name_type);
  H5Tclose (age_type);
  H5Tclose (gender_type);
  H5Tclose (full_length_indicator_type);

  return;
}

Valid XHTML 1.0 Strict

Copyright © 2009 Don Pellegrino All Rights Reserved.