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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
%{
#include "wisebase.h"
#include "histogram.h"
#define HscoreLISTLENGTH 256
#define DATAENTRYSTDPOINTS 8
#define DATASCORESTORAGE_LENGTH 1024
typedef long BytePosition;
%}
struct DataEntry
char * name // name of the entry
int data[DATAENTRYSTDPOINTS] // space for algorithms to use
boolean is_reversed !def="FALSE" // for sequences. handy
BytePosition byte_position !hidden // useful for indexers - hopefully long enough!
char * filename !hidden !link // useful for indexers etc.
%info
A lightweight structure to represent the information
a db search algorithm will want to store and *nothing*
more about a single entry
This object will be stored twice (once for the target
and once for the query) for each comparison: they probably
will be on different databases and different objects.
The data field is just a number (8 at the moment) of int's available
for databases to store useful information (eg, length) of the
object.
A number of extra fields are provided for convience sake for indexers,
including byte_position and filename.
%%
struct DataScore
DataEntry * query
DataEntry * target
int score
double evalue
int is_stored !hidden
%info
The basic one entry vs one entry structure. Each
of the DataEntry datastructures actually store the
information about the indexing etc.
%%
struct DataScoreStorage
DataScore score_array[DATASCORESTORAGE_LENGTH]
DataEntry query_array[DATASCORESTORAGE_LENGTH]
DataEntry target_array[DATASCORESTORAGE_LENGTH]
int curr_pos !def="0"
%info
This object was needed because for very large searches
the memory usage of allocation DataScore and DataEntry's
on the heap was becoming prohibative.
This datastructure holds pre-allocated DataScore and DataEntry
objects read for use. A complicated constructor/deconstructor
pair for DataScore objects ensures that these can be used
cleanly.
%%
struct Hscore
DataScore ** ds !list !hidden
DataScoreStorage ** store !list !len="st_" !hidden
Histogram * his
double score_level // passed into should_store function
boolean (*should_store)(int given_score,double internal_score_level) !func
float (*score_to_his)(int given_score) !func
int report_level // number of sequences to report on
long total // total number of scores (duplicated info in histogram)
%info
Holds the information about a db search.
Meant to be very lightweight
The histogram is carried for on-the-fly histogram storage outside
of the database. The idea is that the function should_store will
tell whether the datascore structure should be stored (if it is
NULL, it is always stored). The score_to_his function maps the
score in datascore to the float in Histogram, allowing the scoring
system of the search method to be on a different basis to the
scoring system of the histogram. For most times, this is going to
be Score2Bits
To prevent too much dependency, the 'standard' way of making a
histogram that has a bits cut off level is done using functions
in the dynlibcross module (cross code), as it needs both Hscore and
Probability. You should read dynlibcross module for the constructors
for Hscore
%%
api
object Hscore
des free_Hscore
func minimum_score_Hscore
func maximum_score_Hscore
func sort_Hscore_by_score
func length_datascore_Hscore
func get_datascore_Hscore
func get_score_Hscore
func get_evalue_Hscore
func basic_show_Hscore
endobject
object DataScore
des free_DataScore
endobject
object DataEntry
des free_DataEntry
endobject
func std_score_Hscore
endapi
%{
#include "hscore.h"
%func
This gives you a standard Hscore
module with a cutoff in score
%%
Hscore * std_score_Hscore(int cut_off,int report_stagger)
{
Hscore * out;
out = Hscore_alloc_std();
out->his = new_Histogram(-1000,1000,100);
out->score_level = cut_off;
out->should_store = raw_should_store_Hscore;
out->score_to_his = raw_score_to_his;
out->report_level = report_stagger;
return out;
}
%func
This function is for the Hscore std constructor,
%type internal
%%
boolean raw_should_store_Hscore(int score,double cutoff)
{
if( score > cutoff ) {
return TRUE;
}
return FALSE;
}
%func
This function is for the Hscore std constructor,
%type internal
%%
float raw_score_to_his(int score)
{
return score;
}
%func
Tells whether this score should be stored
or not. Also updates Histogram if needed
%%
boolean should_store_Hscore(Hscore * hs,int score)
{
hs->total++;
if( hs->report_level != -1 && (hs->total % hs->report_level == 0)) {
if( hs->len > 0) {
info("Done %d comparisons: last stored comparison was %s to %s",hs->total,hs->ds[hs->len-1]->query->name,hs->ds[hs->len-1]->target->name);
} else {
info("Done %d comparisons: No stored comparisons",hs->total);
}
}
if( hs->his != NULL && hs->score_to_his != NULL ) {
AddToHistogram(hs->his,(*hs->score_to_his)(score));
}
if( hs->should_store == NULL ) {
return TRUE;
}
return (*hs->should_store)(score,hs->score_level);
}
%func
Returns the number of datascores in the hscore
structure
%simple length
%arg
obj r Hscore object
%%
int length_datascore_Hscore(Hscore * obj)
{
return obj->len;
}
%func
Returns the specific datascore held at this
position.
This requires a considerable amount of memory
duplication, so please dont process all your
results by looping through this.
%simple datascore
%arg
hs r Hscore object
i position to be read
return o New datascore object
%%
DataScore * get_datascore_Hscore(Hscore * hs,int i)
{
DataScore * out;
out = new_DataScore();
copy_DataEntry(hs->ds[i]->query,out->query);
copy_DataEntry(hs->ds[i]->target,out->target);
out->score = hs->ds[i]->score;
out->evalue = hs->ds[i]->evalue;
return out;
}
%func
Copies the info from one DataEntry to another
%type internal
%%
void copy_DataEntry(DataEntry * from,DataEntry * to)
{
int i;
to->name = stringalloc(from->name);
for(i=0;i<DATAENTRYSTDPOINTS;i++)
to->data[i] = from->data[i];
to->is_reversed = from->is_reversed;
to->byte_position = from->byte_position;
to->filename = from->filename; /* linked! */
}
%func
Returns the score of the specific datascore held at this position.
%simple score
%arg
hs r Hscore object
i position to be read
return score
%%
int get_score_Hscore(Hscore * hs,int i)
{
return hs->ds[i]->score;
}
%func
Returns the evalue of the specific datascore held at this position.
%simple evalue
%arg
hs r Hscore object
i position to be read
return evalue
%%
double get_evalue_Hscore(Hscore * hs,int i)
{
return hs->ds[i]->evalue;
}
%func
If a histogram is present, tries to fit the histogram and
then gives evalues to all the scores in the Hscore model
%%
boolean fit_Hscore_to_EVD(Hscore * hs,float guess_of_outliers)
{
int i;
if( hs->his == NULL ) {
warn("Your Hscore has no histogram structure, and so no EVD can be fitted");
return FALSE;
}
if( ExtremeValueFitHistogram(hs->his,TRUE,guess_of_outliers) == 0 ) {
warn("Extreme Value distribution is unable to be fitted. Sorry!");
return FALSE;
}
for(i=0;i<hs->len;i++) {
hs->ds[i]->evalue = ExtremeValueE((*hs->score_to_his)(hs->ds[i]->score),hs->his->param[EVD_MU],hs->his->param[EVD_LAMBDA],hs->his->total);
}
return TRUE;
}
%func
gets the minimum score from Hscore
%%
int minimum_score_Hscore(Hscore * hs)
{
int i;
int min;
if( hs->len == 0) {
warn("Can't get a minimum score with no entries");
return 0;
}
for(i=1,min=hs->ds[0]->score;i<hs->len;i++) {
if( min > hs->ds[i]->score ) {
min = hs->ds[i]->score;
}
}
return min;
}
%func
gets the maximum score from Hscore
%%
int maximum_score_Hscore(Hscore * hs)
{
int i;
int max;
if( hs->len == 0) {
warn("Can't get a minimum score with no entries");
return 0;
}
for(i=1,max=hs->ds[0]->score;i<hs->len;i++) {
if( max < hs->ds[i]->score ) {
max = hs->ds[i]->score;
}
}
return max;
}
%func
The most baby-talk showing of Hscore
%simple show
%arg
%%
void basic_show_Hscore(Hscore * hs,FILE * ofp)
{
int i;
if( hs == NULL ) {
warn("parsing in a NULL Hscore object - cannot show!");
fprintf(ofp,"parsing in a NULL Hscore object - cannot show!");
}
for(i=0;i<hs->len;i++) {
fprintf(ofp,"%3d Query: %12s Target: %12s Score %d\n",i,
hs->ds[i]->query->name == NULL ? "NoName" : hs->ds[i]->query->name,
hs->ds[i]->target->name == NULL ? "NoName" : hs->ds[i]->target->name,
hs->ds[i]->score);
}
}
%func
As it says, sorts the high score by its score
%arg
hs Hscore to be sorted
%%
void sort_Hscore_by_score(Hscore * hs)
{
sort_Hscore(hs,compare_DataScore_by_score);
}
%func
Used to compare two datascores for
/sort_Hscore_by_score
%type internal
%arg
%%
int compare_DataScore_by_score(DataScore * one,DataScore * two)
{
if( one->score == two->score ) {
if( one->query != NULL && one->query->name != NULL && two->query != NULL && two->query->name != NULL )
return strcmp(one->query->name,two->query->name);
else return 1;
}
return two->score - one->score;
}
%func
The best way to make a new DataScore.
Allocates the query and target DataEntry structures
as well as the DataScore structure.
%type internal
%arg
%%
DataScore * new_DataScore(void)
{
DataScore * ds;
ds = DataScore_alloc();
ds->query = DataEntry_alloc();
ds->target = DataEntry_alloc();
return ds;
}
%func
Gets a new DataScore from Storage
%type internal
%%
DataScore * new_DataScore_from_storage(Hscore * hs)
{
DataScoreStorage * new;
DataScoreStorage * curr;
if( hs->st_len == 0 ) {
new = new_DataScoreStorage();
if( new == NULL ) {
warn("could not make initial data score storage!");
return NULL;
}
add_st_Hscore(hs,new);
curr = new;
} else {
curr = hs->store[hs->st_len-1];
if( curr->curr_pos == DATASCORESTORAGE_LENGTH ) {
new = new_DataScoreStorage();
if( new == NULL ) {
warn("could not make data score storage block %d!",hs->st_len-1);
return NULL;
}
add_st_Hscore(hs,new);
curr = new;
}
}
return &curr->score_array[curr->curr_pos++];
}
%func
Correctly handles destruction of a datascore
%%
!deconstructor
DataScore * free_DataScore(DataScore * obj)
{
if( obj->is_stored == 1 ) {
return NULL; /* don't free! */
}
if( obj->dynamite_hard_link > 1 ) {
obj->dynamite_hard_link--;
return NULL;
}
if( obj->query != NULL )
free_DataEntry(obj->query);
if( obj->target != NULL )
free_DataEntry(obj->target);
return NULL;
}
%func
Correctly handles destruction of DataScoreStorage, by
freeing members in data storage
%%
!deconstructor
DataScoreStorage * free_DataScoreStorage(DataScoreStorage * obj)
{
int i;
for(i=0;i<obj->curr_pos;i++) {
if( obj->query_array[i].name != NULL ) {
ckfree(obj->query_array[i].name);
}
if( obj->target_array[i].name != NULL ) {
ckfree(obj->target_array[i].name);
}
}
ckfree(obj);
return NULL;
}
%func
Makes a new DataScoreStorage with all the pointers connected correctly
%%
DataScoreStorage * new_DataScoreStorage(void)
{
DataScoreStorage * out;
int i;
out = DataScoreStorage_alloc();
if( out == NULL ) {
warn("Unable to make a new DataScoreStorage block with blocksize %d",DATASCORESTORAGE_LENGTH);
return NULL;
}
for(i=0;i<DATASCORESTORAGE_LENGTH;i++) {
out->score_array[i].query = &out->query_array[i];
out->score_array[i].target = &out->target_array[i];
out->score_array[i].is_stored = 1;
}
return out;
}
%}
|