Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

gnodederived.cpp

Go to the documentation of this file.
00001 //****************************************************
00002 //  April, 1993, University of Illinois
00003 // Copyright (C) 1993, 1994 Tianlin Wang
00004 /* Copyright (C) 1994-2003 Matvec Development Team. 
00005 
00006   This program is free software; you can redistribute it and/or
00007   modify it under the terms of the GNU Library General Public
00008   License as published by the Free Software Foundation; either
00009   version 2 of the License, or (at your option) any later version.
00010   
00011   This program is distributed in the hope that it will be useful,
00012   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   Library General Public License for more details.
00015     
00016   You should have received a copy of the GNU Library General Public
00017   License along with this library; if not, write to the Free
00018   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019   MA 02111-1307, USA 
00020 */
00021 
00022 #include <algorithm>
00023 #include <cstdarg>
00024 #include <cstdlib>
00025 #include <cmath>
00026 #include <fstream>
00027 #include <iomanip>
00028 #include <iostream>
00029 #include <ext/hash_map>
00030 #include <map>
00031 #include <set>
00032 #include "safe_vectors.h"
00033 #include "gnodederived.h"
00034 #include "gnodesetderived.h"
00035 #include "gnodestuff.h"
00036 
00037 namespace matvec {
00038   
00039 inline int CutSet::getOldKey(void) {
00040   // Authors: L. Radu Totir and Rohan L. Fernando 
00041   // (June, 2003) 
00042   // Contributors: Robert Benson
00043   CutSet::iterator it;
00044   int key = 0;
00045   CutSet::iterator beginIt = begin();
00046   CutSet::iterator endIt = end();
00047   unsigned i=0;
00048   for (it=beginIt;it!=endIt;it++){
00049     key += ((*it)->getOldState())*keyMultiplicationCode[i];
00050     i++;
00051   }
00052   return key;
00053 }
00054 /*! \fn int CutSet::getKey(void) 
00055  *  \brief returns the string key to be used to put or retrieve
00056            values from the valueVector
00057 */
00058 
00059 double CutSet::getOldValue(void) {
00060   // Authors: L. Radu Totir and Rohan L. Fernando 
00061   // (June, 2003) 
00062   // Contributors: 
00063   int key = getOldKey();
00064   return valueVector[key];
00065 }
00066 /*! \fn double CutSet::getOldValue(void)
00067  *  \brief returns the relevant probability from the valueVector
00068  */  
00069 
00070 inline int CutSet::getKey(void) {
00071   // Authors: L. Radu Totir and Rohan L. Fernando 
00072   // (June, 2003) 
00073   // Contributors: Robert Benson
00074   CutSet::iterator it;
00075   int key = 0;
00076   CutSet::iterator beginIt = begin();
00077   CutSet::iterator endIt = end();
00078   unsigned i=0;
00079   for (it=beginIt;it!=endIt;it++){
00080     key += ((*it)->getState())*keyMultiplicationCode[i];
00081     i++;
00082   }
00083   return key;
00084 }
00085 /*! \fn int CutSet::getKey(void) 
00086  *  \brief returns the string key to be used to put or retrieve
00087  values from the valueVector
00088 */
00089 
00090 double CutSet::getValue(void) {
00091   // Authors: L. Radu Totir and Rohan L. Fernando 
00092   // (June, 2003) 
00093   // Contributors:
00094                 int key = getKey();
00095                 if(key > valueVector.size()){
00096                         cout << "key > size in CutSet::getValue\n";
00097                         exit(1);
00098                 }
00099                 return valueVector[key];
00100 }
00101 /*! \fn double CutSet::getValue(void)
00102  *  \brief returns the relevant probability from the valueVector
00103  */  
00104 
00105 void CutSet::putValue(double x) {
00106         // Authors: L. Radu Totir and Rohan L. Fernando 
00107         // (June, 2003) 
00108         // Contributors:
00109         int key = getKey();
00110         if(key > valueVector.size()){
00111                 cout << "key > size in CutSet::putValue\n";
00112                 exit(1);
00113         }
00114         valueVector[key] = x;
00115 }
00116 /*! \fn void CutSet::putValue(double x)
00117  *  \brief puts the value x in the valueVector
00118  */  
00119   
00120 void CutSet::setKeyMultiplicationCode(void){
00121   // Authors: L. Radu Totir and Rohan L. Fernando 
00122   // (July, 2004) 
00123   // Contributors:
00124   unsigned mySize = (size()) ? size():1;
00125   keyMultiplicationCode.resize(mySize);
00126   CutSet::iterator it;
00127   CutSet::iterator beginIt = begin();
00128   CutSet::iterator endIt = end();
00129   unsigned state = 1;
00130   unsigned i = 0;
00131   for (it=beginIt;it!=endIt;it++){
00132         keyMultiplicationCode[i] = (*it)->sampled ? 0 : state;
00133     i++;
00134     state *= (*it)->sampled ? 1 : (*it)->getWeight();
00135   }
00136   valueVector.resize(state,0.0);
00137 }
00138 /*! \fn void CutSet::setKeyMultiplicationCode(double x) 
00139  * \brief sets the multiplication code used to create the key to store 
00140  *        and retreive CutSet probabilities from valueVector. The code 
00141  *        for the first GNode in the CutSet is set to 1, and as a general 
00142  *        rule, the code for the n'th GNode in the cutset is set to the 
00143  *        product between the code of (n -1)'th GNode multiplied by the 
00144  *        number of possible GNode (genotype or allele) states of GNode n-1. 
00145  *        Also we resize valueVector
00146  */  
00147 
00148 GNode* CutSet::getLocation(void){
00149   // Authors: L. Radu Totir and Rohan L. Fernando 
00150   // (August, 2003) 
00151   // Contributors:
00152   CutSet::iterator it;
00153   set<GNodeSet*>::iterator itFound;
00154   CutSet::iterator beginIt = begin();
00155   CutSet::iterator endIt = end();
00156   for (it=beginIt;it!=endIt;it++){
00157     itFound = (*it)->SetofGNsts.find(this);
00158     if(itFound!=(*it)->SetofGNsts.end()){
00159       return *it;
00160     }
00161   }
00162 }
00163   
00164 void CutSet::normalize(){
00165   // Authors: L. Radu Totir and Rohan L. Fernando 
00166   // (February, 2004) 
00167   // Contributors:    
00168   double sum = 0.0;
00169   double temp= 1.0;
00170   for(unsigned i=0;i!=valueVector.size();i++){
00171     // cout << "sum after summing " << i+1 << " value = " << sum << endl;
00172     sum+=valueVector[i];
00173   }
00174   if(sum!=0.0){
00175     temp = 1/sum;
00176   }
00177   // cout << "Sum in normalize() = " << sum << endl;
00178   if(sum>1e-20){ // original value -14, set to -20 by me (LRT 9/24/04)
00179     //       cout << "Unscaled probabilities: " << endl;
00180     //       displayValues();
00181     for(unsigned i=0;i!=valueVector.size();i++){
00182       valueVector[i]*=temp;
00183     } 
00184     //       cout << "Scaling factor = " << temp << endl;
00185     //       CutSet::iterator itC;
00186     //       cout << "CutSet Members: [";
00187     //       for (itC=begin();itC!=end();itC++){
00188     //  cout << (*itC)->id << " ";
00189     //       } 
00190     //       cout << "]" << endl;
00191     //       cout << "Scaled probabilities: " << endl;
00192     //       displayValues();
00193   }
00194   else if(sum==0.0){
00195     throw matvec::InvalidSample();
00196   }
00197   else{
00198     cout << "Sum used for scaling = " << sum << endl;
00199     CutSet::iterator itC;
00200     cout << "CutSet Members: [";
00201     for (itC=begin();itC!=end();itC++){
00202       cout << (*itC)->id << " ";
00203     } 
00204     cout << "]" << endl;
00205     displayValues();
00206     cerr << " Need scaling in CutSet::normalize(), sum = " << sum << endl;
00207     exit(1);
00208   }
00209 }
00210   
00211 void CutSet::displayValues(void){
00212   // Authors: L. Radu Totir and Rohan L. Fernando 
00213   // (February, 2004) 
00214   // Contributors:
00215   for(unsigned i=0;i!=valueVector.size();i++){
00216     cout << valueVector[i] << endl;
00217   } 
00218 }
00219 /*! \fn void CutSet::displayValues(void)
00220  *  \brief displays the values stored in the valueVector
00221 */  
00222 
00223 void CutSet::replaceMeWith(CutSet& A){
00224   // Authors: L. Radu Totir and Rohan L. Fernando 
00225   // (June, 2003) 
00226   // Contributors:
00227   GNodeSet::iterator it;
00228   connectFlag = A.connectFlag;
00229   numberOfCuts = A.numberOfCuts;
00230   prior = A.prior;
00231   currentLocus = A.currentLocus;
00232   clear();
00233   for (it=A.begin();it!=A.end();it++){
00234     insert(*it);
00235   }
00236   keyMultiplicationCode = A.keyMultiplicationCode;
00237   valueVector = A.valueVector;
00238 }
00239   
00240 CutSet& CutSet::operator= (GNodeSet* A){
00241   // Authors: L. Radu Totir and Rohan L. Fernando 
00242   // (June, 2003) 
00243   // Contributors:
00244   reset();
00245   do{
00246     double Atemp = A->getValue();
00247     (*this).putValue(Atemp);
00248   } while(incr());
00249   return *this;
00250 }
00251 /*! \fn CutSet& CutSet::operator= (GNodeSet* A)
00252  *  \brief overloaded = operator, puts the values contained by the
00253      larger set (the neighborhood set) into the new set.
00254 */  
00255 
00256 CutSet& CutSet::operator+= (GNodeSet* A){
00257   // Authors: L. Radu Totir and Rohan L. Fernando 
00258   // (June, 2003) 
00259   // Contributors:
00260   A->reset();
00261   do{
00262     //double temp  = getValue();
00263         unsigned key = getKey();
00264     double Atemp = A->getValue();
00265     //putValue(temp + Atemp);
00266         valueVector[key] += Atemp;
00267   } while(A->incr());
00268   //scalling next
00269   normalize();
00270   return *this;
00271 }
00272 /*! \fn CutSet& CutSet::operator+= (GNodeSet* A)
00273  *  \brief overloaded += operator, adds the values contained by the
00274  larger set (the neighborhood set) into the new set.
00275 */ 
00276   
00277 CutSet& CutSet::operator*= (GNodeSet* A){
00278   // Authors: L. Radu Totir and Rohan L. Fernando 
00279   // (June, 2003) 
00280   // Contributors:
00281   reset();
00282   do{
00283         unsigned key = getKey();  
00284     //double temp = getValue();
00285     double Atemp = A->getValue();
00286     //putValue(temp*Atemp);
00287         valueVector[key] *= Atemp;
00288   } while(incr());
00289   normalize();
00290   return *this;
00291 }
00292 /*! \fn CutSet& CutSet::operator*= (GNodeSet* A)
00293  *  \brief overloaded *= operator, multiplies the values contained
00294  by the various cutsets.
00295 */   
00296 
00297 bool AlleleStateNode::incr(void){
00298   // Authors: L. Radu Totir and Rohan L. Fernando 
00299   // (based on the incr() of Bernt Guldbrantdsen)
00300   // (June, 2003) 
00301   // Contributors: 
00302   alleleState++;
00303   if (alleleState==alleleStateVector.size()){
00304     alleleState=0;
00305     return 0;
00306   }
00307   else {
00308     return 1;
00309   }
00310 }
00311 /*! \fn bool AlleleStateNode::incr(void)
00312  *  \brief the method used to increment the alleleState of an
00313  AlleleStateNode
00314 */
00315 
00316 bool AlleleOriginNode::incr(void){
00317   // Authors: L. Radu Totir and Rohan L. Fernando 
00318   // (based on the incr() of Bernt Guldbrantdsen)
00319   // (September, 2004) 
00320   // Contributors: 
00321   alleleOrigin++;
00322   if (alleleOrigin==alleleOriginVector.size()){
00323     alleleOrigin=0;
00324     return 0;
00325   }
00326   else {
00327     return 1;
00328   }
00329 }
00330 /*! \fn bool AlleleOriginNode::incr(void)
00331  *  \brief the method used to increment the alleleOrigin of an
00332  AlleleOriginNode
00333 */
00334   
00335 bool GenotypeNode::incr(){
00336   // Authors: L. Radu Totir and Rohan L. Fernando 
00337   // (based on the incr() of Bernt Guldbrantdsen)
00338   // (June, 2003) 
00339   // Contributors:
00340   genotypeState++;
00341   if (genotypeState==genotypeVector.size()){
00342     genotypeState=0;
00343     return 0;
00344   }
00345   else {
00346     return 1;
00347   }
00348 }
00349 /*! \fn bool GenotypeNode::incr(void)
00350  *  \brief the method used to increment the genotypeState of a
00351  GenotypeNode
00352 */
00353 } // end of namespace matvec
00354 
00355 

Generated on Thu Jun 16 17:13:43 2005 for Matvec by doxygen1.2.16