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