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

gnodestuff.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 <map>
00030 #include <set>
00031 #include "safe_vectors.h"
00032 #include "stat.h"
00033 #include "gnodederived.h"
00034 #include "gnodesetderived.h"
00035 #include "gnodestuff.h"
00036 #include "population.h"
00037 #include "model.h"
00038 using namespace std; 
00039 
00040 namespace matvec {
00041 GNodeList* GNode::gNodeListPtr;
00042 GeneticDist *GNodeSet::prior;
00043 unsigned GNodeSet::currentLocus;
00044 double GNodeList::logTarget = 0.0;
00045 double GNodeList::logProposal = 0.0;
00046 double GNodeList::logOldProposal = 0.0;
00047 Population *GNodeList::popPtr;
00048 int TransitionSet::transmissionType = 1;
00049 
00050 ////////////////// GNodeList methods /////////////////////
00051 
00052 void GNodeList::makealleleGNodeSets(char* infilename,char* outfilename){
00053   // Authors: L. Radu Totir and Rohan L. Fernando 
00054   // (June, 2003) 
00055   // Contributors:
00056   ifstream infile(infilename);
00057   ofstream outfile(outfilename);
00058   unsigned ind,mom,dad;
00059   while (infile >> ind >> dad >> mom) {
00060     outfile << "2   " << (ind-1)*2 +1 << setw(5) << (ind-1)*2 +2 << endl;
00061     if(!dad) continue;
00062     outfile << "3   " << (dad-1)*2 +1 << setw(5) << (dad-1)*2 +2 << setw(5) << (ind-1)*2 +1 << endl;
00063     outfile << "3   " << (mom-1)*2 +1 << setw(5) << (mom-1)*2 +2 << setw(5) << (ind-1)*2 +2 << endl;
00064   }
00065 }
00066 /*! \fn void GNodeList::makealleleGNodeSets(char* infilename,char*
00067     outfilename)
00068  *  \brief creates the AlleleNodeSets from the pedigree
00069 */
00070 
00071 void GNodeList::makegenotGNodeSets(char* infilename,char* outfilename){
00072   // Authors: L. Radu Totir and Rohan L. Fernando 
00073   // (June, 2003) 
00074   // Contributors:
00075   ifstream infile(infilename);
00076   ofstream outfile(outfilename);
00077   unsigned ind,mom,dad;
00078   while (infile >> ind >> dad >> mom) {
00079     if(!dad) continue;
00080     outfile << "3   " << dad << setw(5) << mom << setw(5) << ind << endl;
00081   }
00082 }
00083 /*! \fn void GNodeList::makegenotGNodeSets(char* infilename,char*
00084     outfilename)
00085  *  \brief creates the GenotypeNodeSets from the pedigree
00086 */
00087  
00088 void GNodeList::fill(unsigned n){
00089   // Authors: L. Radu Totir and Rohan L. Fernando 
00090   // (June, 2003) 
00091   // Contributors:
00092   resize(n);
00093   for (unsigned i=0;i<n;i++){
00094     GNode* elm_ptr = new GNode;
00095     elm_ptr->peelorder = i+1;
00096     elm_ptr->weight = 1;
00097     (*this)[i] = elm_ptr;
00098   }
00099 }
00100 /*! \fn void GNodeList::fill(unsigned n)
00101  *  \brief method used in determining peeling order for a pedigree
00102     (used only if make....GNodeSets is not called)
00103 */
00104 
00105 void GNodeList::inputGNodeSets(char* fname){
00106   // Authors: L. Radu Totir and Rohan L. Fernando 
00107   // (June, 2003) 
00108   // Contributors:  
00109   ifstream setfile(fname);
00110   unsigned n,member;
00111   lastmember = 0;
00112   while (setfile >> n ) {
00113     for (unsigned i=0;i<n;i++){
00114       setfile >> member;
00115       if (member>lastmember){
00116         lastmember=member;
00117       }
00118     }
00119   }
00120   setfile.close();
00121   ifstream setfile1(fname);
00122   fill(lastmember);
00123   while (setfile1 >> n ) {
00124     GNodeSet* myset = new GNodeSet;
00125     for (unsigned i=0;i<n;i++){
00126       setfile1 >> member;
00127       GNode* elm_ptr = (*this)[member-1];
00128       myset->insert(elm_ptr);
00129       elm_ptr->SetofGNsts.insert(myset);
00130     }
00131   }
00132 }
00133 /*! \fn void GNodeList::inputGNodeSets(char* fname)
00134  *  \brief method used in determining peeling order for a pedigree
00135     (used only if none of the make....GNodeSets methods is called)
00136 */
00137 
00138 void GNodeList::displayGNodeSets(){
00139         // Authors: L. Radu Totir and Rohan L. Fernando 
00140         // (June, 2003) 
00141         // Contributors:
00142         GNodeList::iterator it;
00143         set< GNodeSet*>::iterator GNodeSetit;
00144         GNodeSet::iterator setit;
00145         for (it=begin();it!=end();it++){
00146                 std::cout << (*it)->id << "   ";
00147                 for (GNodeSetit=(*it)->SetofGNsts.begin();GNodeSetit!=(*it)->SetofGNsts.end();GNodeSetit++){
00148                         (*GNodeSetit)->display();
00149                 }
00150                 std::cout << endl;
00151         }
00152         std::cout << "=========== " << endl;
00153 }
00154 /*! \fn void GNodeList::displayGNodeSets()
00155  *  \brief displays the GNodeSets of each Node (pentrance, founder,
00156     transition sets)
00157 */
00158 
00159 void GNodeList::getPeelOrder(char* fname){
00160   // Authors: L. Radu Totir and Rohan L. Fernando 
00161   // (June, 2003) 
00162   // Contributors:
00163   ofstream ordfile(fname);
00164   GNodeList::iterator it1,it2,peel_me;
00165   GNode* temp;
00166   float mag, min;
00167   sum =0.0;
00168   for (it1=begin();it1!=end();it1++){ 
00169     min = 1e+100;
00170     for (it2=it1;it2!=end();it2++){
00171       mag = (*it2)->getCutsetMagnitude();
00172       if (mag<min){
00173         peel_me = it2;
00174         min=mag;
00175       }
00176     }    
00177     sum+=pow(double(4.0),double(min));
00178     ordfile << (*peel_me)->peelorder <<"     "<< min << endl;
00179     //(*peel_me)->peelord = ++ord;
00180     (*peel_me)->updateMysets();
00181     temp = *it1;
00182     *it1 = *peel_me;
00183     *peel_me = temp;  
00184   }
00185   ordfile << "Network size = " << sum << endl; 
00186   std::cout << sum << endl; 
00187 }
00188 /*! \fn void GNodeList::getPeelOrder(char* fname)
00189  *  \brief returns the peeling order for a pedigree provided in the
00190     file given as an argument
00191 */
00192 
00193 void GNodeList::peelCutAndCompute(unsigned maxCutSetSize, unsigned startBlock, unsigned stopBlock, unsigned sizeBlock){
00194   // Authors: L. Radu Totir and Rohan L. Fernando 
00195   // (October, 2003) 
00196   // Contributors: 
00197   unsigned sampledCount = 0;
00198   GNodeList::reverse_iterator it;
00199   setGNodeSampleFlags(startBlock, stopBlock, sizeBlock);        
00200   if (GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].peelOrder.size()==size()){
00201     sampleGNodePtr = peelAndCutFast();
00202   }
00203   else{
00204     // cout << "We determine the peeling order again (inefficient!!)" << endl;
00205     sampleGNodePtr = peelAndCut(maxCutSetSize);
00206   }
00207   // cout << "I am calculating the probability of the previous sample" << endl;
00208   // cout << "Log Old Proposal before processing locus " << Individual::currentLocus+1 << " = " << GNodeList::logOldProposal << endl;
00209   //cout << "Log Target before = " << GNodeList::logTarget << endl;
00210   while (sampleGNodePtr) {
00211     sampledCount++;
00212     //displayGNodeSets();
00213     cout << "GNode sampled after " << sampledCount << " 'th peeling: " << sampleGNodePtr->id << endl;
00214     sampleGNodePtr->reComputeGNode();
00215     reinitGNodeList();
00216     //displayGNodeSets();
00217     sampleGNodePtr = peelAndCut(maxCutSetSize);
00218   }
00219   //cout << "Number of GNodes sampled to break loops = " << sampledCount << endl;
00220   //cout << "GNodeList before sampling all GNodes" << endl;
00221   for (it=rbegin();it!=rend();it++){
00222     if(!(*it)->sampled){
00223       (*it)->reComputeGNode();
00224     }
00225   }
00226   checkSample();
00227   // cout << "Log Old Proposal after processing locus " << Individual::currentLocus+1 << " = " << GNodeList::logOldProposal << endl;
00228   //calculateTargetProb();
00229   //cout << "Log Target after = " << GNodeList::logTarget << endl;
00230   //cout << "Final gNodeList (after peeling, cutting and sampling):" << endl;
00231   //displayGNodeSets();
00232   clearGNodeListForNextLocus();
00233 }
00234 /*! \fn void GNodeList::peelCutAndSample()
00235  *  \brief sample genotypes after peeling and cutting(if necesary)
00236 */
00237 
00238 void GNodeList::peelCutAndSample(unsigned maxCutSetSize, unsigned startBlock, unsigned stopBlock, unsigned sizeBlock){
00239   // Authors: L. Radu Totir and Rohan L. Fernando 
00240   // (October, 2003) 
00241   // Contributors: 
00242   unsigned printFlag = popPtr->model->myRSamplerParms.printFlag;                
00243   unsigned sampledCount = 0;
00244   Model *modelPtr = popPtr->model;      
00245   GNodeList::reverse_iterator it;
00246   setGNodeSampleFlags(startBlock, stopBlock, sizeBlock);  
00247   bool cutLoops = GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].cutLoops;
00248   if (GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].peelOrder.size()==size() && !cutLoops){
00249     sampleGNodePtr = peelAndCutFast();
00250   }
00251   else{
00252           // cout << "We determine the peeling order again (inefficient!!)" << endl;
00253           popPtr->SimpleGenotypeElimination(Individual::currentLocus);                  
00254           if (sizeBlock == size() && GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].qtl_ml=='m') {
00255                   popPtr->LGGenotypeElimination(Individual::currentLocus); 
00256           }
00257           popPtr->setAlleleStateVectors(Individual::currentLocus);
00258           sampleGNodePtr = peelAndCut(maxCutSetSize);
00259   }
00260   //cout << "I am actually sampling a new sample" << endl;
00261   //cout << "Log Proposal before processing locus " << Individual::currentLocus+1 << " = " << GNodeList::logProposal << endl;
00262   //cout << "Log Target before = " << GNodeList::logTarget << endl;
00263   while (sampleGNodePtr) {
00264     sampledCount++;
00265     //displayGNodeSets();
00266     if(printFlag>0) cout << "GNode sampled after " << sampledCount << " 'th peeling: " << sampleGNodePtr->id << endl;
00267     sampleGNodePtr->sampleGNode();
00268     reinitGNodeList();
00269     //displayGNodeSets();
00270         if (modelPtr->myRSamplerParms.samplerType=="genotypic"){
00271                 popPtr->LGGenotypeElimination(Individual::currentLocus);
00272                 popPtr->setAlleleStateVectors(Individual::currentLocus);
00273         }
00274     sampleGNodePtr = peelAndCut(maxCutSetSize);
00275   }
00276   if(printFlag>0) cout << "Number of GNodes sampled to break loops = " << sampledCount << endl;
00277   //cout << "GNodeList before sampling all GNodes" << endl;
00278   for (it=rbegin();it!=rend();it++){
00279     if(!(*it)->sampled){
00280       (*it)->reverseSampleGNode();
00281     }
00282   }
00283   checkSample();
00284   //cout << "Log Proposal after processing locus " << Individual::currentLocus+1 << " = " << GNodeList::logProposal << endl;
00285   calculateTargetProb();
00286   //cout << "Log Target after = " << GNodeList::logTarget << endl;
00287   //cout << "Final gNodeList (after peeling, cutting and sampling):" << endl;
00288   //displayGNodeSets();
00289   clearGNodeListForNextLocus();
00290 }
00291 /*! \fn void GNodeList::peelCutAndSample()
00292  *  \brief sample genotypes after peeling and cutting(if necesary)
00293 */
00294 
00295 void GNodeList::peelOrderCutAndSample(unsigned maxCutSetSize, unsigned startBlock, unsigned stopBlock, unsigned sizeBlock){
00296   // Authors: L. Radu Totir and Rohan L. Fernando 
00297   // (February, 2004) 
00298   // Contributors: 
00299   unsigned printFlag = popPtr->model->myRSamplerParms.printFlag; 
00300   Model *modelPtr = popPtr->model;
00301   unsigned sampledCount = 0;
00302   GNodeList::reverse_iterator it;
00303   setGNodeSampleFlags(startBlock, stopBlock, sizeBlock); 
00304   sampleGNodePtr = peelAndCut(maxCutSetSize);
00305   GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].cutLoops = false;
00306   //cout << "Log Proposal before = " << GNodeList::logProposal << endl;
00307   //cout << "Log Target before = " << GNodeList::logTarget << endl;
00308   while (sampleGNodePtr) {
00309         GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].cutLoops = true;  
00310     sampledCount++;
00311     //displayGNodeSets();
00312     if(printFlag>0) cout << "GNode sampled after " << sampledCount << " 'th peeling: " << sampleGNodePtr->id << endl;
00313     sampleGNodePtr->sampleGNode();
00314     reinitGNodeList();
00315         if (modelPtr->myRSamplerParms.samplerType=="genotypic"){
00316                 popPtr->LGGenotypeElimination(Individual::currentLocus);
00317                 popPtr->setAlleleStateVectors(Individual::currentLocus);
00318         }
00319     //displayGNodeSets();
00320     sampleGNodePtr = peelAndCut(maxCutSetSize);
00321   }
00322   if(printFlag>0) cout << "Number of GNodes sampled to break loops = " << sampledCount << endl;
00323   //cout << "GNodeList before sampling all GNodes" << endl;
00324   for (it=rbegin();it!=rend();it++){
00325     if(!(*it)->sampled){
00326       (*it)->reverseSampleGNode();
00327     }
00328   }
00329   checkSample();
00330   //cout << "Log Proposal after = " << GNodeList::logProposal << endl;
00331   calculateTargetProb();
00332   //cout << "Log Target after = " << GNodeList::logTarget << endl;
00333   //cout << "Final gNodeList (after peeling, cutting and sampling):" << endl;
00334   //displayGNodeSets();
00335   clearGNodeListForNextLocus();
00336 }
00337 /*! \fn void GNodeList::peelOrderCutAndSample()
00338  *  \brief sample genotypes after determining peeling order, peeling
00339     and cutting(if necesary)
00340 */
00341 
00342 GNode* GNodeList::peelAndCut(unsigned maxCutSetSize){
00343         // Authors: L. Radu Totir and Rohan L. Fernando 
00344         // (August, 2003) 
00345         // Contributors: 
00346         unsigned printFlag = popPtr->model->myRSamplerParms.printFlag;          
00347         if (!distanceMatDone) makeDistanceMatrix(); 
00348         cutGNodesVector.clear();
00349         loopSetGNodes.clear();  
00350         searchStart = begin();
00351         resetConnectFlags();
00352         sampleGNodePtr=NULL;
00353         maxDist = 0;
00354         GNodeList::iterator it1,it2,peel_me;
00355         GNode* temp;
00356         float mag, min;
00357         sum =0.0;
00358         unsigned count=0;
00359         unsigned loopCount=0;
00360         it1=begin();
00361         while (it1!=end()){
00362                 min = 1e+100;
00363                 for (it2=it1;it2!=end();it2++){
00364                         mag = (*it2)->getCutsetMagnitude();
00365                         if (mag<min){
00366                                 peel_me = it2;
00367                                 min=mag;
00368                         }
00369                 } 
00370                 if(min<=maxCutSetSize){
00371                         sum+=min;
00372                         (*peel_me)->peelorder = ++count;
00373                         (*peel_me)->updateMysets();
00374                         if(printFlag>1) cout << "Peeling node " << count << endl;
00375                         (*peel_me)->peel();
00376                         if(howToSample=="joint"){
00377                                 GNodeSet::prior->chrom()[0].peelOrder[count-1] = (*peel_me)->id;
00378                         }
00379                         else{
00380                                 GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].peelOrder[count-1] = (*peel_me)->id;
00381                         }
00382                         temp = *it1;
00383                         *it1 = *peel_me;
00384                         *peel_me = temp;
00385                         it1++;
00386                 }
00387                 else {
00388                         if(printFlag>1) cout << "Peeled " << count << " gNodes, now I need to cut ";
00389                         searchStart = it1;
00390                         sort(searchStart,end(),compareGNodesWeight());
00391                         findLoopAndCut();
00392                         loopCount++;
00393                 }
00394                 if(printFlag>2) {
00395                         displayGNodeSets();
00396                 }
00397         }
00398         if (cutGNodesVector.size()>0){
00399                 sampleGNodePtr = findSampleGNode();
00400         }               
00401         return sampleGNodePtr;
00402 }
00403 /*! \fn void GNodeList::peelAndCut()
00404  *  \brief determines peeling order, peels and cuts (if it needs to) 
00405 */ 
00406 
00407 GNode* GNodeList::findSampleGNode(void){
00408         // Authors: Joseph Abraham and Rohan L. Fernando 
00409         // (October, 2004) 
00410         // Contributors: 
00411         unsigned maxDistance = 0;
00412         
00413         GNode* sampleGuy = 0;
00414         set<GNode*>::iterator it;
00415         SafeSTLVector<GNode*>::iterator cutListIter;
00416         for(it=loopSetGNodes.begin();it!=loopSetGNodes.end();it++){
00417                 if((*it)->sampled) continue;
00418                 unsigned rowId = (*it)->id;
00419                 unsigned sumDist = 0;
00420                 for(cutListIter=cutGNodesVector.begin();cutListIter!=cutGNodesVector.end();cutListIter++){
00421                         unsigned colId = (*cutListIter)->id;
00422                         sumDist += distanceMat(rowId,colId);
00423                 }
00424                 cout << "distance " << sumDist << endl;
00425                 if (sumDist>maxDistance){
00426                         maxDistance = sumDist;
00427                         sampleGuy = (*it);
00428                 }
00429         }
00430         if (sampleGuy){
00431                 return sampleGuy;
00432         }
00433         else {
00434                 throw exception("GNodeList::findSampleGNode: Error- possible bug in code\n");
00435         }
00436 }
00437 /*! \fn GNode* GNodeList::findSampleGNode()
00438 *  \brief returns pointer to GNode with largest sum of distances to the "cuts"
00439 */ 
00440 void GNodeList::makeDistanceMatrix(void){
00441         // Authors: Joseph Abraham and Rohan L. Fernando 
00442         // (October, 2004) 
00443         // Contributors: 
00444         unsigned numGnodes = size();
00445         distanceMat.resize(numGnodes,numGnodes);
00446         GNodeList::iterator it;
00447         for (it=begin();it!=end();it++){
00448                 calcDistancefrom(*it);
00449         }
00450         distanceMatDone = true;
00451         //cout << distanceMat;
00452 }
00453 /*! \fn void GNodeList::makeDistanceMatrix
00454 *  \brief fills in distanceMatrix for all pairs of nodes
00455 */ 
00456 void GNodeList::calcDistancefrom(GNode* refGNode){
00457         // Authors: Joseph Abraham and Rohan L. Fernando 
00458         // (October, 2004) 
00459         // Contributors: 
00460         
00461         vector<unsigned> dist;
00462         unsigned UHUGE = 2*ULONG_MAX;
00463         GNode *gnodePtr;
00464         GNodeList::iterator GNodeListiter;
00465         set<GNodeSet*>::iterator GNodeSetiter;
00466         set<GNode*>::iterator GNodeiter;
00467         vector<GNode*>::iterator GreyListiter;
00468         vector<GNode*> GreyList;
00469         
00470         GreyList.clear();
00471         unsigned rowId = refGNode->id;
00472         for(GNodeListiter = begin(); GNodeListiter != end(); GNodeListiter++){
00473                 (*GNodeListiter)->connectFlag =  ULONG_MAX; // every one gets white color
00474         }
00475     refGNode->connectFlag = 0; // this is colored gray
00476         GreyList.push_back(refGNode);
00477         distanceMat(rowId,rowId) = 0;
00478         while (GreyList.size() > 0){
00479                 gnodePtr = GreyList.back();
00480                 GreyList.pop_back();
00481                 unsigned uId = gnodePtr->id;
00482                 for(GNodeSetiter= gnodePtr->SetofGNsts.begin();GNodeSetiter !=gnodePtr->SetofGNsts.end();GNodeSetiter++){      
00483                         for(GNodeiter = (*GNodeSetiter)->begin(); GNodeiter!= (*GNodeSetiter)->end(); GNodeiter++){ 
00484                                 if ( (*GNodeiter)->connectFlag == ULONG_MAX) { // if color white
00485                                         (*GNodeiter)->connectFlag = 0;             // make it gray
00486                                         unsigned colId = (*GNodeiter)->id;
00487                                         distanceMat(rowId,colId) = distanceMat(rowId,uId) + 1;
00488                                         GreyList.push_back(*GNodeiter);  
00489                                 }
00490                         }
00491                 }
00492                 gnodePtr->connectFlag = UHUGE;
00493         }
00494 }
00495 /*! \fn void GNodeList::calcDistancefrom((GNode* refGNode)
00496 *  \brief calculate distance from argument to all the nodes using the breadth first search algorithm (got it from  Boost)
00497 */
00498 
00499 GNode* GNodeList::peelAndCutFast(){
00500   // Authors: L. Radu Totir and Rohan L. Fernando 
00501   // (February, 2004) 
00502   // Contributors: 
00503   // cout << "Before sorting and peeling the GNodeList looks like this: " << endl;
00504   // displayGNodeSets();
00505   //sort(begin(),end(),compareGNodesId());
00506   // cout << "After sorting but before peeling the GNodeList looks like this: " << endl;
00507   // displayGNodeSets();
00508   //cout << "Peeling locus: " << Individual::currentLocus << endl;
00509   searchStart = begin();
00510   resetConnectFlags();
00511   sampleGNodePtr=NULL;
00512   maxDist = 0;
00513   GNode* peel_me;
00514   unsigned loopCount=0,idOfPeelGuy=0;
00515   for (int i=0; i<size();i++){
00516     if(howToSample=="joint"){
00517       idOfPeelGuy = GNodeSet::prior->chrom()[0].peelOrder[i]-1;
00518     }
00519     else{
00520       idOfPeelGuy = GNodeSet::prior->chrom()[0].locus[Individual::currentLocus].peelOrder[i]-1;
00521     }
00522     peel_me = (*this)[idOfPeelGuy];
00523     // if(peel_me->id==76){
00524     //      cout << "Ready to peel GNode: " << peel_me->id << endl;
00525     // }  
00526     peel_me->updateMysets();
00527     peel_me->peel();
00528   }
00529   sort(begin(),end(),compareGNodesPeelId());
00530   // cout << "After peeling the GNodeList looks like this: " << endl;
00531   // displayGNodeSets();
00532 //   }
00533 //   else {
00534 //     cout << "Peeled " << count << " gNodes, now I need to cut" << endl;
00535 //     searchStart = it1;
00536 //     sampleGNodePtr = findLoopAndCut();
00537 //     loopCount++;
00538 //   }
00539   //displayGNodeSets();
00540   //cout << "Number of loops that were cut =" << loopCount << endl;; 
00541   return sampleGNodePtr;
00542   //std::cout << "Network size = " << sum << endl; 
00543 }
00544 /*! \fn void GNodeList::peelAndCut()
00545  *  \brief peels and cut (if it needs to) with known peeling order
00546 */
00547 
00548 void GNodeList::clearGNodeListForNextLocus(void){
00549   // Authors: L. Radu Totir and Rohan L. Fernando 
00550   // (September, 2003) 
00551   // Contributors:
00552   set< CutSet*>::iterator GNodeSetit;
00553   for (GNodeSetit=completeSetofCutSets.begin();GNodeSetit!=completeSetofCutSets.end();GNodeSetit++){ 
00554     delete *GNodeSetit;
00555   }
00556   completeSetofCutSets.clear();
00557   GNodeList::iterator it;
00558   for (it=begin();it!=end();it++){ 
00559     (*it)->SetofGNsts.clear();
00560   }
00561 }
00562 
00563 void GNodeList::releaseGNsts(void){
00564   // Authors: L. Radu Totir and Rohan L. Fernando 
00565   // (February, 2004) 
00566   // Contributors:     
00567   set< GNodeSet*>::iterator GNodeSetit;
00568   for (GNodeSetit=completeSetofGNsts.begin();GNodeSetit!=completeSetofGNsts.end();GNodeSetit++){ 
00569     delete *GNodeSetit;
00570   }
00571   completeSetofGNsts.clear();
00572 }
00573 /*! \fn void GNodeList::releaseGNsts(void)
00574  *  \brief delete GNodeSet (penetrance, founder, transition) pointers
00575  *  and clear the completeSetofGNsts
00576 */
00577 
00578 void GNodeList::reinitGNodeList(void){
00579   // Authors: L. Radu Totir and Rohan L. Fernando 
00580   // (September, 2003) 
00581   // Contributors:
00582   releaseCutSets();
00583   resetGNodeList();
00584 }
00585 /*! \fn void GNodeList::reinitGNodeList(void)
00586  *  \brief reinitialize the GNodeList for a new round of peeling 
00587 */
00588 
00589 void GNodeList::releaseCutSets(void){
00590   // Authors: L. Radu Totir and Rohan L. Fernando 
00591   // (September, 2003) 
00592   // Contributors:     
00593   set< CutSet*>::iterator GNodeSetit;
00594   for (GNodeSetit=completeSetofCutSets.begin();GNodeSetit!=completeSetofCutSets.end();GNodeSetit++){ 
00595     delete *GNodeSetit;
00596   }
00597   completeSetofCutSets.clear();
00598 }
00599 /*! \fn void GNodeList::releaseCutSets(void)
00600  *  \brief release CutSets before reseting the initial GNodeList
00601  *  for a new round of peeling 
00602 */
00603 
00604 void GNodeList::resetGNodeList(void){
00605   // Authors: L. Radu Totir and Rohan L. Fernando 
00606   // (August, 2003) 
00607   // Contributors:
00608   GNodeList::iterator it;
00609   for (it=begin();it!=end();it++){ 
00610     (*it)->SetofGNsts.clear();
00611   }
00612   set< GNodeSet*>::iterator GNodeSetit;
00613   GNodeSet::iterator setit;
00614   for (GNodeSetit=completeSetofGNsts.begin();GNodeSetit!=completeSetofGNsts.end();GNodeSetit++){  
00615     for (setit=(*GNodeSetit)->begin();setit!=(*GNodeSetit)->end();setit++){
00616       (*setit)->SetofGNsts.insert(*GNodeSetit);
00617     }
00618   }
00619 }
00620 /*! \fn void GNodeList::resetGNodeList(void)
00621  *  \brief reset the initial GNodeList for a new round of peeling 
00622 */
00623 
00624 void GNodeList::setGNodeSampleFlags(unsigned startBlock, unsigned stopBlock, unsigned sizeBlock){
00625   // Authors: L. Radu Totir and Rohan L. Fernando 
00626   // (October, 2003) 
00627   // Contributors: 
00628   GNodeList::iterator it;
00629   if ((stopBlock-startBlock)==size()){
00630     for (it=begin();it!=end();it++){ 
00631                 (*it)->sampled = false;
00632     }
00633     return;
00634   }
00635   else {
00636     for (it=begin();it!=end();it++){
00637       if(it>=begin()+startBlock && it<begin()+stopBlock){
00638                   (*it)->sampled = false;
00639       }
00640     }
00641   }
00642 }
00643 /*! \fn void GNodeList::setGNodeSampleFlags()
00644  *  \brief resets the sampled flags to false
00645 */
00646       
00647 void GNodeList::findLoopAndCut(void){
00648   // Authors: L. Radu Totir and Rohan L. Fernando 
00649   // (August, 2003) 
00650   // Contributors: 
00651   unsigned lowFlag;
00652   unsigned highFlag;
00653   GNodeList::iterator it;
00654   set< GNodeSet*>::iterator GNodeSetit, cutGNodeSet;
00655   GNodeSet::iterator setit;
00656   resetConnectFlags();
00657   for (it=searchStart;it!=end();it++){ 
00658           //std::cout << (*it)->id << "   ";
00659           for (GNodeSetit=(*it)->SetofGNsts.begin();GNodeSetit!=(*it)->SetofGNsts.end();GNodeSetit++){
00660                   if((*it)->connectFlag < (*GNodeSetit)->connectFlag){
00661                           lowFlag = (*it)->connectFlag;
00662                           highFlag= (*GNodeSetit)->connectFlag;
00663                           (*GNodeSetit)->connectFlag = (*it)->connectFlag;
00664                           propagateFlags(lowFlag,highFlag);
00665                           //cout << (*GNodeSetit)->connectFlag;
00666                   }
00667                   else if((*it)->connectFlag > (*GNodeSetit)->connectFlag){
00668                           lowFlag = (*GNodeSetit)->connectFlag;
00669                           highFlag= (*it)->connectFlag;
00670                           (*it)->connectFlag = (*GNodeSetit)->connectFlag;
00671                           propagateFlags(lowFlag,highFlag);
00672                   }
00673                   else{
00674                           // we have found a subset of nodes containing at least one loop 
00675                           // and possibly many "dangling branches"
00676                           GNodeList::iterator loopCloser = it;
00677                           GNodeList loopList = isolatePureLoop(loopCloser);
00678                           cutGNode =  loopList.getMinWeightGNode();
00679                           cutGNodesVector.push_back(*cutGNode);
00680                           cutGNodeSet = loopList.getCutGNodeSet(cutGNode);
00681                           // cut the loop
00682                           cutLoop(cutGNode,cutGNodeSet);
00683                           return;
00684                   }
00685           }
00686   }
00687 }
00688 /*! \fn void GNodeList::findLoopAndCut()
00689  *  \brief finds a loop in the unpeeled part of GNodeList and cuts it 
00690 */
00691 
00692 set< GNodeSet*>::iterator GNodeList::getCutGNodeSet(GNodeList::iterator cutGNode){
00693         // Authors: Joseph Abraham and Rohan L. Fernando 
00694         // (October, 2004) 
00695         // Contributors: 
00696         GNodeSet loopGNodesSet;
00697         GNodeList intersectionVector;
00698         GNodeList::iterator it;
00699         for (it=begin();it!=end();it++){
00700                 if(it!=cutGNode) loopGNodesSet.insert(*it);     
00701         }
00702         set< GNodeSet*>::iterator GNodeSetit;
00703         for (GNodeSetit=(*cutGNode)->SetofGNsts.begin();GNodeSetit!=(*cutGNode)->SetofGNsts.end();GNodeSetit++){
00704                 intersectionVector.clear();
00705                 set_intersection(loopGNodesSet.begin(),  loopGNodesSet.end(),
00706                                                  (*GNodeSetit)->begin(), (*GNodeSetit)->end(),
00707                                                  inserter(intersectionVector,intersectionVector.begin())  );
00708                 if (intersectionVector.size()>0) return GNodeSetit;
00709         }
00710         throw exception("GNodeList::getCutGNodeSet: possible error in program");
00711 }
00712 /*! \fn set< GNodeSet*>::iterator GNodeList::getCutGNodeSet(GNodeList::iterator cutGNode)
00713 *  \brief This method is called for a list of nodes in a loop. 
00714           It returns a GNodeSet that is in the loop 
00715 */
00716 
00717 GNodeList::iterator GNodeList::getMinWeightGNode(void){
00718         // Authors: Joseph Abraham and Rohan L. Fernando 
00719         // (October, 2004) 
00720         // Contributors: 
00721         GNodeList::iterator it, minIt;
00722         unsigned minWeight = ULONG_LONG_MAX;
00723         for(it=begin();it!=end();it++){
00724                 unsigned wt = (*it)->sampled ? 1 : (*it)->getWeight();
00725                 if(wt < minWeight){
00726                         minWeight = wt;
00727                         minIt = it;
00728                 }
00729         }
00730         return minIt;
00731 }
00732 /*! \fn void GNodeList::getMinWeightGNode
00733 *  \brief returns GNode with smallest weight in this List
00734 
00735 */
00736 void GNodeList::propagateFlags(unsigned lowFlag, unsigned highFlag){
00737   // Authors: L. Radu Totir and Rohan L. Fernando 
00738   // (August, 2003) 
00739   // Contributors: 
00740   if(highFlag==ULONG_MAX){
00741     return;
00742   }
00743         else{
00744                 GNodeList::iterator it;
00745                 set< GNodeSet*>::iterator GNodeSetit;  
00746                 for (it=searchStart;it!=end();it++){ 
00747                         if((*it)->connectFlag==highFlag){
00748                                 (*it)->connectFlag = lowFlag;
00749                         }
00750                         for (GNodeSetit=(*it)->SetofGNsts.begin();GNodeSetit!=(*it)->SetofGNsts.end();GNodeSetit++){ 
00751                                 if((*GNodeSetit)->connectFlag==highFlag){
00752                                         (*GNodeSetit)->connectFlag=lowFlag;
00753                                 }
00754                         }
00755                 }
00756         }
00757 }
00758 /*! \fn void GNodeList::propagateFlags(void)
00759  *  \brief reassign the flag of a GNode or GNodeSet after the
00760     comparison with an adjacent GNode or GNodeSet
00761 */
00762 
00763 void GNodeList::resetConnectFlags(void){
00764   // Authors: L. Radu Totir and Rohan L. Fernando 
00765   // (August, 2003) 
00766   // Contributors: 
00767   // resseting the flags for each loop
00768   GNodeList::iterator it;
00769   set< GNodeSet*>::iterator GNodeSetit;
00770   for (it=searchStart;it!=end();it++){
00771     (*it)->connectFlag = (*it)->id;
00772     for (GNodeSetit=(*it)->SetofGNsts.begin();GNodeSetit!=(*it)->SetofGNsts.end();GNodeSetit++){
00773       (*GNodeSetit)->connectFlag = ULONG_MAX;
00774     }
00775   }
00776 }
00777 /*! \fn void GNodeList::resetConnectFlags(void)
00778  *  \brief reset the flag of a GNode and GNodeSet respectively before
00779     starting to look for a place to cut
00780 */
00781 
00782 GNodeList GNodeList::isolatePureLoop(GNodeList::iterator loopCloser){
00783         // Authors: L. Radu Totir and Rohan L. Fernando 
00784         // (August, 2003) 
00785         // Contributors: 
00786         GNodeList LoopList;
00787         GNodeList::iterator it;
00788         set< GNodeSet*>::iterator GNodeSetit; 
00789         GNodeSet::iterator setit;
00790         //cout << "Loop members: " << endl; 
00791         unsigned numberEliminated;
00792     do {
00793                 numberEliminated = 0;
00794                 for (it=searchStart;it!=loopCloser;it++){
00795                         if((*loopCloser)->connectFlag==(*it)->connectFlag){
00796                                 unsigned GNodeSetcount = 0;
00797                                 for (GNodeSetit=(*it)->SetofGNsts.begin();GNodeSetit!=(*it)->SetofGNsts.end();GNodeSetit++){
00798                                         unsigned GNodecount = 0;
00799                                         for (setit=(*GNodeSetit)->begin();setit!=(*GNodeSetit)->end();setit++){
00800                                                 if((*setit)->id!=(*it)->id && (*setit)->connectFlag==(*loopCloser)->connectFlag){
00801                                                         GNodecount++;
00802                                                 }
00803                                         }
00804                                         if(GNodecount!=0) GNodeSetcount++;
00805                                 }
00806                                 if(GNodeSetcount<=1){
00807                                         (*it)->connectFlag = ULONG_MAX;
00808                                         numberEliminated++;
00809                                 }
00810                         }
00811                 }
00812         } while(numberEliminated>0);
00813         for (it=searchStart;it!=end();it++){
00814                 if((*loopCloser)->connectFlag==(*it)->connectFlag){
00815                         LoopList.push_back(*it);
00816                 }
00817         }
00818         for (it=LoopList.begin();it!=LoopList.end();it++){
00819                 loopSetGNodes.insert(*it);
00820         }
00821         //LoopList.displayGNodeSets();
00822         return LoopList;
00823 }
00824 /*! \fn GNodeList GNodeList::isolatePureLoop (void)
00825 *   \brief find the members of a loop by removing all terminal GNodes;
00826      returns a GNodeList containing loop members
00827    
00828 */
00829 
00830 
00831 void  GNodeList::cutLoop(GNodeList::iterator cutGNode,set< GNodeSet*>::iterator cutGNodeSet){
00832         // Authors: L. Radu Totir and Rohan L. Fernando 
00833         // (August, 2003) 
00834         // Contributors:  
00835         unsigned printFlag = popPtr->model->myRSamplerParms.printFlag; 
00836         if(printFlag>1){
00837                 cout << "cutGNode: "<< (*cutGNode)->id << " cutGNodeSet: ";
00838                 (*cutGNodeSet)->display();
00839                 cout << endl;
00840         }
00841         GNodeSet::iterator setit;
00842         CutSet* newSet = new CutSet;
00843         completeSetofCutSets.insert(newSet);
00844         for (setit=(*cutGNodeSet)->begin();setit!=(*cutGNodeSet)->end();setit++){
00845                 if((*setit)!=(*cutGNode)){
00846                         newSet->insert(*setit);
00847                 }
00848         }
00849         newSet->setKeyMultiplicationCode();
00850         newSet->connectFlag = (*cutGNode)->connectFlag;
00851         newSet->numberOfCuts++;
00852         (*cutGNode)->numberOfCuts++;
00853         *newSet+=(*cutGNodeSet);
00854         if(dynamic_cast<CutSet*>(*cutGNodeSet)){
00855                 ((CutSet*) *cutGNodeSet)->replaceMeWith(*newSet);
00856                 (*cutGNode)->SetofGNsts.erase(*cutGNodeSet);
00857                 delete newSet;
00858                 completeSetofCutSets.erase(newSet);
00859         }
00860         else {
00861                 for (setit=(*cutGNodeSet)->begin();setit!=(*cutGNodeSet)->end();setit++){
00862                         (*setit)->SetofGNsts.erase(*cutGNodeSet);
00863                         if((*setit)!=(*cutGNode)){
00864                                 (*setit)->SetofGNsts.insert(newSet);
00865                         }
00866                 }
00867         }
00868 }
00869 /*! \fn void GNodeList::cutLoop()
00870 *  \brief cut the loop 
00871 */
00872 
00873 void GNodeList::checkSample(void){
00874   // Authors: L. Radu Totir and Rohan L. Fernando 
00875   // (September, 2003) 
00876   // Contributors:
00877   set< GNodeSet*>::iterator GNodeSetit;
00878   for (GNodeSetit=completeSetofGNsts.begin();GNodeSetit!=completeSetofGNsts.end();GNodeSetit++){ 
00879     if(dynamic_cast<TransitionSet*>(*GNodeSetit)){
00880       if((*GNodeSetit)->getValue()==0.0){
00881         throw matvec::InvalidSample();
00882       }
00883     }
00884     else if(dynamic_cast<TransmissionSet*>(*GNodeSetit)){
00885       if((*GNodeSetit)->getValue()==0.0){
00886         throw matvec::InvalidSample();
00887       }
00888     }
00889   }
00890 }
00891 
00892 void GNodeList::calculateTargetProb(void){
00893   // Authors: L. Radu Totir and Rohan L. Fernando 
00894   // (September, 2003) 
00895   // Contributors:
00896   set< GNodeSet*>::iterator GNodeSetit;
00897   for (GNodeSetit=completeSetofGNsts.begin();GNodeSetit!=completeSetofGNsts.end();GNodeSetit++){ 
00898     if(dynamic_cast<TransitionSet*>(*GNodeSetit)){
00899       //std::cout << "Target = " << (*GNodeSetit)->getTargetValue() << endl;
00900       logTarget += std::log((*GNodeSetit)->getTargetValue());
00901     }
00902     else {
00903       //std::cout << "Target = " << (*GNodeSetit)->getValue() << endl;
00904       logTarget += std::log((*GNodeSetit)->getValue());
00905       //std::cout << "Intermediate logTarget = " << logTarget << endl;
00906     }
00907     //std::cout << "Cumulative logTarget = " << logTarget << endl;
00908   }
00909 }
00910   
00911 ////////////////// GNodeSet methods /////////////////////
00912 
00913 GNodeSet::GNodeSet(){
00914   // Authors: L. Radu Totir and Rohan L. Fernando 
00915   // (August, 2003) 
00916   // Contributors:  
00917   connectFlag = ULONG_MAX;
00918   numberOfCuts= 0;
00919 }
00920 /*! \fn GNodeSet::GNodeSet()
00921  *  \brief GNodeSet constructor
00922 */
00923 
00924 bool GNodeSet::incr(void){
00925   // Authors: L. Radu Totir and Rohan L. Fernando 
00926   // (based on the incr() of Bernt Guldbrantdsen)
00927   // (June, 2003) 
00928   // Contributors:
00929   GNodeSet::iterator iter;
00930   for(iter=begin();iter!=end();iter++) {
00931     if((*iter)->sampled) continue;
00932     if((*iter)->incr()) return 1;
00933   }
00934   return 0;
00935 }
00936 /*! \fn bool GNodeSet::incr(void)
00937  *  \brief the GNodeSet increment method (calls the Gnode increment
00938     method)
00939 */
00940   
00941 void GNodeSet::reset(void) {
00942   // Authors: L. Radu Totir and Rohan L. Fernando 
00943   // (June, 2003) 
00944   // Contributors:
00945   GNodeSet::iterator iter;
00946   if(empty())
00947     return;
00948   for(iter =  begin();iter != end();iter++) {
00949     if((*iter)->sampled) continue;
00950     (*iter)->reset(0);
00951   }
00952 }
00953 /*! \fn void GNodeSet::reset(void)
00954  *  \brief sets all GNodes to 0
00955 */
00956 
00957 void GNodeSet::attachMeToMyGnodes(void){
00958   // Authors: L. Radu Totir and Rohan L. Fernando 
00959   // (September, 2004) 
00960   // Contributors:
00961   GNodeSet::iterator iter;
00962   for(iter =  begin();iter != end();iter++) {
00963     (*iter)->SetofGNsts.insert(this);
00964   }
00965 }
00966 /*! \fn void GNodeSet::attachMeToMyGnodes(void)
00967  *  \brief attaches the current GNodeSet to all its GNode members
00968 */
00969 
00970 void GNodeSet::display(void){
00971         GNodeSet::iterator it;
00972         cout << "(";
00973         for (it=begin();it!=end();it++){
00974                 cout << (*it)->id <<" ";
00975         }
00976         cout << ") ";
00977 }
00978 
00979 ////////////////// GNode methods /////////////////////
00980 
00981 bool GNode::isMyNeighbor(GNode* refGNode){
00982   // Authors: L. Radu Totir and Rohan L. Fernando 
00983   // (August, 2003) 
00984   // Contributors:
00985   set< GNodeSet*>::iterator myGNodeSetit;
00986   GNodeSet::iterator setit; 
00987   for (myGNodeSetit=SetofGNsts.begin();myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
00988     for (setit=(*myGNodeSetit)->begin();setit!=(*myGNodeSetit)->end();setit++){ 
00989       if((*setit)->id==refGNode->id){
00990         return true;
00991       }
00992     }
00993   }
00994   return false;
00995 }
00996 /*! \fn bool GNode::isMyNeighbor(GNode* refGNode)
00997  *  \brief determine the neighbours of the GNode where we will cut;
00998  *  this is done, to determine the GNode that is farthest away from 
00999  *  the cut
01000 */
01001 
01002 CutSet* GNode::makeNeighSet(void){
01003   // Authors: L. Radu Totir and Rohan L. Fernando 
01004   // (June, 2003) 
01005   // Contributors:
01006   set< GNodeSet*>::iterator myGNodeSetit;
01007   GNodeSet::iterator setit;
01008   CutSet* neighSet = new CutSet;
01009   for (myGNodeSetit=SetofGNsts.begin();myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01010     for (setit=(*myGNodeSetit)->begin();setit!=(*myGNodeSetit)->end();setit++){
01011       neighSet->insert(*setit);
01012     }
01013   }
01014   // cout << "Neigh size= " << neighSet->size() << endl;
01015   return neighSet;
01016 }
01017 /*! \fn CutSet* GNode::makeNeighSet(void)
01018  *  \brief creates the neighborhood cutset (the resulting cutset
01019     includes the individual that is going to be peeled as well)
01020 */
01021   
01022 float GNode::getCutsetMagnitude(void){
01023   // Authors: L. Radu Totir and Rohan L. Fernando 
01024   // (June, 2003) 
01025   // Contributors:
01026   GNodeSet::iterator setit;
01027   CutSet* newSet = makeNeighSet();
01028   newSet->erase(this);
01029   double mag=1.0;
01030   for (setit=newSet->begin();setit!=newSet->end();setit++){
01031           mag *= (*setit)->sampled ? 1 : (*setit)->getWeight();
01032   }
01033   // cout << " CutSet size= " << newSet->size() << endl;
01034   delete newSet;
01035   return mag;
01036 }
01037 /*! \fn float GNode::getCutsetMagnitude(void)
01038  *  \brief returns the magnitude of the resulting cutset if the
01039     individual to be peeled is removed from its neighborhood cutset
01040 */
01041 
01042 void GNode::updateMysets(void){
01043   // Authors: L. Radu Totir and Rohan L. Fernando 
01044   // (June, 2003) 
01045   // Contributors:
01046   set< GNodeSet*>::iterator myGNodeSetit;
01047   GNodeSet::iterator setit;
01048   CutSet* newSet = makeNeighSet();
01049   newSet->erase(this);
01050   gNodeListPtr->completeSetofCutSets.insert(newSet);
01051   generatedSet = newSet;
01052   for (myGNodeSetit=SetofGNsts.begin();myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01053     for (setit=(*myGNodeSetit)->begin();setit!=(*myGNodeSetit)->end();setit++){
01054       if(*setit!=this){
01055         (*setit)->SetofGNsts.erase(*myGNodeSetit);
01056       }
01057     }
01058   }
01059   for (setit=newSet->begin();setit!=newSet->end();setit++){
01060     (*setit)->SetofGNsts.insert(newSet);
01061   }
01062 }
01063 /*! \fn void GNode::updateMysets(void)
01064  *  \brief creates the new cutset obtained by removing the node peeled
01065     from the neighborhood cutset. Also sets the pointers of the
01066     affected nodes to the generated cutset
01067 */
01068 
01069 void GNode::peel() {
01070   // Authors: L. Radu Totir and Rohan L. Fernando 
01071   // (June, 2003) 
01072   // Contributors:
01073   CutSet* tempSet = makeNeighSet();
01074   tempSet->setKeyMultiplicationCode();
01075   generatedSet->setKeyMultiplicationCode();
01076   gNodeListPtr->completeSetofCutSets.insert(tempSet);
01077   // cout << "Resulting CutSet size = " << tempSet->size() - 1 << endl;
01078   set< GNodeSet*>::iterator myGNodeSetit;
01079   myGNodeSetit = SetofGNsts.begin();
01080   *tempSet = *myGNodeSetit;
01081   for (myGNodeSetit++;myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01082     *tempSet *= *myGNodeSetit;
01083   }
01084   *generatedSet+= tempSet;
01085 //   if(generatedSet->size()==0){
01086 //     cout << "Likelihood = ";
01087 //     generatedSet->displayValues();
01088 //   }
01089   delete tempSet;
01090   gNodeListPtr->completeSetofCutSets.erase(tempSet);
01091 }
01092 /*! \fn void GNode::peel(void)
01093  *  \brief the GNode peeling method (peeling the GNode in question)
01094 */
01095 
01096 void GNode::reComputeGNode(void) {
01097   // Authors: L. Radu Totir and Rohan L. Fernando 
01098   // (August, 2003) 
01099   // Contributors:
01100   //if(GNodeSet::currentLocus==1){
01101   //cout << "Recompute old sample probability for " << id << " = ";
01102   //}
01103   CutSet* tempSet = makeNeighSet();
01104   tempSet->setKeyMultiplicationCode();
01105   // next line is used to prevent a memory leak
01106   gNodeListPtr->completeSetofCutSets.insert(tempSet);
01107   GNodeSet::iterator setit;
01108   CutSet::iterator cutSetit;
01109   CutSet myGNodeProbs;
01110   myGNodeProbs.insert(this);
01111   myGNodeProbs.setKeyMultiplicationCode();
01112   set< GNodeSet*>::iterator myGNodeSetit;
01113   myGNodeSetit = SetofGNsts.begin();
01114   *tempSet = *myGNodeSetit;
01115   for (myGNodeSetit++;myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01116     *tempSet *= *myGNodeSetit;
01117   }
01118   myGNodeProbs += tempSet;
01119   delete tempSet;
01120   gNodeListPtr->completeSetofCutSets.erase(tempSet);
01121   myGNodeProbs.normalize();
01122   myGNodeProbs.reset();
01123   double oldProb = myGNodeProbs.getOldValue();
01124   if(oldProb==0) cout << "Zero prob when recalculating value for: "<< id << endl; 
01125   GNodeList::logOldProposal += std::log(oldProb);
01126   int oldState = getOldState();
01127   reset(oldState);
01128   sampled = true; 
01129   //if(GNodeSet::currentLocus==1){
01130   //std::cout << getOldState() << "  " << getState() << " (" << getmState()+1 << "|" << getpState()+1 << ")" << endl;
01131   //}
01132 //   std::cout << "oldProb = " << oldProb << endl;
01133 //   cout << "GNodeList::logOldProposal = " << GNodeList::logOldProposal << endl;
01134 }
01135 
01136 void GNode::reverseSampleGNode(void) {
01137   // Authors: L. Radu Totir and Rohan L. Fernando 
01138   // (August, 2003) 
01139   // Contributors:
01140         
01141         try{
01142                 CutSet* tempSet = makeNeighSet();
01143                 tempSet->setKeyMultiplicationCode();
01144                 // next line is used to prevent a memory leak
01145                 gNodeListPtr->completeSetofCutSets.insert(tempSet);
01146                 GNodeSet::iterator setit;
01147                 CutSet::iterator cutSetit;
01148                 CutSet myGNodeProbs;
01149                 myGNodeProbs.insert(this);
01150                 myGNodeProbs.setKeyMultiplicationCode();
01151                 set< GNodeSet*>::iterator myGNodeSetit;
01152                 myGNodeSetit = SetofGNsts.begin();
01153                 *tempSet = *myGNodeSetit;
01154                 for (myGNodeSetit++;myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01155                         *tempSet *= *myGNodeSetit;
01156                 }
01157                 myGNodeProbs += tempSet;
01158                 delete tempSet;
01159                 gNodeListPtr->completeSetofCutSets.erase(tempSet);
01160                 myGNodeProbs.normalize();
01161                 double u = ranf();
01162                 double sum = 0.0;
01163                 myGNodeProbs.reset();
01164                 unsigned sampledState=0;
01165                 do{
01166                         double tempProb = myGNodeProbs.getValue();
01167                         sum+= tempProb;
01168                         if (u<sum){
01169                                 reset(sampledState);
01170                                 sampled = true;
01171                                 GNodeList::logProposal += std::log(tempProb);
01172                                 //if(GNodeSet::currentLocus==1){
01173                                 //std::cout << sampledState << " (" << getmState()+1 << "|" << getpState()+1 << ")" << endl;
01174                                 //}
01175                                 //std::cout << getMyAlleleState() + 1 << endl; 
01176                                 //       std::cout << "newProb = " << tempProb << endl;
01177                                 //       std::cout << "GNodeList::logProposal = " << GNodeList::logProposal << endl;
01178                                 return;
01179                         }
01180                         sampledState++;
01181                 } while(incr());
01182         }
01183         catch (...){
01184                 cout << "in reverse sample\n";
01185         }
01186 }
01187 /*! \fn void GNode::sampleGNode(void)
01188  *  \brief the GNode sampling method (sample the GNode in question)
01189 */
01190 
01191 void GNode::sampleGNode(void) {
01192         // Authors: L. Radu Totir and Rohan L. Fernando 
01193         // (August, 2003) 
01194         // Contributors:
01195         // cout << "Sampled genotype for " << id << " = " ;
01196         CutSet myBCutSet = calcMyBCutSet();
01197         GNodeSet* myBCutSetPtr = &myBCutSet;
01198         CutSet* tempSet = makeNeighSet();
01199         tempSet->setKeyMultiplicationCode();
01200         gNodeListPtr->completeSetofCutSets.insert(tempSet);
01201         GNodeSet::iterator setit;
01202         CutSet::iterator cutSetit;
01203         CutSet myGNodeProbs;
01204         myGNodeProbs.insert(this);
01205         myGNodeProbs.setKeyMultiplicationCode();
01206         set< GNodeSet*>::iterator myGNodeSetit;
01207         myGNodeSetit = SetofGNsts.begin();
01208         *tempSet = *myGNodeSetit;
01209         for (myGNodeSetit++;myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01210                 *tempSet *= *myGNodeSetit;
01211         }
01212         *tempSet *= myBCutSetPtr;
01213         myGNodeProbs += tempSet;
01214         delete tempSet;
01215         gNodeListPtr->completeSetofCutSets.erase(tempSet);
01216         myGNodeProbs.normalize();
01217         double u = ranf();
01218         double sum = 0.0;
01219         myGNodeProbs.reset();
01220         unsigned sampledState=0;
01221         do{
01222                 double tempProb = myGNodeProbs.getValue();
01223                 sum+= tempProb;
01224                 if (u<sum){
01225                         reset(sampledState);
01226                         sampled = true;
01227                         GNodeList::logProposal += std::log(tempProb);
01228                         //cout << sampledState << endl;
01229                         return;
01230                 }
01231                 sampledState++;
01232         } while(incr());
01233 }
01234 /*! \fn void GNode::sampleGNode(void)
01235 *  \brief the GNode sampling method (sample the GNode in question)
01236 */
01237 
01238 CutSet GNode::calcMyBCutSet(void) {
01239   // Authors: L. Radu Totir and Rohan L. Fernando 
01240   // (August, 2003) 
01241   // Contributors:
01242   CutSet myBCutSet;
01243         if(generatedSet->size()==0){
01244                 myBCutSet.setKeyMultiplicationCode();  
01245                 myBCutSet.putValue(1.0);
01246                 return myBCutSet;
01247         }
01248   else {
01249     GNode* nextGNodePtr = generatedSet->getLocation();
01250     myBCutSet = nextGNodePtr->calcBCutSetForPrevGNode(generatedSet); 
01251     return myBCutSet;
01252   }
01253 }
01254 /*! \fn CutSet GNode::calcMyBackCutSet(void)
01255  *  \brief method used to calculate the backward cutset contribution 
01256  *  used in calculating the genotype probabilities of a given GNode
01257 */
01258 
01259 CutSet GNode::calcBCutSetForPrevGNode(CutSet* FCutSetPrevGNode) {
01260   // Authors: L. Radu Totir and Rohan L. Fernando 
01261   // (August, 2003) 
01262   // Contributors:
01263   CutSet myBCutSet = calcMyBCutSet();
01264   GNodeSet* myBCutSetPtr = &myBCutSet;
01265   CutSet* tempSet = makeNeighSet();
01266   tempSet->setKeyMultiplicationCode();
01267   gNodeListPtr->completeSetofCutSets.insert(tempSet);
01268   GNodeSet::iterator setit;
01269   CutSet BCutSet;
01270   for (setit=FCutSetPrevGNode->begin();setit!=FCutSetPrevGNode->end();setit++){
01271     BCutSet.insert(*setit);
01272   }
01273   BCutSet.setKeyMultiplicationCode();
01274   set< GNodeSet*>::iterator myGNodeSetit;
01275   myGNodeSetit = SetofGNsts.begin();
01276   if((*myGNodeSetit)==FCutSetPrevGNode){  // check if forwardCutSet is the first one
01277     myGNodeSetit = SetofGNsts.begin()++;
01278   }
01279   *tempSet = *myGNodeSetit;
01280   for (myGNodeSetit++;myGNodeSetit!=SetofGNsts.end();myGNodeSetit++){
01281     if((*myGNodeSetit)!=FCutSetPrevGNode){
01282       *tempSet *= *myGNodeSetit;
01283     }
01284   }
01285   *tempSet *= myBCutSetPtr;
01286   BCutSet += tempSet;
01287   delete tempSet;
01288   gNodeListPtr->completeSetofCutSets.erase(tempSet);
01289   return BCutSet;
01290 }
01291 /*! \fn CutSet GNode::calcPreviousGNodeBackCutSet(CutSet* forwardCutSet)
01292  *  \brief 
01293 */
01294 
01295 GNode::GNode(){
01296   generatedSet = 0;
01297 }
01298 
01299 void GNode::release(void){
01300 
01301 }
01302 
01303 void GNodeList::peelOrderCutAndSample(unsigned maxCutSetSize){
01304   // Authors: L. Radu Totir and Rohan L. Fernando 
01305   // (September, 2004) 
01306   // Contributors: 
01307   unsigned sampledCount = 0;
01308   GNodeList::reverse_iterator it;
01309   setGNodeSampleFlags();
01310   sampleGNodePtr = peelAndCut(maxCutSetSize);
01311   //cout << "Log Proposal before = " << GNodeList::logProposal << endl;
01312   //cout << "Log Target before = " << GNodeList::logTarget << endl;
01313   while (sampleGNodePtr) {
01314     sampledCount++;
01315     //displayGNodeSets();
01316     //cout << "GNode sampled after " << sampledCount << " 'th peeling: " << sampleGNodePtr->id << endl;
01317     sampleGNodePtr->sampleGNode();
01318     reinitGNodeList();
01319     //displayGNodeSets();
01320     sampleGNodePtr = peelAndCut(maxCutSetSize);
01321   }
01322   //cout << "Number of GNodes sampled to break loops = " << sampledCount << endl;
01323   //cout << "GNodeList before sampling all GNodes" << endl;
01324   for (it=rbegin();it!=rend();it++){
01325     if(!(*it)->sampled){
01326       (*it)->reverseSampleGNode();
01327     }
01328   }
01329   checkSample();
01330   //cout << "Log Proposal after = " << GNodeList::logProposal << endl;
01331   calculateTargetProb();
01332   //cout << "Log Target after = " << GNodeList::logTarget << endl;
01333   //cout << "Final gNodeList (after peeling, cutting and sampling):" << endl;
01334   //displayGNodeSets();
01335   clearGNodeListForNextLocus();
01336 }
01337 /*! \fn void GNodeList::peelOrderCutAndSample()
01338  *  \brief sample genotypes after determining peeling order, peeling
01339     and cutting(if necesary)
01340 */
01341 
01342 void GNodeList::setGNodeSampleFlags(void){
01343   // Authors: L. Radu Totir and Rohan L. Fernando 
01344   // (August, 2003) 
01345   // Contributors: 
01346   GNodeList::iterator it;
01347   for (it=begin();it!=end();it++){ 
01348           (*it)->sampled = false;
01349   }
01350 }
01351 /*! \fn void GNodeList::setGNodeSampleFlags()
01352  *  \brief reset sampled flag to false
01353 */
01354 
01355 void GNodeList::peelCutAndSample(unsigned maxCutSetSize){
01356   // Authors: L. Radu Totir and Rohan L. Fernando 
01357   // (September, 2004) 
01358   // Contributors: 
01359   unsigned sampledCount = 0;
01360   GNodeList::reverse_iterator it;
01361   setGNodeSampleFlags();
01362   if (GNodeSet::prior->chrom()[0].peelOrder.size()==size()){
01363     sampleGNodePtr = peelAndCutFast();
01364   }
01365   else{
01366           // cout << "We determine the peeling order again (inefficient!!)" << endl;
01367           sampleGNodePtr = peelAndCut(maxCutSetSize);
01368   }
01369   //cout << "I am actually sampling a new sample" << endl;
01370   //cout << "Log Proposal before processing locus " << Individual::currentLocus+1 << " = " << GNodeList::logProposal << endl;
01371   //cout << "Log Target before = " << GNodeList::logTarget << endl;
01372   while (sampleGNodePtr) {
01373     sampledCount++;
01374     //displayGNodeSets();
01375     //cout << "GNode sampled after " << sampledCount << " 'th peeling: " << sampleGNodePtr->id << endl;
01376     sampleGNodePtr->sampleGNode();
01377     reinitGNodeList();
01378     //displayGNodeSets();
01379     sampleGNodePtr = peelAndCut(maxCutSetSize);
01380   }
01381   cout << "Number of GNodes sampled to break loops = " << sampledCount << endl;
01382   //cout << "GNodeList before sampling all GNodes" << endl;
01383   for (it=rbegin();it!=rend();it++){
01384     if(!(*it)->sampled){
01385       (*it)->reverseSampleGNode();
01386     }
01387   }
01388   checkSample();
01389   //cout << "Log Proposal after processing locus " << Individual::currentLocus+1 << " = " << GNodeList::logProposal << endl;
01390   calculateTargetProb();
01391   //cout << "Log Target after = " << GNodeList::logTarget << endl;
01392   //cout << "Final gNodeList (after peeling, cutting and sampling):" << endl;
01393   //displayGNodeSets();
01394   clearGNodeListForNextLocus();
01395 }
01396 /*! \fn void GNodeList::peelCutAndSample()
01397  *  \brief sample genotypes after peeling and cutting(if necesary)
01398 */
01399 
01400 }////// end of namespace matvec 

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