Changeset 606 for cpp/frams/_demos


Ignore:
Timestamp:
08/30/16 17:42:29 (4 years ago)
Author:
Maciej Komosinski
Message:

Code formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/_demos/simil_test.cpp

    r605 r606  
    1616
    1717/** 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. */
     18        .gen file, using the matching and measure weights as specified in the
     19        command line. */
    2020int main(int argc, char *argv[])
    2121{
    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()];
     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.zFixed);
     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.zFixed != 0 && M.zFixed != 1)
     115        {
     116                printf("<isZFixed>=%d. <isZFixed> should be equal to 0 or 1\n", M.zFixed);
     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()];
    160162                for (unsigned int l = 0; l < pvGenos.size(); l++)
    161             aaSimil[k][l] = 0.0;
    162     }
    163 
    164     // compute and remember similarities
     163                        aaSimil[k][l] = 0.0;
     164        }
     165
     166        // compute and remember similarities
    165167        for (unsigned int i = 0; i < pvGenos.size(); i++)
    166     {
     168        {
    167169                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());
     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());
    178180                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
     181                {
     182                        printf("%s\n", pvNames.at(iGen));
     183                }
     184        }
     185
     186        // print out the matrix of similarities
    185187        for (unsigned int i = 0; i < pvGenos.size(); i++)
    186     {
     188        {
    187189                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
     190                {
     191                        printf("%.2lf\t", aaSimil[i][j]);
     192                }
     193                printf("\n");
     194        }
     195
     196        // delete vectors and arrays
    195197        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;
     198        {
     199                delete pvGenos.operator[](i);
     200                delete[] pvNames.operator[](i);
     201                delete[] aaSimil[i];
     202        }
     203
     204        delete[] aaSimil;
     205
     206        return 0;
    205207}
Note: See TracChangeset for help on using the changeset viewer.