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

Go to the SVN repository for this file.

1 /* $Id: blast_message.c 100585 2023-08-14 14:05:41Z fongah2 $
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 
27 /** @file blast_message.c
28  * These functions provide access to Blast_Message objects, used by
29  * the BLAST code as a wrapper for error and warning messages.
30  */
31 
34 
35 /** Declared in blast_message.h as extern const. */
36 const int kBlastMessageNoContext = -1;
38  = "Could not calculate ungapped Karlin-Altschul parameters due "
39  "to an invalid query sequence or its translation. Please verify the "
40  "query sequence(s) and/or filtering options";
41 
42 /** Allocate a new SMessageOrigin structure
43  * @param filename name of the file [in]
44  * @param lineno line number in the file above [in]
45  * @return newly allocated structure or NULL in case of memory allocation
46  * failure.
47  */
48 SMessageOrigin* SMessageOriginNew(const char* filename, unsigned int lineno)
49 {
50  SMessageOrigin* retval = NULL;
51 
52  if ( !filename || !(strlen(filename) > 0) ) {
53  return NULL;
54  }
55 
56  retval = calloc(1, sizeof(SMessageOrigin));
57  if ( !retval ) {
58  return NULL;
59  }
60 
61  retval->filename = strdup(filename);
62  retval->lineno = lineno;
63  return retval;
64 }
65 
66 /** Deallocate a SMessageOrigin structure
67  * @param msgo structure to deallocate [in]
68  * @return NULL
69  */
71 {
72  if (msgo) {
73  sfree(msgo->filename);
74  sfree(msgo);
75  }
76  return NULL;
77 }
78 
81 {
82  Blast_Message* var_msg = NULL;
84 
85  if (blast_msg == NULL)
86  return NULL;
87 
88  var_msg = blast_msg;
89  while (var_msg)
90  {
91  sfree(var_msg->message);
92  var_msg->origin = SMessageOriginFree(var_msg->origin);
93  next = var_msg->next;
94  sfree(var_msg);
95  var_msg = next;
96  }
97 
98  return NULL;
99 }
100 
101 Int2
103  int context, const char *message)
104 {
105  Blast_Message* new_msg = NULL;
106 
107  if (blast_msg == NULL)
108  return 1;
109 
110  new_msg = (Blast_Message*) calloc(1, sizeof(Blast_Message));
111  if (new_msg == NULL)
112  return -1;
113 
114  new_msg->severity = severity;
115  new_msg->context = context;
116  new_msg->message = strdup(message);
117 
118  if (*blast_msg)
119  {
120  Blast_Message* var_msg = *blast_msg;
121  while (var_msg->next)
122  {
123  var_msg = var_msg->next;
124  }
125  var_msg->next = new_msg;
126  }
127  else
128  {
129  *blast_msg = new_msg;
130  }
131 
132  return 0;
133 }
134 
135 Int2
137 {
138  if (blast_msg == NULL)
139  return 1;
140 
141  fprintf(stderr, "%s", blast_msg->message); /* FIXME! */
142 
143  return 0;
144 }
145 
146 void
147 Blast_Perror(Blast_Message* *msg, Int2 error_code, int context)
148 {
149  Blast_PerrorEx(msg, error_code, NULL, -1, context);
150  return;
151 }
152 
153 void
155  Int2 error_code,
156  const char* file_name,
157  int lineno,
158  int context)
159 {
160  Blast_Message* new_msg = (Blast_Message*) calloc(1, sizeof(Blast_Message));
161  ASSERT(msg);
162 
163  switch (error_code) {
164 
166  new_msg->message = strdup("Failed to calculate ideal Karlin-Altschul "
167  "parameters");
168  new_msg->severity = eBlastSevError;
169  new_msg->context = context;
170  break;
172  new_msg->message = strdup("Composition based statistics or "
173  "Smith-Waterman not supported for your "
174  "program type");
175  new_msg->severity = eBlastSevError;
176  new_msg->context = context;
177  break;
179  new_msg->message = strdup("BLAST search interrupted at user's request");
180  new_msg->severity = eBlastSevInfo;
181  new_msg->context = context;
182  break;
185  /* this should be a warning when multiple queries are there and at
186  * least 1 is OK */
187  new_msg->severity = eBlastSevError;
188  new_msg->context = context;
189  break;
190 
191  /* Fatal errors */
192  case BLASTERR_MEMORY:
193  /** @todo Ideally this message would be more informative (the error code
194  * already conveys this information) so that this string can be
195  * displayed to the end user via the CATCH_ALL macro. If this string is
196  * ever changed, please update that macro accordingly (ideally this
197  * error code would be caught and would lead to a CBlastSystemException
198  * being thrown with the eOutOfMemory error code) */
199  new_msg->message = strdup("Out of memory");
200  new_msg->severity = eBlastSevFatal;
201  new_msg->context = context;
202  break;
204  new_msg->message = strdup("Invalid argument to function");
205  new_msg->severity = eBlastSevFatal;
206  new_msg->context = context;
207  break;
209  new_msg->message = strdup("search cannot proceed due to errors in all "
210  "contexts/frames of query sequences");
211  new_msg->severity = eBlastSevFatal;
212  new_msg->context = context;
213  break;
214 
216  new_msg->message = strdup("The average subject length is too short");
217  new_msg->severity = eBlastSevFatal;
218  new_msg->context = context;
219  break;
220 
221  case BLASTERR_SEQSRC:
222  new_msg->message = strdup("search cannot proceed due to errors "
223  "retrieving sequences from databases");
224  new_msg->severity = eBlastSevFatal;
225  new_msg->context = context;
226  break;
227  /* No error, just free the structure */
228  case 0:
229  new_msg = Blast_MessageFree(new_msg);
230  break;
231 
232  /* Unknown error */
233  default:
234  {
235  char buf[512];
236  snprintf(buf, sizeof(buf) - 1, "Unknown error code %d", error_code);
237  new_msg->message = strdup(buf);
238  new_msg->severity = eBlastSevError;
239  new_msg->context = context;
240  }
241  break;
242  }
243 
244  if (new_msg && file_name && lineno > 0) {
245  new_msg->origin = SMessageOriginNew(file_name,
246  (unsigned int) lineno);
247  }
248 
249  if (*msg)
250  {
251  Blast_Message* var = *msg;
252  while (var->next)
253  var = var->next;
254  var->next = new_msg;
255  }
256  else
257  {
258  *msg = new_msg;
259  }
260 
261  return;
262 }
Definitions used throughout BLAST.
#define sfree(x)
Safe free a pointer: belongs to a higher level header.
Definition: blast_def.h:112
const char * kBlastErrMsg_CantCalculateUngappedKAParams
Definition: blast_message.c:38
void Blast_PerrorEx(Blast_Message **msg, Int2 error_code, const char *file_name, int lineno, int context)
Extended version of Blast_Perror which includes parameters for the file name and line number where th...
void Blast_Perror(Blast_Message **msg, Int2 error_code, int context)
Analogous to perror.
Int2 Blast_MessagePost(Blast_Message *blast_msg)
Print a message with ErrPostEx.
SMessageOrigin * SMessageOriginFree(SMessageOrigin *msgo)
Deallocate a SMessageOrigin structure.
Definition: blast_message.c:70
Int2 Blast_MessageWrite(Blast_Message **blast_msg, EBlastSeverity severity, int context, const char *message)
Writes a message to a structure.
const int kBlastMessageNoContext
Declared in blast_message.h as extern const.
Definition: blast_message.c:36
SMessageOrigin * SMessageOriginNew(const char *filename, unsigned int lineno)
Allocate a new SMessageOrigin structure.
Definition: blast_message.c:48
Blast_Message * Blast_MessageFree(Blast_Message *blast_msg)
Deallocates message memory.
Definition: blast_message.c:80
Structures for BLAST messages.
EBlastSeverity
Blast error message severities .
Definition: blast_message.h:55
@ eBlastSevError
Definition: blast_message.h:58
@ eBlastSevInfo
Definition: blast_message.h:56
@ eBlastSevFatal
Definition: blast_message.h:59
#define BLASTERR_INTERRUPTED
BLAST search was interrupted via a user-provided callback.
#define BLASTERR_IDEALSTATPARAMCALC
Could not compute the ideal Karlin-Altschul parameters.
#define BLASTERR_SUBJECT_LENGTH_INVALID
Subject seqs min avg length error.
#define BLASTERR_NOVALIDKARLINALTSCHUL
Could not calculate Karlin-Altschul statistics for any context.
#define BLASTERR_INVALIDQUERIES
All queries/contexts are determined invalid in the setup code.
#define BLASTERR_SEQSRC
Blast seqsrc returns BLAST_SEQSRC_ERROR.
#define BLASTERR_REDOALIGNMENTCORE_NOTSUPPORTED
Composition based statistics/Smith-Waterman not supported for a program type.
#define BLASTERR_MEMORY
System error: out of memory condition.
#define BLASTERR_INVALIDPARAM
Invalid parameter: possible programmer error or pre-condition not met.
const char * file_name[]
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
#define NULL
Definition: ncbistd.hpp:225
int16_t Int2
2-byte (16-bit) signed integer
Definition: ncbitype.h:100
char * buf
static size_t lineno
Definition: mdb_load.c:28
#define strdup
Definition: ncbi_ansi_ext.h:70
#define ASSERT
macro for assert.
Definition: ncbi_std.h:107
Structure to hold the a message from the core of the BLAST engine.
Definition: blast_message.h:70
int context
Context, allows us to print message for query number.
Definition: blast_message.h:75
EBlastSeverity severity
severity code
Definition: blast_message.h:72
SMessageOrigin * origin
Optional: origin of the message.
Definition: blast_message.h:74
char * message
User message to be saved.
Definition: blast_message.h:73
struct Blast_Message * next
next message in this list
Definition: blast_message.h:71
Structure to enclose the origin of an error message or warning.
Definition: blast_message.h:46
int lineno
Line number in the file above.
Definition: blast_message.h:48
char * filename
Name of the file.
Definition: blast_message.h:47
static CS_CONTEXT * context
Definition: will_convert.c:21
voidp calloc(uInt items, uInt size)
Modified on Sun Apr 21 03:37:32 2024 by modify_doxy.py rev. 669887