Skip to content

Commit

Permalink
Merge pull request #275 from mcveanlab/wsaf-gt0-IBD
Browse files Browse the repository at this point in the history
Wsaf gt0 ibd
  • Loading branch information
shajoezhu authored Oct 16, 2018
2 parents 2f71665 + c72f578 commit 895e308
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 124 deletions.
3 changes: 3 additions & 0 deletions .ci/checkedList
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ src/txtReader.cpp
src/txtReader.hpp
src/ibd.hpp
src/ibd.cpp
src/vcfReader.hpp
src/debug/mcmcDebug.cpp
src/debug/vcfReaderDebug.cpp
3 changes: 0 additions & 3 deletions .ci/mycppfileList
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ src/mcmc.cpp
src/mcmc.hpp
src/panel.cpp
src/panel.hpp
src/txtReader.cpp
src/txtReader.hpp
src/updateHap.cpp
src/updateHap.hpp
src/utility.cpp
src/utility.hpp
src/vcfReader.cpp
src/vcfReader.hpp
src/debug/mcmcDebug.cpp
src/debug/vcfReaderDebug.cpp
src/export/dEploidIOExport.cpp
src/export/dEploidIOExportPosteriorProb.cpp
Expand Down
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
./tests/test_binary.sh
./tests/testPOS.sh
./tests/test_binaryVcfVsTxt.sh
./tests/test-against-previous-version.sh
#./tests/test-against-previous-version.sh
./tests/test_binaryReproducible.sh
#- valgrind --leak-check=full -v --show-leak-kinds=all ./unit_tests
#- coveralls --exclude lib --exclude tests --exclude src/random --exclude src/codeCogs/ --exclude src/export/ --gcov-options '\-lp'
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
./tests/test_binary.sh
./tests/testPOS.sh
./tests/test_binaryVcfVsTxt.sh
./tests/test-against-previous-version.sh
#./tests/test-against-previous-version.sh
./tests/test_binaryReproducible.sh
"18.04":
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
./tests/test_binary.sh
./tests/testPOS.sh
./tests/test_binaryVcfVsTxt.sh
./tests/test-against-previous-version.sh
#./tests/test-against-previous-version.sh
./tests/test_binaryReproducible.sh
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ script:
- if [ $TRAVIS_OS_NAME == linux ]; then ./tests/test_binary.sh; fi
- ./tests/testPOS.sh
- ./tests/test_binaryVcfVsTxt.sh
- ./tests/test-against-previous-version.sh
#- ./tests/test-against-previous-version.sh
- ./tests/test_binaryReproducible.sh
- if [ $TRAVIS_OS_NAME == linux ]; then cd docs/doxygen; doxygen Doxyfile; cd ../..; fi

Expand Down
17 changes: 11 additions & 6 deletions src/dEploid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,24 @@ int main(int argc, char *argv[]) {
dEploidIO.writeHap(hap, false);
} else {
if (dEploidIO.useIBD()) { // ibd
DEploidIO tmpIO(dEploidIO);
tmpIO.ibdTrimming();
McmcSample * ibdMcmcSample = new McmcSample();
MersenneTwister ibdRg(dEploidIO.randomSeed());
MersenneTwister ibdRg(tmpIO.randomSeed());

McmcMachinery ibdMcmcMachinery(&dEploidIO.plaf_,
&dEploidIO.refCount_,
&dEploidIO.altCount_,
dEploidIO.panel,
&dEploidIO,
McmcMachinery ibdMcmcMachinery(&tmpIO.plaf_,
&tmpIO.refCount_,
&tmpIO.altCount_,
tmpIO.panel,
&tmpIO,
ibdMcmcSample,
&ibdRg,
true);
ibdMcmcMachinery.runMcmcChain(true, // show progress
true); // use IBD
dEploidIO.initialProp = tmpIO.initialProp;
dEploidIO.setInitialPropWasGiven(true);
dEploidIO.setDoUpdateProp(false);
delete ibdMcmcSample;
}
McmcSample * mcmcSample = new McmcSample();
Expand Down
190 changes: 135 additions & 55 deletions src/dEploidIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,62 +745,74 @@ void DEploidIO::readPanel() {
}


DEploidIO::DEploidIO(const DEploidIO &currentDEploidIO) {
DEploidIO::DEploidIO(const DEploidIO &cpFrom) {
this->setIsCopied(true);
this->setDoExportRecombProb(currentDEploidIO.doExportRecombProb());
this->setrandomSeedWasGiven(currentDEploidIO.randomSeedWasGiven());
this->setCompressVcf(currentDEploidIO.compressVcf());
this->setInitialPropWasGiven(currentDEploidIO.initialPropWasGiven());
this->setInitialHapWasGiven(currentDEploidIO.initialHapWasGiven());
this->initialProp = vector <double> (currentDEploidIO.initialProp.begin(),
currentDEploidIO.initialProp.end());
this->setPleaseCheckInitialP(currentDEploidIO.pleaseCheckInitialP());
this->setExcludeSites(currentDEploidIO.excludeSites());
this->excludedMarkers = currentDEploidIO.excludedMarkers;
this->panel = currentDEploidIO.panel;
this->set_seed(currentDEploidIO.randomSeed_);
this->set_help(currentDEploidIO.help());
this->setVersion(currentDEploidIO.version());
this->setUsePanel(currentDEploidIO.usePanel());
this->precision_ = currentDEploidIO.precision_;
this->prefix_ = currentDEploidIO.prefix_;
this->setKStrainWasManuallySet(currentDEploidIO.kStrainWasManuallySet());
this->setKStrainWasSetByHap(currentDEploidIO.kStrainWasSetByHap());
this->setKStrainWasSetByProp(currentDEploidIO.kStrainWasSetByProp());
this->setKstrain(currentDEploidIO.kStrain());
this->nMcmcSample_ = currentDEploidIO.nMcmcSample();
this->setDoUpdateProp(currentDEploidIO.doUpdateProp());
this->setDoUpdatePair(currentDEploidIO.doUpdatePair());
this->setDoUpdateSingle(currentDEploidIO.doUpdateSingle());
this->setDoExportPostProb(currentDEploidIO.doExportPostProb());
this->setDoLsPainting(currentDEploidIO.doLsPainting());
this->setDoIbdPainting(currentDEploidIO.doIbdPainting());
this->setUseIBD(currentDEploidIO.useIBD());
this->setUseLasso(currentDEploidIO.useLasso());
this->setDoExportSwitchMissCopy(currentDEploidIO.doExportSwitchMissCopy());
this->setDoAllowInbreeding(currentDEploidIO.doAllowInbreeding());
this->mcmcBurn_ = currentDEploidIO.mcmcBurn_;
this->mcmcMachineryRate_ = currentDEploidIO.mcmcMachineryRate_;
this->missCopyProb_ = currentDEploidIO.missCopyProb_;
this->useConstRecomb_ = currentDEploidIO.useConstRecomb();
this->setForbidCopyFromSame(currentDEploidIO.forbidCopyFromSame());
this->constRecombProb_ = currentDEploidIO.constRecombProb();
this->averageCentimorganDistance_ = currentDEploidIO.averageCentimorganDistance();
this->setScalingFactor(currentDEploidIO.scalingFactor());
this->setParameterG(currentDEploidIO.parameterG());
this->setParameterSigma(currentDEploidIO.parameterSigma());
this->setIBDSigma(currentDEploidIO.ibdSigma());
this->setUseVcf(currentDEploidIO.useVcf());
this->vcfReaderPtr_ = currentDEploidIO.vcfReaderPtr_;
this->setDoExportVcf(currentDEploidIO.doExportVcf());
this->setDoComputeLLK(currentDEploidIO.doComputeLLK());

this->refCount_ = vector <double> (currentDEploidIO.refCount_.begin(),
currentDEploidIO.refCount_.end());
this->altCount_ = vector <double> (currentDEploidIO.altCount_.begin(),
currentDEploidIO.altCount_.end());
this->plaf_ = vector <double> (currentDEploidIO.plaf_.begin(),
currentDEploidIO.plaf_.end());
this->setDoExportRecombProb(cpFrom.doExportRecombProb());
this->setrandomSeedWasGiven(cpFrom.randomSeedWasGiven());
this->setCompressVcf(cpFrom.compressVcf());
this->setInitialPropWasGiven(cpFrom.initialPropWasGiven());
this->setInitialHapWasGiven(cpFrom.initialHapWasGiven());
this->initialProp = vector <double> (cpFrom.initialProp.begin(),
cpFrom.initialProp.end());
this->setPleaseCheckInitialP(cpFrom.pleaseCheckInitialP());
this->setExcludeSites(cpFrom.excludeSites());
this->excludedMarkers = cpFrom.excludedMarkers;
this->panel = cpFrom.panel;
this->set_seed(cpFrom.randomSeed_);
this->set_help(cpFrom.help());
this->setVersion(cpFrom.version());
this->setUsePanel(cpFrom.usePanel());
this->precision_ = cpFrom.precision_;
this->prefix_ = cpFrom.prefix_;
this->setKStrainWasManuallySet(cpFrom.kStrainWasManuallySet());
this->setKStrainWasSetByHap(cpFrom.kStrainWasSetByHap());
this->setKStrainWasSetByProp(cpFrom.kStrainWasSetByProp());
this->setKstrain(cpFrom.kStrain());
this->nMcmcSample_ = cpFrom.nMcmcSample();
this->setDoUpdateProp(cpFrom.doUpdateProp());
this->setDoUpdatePair(cpFrom.doUpdatePair());
this->setDoUpdateSingle(cpFrom.doUpdateSingle());
this->setDoExportPostProb(cpFrom.doExportPostProb());
this->setDoLsPainting(cpFrom.doLsPainting());
this->setDoIbdPainting(cpFrom.doIbdPainting());
this->setUseIBD(cpFrom.useIBD());
this->setUseLasso(cpFrom.useLasso());
this->setDoExportSwitchMissCopy(cpFrom.doExportSwitchMissCopy());
this->setDoAllowInbreeding(cpFrom.doAllowInbreeding());
this->mcmcBurn_ = cpFrom.mcmcBurn_;
this->mcmcMachineryRate_ = cpFrom.mcmcMachineryRate_;
this->missCopyProb_ = cpFrom.missCopyProb_;
this->useConstRecomb_ = cpFrom.useConstRecomb();
this->setForbidCopyFromSame(cpFrom.forbidCopyFromSame());
this->constRecombProb_ = cpFrom.constRecombProb();
this->averageCentimorganDistance_ = cpFrom.averageCentimorganDistance();
this->setScalingFactor(cpFrom.scalingFactor());
this->setParameterG(cpFrom.parameterG());
this->setParameterSigma(cpFrom.parameterSigma());
this->setIBDSigma(cpFrom.ibdSigma());
this->setUseVcf(cpFrom.useVcf());
this->vcfReaderPtr_ = cpFrom.vcfReaderPtr_;
this->setDoExportVcf(cpFrom.doExportVcf());
this->setDoComputeLLK(cpFrom.doComputeLLK());
this->setNLoci(cpFrom.nLoci());
this->refCount_ = vector <double> (cpFrom.refCount_.begin(),
cpFrom.refCount_.end());
this->altCount_ = vector <double> (cpFrom.altCount_.begin(),
cpFrom.altCount_.end());
this->plaf_ = vector <double> (cpFrom.plaf_.begin(),
cpFrom.plaf_.end());
this->chrom_ = vector <string> (cpFrom.chrom_.begin(),
cpFrom.chrom_.end());
this->position_ = vector < vector <int> > (cpFrom.position_.begin(),
cpFrom.position_.end());
this->indexOfChromStarts_ = vector <size_t> (cpFrom.indexOfChromStarts_.begin(),
cpFrom.indexOfChromStarts_.end());
this->strExportProp = cpFrom.strExportProp;
this->strExportLLK = cpFrom.strExportLLK;
this->strExportHap = cpFrom.strExportHap;
this->strIbdExportProp = cpFrom.strIbdExportProp;
this->strIbdExportLLK = cpFrom.strIbdExportLLK;
this->strIbdExportHap = cpFrom.strIbdExportHap;
}


Expand Down Expand Up @@ -905,3 +917,71 @@ void DEploidIO::dEploidLasso() {
}
}


void DEploidIO::computeObsWsaf() {
assert(this->obsWsaf_.size() == 0);
for ( size_t i = 0; i < this->nLoci(); i++) {
this->obsWsaf_.push_back(this->altCount_[i] /
(this->refCount_[i] + this->altCount_[i] + 0.00000000000001));
}
assert(this->obsWsaf_.size() == this->nLoci());
}


void DEploidIO::findWsafGreaterZeroAt() {
assert(wsafGt0At_.size() == 0);
for ( size_t i = 0; i < this->nLoci(); i++) {
if (this->obsWsaf_[i] > 0 ) {
this->wsafGt0At_.push_back(i);
}
}
// cout << "wsafGt0At_.size() = " << wsafGt0At_.size() << endl;
}


void DEploidIO::trimVec(vector <double> &vec, vector <size_t> &idx) {
vector <double> ret;
for (auto const& value : idx){
ret.push_back(vec[value]);
}
//return ret;
vec.clear();
for (auto const& value : ret){
vec.push_back(value);
}
}


void DEploidIO::ibdTrimming() {
this->computeObsWsaf();
this->findWsafGreaterZeroAt();

this->trimVec(this->refCount_, this->wsafGt0At_);
this->trimVec(this->altCount_, this->wsafGt0At_);
this->trimVec(this->plaf_, this->wsafGt0At_);

this->setNLoci(this->plaf_.size());

vector <string> oldChrom = vector <string> (chrom_.begin(), chrom_.end());
this->chrom_.clear();

vector < vector < int > > oldposition = this->position_;
this->position_.clear();

for (size_t chromI = 0; chromI < oldChrom.size(); chromI++) {
size_t hapIndex = indexOfChromStarts_[chromI];
vector <int> newTrimmedPos;
for (size_t posI = 0; posI < oldposition[chromI].size(); posI++) {
if (std::find(this->wsafGt0At_.begin(),this->wsafGt0At_.end(), hapIndex)
!= this->wsafGt0At_.end()){
if (newTrimmedPos.size() == 0) {
this->chrom_.push_back(oldChrom[chromI]);
}
newTrimmedPos.push_back(oldposition[chromI][posI]);
}

hapIndex++;
}
this->position_.push_back(newTrimmedPos);
}
}
23 changes: 18 additions & 5 deletions src/dEploidIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,17 @@ class DEploidIO{
void writeHap (vector < vector <double> > &hap, bool useIBD = false);
bool doPrintLassoPanel_;

// Trimming related
void ibdTrimming();



void setInitialHapWasGiven(const bool setTo) { this->initialHapWasGiven_ = setTo; }
vector < vector <double> > initialHap;
vector <double> initialProp;
void setDoUpdateProp ( const bool setTo ) { this->doUpdateProp_ = setTo; }
void setInitialPropWasGiven(const bool setTo) {this->initialPropWasGiven_ = setTo; }

private:
void core();
double llkFromInitialHap_;
Expand Down Expand Up @@ -146,11 +157,10 @@ class DEploidIO{
bool useIBD_;
bool useLasso_;

vector <double> initialProp;
vector <double> finalProp;
vector < vector <double> > initialHap;
vector <string> chrom_;
vector <double> obsWsaf_;
vector <size_t> wsafGt0At_;
size_t nLoci_;

// Help related
Expand Down Expand Up @@ -286,7 +296,6 @@ class DEploidIO{
void setIsCopied ( const bool setTo ) { this->isCopied_ = setTo; }
bool isCopied() const { return this->isCopied_; }

void setDoUpdateProp ( const bool setTo ) { this->doUpdateProp_ = setTo; }
bool doUpdateProp() const { return this->doUpdateProp_; }

void setDoUpdateSingle ( const bool setTo ) { this->doUpdateSingle_ = setTo; }
Expand All @@ -311,13 +320,11 @@ class DEploidIO{
void setUseLasso( const bool setTo) { this->useLasso_ = setTo; }

bool initialPropWasGiven() const { return initialPropWasGiven_; }
void setInitialPropWasGiven(const bool setTo) {this->initialPropWasGiven_ = setTo; }

bool pleaseCheckInitialP() const { return pleaseCheckInitialP_; }
void setPleaseCheckInitialP(const bool setTo) {this->pleaseCheckInitialP_ = setTo; }

bool initialHapWasGiven() const { return initialHapWasGiven_; }
void setInitialHapWasGiven(const bool setTo) { this->initialHapWasGiven_ = setTo; }

bool randomSeedWasGiven() const {return this->randomSeedWasGiven_; }

Expand Down Expand Up @@ -406,6 +413,12 @@ class DEploidIO{
vector <double> lassoComputeObsWsaf(size_t segmentStartIndex, size_t nLoci);
vector < vector <double> > lassoSubsetPanel(size_t segmentStartIndex, size_t nLoci);
void writePanel(Panel *panel, size_t chromi, vector <string> hdr);

// Trimming related
void computeObsWsaf();
void findWsafGreaterZeroAt();
void trimVec(vector <double> &vec, vector <size_t> &idx);

};

#endif
17 changes: 6 additions & 11 deletions src/debug/mcmcDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,21 @@
*/

#include "mcmc.hpp"
//#include "utility.hpp"
//#include <math.h> /* ceil */
//#include <random>
//#include "updateHap.hpp"
//#include <stdio.h>


bool McmcMachinery::doutProp(){
bool McmcMachinery::doutProp() {
dout << " Update proportion to: ";

for ( auto const& value: this->currentProp_ ){
for (auto const& value : this->currentProp_) {
dout << value << " ";
}

dout<<endl;
dout << endl;
return true;
}


bool McmcMachinery::doutLLK(){
dout << " Current log likelihood = " << sumOfVec( this->currentLLks_ ) << endl;
bool McmcMachinery::doutLLK() {
dout << " Current log likelihood = " <<
sumOfVec(this->currentLLks_) << endl;
return true;
}
Loading

0 comments on commit 895e308

Please sign in to comment.