source: cpp/frams/_demos/simil_test.cpp @ 644

Last change on this file since 644 was 612, checked in by Maciej Komosinski, 8 years ago

Updated after field rename

File size: 5.7 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2016  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5
6#include <vector>
7#include "common/loggers/loggertostdout.h"
8#include "frams/_demos/genotypeloader.h"
9#include "frams/genetics/preconfigured.h"
10#include "common/virtfile/stdiofile.h"
11#include "frams/model/similarity/simil_model.h"
12
13
14
15using namespace std;
16
17/** Computes a matrix of distances between all genotypes in the specified
18        .gen file, using the matching and measure weights as specified in the
19        command line. */
20int main(int argc, char *argv[])
21{
22        LoggerToStdout messages_to_stdout(LoggerBase::Enable);
23        typedef double *pDouble;
24        int iCurrParam = 0; // index of the currently processed parameter
25        char *szCurrParam = NULL;
26        ModelSimil M; // similarity computing object
27        bool bPrintNames = false; // specifies if names of genotypes are to be printed
28        int nResult = 0; // a temporary result
29
30        if (argc < 7)
31        {
32                printf("Too few parameters!\n");
33                printf("Command line: [-names] <genotypesFile> <w_dP> <w_dDEG> <w_dNEU> <w_dGEO> <fixZaxis?>\n\n");
34
35                printf("Parameters:\n");
36                printf("  <genotypesFile> name of a file with genotypes\n");
37                printf("  <w_dP> weight of the difference in the number of parts\n");
38                printf("  <w_dDEG> weight of the difference in degrees of matched parts\n");
39                printf("  <w_dNEU> weight of the difference in neurons of matched parts\n");
40                printf("  <w_dGEO> weight of the distance of matched parts\n");
41                printf("  <fixZaxis?> should the 'z' (vertical) coordinate be fixed during the alignment? (0 or 1)\n\n");
42
43                printf("Switches:\n");
44                printf("  -names specifies that the number and names of genotypes are to be printed to output\n");
45                printf("  before the distance matrix; by default the number and names are not printed\n\n");
46
47                printf("Outputs a symmetric distance matrix in the format:\n");
48                printf("  <row_1> (columns in a row are separated by TABs)\n");
49                printf("  ...\n");
50                printf("  <row_n>\n");
51
52                return -1;
53        }
54
55        // prepare output parameters from .gen file
56        vector<Geno *> pvGenos;
57        vector<char *> pvNames;
58
59        // check if there is a switch
60        iCurrParam = 1;
61        szCurrParam = argv[iCurrParam];
62        if (strcmp(szCurrParam, "-names") == 0)
63        {
64                // switch "-names" was given; print names also
65                bPrintNames = true;
66                // pass to the next parameter
67                iCurrParam++;
68        }
69
70        // check the parameters
71        // get <genotypesFile> name from command line
72        char *szFileName = argv[iCurrParam];
73
74        // initially set measure components' weights to invalid values (negative)
75        for (int i = 0; i < M.GetNOFactors(); i++)
76        {
77                M.m_adFactors[i] = -1.0;
78        }
79
80        const char *params[] = { "<w_dP>", "<w_dDEG>", "<w_dNEU>", "<w_dGEO>" };
81        for (int i = 0; i < M.GetNOFactors(); i++)
82        {
83                iCurrParam++;
84                szCurrParam = argv[iCurrParam];
85                nResult = sscanf(szCurrParam, " %lf ", &M.m_adFactors[i]);
86                if (nResult != 1)
87                {
88                        // <w_dX> is not a number -- error
89                        printf("%s", params[i]);
90                        printf(" should be a number\n");
91                        return -1;
92                }
93                else
94                {
95                        // <w_dX> is a number; check if nonnegative
96                        if (M.m_adFactors[i] < 0.0)
97                        {
98                                printf("%s", params[i]);
99                                printf(" should be a nonnegative number\n");
100                                return -1;
101                        }
102                }
103        }
104
105        iCurrParam++;
106        szCurrParam = argv[iCurrParam];
107        nResult = sscanf(szCurrParam, " %d", &M.fixedZaxis);
108        if (nResult != 1)
109        {
110                // <isZFixed> is not a number -- error
111                printf("<isZFixed> should be a number\n");
112                return -1;
113        }
114        else if (M.fixedZaxis != 0 && M.fixedZaxis != 1)
115        {
116                printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.fixedZaxis);
117                return -1;
118        }
119
120        // read the input file
121        // prepare loading of genotypes from a .gen file
122        // create some basic genotype converters
123        PreconfiguredGenetics genetics;
124        StdioFileSystem_autoselect stdiofilesys;
125
126        long count = 0, totalsize = 0;
127        MiniGenotypeLoader loader(szFileName);
128        MiniGenotype *loaded;
129        while (loaded = loader.loadNextGenotype())
130        {
131                // while a valid genotype was loaded
132                count++;
133                totalsize += loaded->genotype.len();
134                // create a Geno object based on the MiniGenotype
135                Geno *pNextGenotype = new Geno(loaded->genotype);
136                if ((pNextGenotype != NULL) && (pNextGenotype->isValid()))
137                {
138                        pvGenos.push_back(pNextGenotype);
139                        char *szNewName = new char[loaded->name.len() + 1];
140                        strcpy(szNewName, loaded->name.c_str());
141                        pvNames.push_back(szNewName);
142                }
143                else
144                {
145                        printf("Genotype %2li is not valid\n", count);
146                        if (pNextGenotype != NULL) delete pNextGenotype;
147                }
148        }
149        if (loader.getStatus() == MiniGenotypeLoader::OnError)
150        {
151                printf("Error: %s", loader.getError().c_str());
152        }
153
154        double dSimilarity = 0.0;
155        double **aaSimil = NULL; // array of similarities
156
157        // create the empty array of similarities
158        aaSimil = new pDouble[pvGenos.size()];
159        for (unsigned int k = 0; k < pvGenos.size(); k++)
160        {
161                aaSimil[k] = new double[pvGenos.size()];
162                for (unsigned int l = 0; l < pvGenos.size(); l++)
163                        aaSimil[k][l] = 0.0;
164        }
165
166        // compute and remember similarities
167        for (unsigned int i = 0; i < pvGenos.size(); i++)
168        {
169                for (unsigned int j = 0; j < pvGenos.size(); j++)
170                {
171                        dSimilarity = M.EvaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j));
172                        aaSimil[i][j] = dSimilarity;
173                }
174        }
175
176        if (bPrintNames)
177        {
178                // if "-names" switch was given, print the number of genotypes and their names
179                printf("%li\n", pvGenos.size());
180                for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++)
181                {
182                        printf("%s\n", pvNames.at(iGen));
183                }
184        }
185
186        // print out the matrix of similarities
187        for (unsigned int i = 0; i < pvGenos.size(); i++)
188        {
189                for (unsigned int j = 0; j < pvGenos.size(); j++)
190                {
191                        printf("%.2lf\t", aaSimil[i][j]);
192                }
193                printf("\n");
194        }
195
196        // delete vectors and arrays
197        for (unsigned int i = 0; i < pvGenos.size(); i++)
198        {
199                delete pvGenos.operator[](i);
200                delete[] pvNames.operator[](i);
201                delete[] aaSimil[i];
202        }
203
204        delete[] aaSimil;
205
206        return 0;
207}
Note: See TracBrowser for help on using the repository browser.