1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include <ctype.h> |
---|
6 | #include <frams/genetics/defgenoconv.h> |
---|
7 | #include <frams/model/model.h> |
---|
8 | #include <frams/util/multimap.h> |
---|
9 | |
---|
10 | #include "printconvmap.h" |
---|
11 | #include <frams/errmgr/stdouterr.h> |
---|
12 | |
---|
13 | StdoutErrorHandler err; |
---|
14 | |
---|
15 | /** |
---|
16 | @file |
---|
17 | Sample code: Genotype converter class |
---|
18 | */ |
---|
19 | |
---|
20 | /// Sample Geno converter not using Model class. |
---|
21 | /// (this converter generates the same output for each input). |
---|
22 | /// such a converter is responsible for doing valid |
---|
23 | /// f0 (or other format) output and storing temporary data |
---|
24 | class GenoConv_Test: public GenoConverter |
---|
25 | { |
---|
26 | public: |
---|
27 | GenoConv_Test() |
---|
28 | { |
---|
29 | name="Test Converter"; |
---|
30 | in_format='x'; |
---|
31 | } |
---|
32 | SString convert(SString &i,MultiMap *map) {return SString("after conversion...");} |
---|
33 | ~GenoConv_Test() {} |
---|
34 | }; |
---|
35 | |
---|
36 | /// Sample Geno converter using Model class. |
---|
37 | /// (this converter generates the same output for each input). |
---|
38 | class GenoConv_Test2: public GenoConverter |
---|
39 | { |
---|
40 | public: |
---|
41 | GenoConv_Test2() |
---|
42 | { |
---|
43 | name="Test Converter #2"; |
---|
44 | in_format='y'; |
---|
45 | } |
---|
46 | SString convert(SString &i,MultiMap *map) |
---|
47 | { |
---|
48 | Model mod; |
---|
49 | mod.open(); |
---|
50 | mod.singleStepBuild("p:"); |
---|
51 | mod.singleStepBuild("p:"); |
---|
52 | mod.singleStepBuild("j:0,1"); |
---|
53 | mod.getPart(1)->p=Pt3D(0,0.2,-1); |
---|
54 | mod.close(); |
---|
55 | return mod.getGeno().getGene(); |
---|
56 | } |
---|
57 | ~GenoConv_Test2() {} |
---|
58 | }; |
---|
59 | |
---|
60 | /// Sample Geno converter supporting conversion mapping. |
---|
61 | /// the conversion is very simple: any sequence of <digit><character> |
---|
62 | /// (but not inside neurons) is replaced by the repeated sequence of the character |
---|
63 | class GenoConv_Test3: public GenoConverter |
---|
64 | { |
---|
65 | public: |
---|
66 | GenoConv_Test3() |
---|
67 | { |
---|
68 | name="Test Converter #3"; |
---|
69 | in_format='z'; |
---|
70 | out_format='1'; |
---|
71 | mapsupport=1; |
---|
72 | } |
---|
73 | SString convert(SString &in,MultiMap *map); |
---|
74 | ~GenoConv_Test3() {} |
---|
75 | }; |
---|
76 | |
---|
77 | /** main converting routine - most important: direct conversion map example */ |
---|
78 | SString GenoConv_Test3::convert(SString &in,MultiMap *map) |
---|
79 | { |
---|
80 | SString dst; |
---|
81 | const char* src=in; |
---|
82 | const char* t; |
---|
83 | int insideneuron=0; |
---|
84 | int n; |
---|
85 | for(t=src;*t;t++) |
---|
86 | { |
---|
87 | if (insideneuron&&*t==']') insideneuron=0; |
---|
88 | if (*t=='[') insideneuron=1; |
---|
89 | if ((!insideneuron)&&isdigit(*t)&&t[1]) |
---|
90 | { // special sequence detected! |
---|
91 | n=*t-'0'; |
---|
92 | t++; // *t will be repeated 'n' times |
---|
93 | for (int i=0;i<n;i++) |
---|
94 | dst+=*t; |
---|
95 | if (map) // fill in the map only if requested |
---|
96 | map->add(t-src, t-src, dst.len()-n, dst.len()-1); |
---|
97 | // meaning: source character (t-src) becomes (dst.len()-n ... dst.len()-1) |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | dst+=*t; |
---|
102 | if (map) |
---|
103 | map->add(t-src, t-src, dst.len()-1, dst.len()-1); |
---|
104 | // meaning: map single to single character: (t-src) into (dst.len()-1) |
---|
105 | } |
---|
106 | } |
---|
107 | return dst; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | /////////////////////////////////////////////// |
---|
112 | |
---|
113 | void printGen(Geno &g) |
---|
114 | { |
---|
115 | printf("Genotype:\n%s\nFormat: %c\nValid: %s\nComment: %s\n", |
---|
116 | (const char*)g.getGene(),g.getFormat(),g.isValid()?"yes":"no",(const char*)g.getComment()); |
---|
117 | } |
---|
118 | |
---|
119 | int main(int argc,char *argv[]) |
---|
120 | { |
---|
121 | DefaultGenoConvManager gcm; |
---|
122 | gcm.addConverter(new GenoConv_Test()); |
---|
123 | gcm.addConverter(new GenoConv_Test2()); |
---|
124 | gcm.addConverter(new GenoConv_Test3()); |
---|
125 | |
---|
126 | const char* src=(argc>1)?argv[1]:"X"; |
---|
127 | char dst=(argc>2)?*argv[2]:'0'; |
---|
128 | |
---|
129 | printf("*** source genotype:\n"); |
---|
130 | Geno g1(src); |
---|
131 | printGen(g1); |
---|
132 | MultiMap m; |
---|
133 | Geno g2=g1.getConverted(dst,&m); |
---|
134 | printf("*** converted to f%c:\n",dst); |
---|
135 | printGen(g2); |
---|
136 | if (m.isEmpty()) |
---|
137 | printf("(conversion map not available)\n"); |
---|
138 | else |
---|
139 | { |
---|
140 | printf("conversion map:\n"); |
---|
141 | m.print(); |
---|
142 | printConvMap(g1.getGene(),g2.getGene(),m); |
---|
143 | printf("reverse conversion map:\n"); |
---|
144 | MultiMap rm; |
---|
145 | rm.addReversed(m); |
---|
146 | rm.print(); |
---|
147 | printConvMap(g2.getGene(),g1.getGene(),rm); |
---|
148 | } |
---|
149 | |
---|
150 | Model mod1(g1,1); |
---|
151 | printf("\nmodel map for f%c genotype:\n",g1.getFormat()); |
---|
152 | printModelMap(g1.getGene(),mod1.getMap()); |
---|
153 | Model mod2(g2,1); |
---|
154 | printf("\nmodel map for f%c genotype:\n",g2.getFormat()); |
---|
155 | printModelMap(g2.getGene(),mod2.getMap()); |
---|
156 | return 0; |
---|
157 | } |
---|