Автор: doc58_81oB0t
Дата сообщения: 11.04.2007 08:43
проблема с памятью.. больше 36 таблиц не обрабатывает.. можно как-то решить или все переделывать?
Код:
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
const MAXLEN = 200;
const MAXMASS = 8000;
const int I = 200;
const int J = 200;
const double a = 3.8;
const double b = 0.7;
//------------------------------------------------------------------------------
//--------------------------Auxiliary functions---------------------------------
//---------------------Procedure of getting the substring-----------------------
void *substr(char *line, char *word, int startPos, int substrLen)
{
int i ;
for (i=0;i<startPos;i++)
{
line++;
}
for(i=0; i<substrLen; i++)
{
*word++ = *line++;
}
*word++ = '\0';
}
//---------------Function of getting the position of a symbol-------------------
int number(char *line, char *symbol)
{
int number;
int i;
char *symbol1;
symbol1 = new char[MAXLEN];
i = 0;
substr (line, symbol1, i, 1);
while ((*symbol1 != *symbol)&&(i!=strlen(line)))
{
i = i + 1;
substr(line, symbol1, i, 1);
}
if (i==strlen(line))
{
number = -1;
}
else {number = i;}
//delete symbol1;
return number;
}
//--------------------Procedure of shuffling the seqeuence----------------------
void shuffling(char *line1, char *line2)
{
int i, j, l, n, *mas1, *mas2;
char *line1copy;
line1copy = new char[MAXLEN];
line1copy = line1;
l = strlen(line1copy);
mas1 = new int[l];
mas2 = new int[l];
for (i=0; i<l; i++)
{
mas1[i] = i;
}
for (i=0; i<l; i++)
{
n = random(l-i);
mas2[i] = mas1[n];
for (j=n; j<l-i-1; j++)
{
mas1[j] = mas1[j+1];
}
}
for (i=0; i<l; i++)
{
line2[i] = line1[mas2[i]];
}
line2[l] = '\0';
for (i=0; i<l; i++)
{
*line1++=*line1copy++;
}
*line1++ = '\0';
delete[] line1copy;
delete[] mas1;
delete[] mas2;
}
//--------------------Procedure for finding "konsensus"-------------------------
//--------------------------of PWM----------------------------------------------
void find_kons(int L, double **t_matrix_PWM, char *kons)
{
int i, j;
double pwmmax=-12.00;
char *konstemp;
konstemp = new char[1];
char *s4="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i=1; i<=L; i++, pwmmax=-12.00)
{
for (j=1; j<=26; j++)
{
if (t_matrix_PWM[i][j]>pwmmax)
{
pwmmax=t_matrix_PWM[i][j];
}
}
for (j=1; j<=26; j++)
{
if (t_matrix_PWM[i][j]==pwmmax)
{
substr(s4, konstemp, j-1,1);
kons[i] = *konstemp;
}
}
}
delete [] konstemp;
}
//-------------------Procedure of result output---------------------------------
void output(int L, char *str, char *kons)
{
int a, b, i, j, k,l, k1, kstart, jstart;
int k0, j0, kmax, jmax;
char *way;
char *coord;
char *tokenPtr;
char *tokenPtr1;
char *matrtemp[MAXLEN];
int count;
way = new char[40000];
tokenPtr = new char[MAXLEN];
tokenPtr1 = new char[MAXLEN];
coord = new char[15];
char *symboltemp;
double **t_matrix_way;
t_matrix_way = new double*[J];
for (i=0 ; i<I ; i++)
{
t_matrix_way[i] = new double[I];
}
symboltemp = new char[MAXLEN];
FILE *fpc;
fpc = fopen("coordinates.txt", "r");
fgets (coord, 15, fpc);
tokenPtr1 = strtok (coord, "_");
tokenPtr = strtok (tokenPtr1, "&");
k0 = atoi(tokenPtr);
tokenPtr = strtok (NULL, "&");
j0 = atoi(tokenPtr);
tokenPtr = strtok (NULL, "&");
kmax = atoi(tokenPtr);
tokenPtr = strtok (NULL, "&");
jmax = atoi(tokenPtr);
fclose(fpc);
FILE *fpw;
fpw = fopen("way.txt", "r");
fgets (way, 40000, fpw);
fclose(fpw);
k = k0;
tokenPtr = strtok (way, "_");
while (tokenPtr != NULL)
{
matrtemp[k] = tokenPtr;
k++;
tokenPtr = strtok (NULL, "_");
}
k1 = k-1;
j = j0;
for (k=k0; k<=k1; k++, j=j0)
{
tokenPtr1 = strtok (matrtemp[k], "&");
while (tokenPtr1 != NULL)
{
t_matrix_way[k][j] = atoi(tokenPtr1);
j++;
tokenPtr1 = strtok (NULL, "&");
}
}
delete []coord;
delete []way;
delete []tokenPtr;
delete []tokenPtr1;
FILE *fpo;
fpo = fopen("Result.txt", "a");
if (k0 == 0) {kstart = k0+1;}
else {kstart = k0;}
if (j0 == 0) {jstart = j0+1;}
else {jstart = j0;}
for (k=kstart;k<=kmax; k++)
{
for (j=jstart;j<=jmax;j++)
{
t_matrix_way[k][jstart-1]=0;
t_matrix_way[kstart-1][j]=0;
}
}
fprintf(fpc, "\n");
for (k=kstart; k<=kmax; k++)
{
for (j=jstart; j<=jmax; j++)
{
if (fmod(k,L)==0)
{i=L;}
else
{i=fmod(k,L);}
*symboltemp = kons[i];
if (t_matrix_way[k][j]==1)
{
fwrite(symboltemp,1,1,fpo);
fprintf(fpc, " ");
k++;
j++;
}
else
{
if (t_matrix_way[k][j]==2)
{
fprintf(fpc, "- ");
j++;
}
else
{
if (t_matrix_way[k][j]==3)
{
fwrite(symboltemp,1,1,fpo);
fprintf(fpc, " ");
k++;
}
else
{
if (k0==0)
{k++;}
if (j0==0)
{j++;}
}
}
}
}
}
fprintf(fpc, "\n");
for (k=kstart; k<=kmax; k++)
{
for (j=jstart; j<=jmax; j++)
{
if (t_matrix_way[k][j]==1)
{
substr(str, symboltemp, j-1,1);
fwrite(symboltemp,sizeof(symboltemp)-2,1,fpo);
k++;
j++;
}
else
{
if (t_matrix_way[k][j]==2)
{
substr(str, symboltemp, j-1,1);
fwrite(symboltemp,sizeof(symboltemp)-2,1,fpo);
j++;
}
else
{
if (t_matrix_way[k][j]==3)
{
fprintf(fpc, "- ");
k++;
}
else
{
if (k0==0)
{k++;}
if (j0==0)
{j++;}
}
}
}
}
}
fclose(fpo);
delete []symboltemp;
for (i=0 ; i<I ; i++)
{
delete[] t_matrix_way[i];
}
delete[] t_matrix_way;
}
//------------------------------------------------------------------------------
//----------------------Function of getting PWM from file-----------------------
void get_pwm(int num, double **t_matrix_PWM, char *name)
{
char *tokenPtr;
char *tokenPtr1;
char *matrtemp[27];
char *nametemp;
char *pwm;
char *strtemp;
char *s[5];
int i, k, j, l;
pwm = new char[MAXMASS];
strtemp = new char[MAXLEN];
nametemp = new char[MAXLEN];
tokenPtr = new char[MAXLEN];
tokenPtr1 = new char[MAXLEN];
FILE *fp;
itoa (num, strtemp, 10);
s[num] = strncat(strtemp, ".txt", 4);
fp = fopen(s[num], "r");
fgets (pwm, MAXMASS, fp);
fclose(fp);
k = 1;
j = 1;
nametemp = strtok (pwm, "_");
l = strlen(nametemp);
for(i=0; i<l; i++)
{
*name++ = *nametemp++;
}
*name++ = '\0';
tokenPtr = strtok (NULL, " ");
while (tokenPtr != NULL)
{
matrtemp[j] = tokenPtr;
j++;
tokenPtr = strtok (NULL, " ");
}
for (j=1; j<=26; j++)
{
tokenPtr1 = strtok (matrtemp[j], "&");
while (tokenPtr1 != NULL)
{
t_matrix_PWM[k][j] = atof(tokenPtr1);
k++;
tokenPtr1 = strtok (NULL, "&");
}
k=1;
}
delete[] nametemp;
// delete[] strtemp;
delete[] tokenPtr;
delete[] tokenPtr1;
// delete[] pwm;
}
//------------------------------------------------------------------------------
//-------------------Function of all the calculations---------------------------
double all_calc(int L, char *str, double **t_matrix_allign, double **t_matrix_way, double **t_matrix_PWM)
{
char *substr1;
substr1 = new char;
double Smax;
int kstart, jstart, kdel, jdel, w;
int kmax, jmax, k0, j0;
int kmaxcopy, jmaxcopy, k0copy, j0copy;
double weight;
double maxdelj, maxdelk;
int i, j, k, j1, l, m;
char *s4="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//------------------Filling the matrix of the similarity and--------------------
//-------------------------the matrix of the way--------------------------------
t_matrix_allign[0][0]=0;
t_matrix_way[0][0]=0;
for (k=1; k<=strlen(str); k++)
{
t_matrix_allign[k][0]=-a-b*(k-1);
t_matrix_way[k][0]= 3;
}
for (j=1; j<=strlen(str);j++)
{
t_matrix_allign[0][j]=-a-b*(j-1);
t_matrix_way[0][j]=2;
}
for (k=1;k<=strlen(str); k++)
{
for (j=1;j<=strlen(str);j++)
{
if (fmod(k,L)==0)
{i=L;}
else
{i=fmod(k,L);}
substr(str, substr1, j-1, 1);
j1 = number(s4, substr1)+1;
if (j>20)
{jdel=20;}
else
{jdel=j;}
if (k>20)
{kdel=20;}
else
{kdel=k;}
maxdelj=0;
for (l=1; l<=jdel; l++)
{
if (t_matrix_allign[k][j-l]-a-b*(l-1) > maxdelj)
{
maxdelj = t_matrix_allign[k][j-l]-a-b*(l-1);
}
}
maxdelk=0;
for (m=1; m<=kdel; m++)
{
if (t_matrix_allign[k-m][j]-a-b*(m-1) > maxdelk)
{
maxdelk = t_matrix_allign[k-m][j]-a-b*(m-1);
}
}
if ((t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1]>=maxdelj)&&(t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1]>0))
{
if (t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1]>=maxdelk)
{
t_matrix_allign[k][j]=t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1];
t_matrix_way[k][j]=1;
}
else
{
t_matrix_allign[k][j]= maxdelk;
t_matrix_way[k][j]=3;
}
}
else {if ((maxdelj>t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1])&&(maxdelj>0))
{
if (maxdelj>=maxdelk)
{
t_matrix_allign[k][j]=maxdelj;
t_matrix_way[k][j]=2;
}
else
{
t_matrix_allign[k][j]= maxdelk;
t_matrix_way[k][j]=3;
}
}
else
{
t_matrix_allign[k][j]=0;
t_matrix_way[k][j]=0;
}
}
if (t_matrix_allign[k][j]>Smax)
{
Smax=t_matrix_allign[k][j];
}
}
}
//-------------------Finding coordinates of the way-----------------------------
for (k=1; k<=strlen(str); k++)
{
for (j=1; j<=strlen(str); j++)
{
if (t_matrix_allign[k][j]==Smax)
{
kmax=k;
jmax=j;
}
}
}
w=0;
k=kmax;
j=jmax;
while (t_matrix_way[k][j]>0)
{
if (t_matrix_way[k][j]==1)
{
k=k-1;
j=j-1;
w=w+1;
}
else {if (t_matrix_way[k][j]==2)
{
j=j-1;
w=w+1;
}
else
{
k=k-1;
w=w+1;
} }
}
if (w>0)
{
k0=k;
j0=j;
}
//-----------------Findind the weight of global allignment----------------------
if (k0 == 0) {kstart = k0+1;}
else {kstart = k0;}
if (j0 == 0) {jstart = j0+1;}
else {jstart = j0;}
for (k=kstart;k<=kmax; k++)
{
for (j=jstart;j<=jmax;j++)
{
t_matrix_allign[k][jstart-1]=0;
t_matrix_allign[kstart-1][j]=0;
t_matrix_way[k][jstart-1]=0;
t_matrix_way[kstart-1][j]=0;
}
}
for (k=kstart;k<=kmax; k++)
{
for (j=jstart;j<=jmax;j++)
{
if (fmod(k,L)==0)
{i=L;}
else
{i=fmod(k,L);}
substr(str, substr1, j-1, 1);
j1 = number(s4, substr1)+1;
if (j>20)
{jdel=20;}
else
{jdel=j;}
if (k>20)
{kdel=20;}
else
{kdel=k;}
maxdelj=0;
for (l=1; l<=jdel; l++)
{
if (t_matrix_allign[k][j-l]-a-b*(l-1) > maxdelj)
{
maxdelj = t_matrix_allign[k][j-l]-a-b*(l-1);
}
}
maxdelk=0;
for (m=1; m<=kdel; m++)
{
if (t_matrix_allign[k-m][j]-a-b*(m-1) > maxdelk)
{
maxdelk = t_matrix_allign[k-m][j]-a-b*(m-1);
}
}
if (t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1]>=maxdelj)
{
if(t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1]>=maxdelk)
{
t_matrix_allign[k][j]=t_matrix_allign[k-1][j-1]+t_matrix_PWM[i][j1];
t_matrix_way[k][j]=1;
}
else
{
t_matrix_allign[k][j]=maxdelk;
t_matrix_way[k][j]=3;
}
}
else
{
t_matrix_allign[k][j]=maxdelj;
t_matrix_way[k][j]=2;
}
}
}
weight=t_matrix_allign[kmax][jmax];
FILE *fpway;
fpway = fopen("lengh.txt", "w");
fprintf(fpway, "%.d", w);
//fprintf(fpway, "_");
fclose(fpway);
if (w>30)
{
FILE *fpc;
fpc = fopen("coordinates.txt", "w");
fprintf(fpc, "%.d", k0);
fprintf(fpc, "&");
fprintf(fpc, "%.d", j0);
fprintf(fpc, "&");
fprintf(fpc, "%.d", kmax);
fprintf(fpc, "&");
fprintf(fpc, "%.d", jmax);
fprintf(fpc, "_");
fclose(fpc);
int inttemp;
FILE *fpw;
fpw = fopen("way.txt", "w");
for (k= k0; k<=kmax; k++)
{
for (j=j0; j<=jmax; j++)
{
inttemp = t_matrix_way[k][j];
fprintf(fpw, "%.d", inttemp);
fprintf(fpw, "&");
}
fprintf(fpw, "_");
}
fclose(fpw);
}
return weight;
}
//----------------------Function of calculation---------------------------------
//--------------------the statistical significance------------------------------
double calc_stat_sign (int L, char *str, double **t_matrix_allign, double **t_matrix_way, double **t_matrix_PWM)
{
double Z, W;
W = all_calc(L, str, t_matrix_allign, t_matrix_way, t_matrix_PWM);
int w;
char *w1;
w1 = new char[6];
FILE *fpway;
fpway = fopen("lengh.txt", "r");
fgets (w1, 6, fpway);
w = atoi(w1);
fclose(fpway);
cout << "\nWay: " << w << endl;
if (w < 30)
{return -33;}
else
{
int i, j, k, l, Kmax=100;
double sum=0, sum2=0, aver=0, sigma=0;
double *W1;
W1 = new double[Kmax];
char *strnew;
strnew = new char[MAXLEN];
for (k=0; k<Kmax; k++)
{
shuffling(str, strnew);
W1[k]=all_calc(L, strnew, t_matrix_allign, t_matrix_way, t_matrix_PWM);
sum=sum+W1[k];
}
aver=sum/Kmax;
for (k=0; k<Kmax; k++)
{
sum2=sum2 + pow((W1[k]-aver), 2);
}
sigma=sqrt(sum2/(Kmax-1));
Z=(W - aver)/sigma;
delete[] strnew;
delete[] W1;
return Z;
}
}
//------------------------------------------------------------------------------
//--------------------------The main function-----------------------------------
void main(void)
{
randomize();
int i, k, j, L, l;
int nummax;
char *nummax1;
nummax1 = new char[5];
FILE *fpn;
fpn = fopen("nummax.txt", "r");
fgets (nummax1, 4, fpn);
nummax = atoi(nummax1);
fclose(fpn);
cout << "\nNummax: " << nummax << endl;
double W=0, Z;
double **t_matrix_PWM;
t_matrix_PWM = new double*[80];
for (i=0 ; i<80 ; i++)
{
t_matrix_PWM[i] = new double[80];
}
double **t_matrix_allign, **t_matrix_way;
t_matrix_allign = new double*[J];
t_matrix_way = new double*[J];
for (i=0 ; i<I ; i++)
{
t_matrix_allign[i] = new double[I];
t_matrix_way[i] = new double[I];
}
char *seq;
char *pwm;
char *name;
char *kons;
kons = new char[MAXLEN];
seq = new char[MAXLEN];
pwm = new char[MAXLEN];
name = new char[MAXLEN];
FILE *f;
f = fopen("sequence.txt", "r");
fgets (seq, 5000, f);
fclose(f);
cout << "Sequence: " << seq;
FILE *fpres;
fpres = fopen("Result.txt", "w");
fprintf(fpres, "\nYour sequence can be similar with such families:");
fprintf(fpres, "\n\n");
fclose(fpres);
for (i = 1; i<=37; i++) // Если взять 38 и больше, возникает ошибка!!!!
{for (j=0; j<80; j++)
{
for (k=0; k<80; k++)
{
t_matrix_PWM[k][j] = -33;
}
}
for (j=0; j<J; j++)
{
for (k=0; k<I; k++)
{
t_matrix_allign[k][j] = 0;
t_matrix_way[k][j] = 0;
}
}
get_pwm(i, t_matrix_PWM, name);
cout << "\nName of PWM: " << name;
l = strlen(name);
k = 1;
cout << "\n\n";
while (t_matrix_PWM[k][1] != -33)
{k++;}
L = k-1;
cout << "L = " << L;
cout << "\n\n";
find_kons(L, t_matrix_PWM, kons);
Z = calc_stat_sign(L, seq, t_matrix_allign, t_matrix_way, t_matrix_PWM);
cout << "Z = " << Z;
if (Z!=-33)
{
if (Z>6)
{
fpres = fopen("Result.txt", "a");
fprintf(fpres, "\n\n------------------------------------------------\n");
fwrite(name,sizeof(name),l,fpres);
fprintf(fpres, "\n\nAllignment:");
fclose(fpres);
output(L, seq, kons);
fpres = fopen("Result.txt", "a");
fprintf(fpres, "\n\nZ = %.3lf", Z);
fprintf(fpres, "\n\n------------------------------------------------");
fclose(fpres);
}
}
}
for (i=0 ; i<80 ; i++)
{
delete[] t_matrix_PWM[i];
}
delete[] t_matrix_PWM;
for (i=0 ; i<I ; i++)
{
delete[] t_matrix_allign[i];
delete[] t_matrix_way[i];
}
delete[] t_matrix_allign;
delete[] t_matrix_way;
fflush(stdin);
getch();
}