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

Last change on this file since 605 was 605, checked in by Maciej Komosinski, 4 years ago
  • Added 'fixedZaxis' option to paramtab (so that it is accessible from scripts)
  • Variable renames, improvements in comments
File size: 6.5 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        printf("Parameters:\n");
35        printf("  <genotypesFile> name of a file with genotypes\n");
36        printf("  <w_dP> weight of the difference in the number of parts\n");
37        printf("  <w_dDEG> weight of the difference in degrees of matched parts\n");
38        printf("  <w_dNEU> weight of the difference in neurons of matched parts\n");
39        printf("  <w_dGEO> weight of the distance of matched parts\n");
40        printf("  <fixZaxis?> should the 'z' (vertical) coordinate be fixed during the alignment? (0 or 1)\n\n");
41        printf("Switches:\n");
42        printf("  -names specifies that the number and names of genotypes are to be printed to output\n");
43        printf("  before the distance matrix; by default the number and names are not printed\n\n");
44
45        printf("Outputs a symmetric distance matrix in the format:\n");
46        printf("  <row_1> (columns in a row are separated by TABs)\n");
47        printf("  ...\n");
48        printf("  <row_n>\n");
49
50        return -1;
51    }
52
53    // prepare output parameters from .gen file
54    vector<Geno *> pvGenos;
55    vector<char *> pvNames;
56
57    // check if there is a switch
58    iCurrParam = 1;
59    szCurrParam = argv[ iCurrParam ];
60    if (strcmp(szCurrParam, "-names") == 0)
61    {
62        // switch "-names" was given; print names also
63        bPrintNames = true;
64        // pass to the next parameter
65        iCurrParam++;
66    }
67
68    // check the parameters
69    // get <genotypesFile> name from command line
70    char *szFileName = argv[ iCurrParam ];
71
72    // initially set measure components' weights to invalid values (negative)
73    for (int i = 0; i < M.GetNOFactors(); i++)
74    {
75        M.m_adFactors[i] = -1.0;
76    }
77
78    const char *params[] = {"<w_dP>", "<w_dDEG>", "<w_dNEU>", "<w_dGEO>"};
79    for (int i = 0; i < M.GetNOFactors(); i++)
80    {
81        iCurrParam++;
82        szCurrParam = argv[ iCurrParam ];
83        nResult = sscanf(szCurrParam, " %lf ", & M.m_adFactors[ i ]);
84        if (nResult != 1)
85        {
86            // <w_dX> is not a number -- error
87            printf("%s", params[i]);
88            printf(" should be a number\n");
89            return -1;
90        }
91        else
92        {
93            // <w_dX> is a number; check if nonnegative
94            if (M.m_adFactors[ i ] < 0.0)
95            {
96                printf("%s", params[i]);
97                printf(" should be a nonnegative number\n");
98                return -1;
99            }
100        }
101    }
102
103    iCurrParam++;
104    szCurrParam = argv[ iCurrParam ];
105    nResult = sscanf(szCurrParam, " %d", & M.zFixed);
106    if (nResult != 1)
107    {
108        // <isZFixed> is not a number -- error
109        printf("<isZFixed> should be a number\n");
110        return -1;
111    }
112    else if (M.zFixed != 0 && M.zFixed != 1)
113    {
114        printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.zFixed);
115        return -1;
116    }
117   
118    // read the input file
119    // prepare loading of genotypes from a .gen file
120    // create some basic genotype converters
121    PreconfiguredGenetics genetics;
122    StdioFileSystem_autoselect stdiofilesys;
123
124    long count = 0, totalsize = 0;
125    MiniGenotypeLoader loader(szFileName);
126    MiniGenotype *loaded;
127    while (loaded = loader.loadNextGenotype())
128    {
129        // while a valid genotype was loaded
130        count++;
131        totalsize += loaded->genotype.len();
132        // create a Geno object based on the MiniGenotype
133        Geno *pNextGenotype = new Geno(loaded->genotype);
134        if ((pNextGenotype != NULL) && (pNextGenotype->isValid()))
135        {
136            pvGenos.push_back(pNextGenotype);
137            char *szNewName = new char [ loaded->name.len() + 1];
138            strcpy(szNewName, loaded->name.c_str());
139            pvNames.push_back(szNewName);
140        }
141        else
142        {
143            printf("Genotype %2li is not valid\n", count);
144            if (pNextGenotype!=NULL) delete pNextGenotype;
145        }
146    }
147    if (loader.getStatus() == MiniGenotypeLoader::OnError)
148    {
149        printf("Error: %s", loader.getError().c_str());
150    }
151
152    double dSimilarity = 0.0;
153    double **aaSimil = NULL; // array of similarities
154
155    // create the empty array of similarities
156    aaSimil = new pDouble [pvGenos.size()];
157    for (unsigned int k = 0; k < pvGenos.size(); k++)
158    {
159        aaSimil[k] = new double [pvGenos.size()];
160                for (unsigned int l = 0; l < pvGenos.size(); l++)
161            aaSimil[k][l] = 0.0;
162    }
163
164    // compute and remember similarities
165        for (unsigned int i = 0; i < pvGenos.size(); i++)
166    {
167                for (unsigned int j = 0; j < pvGenos.size(); j++)
168        {
169            dSimilarity = M.EvaluateDistance(pvGenos.operator[](i), pvGenos.operator[](j));
170            aaSimil[i][j] = dSimilarity;
171        }
172    }
173
174    if (bPrintNames)
175    {
176        // if "-names" switch was given, print the number of genotypes and their names
177        printf("%li\n", pvGenos.size());
178                for (unsigned int iGen = 0; iGen < pvNames.size(); iGen++)
179        {
180            printf("%s\n", pvNames.at(iGen));
181        }
182    }
183
184    // print out the matrix of similarities
185        for (unsigned int i = 0; i < pvGenos.size(); i++)
186    {
187                for (unsigned int j = 0; j < pvGenos.size(); j++)
188        {
189            printf("%.2lf\t", aaSimil[i][j]);
190        }
191        printf("\n");
192    }
193
194    // delete vectors and arrays
195        for (unsigned int i = 0; i < pvGenos.size(); i++)
196    {
197        delete pvGenos.operator[](i);
198        delete [] pvNames.operator[](i);
199        delete [] aaSimil[i];
200    }
201
202    delete [] aaSimil;
203
204    return 0;
205}
Note: See TracBrowser for help on using the repository browser.