#ifndef INCLUDED_SLS_ALP_DATA #define INCLUDED_SLS_ALP_DATA /* $Id: $ * =========================================================================== * * PUBLIC DOMAIN NOTICE * National Center for Biotechnology Information * * This software/database is a "United States Government Work" under the * terms of the United States Copyright Act. It was written as part of * the author's offical duties as a United States Government employee and * thus cannot be copyrighted. This software/database is freely available * to the public for use. The National Library of Medicine and the U.S. * Government have not placed any restriction on its use or reproduction. * * Although all reasonable efforts have been taken to ensure the accuracy * and reliability of the software and data, the NLM and the U.S. * Government do not and cannot warrant the performance or results that * may be obtained by using this software or data. The NLM and the U.S. * Government disclaim all warranties, express or implied, including * warranties of performance, merchantability or fitness for any particular * purpose. * * Please cite the author in any work or product based on this material. * * ===========================================================================*/ /***************************************************************************** File name: sls_alp_data.hpp Author: Sergey Sheetlin, Martin Frith Contents: Contains input data ******************************************************************************/ #include "sls_basic.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef _MSC_VER //UNIX program #else #define _CRTDBG_MAP_ALLOC #include #endif #include "sls_alp_regression.hpp" #include "njn_random.hpp" #include "njn_uniform.hpp" const double mb_bytes=1048576.0; namespace Sls { struct struct_for_randomization { long int d_random_seed; std::vector d_first_stage_preliminary_realizations_numbers_ALP; std::vector d_preliminary_realizations_numbers_ALP; std::vector d_preliminary_realizations_numbers_killing; long int d_total_realizations_number_with_ALP; long int d_total_realizations_number_with_killing; }; struct error_for_single_realization//struct to handle exceptions during calclation of single realization { std::string st; error_for_single_realization(){}; }; struct data_for_lambda_equation//struct for lambda_equation { long int d_number_of_AA;//number of AA long int** d_smatr;//scoring matrix double *d_RR1;//AA probabilities double *d_RR2;//AA probabilities }; class alp_data; template class array_positive{ public: array_positive(alp_data *alp_data_)// constructor { d_elem=NULL; d_alp_data=alp_data_; if(!d_alp_data) { throw error("Unexpected error\n",4); }; d_dim=-1; d_step=10; } ~array_positive(); void increment_array(long int ind_); inline void set_elem( long int ind_, T elem_) { if(ind_>d_dim) { increment_array(ind_); }; d_elem[ind_]=elem_; } inline void increase_elem_by_1( long int ind_) { if(ind_>d_dim) { increment_array(ind_); }; d_elem[ind_]++; } inline void increase_elem_by_x( long int ind_, T x_) { if(ind_>d_dim) { increment_array(ind_); }; d_elem[ind_]+=x_; } public: long int d_step; long int d_dim;//dimension of the array is d_dim+1 T * d_elem; alp_data *d_alp_data;//initial data }; template class array{ public: array(alp_data *alp_data_)// constructor { d_elem=NULL; d_alp_data=alp_data_; d_dim=-1; d_ind0=0; d_step=10; d_dim_plus_d_ind0=d_dim+d_ind0; } ~array(); void increment_array_on_the_right(long int ind_); void increment_array_on_the_left(long int ind_); void set_elems(const array *a_); inline void set_elem( long int ind_, T elem_) { if(ind_>d_dim_plus_d_ind0) { increment_array_on_the_right(ind_); }; if(ind_d_dim_plus_d_ind0) { increment_array_on_the_right(ind_); }; if(ind_ (0,1); } static void read_smatr( std::string smatr_file_name_, long int **&smatr_, long int &number_of_AA_smatr_); void check_out_file( std::string out_file_name_); static std::string long_to_string(//convert interer ot string long int number_); static char digit_to_string(//convert interer ot string long int digit_); static bool the_value_is_double( std::string str_, double &val_); static bool the_value_is_long( std::string str_, long int &val_); static void read_RR( std::string RR_file_name_, double *&RR_, double *&RR_sum_, long int *&RR_sum_elements_, long int &number_of_AA_RR_); static void read_RR( std::string RR_file_name_, double *&RR_, long int &number_of_AA_RR_); static void calculate_RR_sum( double *RR_, long int number_of_AA_RR_, double *&RR_sum_, long int *&RR_sum_elements_); static void check_RR_sum( double sum_tmp_, long int number_of_AA_RR_, std::string RR_file_name_); template static void get_memory_for_matrix( long int dim_, T ** &matr_, alp_data *alp_data_=NULL) { matr_=NULL; try { long int i; matr_=new T *[dim_]; assert_mem(matr_); for(i=0;id_memory_size_in_MB+=(double)sizeof(T)*(double)dim_*(double)dim_/mb_bytes; }; } catch (...) { if(matr_) { long int i; for(i=0;i static void delete_memory_for_matrix( long int dim_, T ** &matr_, alp_data *alp_data_=NULL) { long int i; if(matr_) { for(i=0;id_memory_size_in_MB-=(double)sizeof(T)*(double)dim_*(double)dim_/mb_bytes; }; } static long int random_long( double value_, long int dim_); template static T random_long( double value_, long int dim_, double *sum_distr_, T* elements_)//sum_distr_[dim_-1] must be equal to 1 { if(value_<0||value_>1) { throw error("Unexpected error in alp_data::random_long\n",4); }; long int v1=0; long int v2=dim_; while(v2-v1>1) { long int v3=(long int)(Sls::alp_data::round(double(v2+v1)/2.0)); if(sum_distr_[v3-1]==value_) { v1=v3-1; v2=v3; break; }; if(sum_distr_[v3-1]>value_) { v2=v3; } else { v1=v3; }; }; if(elements_) { long int v2_1=v2-1; long int v2_minus=-1; long int j; for(j=v2_1;j>=1;j--) { if(sum_distr_[j]!=sum_distr_[j-1]) { v2_minus=j; break; }; }; if(v2_minus<0) { if(sum_distr_[0]>0) { v2_minus=0; }; }; if(v2_minus>=0) { return elements_[v2_minus]; }; long int v2_plus=-1; for(j=v2;j array_positive::~array_positive() { delete[]d_elem;d_elem=NULL; if(d_alp_data) { d_alp_data->d_memory_size_in_MB-=(double)sizeof(T)*(double)(d_dim+1)/mb_bytes; }; } template void array_positive::increment_array(long int ind_) { T *d_elem_new=NULL; try { long int o_dim=d_dim; do{ d_dim+=d_step; }while(ind_>d_dim); long int jump=d_dim-o_dim; d_elem_new=new T[d_dim+1]; alp_data::assert_mem(d_elem_new); long int i; for(i=0;id_memory_size_in_MB+=(double)sizeof(T)*(double)jump/mb_bytes; }; d_elem=d_elem_new;d_elem_new=NULL; } catch (...) { delete[]d_elem_new;d_elem_new=NULL; throw; }; } //array functions template array::~array() { delete[]d_elem;d_elem=NULL; if(d_alp_data) { d_alp_data->d_memory_size_in_MB-=(double)sizeof(T)*(double)(d_dim+1)/mb_bytes; }; } template void array::set_elems(const array *a_) { long int a0=a_->d_ind0; long int a1=a_->d_dim_plus_d_ind0; if(a0>a1)return; while(a1>d_dim_plus_d_ind0) { d_dim_plus_d_ind0+=d_step; }; while(a0d_memory_size_in_MB+=(double)sizeof(T)*(double)(d_dim+1)/mb_bytes; }; long int i; for(i=a0;i<=a1;i++) { d_elem[i-d_ind0]=a_->d_elem[i-a0]; } } template void array::increment_array_on_the_right(long int ind_) { bool ee_error_flag=false; error ee_error("",0); T *d_elem_new=NULL; try { try { long int o_dim=d_dim; do{ d_dim+=d_step; d_dim_plus_d_ind0+=d_step; }while(ind_>d_dim_plus_d_ind0); long int jump=d_dim-o_dim; d_elem_new=new T[d_dim+1]; alp_data::assert_mem(d_elem_new); long int i; for(i=0;id_memory_size_in_MB+=(double)sizeof(T)*(double)jump/mb_bytes; }; delete[]d_elem;d_elem=NULL; d_elem=d_elem_new;d_elem_new=NULL; } catch (error er) { ee_error_flag=true; ee_error=er; }; } catch (...) { ee_error_flag=true; ee_error=error("Internal error in the program\n",4); }; //memory release if(ee_error_flag) { delete[]d_elem_new;d_elem_new=NULL; throw error(ee_error.st,ee_error.error_code); }; } template void array::increment_array_on_the_left(long int ind_) { T *d_elem_new=NULL; try { long int o_dim=d_dim; do{ d_dim+=d_step; d_ind0-=d_step; }while(ind_d_memory_size_in_MB+=(double)sizeof(T)*(double)jump/mb_bytes; }; delete[]d_elem;d_elem=NULL; d_elem=d_elem_new;d_elem_new=NULL; } catch (...) { delete[]d_elem_new;d_elem_new=NULL; throw; }; } } #endif