Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

geno_ftest.cpp

00001 #include "geno_ftest.h"
00002 #include "nonstd.h" //randomN, rnd01
00003 
00004 #define FIELDSTRUCT Geno_ftest
00005 static ParamEntry GENOtestparam_tab[]=   //external access to ftest genetic parameters
00006 {
00007 {"Genetics: ftest",1,1,},
00008 {"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)",},
00009 {0,},
00010 };
00011 #undef FIELDSTRUCT
00012 
00013 Geno_ftest::Geno_ftest() : par(GENOtestparam_tab,this)
00014 {
00015    prob=0.1;
00016 }
00017 
00019 int Geno_ftest::check(const char* gene)
00020 {
00021    if (!gene[0]) return 1; //empty is not valid
00022    bool ok=true;
00023    int i;
00024    for(i=0;i<strlen(gene);i++) if (!strchr("ATGC",gene[i])) {ok=false; break;}
00025    return ok ? GENOPER_OK : i+1;
00026 }
00027 
00029 int Geno_ftest::validate(char *&gene)
00030 {
00031    SString validated; //new genotype (everything except ATGC is skipped)
00032    for(int i=0;i<strlen(gene);i++)
00033       if (strchr("ATGC",gene[i])) validated+=gene[i];  //validated contains only ATGC
00034    free(gene);
00035    gene=strdup(validated); //reallocate
00036    return GENOPER_OK;
00037 }
00038 
00040 int Geno_ftest::mutate(char *&gene,float &chg)
00041 {
00042    static char a[]="ATGC";
00043    int changes=0,len=strlen(gene);
00044    for(int i=0;i<len;i++)
00045       if (rnd01<prob) //normalize prob with length of genotype
00046          {gene[i]=a[randomN(4)]; changes++;}
00047    chg=(float)changes/len;
00048    return GENOPER_OK;
00049 }
00050 
00052 int Geno_ftest::crossOver(char *&g1,char *&g2,float& chg1,float& chg2)
00053 {
00054    int len1=strlen(g1),len2=strlen(g2);
00055    int p1=randomN(len1);  //random cut point for first genotype
00056    int p2=randomN(len2);  //random cut point for second genotype
00057    char *child1=(char*)malloc(p1+len2-p2+1);
00058    char *child2=(char*)malloc(p2+len1-p1+1);
00059    strncpy(child1,g1,p1);   strcpy(child1+p1,g2+p2);
00060    strncpy(child2,g2,p2);   strcpy(child2+p2,g1+p1);
00061    free(g1); g1=child1;
00062    free(g2); g2=child2;
00063    chg1=(float)p1/strlen(child1);
00064    chg2=(float)p2/strlen(child2);
00065    return GENOPER_OK;
00066 }
00067 
00069 unsigned long Geno_ftest::style(const char *g, int pos)
00070 {
00071    char ch=g[pos];
00072    unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below
00073    if (ch=='A') style=GENSTYLE_RGBS(200,0,0,GENSTYLE_NONE);
00074    if (ch=='T') style=GENSTYLE_RGBS(0,200,0,GENSTYLE_NONE);
00075    if (ch=='G') style=GENSTYLE_RGBS(0,0,200,GENSTYLE_NONE);
00076    if (ch=='C') style=GENSTYLE_RGBS(200,200,0,GENSTYLE_NONE);
00077    return style;
00078 }
00079 
00080 
00081 //#ifdef GENO_FTEST_APPL ///<define this macro to compile a simple testing main function
00082 
00083 Geno_ftest gft;
00084 
00085 void main()
00086 {
00087    float chg;
00088    char *g=strdup(gft.getSimplest());
00089    for(int i=0;i<10;i++)
00090    {
00091       int result=gft.mutate(g,chg);
00092       printf("%s   [mutated %.1f%%]\n",g,chg*100);
00093    }
00094 
00095    char *g2=strdup(gft.getSimplest());
00096    float chg2;
00097    printf("\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n",g,g2);
00098    gft.crossOver(g,g2,chg,chg2);
00099    printf("Offspring 1:\n\t%s  (%.1f%% genes from parent1)\n",g,chg*100);
00100    printf("Offspring 2:\n\t%s  (%.1f%% genes from parent2)\n",g2,chg2*100);
00101    free(g);
00102    free(g2);
00103 
00104    g=strdup("ATGsomethingCG");
00105    printf("\nChecking genotype:\n\t%s... error at position %d.\n",g,gft.check(g));
00106    gft.validate(g);
00107    printf("After validation:\n\t%s\n",g);
00108    free(g);
00109    printf("...and is YOUR genotype O.K.?\n\n");
00110 }
00111 
00112 //#endif
00113 

Generated on Tue Jul 30 23:21:19 2002 for Framsticks GenoFX by doxygen1.2.17