#include "../headers/utils.h"
Go to the source code of this file.
Functions | |
void | start_rand_generator (void) |
float | rand_uniform (float min, float max) |
tab_str * | f_readl (const char *fpath, int nchar_max) |
float | float_get_max_in_2D_array (float **t, size_t n, int col) |
float | float_get_min_in_2D_array (float **t, size_t n, int col) |
void | free_tab_str (tab_str *tstr) |
void | print_tab_str (tab_str *strings) |
int | str_is_number (const char *str, const int sign) |
int | str_is_float (const char *str, const int sign) |
int | in_tab (int *tab, int size, int val) |
int | index_of (int *tab, int size, int val) |
void | str_trim (char *str) |
void | extract_path (char *str, char *dest) |
void | extract_ext (char *str, char *dest) |
void | remove_path (char *str) |
void | remove_ext (char *str) |
FILE * | fopen_pdb_check_case (char *name, const char *mode) |
tab_str * | str_split (const char *str, const int sep) |
Variables | |
static int | ST_is_rand_init = 0 |
void extract_ext | ( | char * | str, | |
char * | dest | |||
) |
## FUNCTION: extract_ext
## SPECIFICATION: Get rid of the extension of a string (.pdb eg.)
## PARAMETRES: @ char *str: String to deal with @ char *dest : OUTPUT The destination string
## RETURN:
Definition at line 601 of file utils.c.
00602 { 00603 char *pstr = str, 00604 *last_dot = NULL ; 00605 00606 while(*pstr) { 00607 /* Advance in the path name while it is possible */ 00608 if(*pstr == '.') { 00609 /* If we encounter a '/', save its position */ 00610 last_dot = pstr ; 00611 } 00612 pstr ++ ; 00613 } 00614 00615 if(last_dot) { 00616 strcpy(dest, last_dot+1) ; 00617 } 00618 else { 00619 /* If no '/' has been found, just return a dot as current folder */ 00620 dest[0] = '\0' ; 00621 } 00622 }
void extract_path | ( | char * | str, | |
char * | dest | |||
) |
## FUNCTION: extract_path
## SPECIFICATION: Extract path from a string
## PARAMETRES: @ char *str : String to deal with @ char *dest : OUTPUT The destination string
## RETURN:
Definition at line 558 of file utils.c.
Referenced by write_out_fpocket(), and write_out_fpocket_DB().
00559 { 00560 char sav ; 00561 char *pstr = str, 00562 *last_backsl = NULL ; 00563 00564 while(*pstr) { 00565 /* Advance in the path name while it is possible */ 00566 if(*pstr == '/') { 00567 /* If we encounter a '/', save its position */ 00568 last_backsl = pstr ; 00569 } 00570 pstr ++ ; 00571 } 00572 00573 if(last_backsl) { 00574 /* If we have found one '/' at least, copy the path */ 00575 sav = *(last_backsl) ; 00576 (*last_backsl) = '\0' ; 00577 00578 strcpy(dest, str) ; 00579 (*last_backsl) = sav ; 00580 } 00581 else { 00582 /* If no '/' has been found, just return a dot as current folder */ 00583 dest[0] = '\0' ; 00584 } 00585 }
tab_str* f_readl | ( | const char * | fpath, | |
int | nchar_max | |||
) |
## FUNCTION: tab_str* f_readl(const char fpath[], int nchar_max)
## SPECIFICATION: Read file line and store them in a tab_str structure. The function skip empty lines.
## PARAMETRES: @ const char *fpath : Full path of the file. @ int nchar_max : A number giving the max number of caractere in each line.
## RETURN: tab_str* : Pointer to the sab_str structure containing lines of the file.
Definition at line 156 of file utils.c.
References my_free(), my_malloc(), tab_str::nb_str, and tab_str::t_str.
00157 { 00158 /* Variable declaration */ 00159 00160 FILE *f ; 00161 int i, n, 00162 nb_string ; 00163 char *cline, 00164 **f_lines ; 00165 tab_str *lines ; 00166 00167 /* Variable initialisation */ 00168 00169 i = nb_string = 0 ; 00170 cline = (char *) my_malloc(nchar_max*sizeof(char)) ; 00171 00172 /* How many lines is there in the file? */ 00173 00174 f = fopen(fpath, "r") ; 00175 if(f == NULL) { 00176 my_free(cline) ; 00177 return NULL ; 00178 } 00179 00180 while(fgets(cline, nchar_max, f) != NULL) { 00181 if(strcmp("\n", cline) != 0) { 00182 nb_string ++ ; 00183 } 00184 } 00185 fclose(f) ; 00186 00187 /* Once we have the number of lines, lets allocate memory and get the lines */ 00188 00189 f = fopen(fpath, "r") ; 00190 if(f == NULL) { 00191 my_free(cline) ; 00192 return NULL ; 00193 } 00194 00195 lines = (tab_str *)my_malloc(sizeof(tab_str)) ; 00196 f_lines = (char **)my_malloc(nb_string*sizeof(char*)) ; 00197 00198 /* Getting lines. */ 00199 00200 while(fgets(cline, nchar_max, f) != NULL) { 00201 if(strcmp("\n", cline) != 0) { 00202 n = strlen(cline) ; 00203 if(cline[n-1] == '\n') { 00204 n-- ; 00205 cline[n] = '\0' ; 00206 } 00207 00208 char *line =(char *) my_malloc((n+1)*sizeof(char)) ; 00209 memcpy (line, cline, n+1); 00210 00211 f_lines[i] = line ; 00212 i++ ; 00213 } 00214 } 00215 00216 lines->nb_str = nb_string ; 00217 lines->t_str = f_lines ; 00218 00219 /* Free memory and close file */ 00220 00221 fclose(f) ; 00222 my_free(cline); 00223 00224 return lines ; 00225 }
float float_get_max_in_2D_array | ( | float ** | t, | |
size_t | n, | |||
int | col | |||
) |
## FUNCTION: float_get_max_in_2D_array
## SPECIFICATION: receive the maximum value in a give column in a 2D float array
## PARAMETRES: @ **t float : pointer to pointer to float (2D float array) @ n int : size of the array (number of lines) @ col int : column on which the maximum is to extract
## RETURN: float
Definition at line 243 of file utils.c.
00243 { 00244 float c=0.0,m=-1e15; 00245 size_t i; 00246 for(i=0;i<n;i++){ 00247 c=t[i][col]; 00248 if(c>m)m=c; 00249 } 00250 return m; 00251 }
float float_get_min_in_2D_array | ( | float ** | t, | |
size_t | n, | |||
int | col | |||
) |
## FUNCTION: float_get_min_in_2D_array
## SPECIFICATION: receive the minimum value in a give column in a 2D float array
## PARAMETRES: @ **t float : pointer to pointer to float (2D float array) @ n int : size of the array (number of lines) @ col int : column on which the minimum is to extract
## RETURN: float
Definition at line 269 of file utils.c.
00269 { 00270 float c=0.0,m=1e15; 00271 size_t i; 00272 for(i=0;i<n;i++){ 00273 c=t[i][col]; 00274 if(c<m)m=c; 00275 } 00276 return m; 00277 }
FILE* fopen_pdb_check_case | ( | char * | name, | |
const char * | mode | |||
) |
## FUNCTION: fopen_pdb_check_case
## SPECIFICATION: Try to open a pdb file. If the open failed, put the 4 letter before extention at the lower case and try again. This function assume that the file name has the format path/file.pdb !
## PARAMETERS: @ char *name : The string to parse @ const char *mode : Opening mode
## RETURN: The file, NULL if the openning fails.
Definition at line 714 of file utils.c.
Referenced by add_complexe(), add_prot(), add_snapshot(), and rpdb_open().
00715 { 00716 FILE *f = fopen(name, mode) ; 00717 if(!f) { 00718 int len = strlen(name) ; 00719 name[len-5] = toupper(name[len-5]); 00720 name[len-6] = toupper(name[len-6]); 00721 name[len-7] = toupper(name[len-7]); 00722 name[len-8] = toupper(name[len-8]); 00723 00724 f = fopen(name, mode) ; 00725 if(!f) { 00726 name[len-5] = tolower(name[len-5]); 00727 name[len-6] = tolower(name[len-6]); 00728 name[len-7] = tolower(name[len-7]); 00729 name[len-8] = tolower(name[len-8]); 00730 f = fopen(name, mode) ; 00731 00732 } 00733 } 00734 00735 return f ; 00736 00737 }
void free_tab_str | ( | tab_str * | tstr | ) |
## FUNCTION: free_tab_str
## SPECIFICATION: Free the given structure
## PARAMETRES: @ tab_str* strings : Pointer to the tab_str to print
## RETURN: void
Definition at line 294 of file utils.c.
References my_free(), tab_str::nb_str, and tab_str::t_str.
00295 { 00296 if(tstr) { 00297 int i ; 00298 if(tstr->t_str) { 00299 for(i = 0 ; i < tstr->nb_str ; i++) { 00300 if(tstr->t_str[i]) { 00301 my_free(tstr->t_str[i]) ; 00302 tstr->t_str[i] = NULL ; 00303 } 00304 } 00305 my_free(tstr->t_str) ; 00306 } 00307 00308 my_free(tstr) ; 00309 } 00310 }
int in_tab | ( | int * | tab, | |
int | size, | |||
int | val | |||
) |
## FUNCTION: in_tab
## SPECIFICATION: Check if val is present in tab.
## PARAMETRES: @ int *tab: Tab @ int size: Size of the tab @ int val: Value to check
## RETURN: int: 1 if val is in tab, 0 if not
Definition at line 475 of file utils.c.
Referenced by count_pocket_contacted_atms(), get_pocket_contacted_atms(), get_surrounding_atoms_idx(), get_unique_atoms(), get_vert_contacted_atms(), and set_atom_based_descriptors().
00476 { 00477 if(tab) { 00478 int i ; 00479 for(i = 0 ; i < size ; i++) { 00480 if(tab[i] == val) return 1 ; 00481 } 00482 } 00483 00484 return 0 ; 00485 }
int index_of | ( | int * | tab, | |
int | size, | |||
int | val | |||
) |
## FUNCTION: index_of
## SPECIFICATION: Check if val is present in tab and return its index if so
## PARAMETRES: @ int *tab: Tab @ int size: Size of the tab @ int val: Value to check
## RETURN: int: index if val is in tab, -1 if not
Definition at line 503 of file utils.c.
00504 { 00505 if(tab) { 00506 int i; 00507 for(i = 0 ; i < size ; i++) { 00508 if(val == tab[i]) return i ; 00509 } 00510 } 00511 00512 return -1 ; 00513 }
void print_tab_str | ( | tab_str * | strings | ) |
## FUNCTION: print_tab_str
## SPECIFICATION: Print strings contained in the given tab_str.
## PARAMETRES: @ tab_str* strings : Pointer to the tab_str to print
## RETURN: void
Definition at line 326 of file utils.c.
References tab_str::nb_str, and tab_str::t_str.
00327 { 00328 if(strings) { 00329 int i ; 00330 char **strs = strings->t_str ; 00331 00332 printf("\n-- String tab: \n"); 00333 for (i = 0 ; i < strings->nb_str ; i++) { 00334 fprintf(stdout, "<%s>\n", strs[i]) ; 00335 } 00336 printf("--\n") ; 00337 } 00338 else { 00339 fprintf(stderr, "! Argument NULL in print_tab_str().\n"); 00340 } 00341 }
float rand_uniform | ( | float | min, | |
float | max | |||
) |
## FONCTION: rand_uniform
## SPECIFICATION: Generate a random number between 0 and 1 using a uniform distribution.
## PARAMETRES: @ float min : Lower boundary @ float max : Upper boundary
## RETURN: double: A uniform random number between min and max
Definition at line 123 of file utils.c.
References ST_is_rand_init, and start_rand_generator().
Referenced by get_mol_volume_ptr(), get_verts_volume_ptr(), set_mc_overlap_volume(), and set_pocket_mtvolume().
00124 { 00125 if(ST_is_rand_init == 0){ 00126 start_rand_generator() ; 00127 } 00128 00129 float rnd = 0.0 ; 00130 00131 #ifdef MD_USE_GSL /* GSL */ 00132 rnd = gsl_rng_uniform(ST_r) * (max-min) ; 00133 #else /* /GSL */ 00134 rnd = ((float)rand()/(float)RAND_MAX) * (max-min) ; 00135 #endif 00136 00137 return min+rnd ; 00138 }
void remove_ext | ( | char * | str | ) |
## FUNCTION: remove_ext
## SPECIFICATION: Remove the extention of a given string
## PARAMETRES: @ char *str: INOUT String to deal with
## RETURN: The input string is modified
Definition at line 678 of file utils.c.
Referenced by get_dpocket_args(), get_mdpocket_args(), mdpocket_detect(), write_out_fpocket(), write_out_fpocket_DB(), write_pymol(), and write_vmd().
00679 { 00680 char *pstr = str, 00681 *last_dot = NULL ; 00682 00683 while(*pstr) { 00684 /* Advance in the path name while it is possible */ 00685 if(*pstr == '.') { 00686 /* If we encounter a '/', save its position */ 00687 last_dot = pstr ; 00688 } 00689 pstr ++ ; 00690 } 00691 00692 if(last_dot) { 00693 *last_dot = '\0' ; 00694 } 00695 }
void remove_path | ( | char * | str | ) |
## FUNCTION: remove_path
## SPECIFICATION: Remove the path from a string
## PARAMETRES: @ char *str: INOUT String to deal with
## RETURN: The input string is modified
Definition at line 638 of file utils.c.
Referenced by mdpocket_detect(), test_fpocket(), write_out_fpocket(), write_out_fpocket_DB(), write_pymol(), and write_vmd().
00639 { 00640 int i, filelen ; 00641 char *pstr = str, 00642 *last_backsl = NULL ; 00643 00644 while(*pstr) { 00645 /* Advance in the path name while it is possible */ 00646 if(*pstr == '/') { 00647 /* If we encounter a '/', save its position */ 00648 last_backsl = pstr ; 00649 } 00650 pstr = pstr + 1 ; 00651 } 00652 00653 if(last_backsl) { 00654 /* If we have found one '/' at least, copy the path, else dont do anything */ 00655 last_backsl = last_backsl + 1 ; 00656 filelen = strlen(last_backsl) ; 00657 for(i = 0 ; i < filelen ; i++) { 00658 str[i] = *(last_backsl+i) ; 00659 } 00660 str[i] = '\0' ; 00661 } 00662 }
void start_rand_generator | ( | void | ) |
## FONCTION: start_rand_generator
## SPECIFICATION: Initialize generator. This initialisation depends on the library to use.
## PARAMETRES:
## RETURN: void
Definition at line 81 of file utils.c.
References ST_is_rand_init.
Referenced by rand_uniform().
00082 { 00083 if(ST_is_rand_init == 0) { 00084 00085 #ifdef MD_USE_GSL /* use GSL if defined */ 00086 /* fprintf(stdout, "> GSL generator used\n"); */ 00087 if(ST_r != NULL) { 00088 gsl_rng_free(ST_r); 00089 } 00090 00091 gsl_rng_default_seed = time(NULL); 00092 00093 const gsl_rng_type *T = M_GEN_MTWISTER ; 00094 ST_r = gsl_rng_alloc(T); 00095 gsl_rng_set (ST_r, gsl_rng_default_seed); 00096 00097 #else /* /GSL */ 00098 00099 /* fprintf(stdout, "> Standard C generator used\n"); */ 00100 srand((int)time(NULL)); 00101 00102 #endif 00103 ST_is_rand_init = 1 ; 00104 } 00105 }
int str_is_float | ( | const char * | str, | |
const int | sign | |||
) |
## FUNCTION: str_is_float
## SPECIFICATION: Check if the string given in argument is a valid float.
## PARAMETRES: @ char *str : The string to deal with @ const int sign : The first caractere is the sign?
## RETURN: int: 1 if its a valid float, 0 else
Definition at line 412 of file utils.c.
Referenced by parse_asph_max_size(), parse_asph_min_size(), parse_clust_max_dist(), parse_dist_crit(), parse_lig_neigh_dist(), parse_mc_niter(), parse_refine_dist(), parse_refine_minaap(), and parse_sclust_max_dist().
00413 { 00414 int ok = 0 ; 00415 int nb_dot = 0 ; 00416 00417 if (str != NULL) { 00418 const char *p = str ; 00419 int c = *p ; 00420 00421 /* Checkthe first caractere if the sign has to be taken into account */ 00422 if (sign) { 00423 00424 if(isdigit (c) || ((c == '+' || c == '-') && str[1] != 0)) { 00425 ok = 1 ; 00426 } 00427 p++; 00428 } 00429 else { 00430 ok = 1 ; 00431 } 00432 00433 if (ok) { 00434 while (*p != 0) { 00435 if (!isdigit (*p)) { 00436 if((*p) == '.') { 00437 nb_dot++ ; 00438 if(nb_dot > 1) { 00439 ok = 0; 00440 break ; 00441 } 00442 } 00443 else { 00444 ok = 0; 00445 break ; 00446 } 00447 } 00448 p++; 00449 } 00450 } 00451 } 00452 else { 00453 fprintf(stderr, "! Argument NULL in str_is_number().\n"); 00454 } 00455 00456 return ok; 00457 }
int str_is_number | ( | const char * | str, | |
const int | sign | |||
) |
## FUNCTION: str_is_number
## SPECIFICATION: Check if the string given in argument is a number.
## PARAMETRES: @ char *str : The string to deal with @ const int sign : The first caractere is the sign?
## RETURN: int: 1 if its a valid number, 0 else
Definition at line 359 of file utils.c.
Referenced by is_N(), parse_basic_vol_div(), parse_min_apol_neigh(), parse_min_pock_nb_asph(), and parse_sclust_min_nneigh().
00360 { 00361 int ok = 0 ; 00362 00363 if (str != NULL) { 00364 const char *p = str ; 00365 int c = *p ; 00366 00367 /* Checkthe first caractere if the sign has to be taken into account */ 00368 if (sign) { 00369 00370 if(isdigit (c) || ((c == '+' || c == '-') && str[1] != 0)) { 00371 ok = 1 ; 00372 } 00373 p++; 00374 } 00375 else { 00376 ok = 1 ; 00377 } 00378 00379 if (ok) { 00380 while (*p != 0) { 00381 if (!isdigit (*p)) { 00382 ok = 0; 00383 break ; 00384 } 00385 p++; 00386 } 00387 } 00388 } 00389 else { 00390 fprintf(stderr, "! Argument NULL in str_is_number().\n"); 00391 } 00392 00393 return ok; 00394 }
tab_str* str_split | ( | const char * | str, | |
const int | sep | |||
) |
## FONCTION: tab_str* str_split_memopt(const char *str, const char sep)
## SPECIFICATION: Split the string given using a char separator. Every token will be stored in a tab_str structure which will be returned by the function. Empty tokens (two consecutives separator) will be stored as an empty string containing only the NULL caractere.
We optimise here the memory, and allocate the exact memory for each token.
!! MEMORY OF THE RETURNED ARGUMENT MUST BE FREED BY CALLING free_tab_str() !!
COULD BE OPTIMISED.
## PARAMETRES: @ const char *str: The string to deal with @ const char sep: The separator
## RETURN: tab_str*: A pointer to a structure tab_str which will contain all elements of the string.
Definition at line 764 of file utils.c.
References my_calloc(), tab_str::nb_str, and tab_str::t_str.
00765 { 00766 tab_str *ts = (tab_str*) my_calloc(1, sizeof(tab_str)) ; 00767 00768 const char *pstr = str ; // A temp pointer to str 00769 int n = 1 ; // At least one token (no separator in the string) 00770 00771 // Count the number of token 00772 00773 while(*pstr) { 00774 if(*pstr == sep) { 00775 n++ ; 00776 } 00777 pstr ++ ; 00778 } 00779 00780 ts->nb_str = n ; 00781 ts->t_str = (char**)my_calloc(n, sizeof(char*)) ; 00782 00783 // If there is more than one token, split the string 00784 00785 if(n > 1) { 00786 char **ptab_str = ts->t_str ; 00787 char *s_pctok = NULL ; // A pointer to the current created token 00788 const char *s_beg = str ; // A pointer used to copy each token from s_beg to next separator 00789 00790 size_t tok_i = 0, 00791 size_tok = 1 ; 00792 00793 pstr = str ; 00794 00795 // Allocate exact memory and copy each tokens 00796 00797 while(*pstr) { 00798 if(*pstr == sep) { 00799 ptab_str[tok_i] = (char*)my_calloc(size_tok, sizeof(char)) ; 00800 s_pctok = ptab_str[tok_i] ; 00801 00802 while(*s_beg != *pstr) { 00803 *s_pctok = *s_beg ; 00804 s_pctok ++ ; 00805 s_beg ++ ; 00806 } 00807 *s_pctok = '\0' ; 00808 00809 size_tok = 0 ; 00810 tok_i ++ ; 00811 s_beg ++ ; // Skip the separator for next token 00812 } 00813 size_tok ++ ; 00814 pstr ++ ; 00815 } 00816 00817 // Copy the last token 00818 00819 ptab_str[tok_i] = (char*)my_calloc(size_tok, sizeof(char)) ; 00820 s_pctok = ptab_str[tok_i] ; 00821 while(*s_beg) { 00822 *s_pctok = *s_beg ; 00823 s_pctok ++ ; 00824 s_beg ++ ; 00825 } 00826 *s_pctok = '\0' ; 00827 } 00828 else { 00829 // One token only, just copy the original string 00830 ts->t_str[0] = (char*)my_calloc(strlen(str), sizeof(char)); 00831 strcpy(ts->t_str[0], str); 00832 } 00833 00834 return ts ; 00835 00836 }
void str_trim | ( | char * | str | ) |
## FUNCTION: str_trim
## SPECIFICATION: Remove spaces from a given string
## PARAMETRES: @ char *str: String to deal with
## RETURN:
Definition at line 528 of file utils.c.
Referenced by guess_element(), is_valid_element(), is_valid_nucl_acid_element(), is_valid_prot_element(), load_pdb_line(), rpdb_extract_atm_resname(), and rpdb_extract_pdb_atom().
00529 { 00530 int i, len; 00531 00532 len = strlen(str); 00533 while (len > 0 && str[len-1] == ' ') { 00534 str[len-1] = '\0'; 00535 len--; 00536 } 00537 00538 while (len > 0 && str[0] == ' ') { 00539 for (i=0; i < len; i++) str[i] = str[i+1]; 00540 len--; 00541 } 00542 }
int ST_is_rand_init = 0 [static] |
Says wether we have seeded the generator.
Definition at line 62 of file utils.c.
Referenced by rand_uniform(), and start_rand_generator().