summaryrefslogtreecommitdiffstats
path: root/src/assign/assign_protein_type.c (plain)
blob: 3947800e99245dcc5c9a21c0a923b6df15fafc3f
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
#define _GNU_SOURCE
#include "assign_protein_type.h"
#include "error/check_error.h"
#include "error/check_h5_error.h"
#include "error/check_ncbi_error.h"
#include "model/gi_type_data.h"
#include "model/gi_type_data_init.h"
#include "model/sequence_data.h"
#include "model/sequence_data_init.h"
#include <blast.h>
#include <error.h>
#include <hdf5_hl.h>
#include <ncbi.h>
#include <readdb.h>
#include <salpacc.h>
#include <search.h>
#include <stdbool.h>

/*
 * BLAST database containing all of the influenza protein sequences.
 */
#define SEQDB "/home/don/exp004/influenzadb/influenzadb"

/*
 * BLAST reference database of prototypical protein types.
 */
#define REFDB "/home/don/exp004/influenzadb/proteinnames"

#define BUFFER_LEN 50

void
assign_protein_type (hid_t file_id)
{
  /*
   * Iterate through the records for which no protein type has been
   * assigned.  Create a BioSeq Pointer to the data and then use this
   * as input to the BLAST run against the reference database of
   * prototypical sequence records.
   */

  /*
   * Make BLAST messages reportable.
   */
  ErrSetMessageLevel (SEV_WARNING);

  /*
   * Open the BLAST sequence database.
   */
  ReadDBFILEPtr seqdb = readdb_new (SEQDB, true);

  /*
   * Get default BLAST options.
   */
  BLAST_OptionsBlkPtr options = BLASTOptionNew ("blastp", true);

  /*
   * Set to single hit mode.
   */
  options->window_size = 0;
  options->multiple_hits_only = false;

  ValNodePtr error_returns = NULL;

  /*
   * Read the data from HDF5 influenza.faa.
   */
  hsize_t faa_nfields;
  hsize_t faa_nrecords;
  herr_t status = H5TBget_table_info (file_id, "influenza.faa", &faa_nfields,
				      &faa_nrecords);
  if (status < 0)
    check_h5_error (status, __FILE__, __LINE__);

  sequence_data* faa_buf = malloc (sizeof(sequence_data) * faa_nrecords);

  size_t faa_size;
  size_t faa_offset[SEQUENCE_DATA_FIELD_NUM];
  size_t faa_sizes[SEQUENCE_DATA_FIELD_NUM];
  hid_t faa_field_type[SEQUENCE_DATA_FIELD_NUM];
  sequence_data_init (&faa_size, faa_offset, faa_sizes, faa_field_type);

  status = H5TBread_table (file_id, "influenza.faa", faa_size, faa_offset,
			   faa_sizes, faa_buf);
  if (status < 0)
    check_h5_error (status, __FILE__, __LINE__);

  /*
   * Allocate memory for the new table.
   */
  gi_type_data* new_buf = malloc (sizeof (gi_type_data) * faa_nrecords);
  if (new_buf == NULL)
    check_error (__FILE__, __LINE__);

  /*
   * Read the data from HDF5 gi_type_data.
   */
  hsize_t gi_nfields = 0;
  hsize_t gi_nrecords = 0;
  size_t gi_size = 0;
  size_t gi_offset[GI_TYPE_DATA_FIELD_NUM];
  size_t gi_sizes[GI_TYPE_DATA_FIELD_NUM];
  hid_t gi_field_type[GI_TYPE_DATA_FIELD_NUM];
  gi_type_data_init (&gi_size, gi_offset, gi_sizes, gi_field_type);

  gi_type_data* old_buf = NULL;

  /*
   * If the table is already present read the values into memory and
   * then clear the table.
   */
  if (H5LTfind_dataset (file_id, "gi_type_data") == 1)
    {

      printf ("Updating gi_type_data.\n");

      status = H5TBget_table_info (file_id, "gi_type_data", &gi_nfields,
				   &gi_nrecords);
      if (status < 0)
	check_h5_error (status, __FILE__, __LINE__);

      printf ("  Using gi_type_data cache of %i records.\n", (int)gi_nrecords);

      old_buf = malloc (sizeof(gi_type_data) * gi_nrecords);

      status = H5TBread_table (file_id, "gi_type_data", gi_size, gi_offset,
			       gi_sizes, old_buf);
      if (status < 0)
	check_h5_error (status, __FILE__, __LINE__);

      status = H5TBdelete_record (file_id, "gi_type_data", 0, gi_nrecords);
      if (status < 0)
	check_h5_error (status, __FILE__, __LINE__);

    }

  /*
   * If the table is not already present create it.
   */
  else
    {

      printf ("Creating gi_type_data.\n");

      const char* gi_type_data_field_names[GI_TYPE_DATA_FIELD_NUM] =
	GI_TYPE_DATA_FIELD_NAMES;

      hsize_t chunk_size = 10;
      int *fill_data = NULL;
      int compress = 0;

      status = H5TBmake_table ("gi_type_data", file_id,
			       "gi_type_data",
			       GI_TYPE_DATA_FIELD_NUM, 0,
			       gi_size, gi_type_data_field_names,
			       gi_offset, gi_field_type,
			       chunk_size, fill_data, compress,
			       NULL);
      if (status < 0)
	check_h5_error (status, __FILE__, __LINE__);

    }

  /*
   * Copy the contents of the old table into a hash.
   */
  struct hsearch_data htab;
  bzero (&htab, sizeof (htab));
  if (hcreate_r (gi_nrecords * 2, &htab) == 0)
    error_at_line (EXIT_FAILURE, 0, __FILE__, __LINE__,
		   "Allocation of cache failed.");
  ENTRY e, *ep;

  for (int i = 0; i < (int)gi_nrecords; i++)
    {
      char gi_chr[25];
      snprintf (gi_chr, 25, "%i", old_buf[i].gi);
      e.key = strdup (gi_chr);
      e.data = &old_buf[i];
      if (hsearch_r (e, ENTER, &ep, &htab) == 0)
	error_at_line (EXIT_FAILURE, 0, __FILE__, __LINE__,
		       "Allocation failed.");
    }

  /*
   * Assign protein types to records for which the field is empty.
   */
  printf ("Records to process: %i\n", (int)faa_nrecords);
  int written = 0;
  for (int i = 0; i < (int)faa_nrecords; i++)
    {
      new_buf[i].gi = faa_buf[i].gi;
      strncpy (new_buf[i].type, "", sizeof (new_buf[i].type));
      strncpy (new_buf[i].protein, "", sizeof (new_buf[i].protein));

      char gi_chr[25];
      snprintf (gi_chr, 25, "%i", faa_buf[i].gi);
      e.key = gi_chr;
      e.data = NULL;

      /*
       * A record was not found in the cache for this gi.
       */
      if (hsearch_r (e, FIND, &ep, &htab) == 0)
	{

	  /*
	   * Read the sequence from the database by GI.
	   */
	  Int4 sequence_number = readdb_gi2seq (seqdb, faa_buf[i].gi, NULL);
	  BioseqPtr bsp = readdb_get_bioseq (seqdb, sequence_number);
	  if (bsp == NULL)
	    {
	      error_at_line (EXIT_FAILURE, 0, __FILE__, __LINE__,
			     "Unable to find BLAST record for gi|%i.  Ensure "
			     "the BLAST database is up-to-date with the HDF5 "
			     "record set.  See the BLAST formatdb.log file "
			     "for details.\n",
			     faa_buf[i].gi);
	    }

	  SeqAlignPtr seqalign = BioseqBlastEngine (bsp,
						    "blastp",
						    REFDB,
						    options,
						    NULL,
						    &error_returns,
						    NULL);

	  /*
	   * BLAST reported an error.  Write it out and continue processing.
	   */
	  if (error_returns != NULL)
	    {
	      CharPtr msg = BlastErrorToString (error_returns);
	      printf ("Warning: An error has been reported by the NCBI Toolkit "
		      "API for sequence gi|%i: %s",
		      faa_buf[i].gi, msg);
	      free (msg);
	    }

	  /*
	   * A hit was found.  Record the first hit as the protein type.
	   * Skip the first 6 characters and eat the "lcl|x_".
	   */
	  else if (seqalign != NULL)
	    {
	      Char target_id_buf[BUFFER_LEN + 1];
	      SeqIdPtr target_id = SeqAlignId (seqalign, 1);
	      SeqIdWrite (target_id, target_id_buf, PRINTID_FASTA_SHORT,
			  BUFFER_LEN);

	      // Species Type
	      new_buf[i].type[0] = target_id_buf[4];
	      new_buf[i].type[1] = '\0';

	      // Protein Type
	      strncpy (new_buf[i].protein, &target_id_buf[6],
		       sizeof (new_buf[i].protein));
	    }

	  /*
	   * BLAST did not find any hits.
	   */
	  else
	    {
	      printf ("Warning: Unable to identify protein type for sequence "
		      "gi|%i\n", faa_buf[i].gi);
	    }

	  /*
	   * Clean up memory for the next ieration.
	   */
	  seqalign = SeqAlignSetFree (seqalign);
	  bsp = BioseqFree (bsp);

	} // End existing entry not found.

      /*
       * Hash table entry found.  Keep the old value.
       */
      else
	{
	  gi_type_data* old_value = (gi_type_data*)ep->data;
	  new_buf[i].gi = old_value->gi;
	  strncpy (new_buf[i].type, old_value->type, sizeof (new_buf[i].type));
	  strncpy (new_buf[i].protein, old_value->protein, sizeof (new_buf[i].protein));
	}

      /*
       * Write the data out to the file.
       */
      if ( (i % 1000 == 0) && (i > 0) )
	{
	  status = H5TBappend_records (file_id, "gi_type_data", 1000,
				      gi_size, gi_offset, gi_sizes,
				      &new_buf[i-1000]);
	  if (status < 0)
	    check_h5_error (status, __FILE__, __LINE__);

	  status = H5Fflush (file_id, H5F_SCOPE_GLOBAL);
	  if (status < 0)
	    check_h5_error (status, __FILE__, __LINE__);

	  written = i;

	  printf ("Processed %i of %i records.\n", i, (int)faa_nrecords);
	}

    }

  /*
   * Write out records from the last bin if it was less than 1000
   * records in size.
   */
  if ((int)faa_nrecords < 1000)
    {
      status = H5TBappend_records (file_id, "gi_type_data", faa_nrecords,
				  gi_size, gi_offset, gi_sizes,
				  new_buf);
    }

  else
    {
      status = H5TBappend_records (file_id, "gi_type_data", faa_nrecords - written,
				  gi_size, gi_offset, gi_sizes,
				   &new_buf[written]);
    }

  if (status < 0)
    check_h5_error (status, __FILE__, __LINE__);

  status = H5Fflush (file_id, H5F_SCOPE_GLOBAL);
  if (status < 0)
    check_h5_error (status, __FILE__, __LINE__);

  free (faa_buf);
  free (old_buf);
  free (new_buf);
  hdestroy_r (&htab);

  options = BLASTOptionDelete (options);
  readdb_destruct (seqdb);

  return;
}

Valid XHTML 1.0 Strict

Copyright © 2009 Don Pellegrino All Rights Reserved.