#include "geno_ftest.h"
#include "nonstd.h"
#define FIELDSTRUCT Geno_ftest
static ParamEntry GENOtestparam_tab[]=
{
{"Genetics: ftest",1,1,},
{"ftest_mut",0,0,"Mutation probability","f 0 1",FIELD(prob),"How many genes should be mutated during single mutation (1=all genes, 0.1=ten percent)",},
{0,},
};
#undef FIELDSTRUCT
Geno_ftest::Geno_ftest()
{
par.setParamTab(GENOtestparam_tab);
par.select(this);
supported_format='t';
prob=0.1;
}
int Geno_ftest::checkValidity(const char* gene)
{
if (!gene[0]) return 1;
bool ok=true;
int i;
for(i=0;i<strlen(gene);i++) if (!strchr("ATGC",gene[i])) {ok=false; break;}
return ok ? GENOPER_OK : i+1;
}
int Geno_ftest::validate(char *&gene)
{
SString validated;
for(int i=0;i<strlen(gene);i++)
if (strchr("ATGC",gene[i])) validated+=gene[i];
free(gene);
gene=strdup(validated);
return GENOPER_OK;
}
int Geno_ftest::mutate(char *&gene,float &chg)
{
static char a[]="ATGC";
int changes=0,len=strlen(gene);
for(int i=0;i<len;i++)
if (rnd01<prob)
{gene[i]=a[randomN(4)]; changes++;}
chg=(float)changes/len;
return GENOPER_OK;
}
int Geno_ftest::crossOver(char *&g1,char *&g2,float& chg1,float& chg2)
{
int len1=strlen(g1),len2=strlen(g2);
int p1=randomN(len1);
int p2=randomN(len2);
char *child1=(char*)malloc(p1+len2-p2+1);
char *child2=(char*)malloc(p2+len1-p1+1);
strncpy(child1,g1,p1); strcpy(child1+p1,g2+p2);
strncpy(child2,g2,p2); strcpy(child2+p2,g1+p1);
free(g1); g1=child1;
free(g2); g2=child2;
chg1=(float)p1/strlen(child1);
chg2=(float)p2/strlen(child2);
return GENOPER_OK;
}
unsigned long Geno_ftest::style(const char *g, int pos)
{
char ch=g[pos];
unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID);
if (ch=='A') style=GENSTYLE_RGBS(200,0,0,GENSTYLE_BOLD);
if (ch=='T') style=GENSTYLE_RGBS(0,200,0,GENSTYLE_BOLD);
if (ch=='G') style=GENSTYLE_RGBS(0,0,200,GENSTYLE_NONE);
if (ch=='C') style=GENSTYLE_RGBS(200,200,0,GENSTYLE_NONE);
return style;
}
#ifdef GENO_FTEST_APPL //define this macro to compile a simple testing main function
Geno_ftest gft;
void main()
{
float chg;
char *g=strdup(gft.getSimplest());
for(int i=0;i<10;i++)
{
int result=gft.mutate(g,chg);
printf("%s [mutated %.1f%%]\n",g,chg*100);
}
char *g2=strdup(gft.getSimplest());
float chg2;
printf("\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n",g,g2);
gft.crossOver(g,g2,chg,chg2);
printf("Offspring 1:\n\t%s (%.1f%% genes from parent1)\n",g,chg*100);
printf("Offspring 2:\n\t%s (%.1f%% genes from parent2)\n",g2,chg2*100);
free(g);
free(g2);
g=strdup("ATGsomethingCG");
printf("\nChecking genotype:\n\t%s... error at position %d.\n",g,gft.checkValidity(g));
gft.validate(g);
printf("After validation:\n\t%s\n",g);
free(g);
printf("...and is YOUR genotype O.K.?\n\n");
}
#endif