source: cpp/geno_fx/geno_ftest.cpp @ 111

Last change on this file since 111 was 66, checked in by Maciej Komosinski, 14 years ago

set 'eol-style' to 'native'

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// This file is a part of the Framsticks GenoFX library.
2// Copyright (C) 2002-2011  Maciej Komosinski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include "geno_ftest.h"
6#include "nonstd.h" //randomN, rnd01
7
8/** \file
9Sample output (simple examples of various genetic operations):
10\anchor geno_ftest_example
11\include geno_ftest_example
12Produced by the source:
13\include geno_ftest.cpp
14*/
15
16#define FIELDSTRUCT Geno_ftest
17static ParamEntry GENOtestparam_tab[]=   //external access to ftest genetic parameters
18{
19{"Genetics: ftest",1,1,},
20{"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)",},
21{0,},
22};
23#undef FIELDSTRUCT
24
25Geno_ftest::Geno_ftest()
26{
27   par.setParamTab(GENOtestparam_tab);
28   par.select(this);
29   supported_format='t'; //'0' for f0, '1' for f1, etc.
30   prob=0.1;
31}
32
33///The only letters allowed are A,T,G,C
34int Geno_ftest::checkValidity(const char* gene)
35{
36   if (!gene[0]) return 1; //empty is not valid
37   bool ok=true;
38   int i;
39   for(i=0;i<strlen(gene);i++) if (!strchr("ATGC",gene[i])) {ok=false; break;}
40   return ok ? GENOPER_OK : i+1;
41}
42
43///Remove all invalid letters from the genotype
44int Geno_ftest::validate(char *&gene)
45{
46   SString validated; //new genotype (everything except ATGC is skipped)
47   for(int i=0;i<strlen(gene);i++)
48      if (strchr("ATGC",gene[i])) validated+=gene[i];  //validated contains only ATGC
49   free(gene);
50   gene=strdup(validated); //reallocate
51   return GENOPER_OK;
52}
53
54///Very simple mutation
55int Geno_ftest::mutate(char *&gene,float &chg)
56{
57   static char a[]="ATGC";
58   int changes=0,len=strlen(gene);
59   for(int i=0;i<len;i++)
60      if (rnd01<prob) //normalize prob with length of genotype
61         {gene[i]=a[randomN(4)]; changes++;}
62   chg=(float)changes/len;
63   return GENOPER_OK;
64}
65
66///A simple one-point crossover
67int Geno_ftest::crossOver(char *&g1,char *&g2,float& chg1,float& chg2)
68{
69   int len1=strlen(g1),len2=strlen(g2);
70   int p1=randomN(len1);  //random cut point for first genotype
71   int p2=randomN(len2);  //random cut point for second genotype
72   char *child1=(char*)malloc(p1+len2-p2+1);
73   char *child2=(char*)malloc(p2+len1-p1+1);
74   strncpy(child1,g1,p1);   strcpy(child1+p1,g2+p2);
75   strncpy(child2,g2,p2);   strcpy(child2+p2,g1+p1);
76   free(g1); g1=child1;
77   free(g2); g2=child2;
78   chg1=(float)p1/strlen(child1);
79   chg2=(float)p2/strlen(child2);
80   return GENOPER_OK;
81}
82
83///Applying some colors and font styles...
84unsigned long Geno_ftest::style(const char *g, int pos)
85{
86   char ch=g[pos];
87   unsigned long style=GENSTYLE_CS(0,GENSTYLE_INVALID); //default, should be changed below
88   if (ch=='A') style=GENSTYLE_RGBS(200,0,0,GENSTYLE_BOLD);
89   if (ch=='T') style=GENSTYLE_RGBS(0,200,0,GENSTYLE_BOLD);
90   if (ch=='G') style=GENSTYLE_RGBS(0,0,200,GENSTYLE_NONE);
91   if (ch=='C') style=GENSTYLE_RGBS(200,200,0,GENSTYLE_NONE);
92   return style;
93}
94
95
96#ifdef GENO_FTEST_APPL //define this macro to compile a simple testing main function
97
98Geno_ftest gft;
99
100void main()
101{
102   float chg;
103   char *g=strdup(gft.getSimplest());
104   for(int i=0;i<10;i++)
105   {
106      int result=gft.mutate(g,chg);
107      printf("%s   [mutated %.1f%%]\n",g,chg*100);
108   }
109
110   char *g2=strdup(gft.getSimplest());
111   float chg2;
112   printf("\nCrossing over the last mutant, \n\t%s\nand the simplest genotype\n\t%s\n:\n",g,g2);
113   gft.crossOver(g,g2,chg,chg2);
114   printf("Offspring 1:\n\t%s  (%.1f%% genes from parent1)\n",g,chg*100);
115   printf("Offspring 2:\n\t%s  (%.1f%% genes from parent2)\n",g2,chg2*100);
116   free(g);
117   free(g2);
118
119   g=strdup("ATGsomethingCG");
120   printf("\nChecking genotype:\n\t%s... error at position %d.\n",g,gft.checkValidity(g));
121   gft.validate(g);
122   printf("After validation:\n\t%s\n",g);
123   free(g);
124   printf("...and is YOUR genotype O.K.?\n\n");
125}
126
127#endif
128
Note: See TracBrowser for help on using the repository browser.