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

geno_ftest.cpp File Reference


Defines

#define FIELDSTRUCT   Geno_ftest

Detailed Description

Sample output (simple examples of various genetic operations):
GTTCCGATC   [mutated 11.1%]
GATCCGATC   [mutated 33.3%]
GATCCGAAC   [mutated 11.1%]
GATCCGAAC   [mutated 0.0%]
GATCCGAAC   [mutated 0.0%]
GATCCGAAC   [mutated 0.0%]
GATCCGAAC   [mutated 22.2%]
GATGCGAAC   [mutated 11.1%]
GATGCGAAC   [mutated 0.0%]
GATGCGAAC   [mutated 11.1%]

Crossing over the last mutant, 
        GATGCGAAC
and the simplest genotype
        GTTCAGATC
:
Offspring 1:
        GATGCGAATTCAGATC  (50.0% genes from parent1)
Offspring 2:
        GC  (50.0% genes from parent2)

Checking genotype:
        ATGsomethingCG... error at position 4.
After validation:
        ATGCG
...and is YOUR genotype O.K.?

Produced by the source:
// This file is a part of Framsticks GenoFX library.
// Copyright (C) 2002  Maciej Komosinski.  See LICENSE.txt for details.
// Refer to http://www.frams.alife.pl/ for further information.

#include "geno_ftest.h"
#include "nonstd.h" //randomN, rnd01

#define FIELDSTRUCT Geno_ftest
static ParamEntry GENOtestparam_tab[]=   //external access to ftest genetic parameters
{
{"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; //empty is not valid
   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; //new genotype (everything except ATGC is skipped)
   for(int i=0;i<strlen(gene);i++)
      if (strchr("ATGC",gene[i])) validated+=gene[i];  //validated contains only ATGC
   free(gene);
   gene=strdup(validated); //reallocate
   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) //normalize prob with length of genotype
         {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);  //random cut point for first genotype
   int p2=randomN(len2);  //random cut point for second genotype
   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); //default, should be changed below
   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


Generated on Sun Sep 15 00:58:40 2002 for Framsticks GenoFX by doxygen1.2.17