[BioPython] Trie API for serializing a trie

Virginia S. Lopez vsla at wanadoo.es
Sun Apr 13 20:43:44 UTC 2008


Hi all,



I'd like to use the trie in Bio/trie.h from other C program (which I
access using python). I need to implement a save() and load() methods,
that will call serialize() and deserialize() in Bio/trie.c. I'm having
problems to get this working, it compiles fine, and I can serialize
data to disk, but I can't deserialize data to create a trie from the
file. I don't know wheter the problem is in how I write trie's data to
disk, or when I load it from disk, or both. I'm new to C and I possibly
do a silly mistake. 



I'm copying some of my code below, and attach the full code. It takes one
argument that is a name of a txt file containing the keys (one per
line), and builds the trie, and saves it to file "savefile.txt" in
binary. (To test to load the trie from the saved file, I simply comment
a code block in main().)



I'd massively appreciate any suggestions about what I'm doing wrong.



Many thanks.



Virginia.


---------------------
/* Methods called when serializing trie data */

static int _write_to_handle(const void *towrite, const int length, void *handle)

{

    fwrite(&towrite, length, 1, (FILE *) handle);  

    return 1;    

}



static int _write_value_to_handle(const void *towrite, void *handle)

{

    fwrite(&towrite, sizeof(int), 1, handle);  //trie key values is an int

    return 1;

}



/* Methods called when deserializing trie data */

static int _read_from_handle(void *wasread, const int length, void *handle)

{  

     fread(&wasread, length, 1, (FILE *) handle);  

    return 1;    

}



static void * _read_value_from_handle(void *handle)   //returns a pointer to the value

{

  //In my tests, this method never gets called, because a problem happens with the data

 // read in function _read_from_handle(). No error given, deserialize() function quits.

   int *value;

    fread(&value, sizeof(int), 1, handle);  

    return value;

}



int trie_save(){   

  FILE *out = fopen("savefile.txt", "wb"); 

  int success=Trie_serialize(trie, _write_handle, _write_value_handle, (void *)out); 

  fclose(out);           

 return success;

}



int trie_load(){ 

  int success=1;

  FILE *in = fopen("savefile.txt", "rb"); 

  trie = Trie_deserialize(_read_handle, _read_value_handle,in);

  if(!trie) {

   success=0;

   printf("Error: trie could not be created from data.\n");

  }

  fclose(in);           

 return success;

}

-------------------------
Files in /Bio/:
- trie.h 
http://www.codase.com/search/showtext?file=L2dlbnRvbzIvdmFyL3RtcC9yZXBvcy9jb2Rhc2UuYy9iaW9weXRob24tMS40MGIvd29yay9iaW9weXRob24tMS40MGIvQmlvL3RyaWUuaA==
- trie.c 
http://www.codase.com/search/showtext?file=L2dlbnRvbzIvdmFyL3RtcC9yZXBvcy9jb2Rhc2UuYy9iaW9weXRob24tMS40MGIvd29yay9iaW9weXRob24tMS40MGIvQmlvL3RyaWUuYw==
- triemodule.c (Interface of the trie API for Python. I looked into this
file to see how to do serialization from C, but I can't understand it)
http://www.codase.com/search/showtext?file=L2dlbnRvbzIvdmFyL3RtcC9yZXBvcy9jb2Rhc2UuYy9iaW9weXRob24tMS40MGIvd29yay9iaW9weXRob24tMS40MGIvQmlvL3RyaWVtb2R1bGUuYw==









"Miles de personas buscan a alguien como tú. ¿Quieres conocerlas? Las reglas del juego han cambiado."    http://ad.doubleclick.net/clk;198970522;7829128;x?http://www.meetic.es/index.php?mtcmk=016799
-------------- next part --------------
A non-text attachment was scrubbed...
Name: trie_test.c
Type: application/octet-stream
Size: 3054 bytes
Desc: not available
URL: <http://lists.open-bio.org/pipermail/biopython/attachments/20080413/ad547c1c/attachment-0002.obj>


More information about the Biopython mailing list