NCBI C++ ToolKit
blast_hspstream_mt_utils.c
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: blast_hspstream_mt_utils.c 72378 2016-05-04 14:59:01Z camacho $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Christiam Camacho
27  *
28  */
29 
30 /** @file blast_hspstream_mt_utils.c
31  * Private interfaces to support the multi-threaded traceback in conjunction
32  * with the BlastHSPStream
33  */
34 
36 
37 /** Reset and free the BlastHSPStreamResultBatch structures contained in the
38  * object manipulated by this function
39  * @param it object to operate on [in|out]
40  */
41 static void
43 {
44  Uint4 i;
45  if ( !it ) {
46  return;
47  }
48 
49  for (i = 0; i < it->num_batches; i++) {
52  }
53  it->num_batches = 0;
54 }
55 
58 {
59  if (it) {
61  if (it->array_of_batches)
63  sfree(it);
64  }
65  return NULL;
66 }
67 
68 /** Allocate a new BlastHSPStreamResultsBatchArray with the specified capacity
69  * @param num_elements capacity for the allocated structure [in]
70  */
73 {
75 
78  if ( !retval ) {
80  }
81  num_elements = (num_elements == 0) ? 10 : num_elements;
83  calloc(num_elements, sizeof(*retval->array_of_batches));
84  if ( !retval->array_of_batches ) {
86  }
87  retval->num_batches = 0;
88  retval->num_allocated = num_elements;
89  return retval;
90 }
91 
94 {
96 }
97 
98 /** Append a value to the array, reallocating it if necessary
99  * @param batches array to append data to [in|out]
100  * @param batch value to append to array [in]
101  * @return 0 on success, otherwise non-zero */
102 static int
105 {
106  const size_t kResizeFactor = 2;
107 
108  if ( !batches || !batch ) {
109  return BLASTERR_INVALIDPARAM;
110  }
111 
112  if ((batches->num_batches+1) > batches->num_allocated) {
113  /* re-allocate */
115  realloc(batches->array_of_batches,
116  kResizeFactor * batches->num_allocated *
117  sizeof(*batches->array_of_batches));
118  if ( !reallocation ) {
119  return BLASTERR_MEMORY;
120  }
121  batches->num_allocated *= kResizeFactor;
122  batches->array_of_batches = reallocation;
123  }
124  batches->array_of_batches[batches->num_batches] = batch;
125  batches->num_batches++;
126  return 0;
127 }
128 
129 static Uint4
131 {
132  Uint4 retval = 0;
133  if (hsp_stream && hsp_stream->num_hsplists > 0) {
134  Int4 current_oid = -1, i;
135  for (i = hsp_stream->num_hsplists - 1; i >= 0; i--) {
136  if (current_oid != hsp_stream->sorted_hsplists[i]->oid) {
137  current_oid = hsp_stream->sorted_hsplists[i]->oid;
138  retval++;
139  }
140  }
141  }
142  return retval;
143 }
144 
147 {
149 
150  if (!batches || !hsp_stream) {
151  return BLASTERR_INVALIDPARAM;
152  }
153 
154  *batches =
156  if ( !*batches ) {
157  return BLASTERR_MEMORY;
158  }
159 
160  for (batch = Blast_HSPStreamResultBatchInit(hsp_stream->results->num_queries);
161  BlastHSPStreamBatchRead(hsp_stream, batch) != kBlastHSPStream_Eof;
162  batch = Blast_HSPStreamResultBatchInit(hsp_stream->results->num_queries)) {
163  if (s_BlastHSPStreamResultsBatchArrayAppend(*batches, batch) != 0) {
165  *batches = BlastHSPStreamResultsBatchArrayFree(*batches);
166  return BLASTERR_MEMORY;
167  }
168  }
169  batch = Blast_HSPStreamResultBatchFree(batch);
171 }
#define sfree(x)
Safe free a pointer: belongs to a higher level header.
Definition: blast_def.h:112
const size_t kResizeFactor
factor by which these arrays are resized
BlastHSPStreamResultBatch * Blast_HSPStreamResultBatchReset(BlastHSPStreamResultBatch *batch)
free the list of HSPLists within a batch
const int kBlastHSPStream_Eof
Return value when the end of the stream is reached (applicable to read method only)
const int kBlastHSPStream_Success
Standard success return value for BlastHSPStream methods.
BlastHSPStreamResultBatch * Blast_HSPStreamResultBatchInit(Int4 num_hsplists)
create a new batch to hold HSP results
BlastHSPStreamResultBatch * Blast_HSPStreamResultBatchFree(BlastHSPStreamResultBatch *batch)
free a batch of HSP results.
int BlastHSPStreamBatchRead(BlastHSPStream *hsp_stream, BlastHSPStreamResultBatch *batch)
Batch read function for this BlastHSPStream implementation.
static Uint4 s_BlastHSPStreamCountNumOids(const BlastHSPStream *hsp_stream)
int BlastHSPStreamToHSPStreamResultsBatch(BlastHSPStream *hsp_stream, BlastHSPStreamResultsBatchArray **batches)
Extracts all data from the BlastHSPStream into its output parameters.
static BlastHSPStreamResultsBatchArray * s_BlastHSPStreamResultsBatchArrayNew(Uint4 num_elements)
Allocate a new BlastHSPStreamResultsBatchArray with the specified capacity.
static int s_BlastHSPStreamResultsBatchArrayAppend(BlastHSPStreamResultsBatchArray *batches, BlastHSPStreamResultBatch *batch)
Append a value to the array, reallocating it if necessary.
static void s_BlastHSPStreamResultsBatchArrayReset(BlastHSPStreamResultsBatchArray *it)
Reset and free the BlastHSPStreamResultBatch structures contained in the object manipulated by this f...
BlastHSPStreamResultsBatchArray * BlastHSPStreamResultsBatchNew(void)
Creates a BlastHSPStreamResultsBatchArray with a single element.
BlastHSPStreamResultsBatchArray * BlastHSPStreamResultsBatchArrayFree(BlastHSPStreamResultsBatchArray *it)
Releases memory acquired in BlastHSPStreamToHSPStreamResultsBatch.
Private interfaces to support the multi-threaded traceback in conjunction with the BlastHSPStream.
#define BLASTERR_MEMORY
System error: out of memory condition.
#define BLASTERR_INVALIDPARAM
Invalid parameter: possible programmer error or pre-condition not met.
#define NULL
Definition: ncbistd.hpp:225
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int i
Int4 oid
The ordinal id of the subject sequence this HSP list is for.
Definition: blast_hits.h:154
Int4 num_queries
Number of query sequences.
Definition: blast_hits.h:184
structure used to hold a collection of hits retrieved from the HSPStream
Structure to extract the contents of the BlastHSPStream for MT traceback processing.
Uint4 num_batches
number of batches populated in the array_of_batches element
BlastHSPStreamResultBatch ** array_of_batches
Array of batches, each corresponding to a single OID with BLAST hits.
Uint4 num_allocated
number of batches allocated in the array_of_batches element
Default implementation of BlastHSPStream.
Int4 num_hsplists
number of HSPlists saved
BlastHSPList ** sorted_hsplists
list of all HSPlists from 'results' combined, sorted in order of decreasing subject OID
BlastHSPResults * results
Structure for saving HSP lists.
voidp calloc(uInt items, uInt size)
Modified on Wed Sep 04 15:01:13 2024 by modify_doxy.py rev. 669887