diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dbd4a1e..1664426 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -78,7 +78,7 @@ endif(MPI_FOUND AND USE_MPI) configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h") -set(core_SRCS ehhfinder.cpp ihsfinder.cpp hapmap.cpp hapbin.cpp) +set(core_SRCS ehhfinder.cpp ihsfinder.cpp ehhfinder.cpp hapmap.cpp hapbin.cpp ehhfinder-impl.hpp) add_library(hapbin SHARED ${core_SRCS}) set_target_properties(hapbin PROPERTIES VERSION 0 SOVERSION 0.0.0) @@ -109,7 +109,7 @@ install(TARGETS ihsbin DESTINATION bin) install(TARGETS ehhbin DESTINATION bin) install(TARGETS xpehhbin DESTINATION bin) install(TARGETS hapbinconv DESTINATION bin) -install(FILES calcmpiselect.hpp calcnompiselect.hpp calcselect.hpp argparse.hpp hapmap.hpp hapbin.hpp ihsfinder.hpp ehhfinder.hpp DESTINATION include/hapbin) +install(FILES calcmpiselect.hpp calcnompiselect.hpp calcselect.hpp argparse.hpp hapmap.hpp hapbin.hpp ihsfinder.hpp ihsfinder-impl.hpp ehhfinder-impl.hpp DESTINATION include/hapbin) include(InstallRequiredSystemLibraries) set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Hapbin is a fast and efficient implementation of EHH and iHS calculations using a bitwise algorithm.") diff --git a/src/calcmpiselect.hpp b/src/calcmpiselect.hpp index a0763fe..d731e4e 100644 --- a/src/calcmpiselect.hpp +++ b/src/calcmpiselect.hpp @@ -57,12 +57,13 @@ ParameterStream& operator>>(ParameterStream& in, XPEHH& info) void calcIhsMpi(const std::string& hapfile, const std::string& mapfile, - const std::string& outfile, - double cutoff, - double minMAF, - double scale, - unsigned long long maxExtend, - int binFactor) + const std::string& outfile, + double cutoff, + double minMAF, + double scale, + unsigned long long maxExtend, + int binFactor, + bool binom) { std::cout << "Calculating iHS using MPI." << std::endl; HapMap hap; @@ -101,7 +102,10 @@ void calcIhsMpi(const std::string& hapfile, mpirpc::ObjectWrapperBase* mainihsfinder = *(manager->getObjectsOfType().cbegin()); mpirpc::FunctionHandle runEHH = manager->registerLambda([&](std::size_t start, std::size_t end) { std::cout << "Computing EHH for " << (end-start) << " lines on rank " << manager->rank() << std::endl; - ihsfinder->run(&hap, start, end); + if (binom) + ihsfinder->run(&hap, start, end); + else + ihsfinder->run(&hap, start, end); IHSFinder::LineMap fbl = ihsfinder->freqsByLine(); manager->invokeFunction(mainihsfinder, &IHSFinder::addData, false, fbl, ihsfinder->unStdIHSByLine(), ihsfinder->unStdIHSByFreq(), ihsfinder->numReachedEnd(), ihsfinder->numOutsideMaf(), ihsfinder->numNanResults()); manager->invokeFunction(0, done); @@ -132,7 +136,10 @@ void calcIhsMpi(const std::string& hapfile, std::size_t end = snpsPerRank; if (manager->numProcs() == 1) end = numSnps; - ihsfinder->run(&hap, 0UL, end); + if (binom) + ihsfinder->run(&hap, 0UL, end); + else + ihsfinder->run(&hap, 0UL, end); --procsToGo; } @@ -178,13 +185,14 @@ void calcIhsMpi(const std::string& hapfile, void calcXpehhMpi(const std::string& hapA, const std::string& hapB, - const std::string& mapfile, - const std::string& outfile, - double cutoff, - double minMAF, - double scale, - unsigned long long maxExtend, - int binFactor) + const std::string& mapfile, + const std::string& outfile, + double cutoff, + double minMAF, + double scale, + unsigned long long maxExtend, + int binFactor, + bool binom) { std::cout << "Calculating XPEHH using MPI." << std::endl; HapMap mA, mB; @@ -229,7 +237,10 @@ void calcXpehhMpi(const std::string& hapA, mpirpc::ObjectWrapperBase* mainihsfinder = *(manager->getObjectsOfType().cbegin()); mpirpc::FunctionHandle runXPEHH = manager->registerLambda([&](std::size_t start, std::size_t end) { std::cout << "Computing XPEHH for " << (end-start) << " lines on rank " << manager->rank() << std::endl; - ihsfinder->runXpehh(&mA, &mB, start, end); + if (binom) + ihsfinder->runXpehh(&mA, &mB, start, end); + else + ihsfinder->runXpehh(&mA, &mB, start, end); IHSFinder::LineMap fbl = ihsfinder->freqsByLine(); manager->invokeFunction(mainihsfinder, &IHSFinder::addXData, false, fbl, ihsfinder->unStdXIHSByLine(), ihsfinder->unStdIHSByFreq(), ihsfinder->numReachedEnd(), ihsfinder->numOutsideMaf(), ihsfinder->numNanResults()); manager->invokeFunction(0, done); @@ -261,7 +272,10 @@ void calcXpehhMpi(const std::string& hapA, std::size_t end = snpsPerRank; if (manager->numProcs() == 1) end = numSnps; - ihsfinder->runXpehh(&mA, &mB, 0ULL, end); + if (binom) + ihsfinder->runXpehh(&mA, &mB, 0ULL, end); + else + ihsfinder->runXpehh(&mA, &mB, 0ULL, end); --procsToGo; } diff --git a/src/ehhfinder-impl.hpp b/src/ehhfinder-impl.hpp new file mode 100644 index 0000000..0009246 --- /dev/null +++ b/src/ehhfinder-impl.hpp @@ -0,0 +1,592 @@ +/* + * Hapbin: A fast binary implementation EHH, iHS, and XPEHH + * Copyright (C) 2014 Colin MacLean + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +template +void EHHFinder::calcBranch(HapMap* hm, HapMap::PrimitiveType* parent, std::size_t parentcount, HapMap::PrimitiveType* branch, std::size_t& branchcount, std::size_t currLine, double freq, double &probs, std::size_t& singlecount, std::size_t maxBreadth, bool* overflow) +{ + HapMap::PrimitiveType* mapData = hm->rawData(); + std::size_t snpDataSize = hm->snpDataSize(); + std::size_t snpDataSizeULL = hm->snpDataSizeULL(); + std::size_t bcnt = 0; + for(std::size_t i = 0; i < parentcount; ++i) + { + int count = 0; + unsigned long long *leaf = (unsigned long long*) &parent[i*snpDataSize]; + for (std::size_t j = 0; j < snpDataSizeULL; ++j) + { + count += popcount1(leaf[j]); + } + + if (count == 0) + { + continue; + } + else if (!Binom && count == 1) + { + ++singlecount; + continue; + } + else + { + if (Binom) + probs += binom_2(count)*freq; + else + probs += (count*freq)*(count*freq); + for(std::size_t j = 0; j < snpDataSize; ++j) + { + branch[bcnt*snpDataSize+j] = parent[i*snpDataSize+j] & mapData[currLine*snpDataSize+j]; + } + ++bcnt; + for(std::size_t j = 0; j < snpDataSize-1; ++j) + { + branch[bcnt*snpDataSize+j] = parent[i*snpDataSize+j] & ~mapData[currLine*snpDataSize+j]; + } + branch[bcnt*snpDataSize+snpDataSize-1] = (parent[i*snpDataSize+snpDataSize-1] & ~mapData[currLine*snpDataSize+snpDataSize-1]) & m_maskA; + ++bcnt; + } + if (bcnt > maxBreadth-2) + { + *overflow = true; + return; + } + } + branchcount = bcnt; +} + +template +inline void EHHFinder::calcBranchXPEHH(std::size_t currLine, std::size_t& singleA, std::size_t& singleB, std::size_t& singleP, bool* overflow) +{ + std::size_t snpDataSize = m_snpDataSizeA + m_snpDataSizeB; + std::size_t bcnt = 0; + for(std::size_t i = 0; i < m_parent0count; ++i) + { + int countA = 0, countB = 0; + unsigned long long *leaf = (unsigned long long*) &m_parent0[i*snpDataSize]; + for (std::size_t j = 0; j < m_snpDataSizeULL_A; ++j) + { + countA += popcount1(leaf[j]); + } + leaf = (unsigned long long*) &m_parent0[i*snpDataSize+ m_snpDataSizeA]; + for (std::size_t j = 0; j < m_snpDataSizeULL_B; ++j) + { + countB += popcount1(leaf[j]); + } + int count = countA+countB; + + if (count == 0) + { + continue; + } + else if (!Binom && count == 1) + { + ++singleP; + singleA+=countA; + singleB+=countB; + continue; + } + else + { + if (Binom) + { + m_ehhA += binom_2(countA)*m_freqA; + m_ehhB += binom_2(countB)*m_freqB; + m_ehhP += binom_2(count)*m_freqP; + } + else + { + m_ehhA += (countA*m_freqA)*(countA*m_freqA); + m_ehhB += (countB*m_freqB)*(countB*m_freqB); + m_ehhP += (count*m_freqP)*(count*m_freqP); + } + //A part of the 1 branch + for(std::size_t j = 0; j < m_snpDataSizeA; ++j) + { + m_branch0[bcnt*snpDataSize+j] = m_parent0[i*snpDataSize+j] & m_hdA[currLine*m_snpDataSizeA+j]; + } + //B part of the 1 branch + for(std::size_t j = 0; j < m_snpDataSizeB; ++j) + { + m_branch0[bcnt*snpDataSize+m_snpDataSizeA+j] = m_parent0[i*snpDataSize+m_snpDataSizeA+j] & m_hdB[currLine*m_snpDataSizeB+j]; + } + ++bcnt; + //A part of the 0 branch + for(std::size_t j = 0; j < m_snpDataSizeA-1; ++j) + { + m_branch0[bcnt*snpDataSize+j] = m_parent0[i*snpDataSize+j] & ~m_hdA[currLine*m_snpDataSizeA+j]; + } + m_branch0[bcnt*snpDataSize+m_snpDataSizeA-1] = (m_parent0[i*snpDataSize+m_snpDataSizeA-1] & ~m_hdA[(currLine+1)*m_snpDataSizeA-1]) & m_maskA; + //B part of the 0 branch + for(std::size_t j = 0; j < m_snpDataSizeB-1; ++j) + { + m_branch0[bcnt*snpDataSize+m_snpDataSizeA+j] = m_parent0[i*snpDataSize+m_snpDataSizeA+j] & ~m_hdB[currLine*m_snpDataSizeB+j]; + } + m_branch0[(bcnt+1)*snpDataSize-1] = (m_parent0[(i+1)*snpDataSize-1] & ~m_hdB[(currLine+1)*m_snpDataSizeB-1]) & m_maskB; + ++bcnt; + } + if (bcnt > m_maxBreadth0-2) + { + *overflow = true; + return; + } + } + m_branch0count = bcnt; +} + +template +void EHHFinder::calcBranchesXPEHH(std::size_t currLine) +{ + bool overflow; + bool realloced0 = false; + std::size_t singleA{}, singleB{}, singleP{}; + do + { + overflow = false; + if (!Binom) + { + singleA = 0; + singleB = 0; + singleP = 0; + } + m_ehhA = 0.0; + m_ehhB = 0.0; + m_ehhP = 0.0; + m_branch0count = 0; + calcBranchXPEHH(currLine, singleA, singleB, singleP, &overflow); + if (overflow) + { + m_maxBreadth0 += 100; + aligned_free(m_branch0); + m_branch0 = reinterpret_cast(aligned_alloc(128, (m_snpDataSizeA+m_snpDataSizeB)*m_maxBreadth0*sizeof(HapMap::PrimitiveType))); + realloced0 = true; + continue; + } + } while (false); + if (realloced0) + { + aligned_free(m_parent0); + m_parent0 = reinterpret_cast(aligned_alloc(128, (m_snpDataSizeA+m_snpDataSizeB)*m_maxBreadth0*sizeof(HapMap::PrimitiveType))); + } + if (!Binom) + { + m_single0count += singleA; + m_single1count += singleB; + m_singlePcount += singleP; + } + m_parent0count = m_branch0count; + m_branch0count = 0ULL; + std::swap(m_parent0, m_branch0); +} + +template +XPEHH EHHFinder::findXPEHH(HapMap* hmA, HapMap* hmB, std::size_t focus, std::atomic* reachedEnd) +{ + if (focus <= 1 || focus >= hmA->numSnps()-2) + return XPEHH(); + m_hmA = hmA; + m_hmB = hmB; + m_hdA = hmA->rawData(); + m_snpDataSizeA = hmA->snpDataSize(); + m_snpDataSizeULL_A = hmA->snpDataSizeULL(); + m_hdB = hmB->rawData(); + m_snpDataSizeB = hmB->snpDataSize(); + m_snpDataSizeULL_B = hmB->snpDataSizeULL(); +#if VEC==4 + m_maskA = ::bitsetMask4(hmA->snpLength()); + m_maskB = ::bitsetMask4(hmB->snpLength()); +#elif VEC==2 + m_maskA = ::bitsetMask2(hmA->snpLength()); + m_maskB = ::bitsetMask2(hmB->snpLength()); +#else + m_maskA = ::bitsetMask(hmA->snpLength()); + m_maskB = ::bitsetMask(hmB->snpLength()); +#endif + m_parent0count = 2ULL; + m_parent1count = 2ULL; + m_branch0count = 0ULL; + m_branch1count = 0ULL; + if (!Binom) + { + m_single0count = 0ULL; + m_single1count = 0ULL; + m_singlePcount = 0ULL; + } + unsigned long long locusPysPos = hmA->physicalPosition(focus); + XPEHH ret; + ret.index = focus; + for(std::size_t i = 0; i < m_snpDataSizeA; ++i) + { + ret.numA += POPCOUNT(m_hdA[focus*m_snpDataSizeA+i]); + } + ret.numNotA = hmA->snpLength() - ret.numA; + for(std::size_t i = 0; i < m_snpDataSizeB; ++i) + { + ret.numB += POPCOUNT(m_hdB[focus*m_snpDataSizeB+i]); + } + ret.numNotB = hmA->snpLength() - ret.numB; + double probASingle, probBSingle, probPSingle; + double lastEhhA, lastEhhB, lastEhhP; + if (Binom) + { + m_freqA = 1.0/binom_2(hmA->snpLength()); + m_freqB = 1.0/binom_2(hmB->snpLength()); + m_freqP = 1.0/binom_2(hmA->snpLength()+hmB->snpLength()); + lastEhhA = (binom_2(ret.numA)+binom_2(ret.numNotA))*m_freqA; + lastEhhB = (binom_2(ret.numB)+binom_2(ret.numNotB))*m_freqB; + lastEhhP = (binom_2(ret.numA+ret.numB)+binom_2(ret.numNotA+ret.numNotB))*m_freqP; + } + else + { + m_freqA = 1.0/(double)hmA->snpLength(); + m_freqB = 1.0/(double)hmB->snpLength(); + m_freqP = 1.0/(double)(hmA->snpLength()+hmB->snpLength()); + probASingle = m_freqA*m_freqA; + probBSingle = m_freqB*m_freqB; + probPSingle = m_freqP*m_freqP; + double f = ret.numA*m_freqA; + lastEhhA = f*f+(1.0-f)*(1.0-f); + f = ret.numB*m_freqB; + lastEhhB = f*f+(1.0-f)*(1.0-f); + f = (ret.numA+ret.numB)*m_freqP; + lastEhhP = f*f+(1.0-f)*(1.0-f); + m_single0count = 0ULL; + m_single1count = 0ULL; + } + + if (!Binom) + { + m_single0count = 0ULL; + m_single1count = 0ULL; + m_singlePcount = 0ULL; + } + setInitialXPEHH(focus); + calcBranchesXPEHH(focus-1); + if (focus > 1) + { + for (std::size_t currLine = focus - 2;; --currLine) + { + unsigned long long currPhysPos = hmA->physicalPosition(currLine+1); + double scale = (double)(m_scale) / (double)(hmA->physicalPosition(currLine+2) - currPhysPos); + if (scale > 1) + scale=1; + + calcBranchesXPEHH(currLine); + + if (!Binom) + { + m_ehhA += probASingle*m_single0count; + m_ehhB += probBSingle*m_single1count; + m_ehhP += probPSingle*(m_single0count+m_single1count); + } + + if (m_ehhP <= m_cutoff - 1e-15) + break; + + ret.iHH_A1 += (hmA->geneticPosition(currLine+2)-hmA->geneticPosition(currLine+1))*(lastEhhA + m_ehhA)*scale*0.5; + ret.iHH_B1 += (hmA->geneticPosition(currLine+2)-hmA->geneticPosition(currLine+1))*(lastEhhB + m_ehhB)*scale*0.5; + ret.iHH_P1 += (hmA->geneticPosition(currLine+2)-hmA->geneticPosition(currLine+1))*(lastEhhP + m_ehhP)*scale*0.5; + + lastEhhA = m_ehhA; + lastEhhB = m_ehhB; + lastEhhP = m_ehhP; + + if (m_maxExtend != 0 && locusPysPos - currPhysPos > m_maxExtend) + break; + if (Binom && m_ehhP == 0) + break; + if (!Binom && (m_single0count+m_single1count) == (hmA->snpLength()+hmB->snpLength())) + break; + if(currLine == 0) + { + ++(*reachedEnd); + return XPEHH(); + } + } + } + + if (Binom) + { + lastEhhA = (binom_2(ret.numA)+binom_2(ret.numNotA))*m_freqA; + lastEhhB = (binom_2(ret.numB)+binom_2(ret.numNotB))*m_freqB; + lastEhhP = (binom_2(ret.numA+ret.numB)+binom_2(ret.numNotA+ret.numNotB))*m_freqP; + } + else + { + double f = ret.numA*m_freqA; + lastEhhA = f*f+(1.0-f)*(1.0-f); + f = ret.numB*m_freqB; + lastEhhB = f*f+(1.0-f)*(1.0-f); + f = (ret.numA+ret.numB)*m_freqP; + lastEhhP = f*f+(1.0-f)*(1.0-f); + m_single0count = 0ULL; + m_single1count = 0ULL; + } + + setInitialXPEHH(focus); + calcBranchesXPEHH(focus+1); + for (std::size_t currLine = focus + 2; currLine < hmA->numSnps(); ++currLine) + { + unsigned long long currPhysPos = hmA->physicalPosition(currLine-1); + double scale = (double)(m_scale) / (double)(currPhysPos - hmA->physicalPosition(currLine-2)); + if (scale > 1) + scale=1; + + calcBranchesXPEHH(currLine); + + if (!Binom) + { + m_ehhA += probASingle*m_single0count; + m_ehhB += probBSingle*m_single1count; + m_ehhP += probPSingle*(m_single0count+m_single1count); + } + + if (m_ehhP <= m_cutoff - 1e-15) + break; + + ret.iHH_A1 += (hmA->geneticPosition(currLine-1)-hmA->geneticPosition(currLine-2))*(lastEhhA + m_ehhA)*scale*0.5; + ret.iHH_B1 += (hmA->geneticPosition(currLine-1)-hmA->geneticPosition(currLine-2))*(lastEhhB + m_ehhB)*scale*0.5; + ret.iHH_P1 += (hmA->geneticPosition(currLine-1)-hmA->geneticPosition(currLine-2))*(lastEhhP + m_ehhP)*scale*0.5; + + lastEhhA = m_ehhA; + lastEhhB = m_ehhB; + lastEhhP = m_ehhP; + + if (m_maxExtend != 0 && currPhysPos - locusPysPos > m_maxExtend) + break; + if (!Binom && (m_single0count+m_single1count) == (hmA->snpLength()+hmB->snpLength())) + break; + if (Binom && m_ehhP == 0) + break; + if (currLine == hmA->numSnps()-1) + { + ++(*reachedEnd); + return XPEHH(); + } + } + return ret; +} + +template +void EHHFinder::calcBranches(HapMap* hapmap, std::size_t focus, std::size_t currLine, double freq0, double freq1, HapStats &stats) +{ + bool overflow; + bool realloced0 = false, realloced1 = false; + std::size_t single0{}; + do + { + overflow = false; + if (!Binom) + single0 = 0; + stats.probsNot = 0.0; + calcBranch(hapmap, m_parent0, m_parent0count, m_branch0, m_branch0count, currLine, freq0, stats.probsNot, single0, m_maxBreadth0, &overflow); + if(overflow) + { + m_maxBreadth0 += 100; + aligned_free(m_branch0); + m_branch0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth0*sizeof(HapMap::PrimitiveType))); + realloced0 = true; + continue; + } + } while (false); + if (realloced0) + { + aligned_free(m_parent0); + m_parent0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth0*sizeof(HapMap::PrimitiveType))); + } + std::size_t single1{}; + do + { + overflow = false; + if (!Binom) + single1 = 0; + stats.probs = 0.0; + calcBranch(hapmap, m_parent1, m_parent1count, m_branch1, m_branch1count, currLine, freq1, stats.probs, single1, m_maxBreadth1, &overflow); + if(overflow) + { + m_maxBreadth1 += 100; + aligned_free(m_branch1); + m_branch1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth1*sizeof(HapMap::PrimitiveType))); + realloced1 = true; + continue; + } + } while (false); + if (realloced1) + { + aligned_free(m_parent1); + m_parent1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth1*sizeof(HapMap::PrimitiveType))); + } + m_parent0count = m_branch0count; + m_parent1count = m_branch1count; + m_branch0count = 0ULL; + m_branch1count = 0ULL; + if (!Binom) + { + m_single0count += single0; + m_single1count += single1; + } + std::swap(m_parent0, m_branch0); + std::swap(m_parent1, m_branch1); +} + +template +EHH EHHFinder::find(HapMap* hapmap, std::size_t focus, std::atomic* reachedEnd, std::atomic* outsideMaf, bool ehhsave) +{ + m_parent0count = 2ULL; + m_parent1count = 2ULL; + m_branch0count = 0ULL; + m_branch1count = 0ULL; + if (!Binom) + { + m_single0count = 0ULL; + m_single1count = 0ULL; + } + m_hdA = hapmap->rawData(); + m_snpDataSizeA = hapmap->snpDataSize(); + if (!m_parent1) + m_parent1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth1*sizeof(HapMap::PrimitiveType))); + if (!m_branch1) + m_branch1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth1*sizeof(HapMap::PrimitiveType))); +#if VEC==4 + m_maskA = ::bitsetMask4(hapmap->snpLength()); +#elif VEC==2 + m_maskA = ::bitsetMask2(hapmap->snpLength()); +#else + m_maskA = ::bitsetMask(hapmap->snpLength()); +#endif + EHH ret; + ret.index = focus; + + for(std::size_t i = 0; i < m_snpDataSizeA; ++i) + { + ret.num += POPCOUNT(m_hdA[focus*m_snpDataSizeA+i]); + } + ret.numNot = hapmap->snpLength() - ret.num; + + double maxEHH = ret.num/(double)hapmap->snpLength(); + if (!(maxEHH <= 1.0 - m_minMAF && maxEHH >= m_minMAF) && m_minMAF != 0.0) + { + ++(*outsideMaf); + return EHH(); + } + if (focus < 2 || focus == hapmap->numSnps()-2) + { + ++(*reachedEnd); + return EHH(); + } + + double freq0, freq1; + double probSingle, probNotSingle; + if (Binom) + { + freq0 = 1.0/binom_2(ret.numNot); + freq1 = 1.0/binom_2(ret.num); + } + else + { + freq0 = 1.0/(double)ret.numNot; + freq1 = 1.0/(double)ret.num; + probSingle = freq1*freq1; + probNotSingle = freq0*freq0; + } + double lastProbs = 1.0, lastProbsNot = 1.0; + unsigned long long locusPysPos = hapmap->physicalPosition(focus); + + setInitial(focus, focus-1); + + for (std::size_t currLine = focus - 2;; --currLine) + { + HapStats stats; + unsigned long long currPhysPos = hapmap->physicalPosition(currLine+1); + double scale = (double)(m_scale) / (double)(hapmap->physicalPosition(currLine+2) - currPhysPos); + if (scale > 1) + scale=1; + + calcBranches(hapmap, focus, currLine, freq0, freq1, stats); + + if (!Binom) + { + stats.probs += probSingle*m_single1count; + stats.probsNot += probNotSingle*m_single0count; + } + + if (lastProbs > m_cutoff - 1e-15) + ret.iHH_1 += (hapmap->geneticPosition(currLine+2)-hapmap->geneticPosition(currLine+1))*(lastProbs + stats.probs)*scale*0.5; + if (lastProbsNot > m_cutoff - 1e-15) + ret.iHH_0 += (hapmap->geneticPosition(currLine+2)-hapmap->geneticPosition(currLine+1))*(lastProbsNot + stats.probsNot)*scale*0.5; + + lastProbs = stats.probs; + lastProbsNot = stats.probsNot; + if (ehhsave) + ret.upstream.push_back(std::move(stats)); + + + if (m_maxExtend != 0 && locusPysPos - currPhysPos > m_maxExtend) + break; + if (lastProbs <= m_cutoff - 1e-15 && lastProbsNot <= m_cutoff - 1e-15) + break; + if (!Binom && (m_single0count+m_single1count) == hapmap->snpLength()) + break; + if (currLine == 0) + { + ++(*reachedEnd); + return EHH(); + } + } + + setInitial(focus,focus+1); + lastProbs = 1.0, lastProbsNot = 1.0; + for (std::size_t currLine = focus + 2; currLine < hapmap->numSnps(); ++currLine) + { + HapStats stats; + unsigned long long currPhysPos = hapmap->physicalPosition(currLine-1); + double scale = double(m_scale) / double(hapmap->physicalPosition(currLine-1) - hapmap->physicalPosition(currLine-2)); + if (scale > 1) + scale=1; + + calcBranches(hapmap, focus, currLine, freq0, freq1, stats); + + if (!Binom) + { + stats.probs += probSingle*m_single1count; + stats.probsNot += probNotSingle*m_single0count; + } + + if (lastProbs > m_cutoff - 1e-15) { + ret.iHH_1 += (hapmap->geneticPosition(currLine-1)-hapmap->geneticPosition(currLine-2))*(lastProbs + stats.probs)*scale*0.5; + } + if (lastProbsNot > m_cutoff - 1e-15) { + ret.iHH_0 += (hapmap->geneticPosition(currLine-1)-hapmap->geneticPosition(currLine-2))*(lastProbsNot + stats.probsNot)*scale*0.5; + } + + lastProbs = stats.probs; + lastProbsNot = stats.probsNot; + if (ehhsave) + ret.downstream.push_back(std::move(stats)); + + if (lastProbs <= m_cutoff - 1e-15 && lastProbsNot <= m_cutoff - 1e-15 || (m_single0count+m_single1count) == hapmap->snpLength()) + break; + if (m_maxExtend != 0 && currPhysPos - locusPysPos > m_maxExtend) + break; + if (!Binom && currLine == hapmap->numSnps()-1) + { + ++(*reachedEnd); + return EHH(); + } + } + return ret; +} diff --git a/src/ehhfinder.cpp b/src/ehhfinder.cpp index 97d7bbc..d9c5065 100644 --- a/src/ehhfinder.cpp +++ b/src/ehhfinder.cpp @@ -20,11 +20,17 @@ #include "ehhfinder.hpp" #include "hapmap.hpp" #include +#include -EHHFinder::EHHFinder(std::size_t snpDataSizeA, std::size_t snpDataSizeB, std::size_t maxBreadth, double cutoff, double minMAF, double scale, long long unsigned int maxExtend) - : m_maxBreadth(maxBreadth) +EHHFinder::EHHFinder(std::size_t snpDataSizeA, std::size_t snpDataSizeB, std::size_t maxBreadth, double cutoff, double minMAF, double scale, unsigned long long maxExtend) + : m_maxBreadth0(maxBreadth) + , m_maxBreadth1(maxBreadth) , m_bufferSize(snpDataSizeA*maxBreadth) , m_maxExtend(maxExtend) + , m_parent0(reinterpret_cast(aligned_alloc(128, (snpDataSizeA+snpDataSizeB)*maxBreadth*sizeof(HapMap::PrimitiveType)))) + , m_parent1(NULL) + , m_branch0(reinterpret_cast(aligned_alloc(128, (snpDataSizeA+snpDataSizeB)*maxBreadth*sizeof(HapMap::PrimitiveType)))) + , m_branch1(NULL) , m_cutoff(cutoff) , m_minMAF(minMAF) , m_scale(scale) @@ -35,18 +41,13 @@ EHHFinder::EHHFinder(std::size_t snpDataSizeA, std::size_t snpDataSizeB, std::si , m_ehhA{} , m_ehhB{} , m_ehhP{} -{ - m_parent0 = reinterpret_cast(aligned_alloc(128, snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_parent1 = reinterpret_cast(aligned_alloc(128, snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_branch0 = reinterpret_cast(aligned_alloc(128, snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_branch1 = reinterpret_cast(aligned_alloc(128, snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); -} +{} /** * Set initial state. Set m_parent0 to '0' core haplotype positions, m_parent1 to '1' core haplotype positions. - * + * * calcBranch counts the state of the parent (previous) branch, not the counts of the new (current) level. Therefore, we must advance - * the calculations by one iteration before starting. + * the calculations by one iteration before starting. */ void EHHFinder::setInitial(std::size_t focus, std::size_t line) { @@ -56,7 +57,6 @@ void EHHFinder::setInitial(std::size_t focus, std::size_t line) m_branch1count = 0ULL; m_single0count = 0ULL; m_single1count = 0ULL; - for (int j = 0; j < m_snpDataSizeA; ++j) { @@ -71,509 +71,30 @@ void EHHFinder::setInitial(std::size_t focus, std::size_t line) } void EHHFinder::setInitialXPEHH(std::size_t focus) -{ - for (std::size_t j = 0; j < m_snpDataSizeA; ++j) - { - m_parent0[j] = ~m_hdA[focus*m_snpDataSizeA+j]; - } - m_parent0[m_snpDataSizeA-1] &= m_maskA; - for (std::size_t j = 0; j < m_snpDataSizeA; ++j) - { - m_parent0[j+m_snpDataSizeA] = m_hdA[focus*m_snpDataSizeA+j]; - } - for (std::size_t j = 0; j < m_snpDataSizeB; ++j) - { - m_parent1[j] = ~m_hdB[focus*m_snpDataSizeB+j]; - } - m_parent1[m_snpDataSizeB-1] &= m_maskB; - for (std::size_t j = 0; j < m_snpDataSizeB; ++j) - { - m_parent1[j+m_snpDataSizeB] = m_hdB[focus*m_snpDataSizeB+j]; - } -} - -void EHHFinder::calcBranch(HapMap* hm, std::size_t focus, HapMap::PrimitiveType* parent, std::size_t parentcount, HapMap::PrimitiveType* branch, std::size_t& branchcount, std::size_t currLine, double freq, double &probs, std::size_t& singlecount, bool* overflow) -{ - HapMap::PrimitiveType* mapData = hm->rawData(); - std::size_t snpDataSize = hm->snpDataSize(); - std::size_t snpDataSizeULL = hm->snpDataSizeULL(); - std::size_t bcnt = 0; - for(std::size_t i = 0; i < parentcount; ++i) - { - int count = 0; - unsigned long long *leaf = (unsigned long long*) &parent[i*snpDataSize]; - for (std::size_t j = 0; j < snpDataSizeULL; ++j) - { - count += popcount1(leaf[j]); - } - - if (count == 0) - { - continue; - } - else if (count == 1) - { - ++singlecount; - continue; - } - else - { - probs += (count*freq)*(count*freq); - for(std::size_t j = 0; j < snpDataSize; ++j) - { - branch[bcnt*snpDataSize+j] = parent[i*snpDataSize+j] & mapData[currLine*snpDataSize+j]; - } - ++bcnt; - for(std::size_t j = 0; j < snpDataSize-1; ++j) - { - branch[bcnt*snpDataSize+j] = parent[i*snpDataSize+j] & ~mapData[currLine*snpDataSize+j]; - } - branch[bcnt*snpDataSize+snpDataSize-1] = (parent[i*snpDataSize+snpDataSize-1] & ~mapData[currLine*snpDataSize+snpDataSize-1]) & m_maskA; - ++bcnt; - } - if (bcnt > m_maxBreadth-2) - { - *overflow = true; - return; - } - } - branchcount = bcnt; -} - -void EHHFinder::calcBranchXPEHH(HapMap* hmA, HapMap* hmB, std::size_t currLine, std::size_t& single0, std::size_t& single1, bool* overflow) -{ - for(std::size_t i = 0; i < m_parent0count; ++i) - { - int numA = 0; - unsigned long long *parentA_ULL = (unsigned long long*) &m_parent0[i*m_snpDataSizeA]; - for (std::size_t j = 0; j < m_snpDataSizeULL_A; ++j) - { - numA += popcount1(parentA_ULL[j]); - } - int numB = 0; - unsigned long long *parentB_ULL = (unsigned long long*) &m_parent1[i*m_snpDataSizeB]; - for (size_t j = 0; j < m_snpDataSizeULL_B; ++j) - { - numB += popcount1(parentB_ULL[j]); - } - int numPooled = numA + numB; - //std::cout << i << "numA: " << numA << " numB: " << numB << " numP: " << numPooled << " freqP: " << m_freqP << " " << m_snpDataSizeUL_A << std::endl; - if (numPooled == 0) - { - continue; - } - else if (numPooled == 1) - { - single0 += numA; - single1 += numB; - continue; - } - else - { - m_ehhA += (numA*m_freqA)*(numA*m_freqA); - m_ehhB += (numB*m_freqB)*(numB*m_freqB); - m_ehhP += (numPooled*m_freqP)*(numPooled*m_freqP); - - //Population A - for(std::size_t j = 0; j < m_snpDataSizeA; ++j) - { - m_branch0[m_branch0count*m_snpDataSizeA+j] = m_parent0[i*m_snpDataSizeA+j] & m_hdA[currLine*m_snpDataSizeA+j]; - } - ++m_branch0count; - for(std::size_t j = 0; j < m_snpDataSizeA-1; ++j) - { - m_branch0[m_branch0count*m_snpDataSizeA+j] = m_parent0[i*m_snpDataSizeA+j] & ~m_hdA[currLine*m_snpDataSizeA+j]; - } - m_branch0[m_branch0count*m_snpDataSizeA+m_snpDataSizeA-1] = (m_parent0[i*m_snpDataSizeA+m_snpDataSizeA-1] & ~m_hdA[currLine*m_snpDataSizeA+m_snpDataSizeA-1]) & m_maskA; - ++m_branch0count; - - //Population B - for(std::size_t j = 0; j < m_snpDataSizeB; ++j) - { - m_branch1[m_branch1count*m_snpDataSizeB+j] = m_parent1[i*m_snpDataSizeB+j] & m_hdB[currLine*m_snpDataSizeB+j]; - } - ++m_branch1count; - for(std::size_t j = 0; j < m_snpDataSizeB-1; ++j) - { - m_branch1[m_branch1count*m_snpDataSizeB+j] = m_parent1[i*m_snpDataSizeB+j] & ~m_hdB[currLine*m_snpDataSizeB+j]; - } - m_branch1[m_branch1count*m_snpDataSizeB+m_snpDataSizeB-1] = (m_parent1[i*m_snpDataSizeB+m_snpDataSizeB-1] & ~m_hdB[currLine*m_snpDataSizeB+m_snpDataSizeB-1]) & m_maskB; - ++m_branch1count; - } - if (m_branch0count > m_maxBreadth-2) - { - *overflow = true; - return; - } - } -} - -void EHHFinder::calcBranchesXPEHH(std::size_t currLine) -{ - bool overflow; - bool realloced = false; - std::size_t single0{}, single1{}; - m_ehhA = 0.0; - m_ehhB = 0.0; - m_ehhP = 0.0; - while (true) - { - overflow = false; - single0 = 0; single1 = 0; - m_branch0count = 0; - m_branch1count = 0; - calcBranchXPEHH(m_hmA, m_hmB, currLine, single0, single1, &overflow); - if (overflow) - { - m_maxBreadth += 500; - aligned_free(m_branch0); - aligned_free(m_branch1); - m_branch0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_branch1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - realloced = true; - } - else - break; - } - if (realloced) - { - aligned_free(m_parent0); - aligned_free(m_parent1); - m_parent0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_parent1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - } - m_single0count += single0; - m_single1count += single1; - m_parent0count = m_branch0count; - m_parent1count = m_branch1count; - m_branch0count = 0ULL; - m_branch1count = 0ULL; - std::swap(m_parent0, m_branch0); - std::swap(m_parent1, m_branch1); -} - -XPEHH EHHFinder::findXPEHH(HapMap* hmA, HapMap* hmB, std::size_t focus, std::atomic* reachedEnd) -{ - m_hmA = hmA; - m_hmB = hmB; - m_hdA = hmA->rawData(); - m_snpDataSizeA = hmA->snpDataSize(); - m_snpDataSizeULL_A = hmA->snpDataSizeULL(); - m_hdB = hmB->rawData(); - m_snpDataSizeB = hmB->snpDataSize(); - m_snpDataSizeULL_B = hmB->snpDataSizeULL(); -#if VEC==4 - m_maskA = ::bitsetMask4(hmA->snpLength()); - m_maskB = ::bitsetMask4(hmB->snpLength()); -#elif VEC==2 - m_maskA = ::bitsetMask2(hmA->snpLength()); - m_maskB = ::bitsetMask2(hmB->snpLength()); -#else - m_maskA = ::bitsetMask(hmA->snpLength()); - m_maskB = ::bitsetMask(hmB->snpLength()); -#endif - m_parent0count = 2ULL; - m_parent1count = 2ULL; - m_branch0count = 0ULL; - m_branch1count = 0ULL; - m_single0count = 0ULL; - m_single1count = 0ULL; - XPEHH ret; - ret.index = focus; - for(std::size_t i = 0; i < m_snpDataSizeA; ++i) - { - ret.numA += POPCOUNT(m_hdA[focus*m_snpDataSizeA+i]); - } - ret.numNotA = hmA->snpLength() - ret.numA; - for(std::size_t i = 0; i < m_snpDataSizeB; ++i) - { - ret.numB += POPCOUNT(m_hdB[focus*m_snpDataSizeB+i]); - } - ret.numNotB = hmA->snpLength() - ret.numB; - m_freqA = 1.0/(double)hmA->snpLength(); - m_freqB = 1.0/(double)hmB->snpLength(); - m_freqP = 1.0/(double)(hmA->snpLength()+hmB->snpLength()); - double probASingle = m_freqA*m_freqA; - double probBSingle = m_freqB*m_freqB; - double probPSingle = m_freqP*m_freqP; - double f = ret.numA*m_freqA; - double lastEhhA = f*f+(1.0-f)*(1.0-f); - f = ret.numB*m_freqB; - double lastEhhB = f*f+(1.0-f)*(1.0-f); - f = (ret.numA+ret.numB)*m_freqP; - double lastEhhP = f*f+(1.0-f)*(1.0-f); - unsigned long long locusPysPos = hmA->physicalPosition(focus); - - setInitialXPEHH(focus); - calcBranchesXPEHH(focus-1); - m_single0count = 0ULL; - m_single1count = 0ULL; - if (focus > 1) - { - for (std::size_t currLine = focus - 2;; --currLine) - { - unsigned long long currPhysPos = hmA->physicalPosition(currLine+1); - double scale = (double)(m_scale) / (double)(hmA->physicalPosition(currLine+2) - currPhysPos); - if (scale > 1) - scale=1; - - calcBranchesXPEHH(currLine); - - m_ehhA += probASingle*m_single0count; - m_ehhB += probBSingle*m_single1count; - m_ehhP += probPSingle*(m_single0count+m_single1count); - - if (lastEhhP <= m_cutoff + 1e-15) - break; - ret.iHH_A1 += (hmA->geneticPosition(currLine+2)-hmA->geneticPosition(currLine+1))*(lastEhhA + m_ehhA)*scale*0.5; - ret.iHH_B1 += (hmA->geneticPosition(currLine+2)-hmA->geneticPosition(currLine+1))*(lastEhhB + m_ehhB)*scale*0.5; - ret.iHH_P1 += (hmA->geneticPosition(currLine+2)-hmA->geneticPosition(currLine+1))*(lastEhhP + m_ehhP)*scale*0.5; - - lastEhhA = m_ehhA; - lastEhhB = m_ehhB; - lastEhhP = m_ehhP; - - if ((m_single0count+m_single1count) == (hmA->snpLength()+hmB->snpLength())) - break; - if (m_maxExtend != 0 && locusPysPos - currPhysPos > m_maxExtend) - break; - if(currLine == 0) - { - ++(*reachedEnd); - return XPEHH(); - } - } - } - - f = ret.numA*m_freqA; - lastEhhA = f*f+(1.0-f)*(1.0-f); - f = ret.numB*m_freqB; - lastEhhB = f*f+(1.0-f)*(1.0-f); - f = (ret.numA+ret.numB)*m_freqP; - lastEhhP = f*f+(1.0-f)*(1.0-f); - - setInitialXPEHH(focus); - calcBranchesXPEHH(focus+1); - m_single0count = 0ULL; - m_single1count = 0ULL; - for (std::size_t currLine = focus + 2; currLine < hmA->numSnps(); ++currLine) - { - unsigned long long currPhysPos = hmA->physicalPosition(currLine-1); - double scale = (double)(m_scale) / (double)(currPhysPos - hmA->physicalPosition(currLine-2)); - if (scale > 1) - scale=1; - - calcBranchesXPEHH(currLine); - - m_ehhA += probASingle*m_single0count; - m_ehhB += probBSingle*m_single1count; - m_ehhP += probPSingle*(m_single0count+m_single1count); - - if (lastEhhP <= m_cutoff + 1e-15) - break; - ret.iHH_A1 += (hmA->geneticPosition(currLine-1)-hmA->geneticPosition(currLine-2))*(lastEhhA + m_ehhA)*scale*0.5; - ret.iHH_B1 += (hmA->geneticPosition(currLine-1)-hmA->geneticPosition(currLine-2))*(lastEhhB + m_ehhB)*scale*0.5; - ret.iHH_P1 += (hmA->geneticPosition(currLine-1)-hmA->geneticPosition(currLine-2))*(lastEhhP + m_ehhP)*scale*0.5; - - lastEhhA = m_ehhA; - lastEhhB = m_ehhB; - lastEhhP = m_ehhP; - - if ((m_single0count+m_single1count) == (hmA->snpLength()+hmB->snpLength())) - break; - if (m_maxExtend != 0 && currPhysPos - locusPysPos > m_maxExtend) - break; - if (currLine == hmA->numSnps()-1) - { - ++(*reachedEnd); - return XPEHH(); - } - } - return ret; -} - -void EHHFinder::calcBranches(HapMap* hapmap, std::size_t focus, std::size_t currLine, double freq0, double freq1, HapStats &stats) -{ - bool overflow; - bool realloced = false; - std::size_t single0{}; - std::size_t single1{}; - while(true) - { - overflow = false; - single0 = 0; single1 = 0; - calcBranch(hapmap, focus, m_parent0, m_parent0count, m_branch0, m_branch0count, currLine, freq0, stats.probsNot, single0, &overflow); - if(overflow) - { - m_maxBreadth += 100; - aligned_free(m_branch0); - aligned_free(m_branch1); - m_branch0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_branch1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - realloced = true; - continue; - } - overflow = false; - calcBranch(hapmap, focus, m_parent1, m_parent1count, m_branch1, m_branch1count, currLine, freq1, stats.probs, single1, &overflow); - if(overflow) - { - m_maxBreadth += 100; - aligned_free(m_branch0); - aligned_free(m_branch1); - m_branch0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_branch1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - realloced = true; - continue; - } - break; - } - if (realloced) - { - aligned_free(m_parent0); - aligned_free(m_parent1); - m_parent0 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeA*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - m_parent1 = reinterpret_cast(aligned_alloc(128, m_snpDataSizeB*m_maxBreadth*sizeof(HapMap::PrimitiveType))); - } - m_parent0count = m_branch0count; - m_parent1count = m_branch1count; - m_branch0count = 0ULL; - m_branch1count = 0ULL; - m_single0count += single0; - m_single1count += single1; - std::swap(m_parent0, m_branch0); - std::swap(m_parent1, m_branch1); -} - -EHH EHHFinder::find(HapMap* hapmap, std::size_t focus, std::atomic* reachedEnd, std::atomic* outsideMaf, bool ehhsave) { m_parent0count = 2ULL; m_parent1count = 2ULL; m_branch0count = 0ULL; m_branch1count = 0ULL; - m_single0count = 0ULL; - m_single1count = 0ULL; - m_hdA = hapmap->rawData(); - m_snpDataSizeA = hapmap->snpDataSize(); -#if VEC==4 - m_maskA = ::bitsetMask4(hapmap->snpLength()); -#elif VEC==2 - m_maskA = ::bitsetMask2(hapmap->snpLength()); -#else - m_maskA = ::bitsetMask(hapmap->snpLength()); -#endif - EHH ret; - ret.index = focus; - - for(std::size_t i = 0; i < m_snpDataSizeA; ++i) - { - ret.num += POPCOUNT(m_hdA[focus*m_snpDataSizeA+i]); - } - ret.numNot = hapmap->snpLength() - ret.num; - - double maxEHH = ret.num/(double)hapmap->snpLength(); - if (!(maxEHH <= 1.0 - m_minMAF && maxEHH >= m_minMAF)) - { - ++(*outsideMaf); - return EHH(); - } - if (focus < 2) - { - ++(*reachedEnd); - return EHH(); - } - - double freq0 = 1.0/(double)ret.numNot; - double freq1 = 1.0/(double)ret.num; - double probSingle = freq1*freq1; - double probNotSingle = freq0*freq0; - double lastProbs = 1.0, lastProbsNot = 1.0; - unsigned long long locusPysPos = hapmap->physicalPosition(focus); - - setInitial(focus, focus-1); - - for (std::size_t currLine = focus - 2;; --currLine) - { - HapStats stats; - unsigned long long currPhysPos = hapmap->physicalPosition(currLine+1); - double scale = (double)(m_scale) / (double)(hapmap->physicalPosition(currLine+2) - currPhysPos); - if (scale > 1) - scale=1; - - calcBranches(hapmap, focus, currLine, freq0, freq1, stats); - - stats.probs += probSingle*m_single1count; - stats.probsNot += probNotSingle*m_single0count; - if (lastProbs > m_cutoff + 1e-15) - ret.iHH_1 += (hapmap->geneticPosition(currLine+2)-hapmap->geneticPosition(currLine+1))*(lastProbs + stats.probs)*scale*0.5; - if (lastProbsNot > m_cutoff + 1e-15) - ret.iHH_0 += (hapmap->geneticPosition(currLine+2)-hapmap->geneticPosition(currLine+1))*(lastProbsNot + stats.probsNot)*scale*0.5; - - lastProbs = stats.probs; - lastProbsNot = stats.probsNot; - if (ehhsave) - ret.upstream.push_back(std::move(stats)); - - if (m_maxExtend != 0 && locusPysPos - currPhysPos > m_maxExtend) { - //std::cout << m_maxExtend << std::endl; - break; - } - if (lastProbs <= m_cutoff + 1e-15 && lastProbsNot <= m_cutoff + 1e-15) - break; - if ((m_single0count+m_single1count) == hapmap->snpLength()) - break; - if (currLine == 0) - { - ++(*reachedEnd); - return EHH(); - } - } - - setInitial(focus,focus+1); - lastProbs = 1.0, lastProbsNot = 1.0; - for (std::size_t currLine = focus + 2; currLine < hapmap->numSnps(); ++currLine) - { - HapStats stats; - unsigned long long currPhysPos = hapmap->physicalPosition(currLine-1); - double scale = double(m_scale) / double(hapmap->physicalPosition(currLine-1) - hapmap->physicalPosition(currLine-2)); - if (scale > 1) - scale=1; - - calcBranches(hapmap, focus, currLine, freq0, freq1, stats); - - stats.probs += probSingle*m_single1count; - stats.probsNot += probNotSingle*m_single0count; - - if (lastProbs > m_cutoff + 1e-15) { - ret.iHH_1 += (hapmap->geneticPosition(currLine-1)-hapmap->geneticPosition(currLine-2))*(lastProbs + stats.probs)*scale*0.5; - } - if (lastProbsNot > m_cutoff + 1e-15) { - ret.iHH_0 += (hapmap->geneticPosition(currLine-1)-hapmap->geneticPosition(currLine-2))*(lastProbsNot + stats.probsNot)*scale*0.5; - } - - lastProbs = stats.probs; - lastProbsNot = stats.probsNot; - if (ehhsave) - ret.downstream.push_back(std::move(stats)); - - if (m_maxExtend != 0 && currPhysPos - locusPysPos > m_maxExtend) - break; - if ((lastProbs <= m_cutoff + 1e-15 && lastProbsNot <= m_cutoff + 1e-15) || (m_single0count+m_single1count) == hapmap->snpLength()) - break; - - if (currLine == hapmap->numSnps()-1) - { - ++(*reachedEnd); - return EHH(); - } - } - return ret; + for (std::size_t i = 0; i < m_snpDataSizeA; ++i) + m_parent0[i] = ~m_hdA[focus*m_snpDataSizeA+i]; + m_parent0[m_snpDataSizeA-1] &= m_maskA; + for (std::size_t i = 0; i < m_snpDataSizeB; ++i) + m_parent0[i+m_snpDataSizeA] = ~m_hdB[focus*m_snpDataSizeB+i]; + m_parent0[m_snpDataSizeA+m_snpDataSizeB-1] &= m_maskB; + for (std::size_t i = 0; i < m_snpDataSizeA; ++i) + m_parent0[i+m_snpDataSizeA+m_snpDataSizeB] = m_hdA[focus*m_snpDataSizeA+i]; + for (std::size_t i = 0; i < m_snpDataSizeB; ++i) + m_parent0[i+2*m_snpDataSizeA+m_snpDataSizeB] = m_hdB[focus*m_snpDataSizeB+i]; } EHHFinder::~EHHFinder() { aligned_free(m_branch0); - aligned_free(m_branch1); aligned_free(m_parent0); - aligned_free(m_parent1); + if (m_branch1 != NULL) + aligned_free(m_branch1); + if (m_parent1 != NULL) + aligned_free(m_parent1); } diff --git a/src/ehhfinder.hpp b/src/ehhfinder.hpp index d44e33d..ad73fa3 100644 --- a/src/ehhfinder.hpp +++ b/src/ehhfinder.hpp @@ -27,18 +27,25 @@ class EHHFinder { public: explicit EHHFinder(std::size_t snpDataSizeA, std::size_t snpDataSizeB, std::size_t maxBreadth, double cutoff, double minMAF, double scale, unsigned long long maxExtend); + template EHH find(HapMap* hapmap, std::size_t focus, std::atomic* reachedEnd, std::atomic* outsideMaf, bool ehhsave = false); + template XPEHH findXPEHH(HapMap* hmA, HapMap *hmB, std::size_t focus, std::atomic* reachedEnd); ~EHHFinder(); protected: - inline void calcBranch(HapMap* hm, std::size_t focus, HapMap::PrimitiveType* parent, std::size_t parentcount, HapMap::PrimitiveType* branch, std::size_t& branchcount, std::size_t currLine, double freq, double& probs, std::size_t& singlecount, bool* overflow); - inline void calcBranchXPEHH(HapMap* hmA, HapMap* hmB, std::size_t currLine, std::size_t& single0, std::size_t& single1, bool* overflow); - inline void setInitial(std::size_t focus, std::size_t line); - inline void setInitialXPEHH(std::size_t focus); + template + inline void calcBranch(HapMap* hm, HapMap::PrimitiveType* parent, std::size_t parentcount, HapMap::PrimitiveType* branch, std::size_t& branchcount, std::size_t currLine, double freq, double& probs, std::size_t& singlecount, std::size_t maxBreadth, bool* overflow); + template + inline void calcBranchXPEHH(std::size_t currLine, std::size_t& singleA, std::size_t& singleB, std::size_t& singleP, bool* overflow); + void setInitial(std::size_t focus, std::size_t line); + void setInitialXPEHH(std::size_t focus); + template inline void calcBranches(HapMap* hapmap, std::size_t focus, std::size_t currLine, double freq0, double freq1, HapStats& stats); + template inline void calcBranchesXPEHH(std::size_t currLine); - std::size_t m_maxBreadth; + std::size_t m_maxBreadth0; + std::size_t m_maxBreadth1; std::size_t m_bufferSize; unsigned long long m_maxExtend; HapMap::PrimitiveType *m_parent0; @@ -57,7 +64,7 @@ class EHHFinder std::size_t m_branch1count; std::size_t m_single0count; std::size_t m_single1count; - std::size_t m_singlePooledCount; + std::size_t m_singlePcount; double m_freqA; double m_freqB; double m_freqP; @@ -74,4 +81,6 @@ class EHHFinder HapMap* m_hmB; }; +#include "ehhfinder-impl.hpp" + #endif // LLEHHFINDER_H diff --git a/src/hapbin.cpp b/src/hapbin.cpp index ebd513e..ad15513 100644 --- a/src/hapbin.cpp +++ b/src/hapbin.cpp @@ -40,6 +40,11 @@ void* aligned_alloc(size_t alignment, size_t size) } #endif +double binom_2(double n) +{ + return n*(n-1.0)*0.5; +} + double nearest(double target, double number) { if (target == 0) diff --git a/src/hapbin.hpp b/src/hapbin.hpp index 316df36..ac6d1fd 100644 --- a/src/hapbin.hpp +++ b/src/hapbin.hpp @@ -187,6 +187,7 @@ struct Stats double stddev; }; +double binom_2(double n); double nearest(double target, double number); Stats stats(const std::vector& list); std::vector splitString(const std::string input, char delim); diff --git a/src/ihsfinder-impl.hpp b/src/ihsfinder-impl.hpp new file mode 100644 index 0000000..3070126 --- /dev/null +++ b/src/ihsfinder-impl.hpp @@ -0,0 +1,62 @@ +/* + * Hapbin: A fast binary implementation EHH, iHS, and XPEHH + * Copyright (C) 2014 Colin MacLean + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +template +void IHSFinder::runXpehh(HapMap* mA, HapMap* mB, std::size_t start, std::size_t end) +{ + #pragma omp parallel shared(mA,mB,start,end) + { + EHHFinder finder(mA->snpDataSize(), mB->snpDataSize(), 2000, m_cutoff, m_minMAF, m_scale, m_maxExtend); + #pragma omp for schedule(dynamic,10) + for(size_t i = start; i < end; ++i) + { + XPEHH xpehh = finder.findXPEHH(mA, mB, i, &m_reachedEnd); + processXPEHH(xpehh, i); + ++m_counter; + unsigned long long tmp = m_counter; + if (tmp % 1000 == 0) + { + std::cout << '\r' << tmp << "/" << (end-start); + } + } + } + std::cout << std::endl; +} + +template +void IHSFinder::run(HapMap* map, std::size_t start, std::size_t end) +{ + #pragma omp parallel shared(map, start, end) + { + EHHFinder finder(map->snpDataSize(), 0, 2000, m_cutoff, m_minMAF, m_scale, m_maxExtend); + #pragma omp for schedule(dynamic,10) + for(size_t i = start; i < end; ++i) + { + EHH ehh = finder.find(map, i, &m_reachedEnd, &m_outsideMaf); + processEHH(ehh, i); + ++m_counter; + unsigned long long tmp = m_counter; + if (tmp % 1000 == 0) + { + std::cout << '\r' << tmp << "/" << (end-start); + } + } + } + std::cout << std::endl; +} diff --git a/src/ihsfinder.cpp b/src/ihsfinder.cpp index c35b560..a2f75e1 100644 --- a/src/ihsfinder.cpp +++ b/src/ihsfinder.cpp @@ -30,9 +30,9 @@ void IHSFinder::processEHH(const EHH& ehh, std::size_t line) double freqs = 0.0; double iHS = 0.0; - if (ehh.iHH_0 > 0) + if (ehh.iHH_1 > 0) { - iHS = log(ehh.iHH_1/ehh.iHH_0); + iHS = log(ehh.iHH_0/ehh.iHH_1); if (iHS == -std::numeric_limits::infinity() || iHS == std::numeric_limits::infinity()) { ++m_nanResults; @@ -45,7 +45,7 @@ void IHSFinder::processEHH(const EHH& ehh, std::size_t line) freqs = ((int) (m_bins*ehh.num/(double)m_snpLength))/(double)m_bins; - if (ehh.iHH_0 > 0) + if (ehh.iHH_1 > 0) { m_freqmutex.lock(); @@ -81,8 +81,6 @@ IHSFinder::LineMap IHSFinder::normalize() for (const auto& it : m_unStandIHSByLine) { double freq = m_freqsByLine[it.first]; - if (iHSStatsByFreq[freq].stddev == 0) - continue; m_standIHSSingle[it.first] = (it.second.iHS - iHSStatsByFreq[freq].mean)/iHSStatsByFreq[freq].stddev; } @@ -134,45 +132,3 @@ void IHSFinder::addXData(const IHSFinder::LineMap& freqsBySite, } m_freqmutex.unlock(); } - -void IHSFinder::runXpehh(HapMap* mA, HapMap* mB, std::size_t start, std::size_t end) -{ - #pragma omp parallel shared(mA,mB,start,end) - { - EHHFinder finder(mA->snpDataSize(), mB->snpDataSize(), 2000, m_cutoff, m_minMAF, m_scale, m_maxExtend); - #pragma omp for schedule(dynamic,10) - for(size_t i = start; i < end; ++i) - { - XPEHH xpehh = finder.findXPEHH(mA, mB, i, &m_reachedEnd); - processXPEHH(xpehh, i); - ++m_counter; - unsigned long long tmp = m_counter; - if (tmp % 1000 == 0) - { - std::cout << '\r' << tmp << "/" << (end-start); - } - } - } - std::cout << std::endl; -} - -void IHSFinder::run(HapMap* map, std::size_t start, std::size_t end) -{ - #pragma omp parallel shared(map, start, end) - { - EHHFinder finder(map->snpDataSize(), map->snpDataSize(), 2000, m_cutoff, m_minMAF, m_scale, m_maxExtend); - #pragma omp for schedule(dynamic,10) - for(size_t i = start; i < end; ++i) - { - EHH ehh = finder.find(map, i, &m_reachedEnd, &m_outsideMaf); - processEHH(ehh, i); - ++m_counter; - unsigned long long tmp = m_counter; - if (tmp % 1000 == 0) - { - std::cout << '\r' << tmp << "/" << (end-start); - } - } - } - std::cout << std::endl; -} diff --git a/src/ihsfinder.hpp b/src/ihsfinder.hpp index e402067..003bd02 100644 --- a/src/ihsfinder.hpp +++ b/src/ihsfinder.hpp @@ -50,7 +50,9 @@ class IHSFinder unsigned long long numOutsideMaf() const { return m_outsideMaf; } unsigned long long numNanResults() const { return m_nanResults; } + template void run(HapMap* map, std::size_t start, std::size_t end); + template void runXpehh(HapMap* mA, HapMap* mB, std::size_t start, std::size_t end); LineMap normalize(); @@ -82,5 +84,6 @@ class IHSFinder std::atomic m_nanResults; }; +#include "ihsfinder-impl.hpp" #endif // IHSFINDER_H diff --git a/src/main-ehh.cpp b/src/main-ehh.cpp index 501a51b..7b499b4 100644 --- a/src/main-ehh.cpp +++ b/src/main-ehh.cpp @@ -35,9 +35,10 @@ int main(int argc, char** argv) Argument cutoff('c', "cutoff", "EHH cutoff value (default: 0.05)", false, false, 0.05); Argument minMAF('b', "minmaf", "Minimum allele frequency (default: 0.05)", false, false, 0.05); Argument scale('s', "scale", "Gap scale parameter in bp, used to scale gaps > scale parameter as in Voight, et al.", false, false, 20000); + Argument binom('a', "binom", "Use binomial coefficients rather than frequency squared for EHH", true, false); Argument maxExtend('e', "max-extend", "Maximum distance in bp to traverse when calculating EHH (default: 0 (disabled))", false, false, 0); Argument locus('l', "locus", "Locus", false, false, 0); - ArgParse argparse({&help, &version, &hap, &map, &locus, &cutoff, &minMAF, &scale, &maxExtend}, "Usage: ehhbin --map input.map --hap input.hap --locus id"); + ArgParse argparse({&help, &version, &hap, &map, &locus, &cutoff, &minMAF, &scale, &maxExtend, &binom}, "Usage: ehhbin --map input.map --hap input.hap --locus id"); if (!argparse.parseArguments(argc, argv)) { return 3; @@ -73,8 +74,11 @@ int main(int argc, char** argv) } std::atomic reachedEnd{}; std::atomic outsideMaf{}; - EHHFinder finder(hmap.snpDataSize(), hmap.snpDataSize(), 1000, maxExtend.value(), cutoff.value(), minMAF.value(), (double) scale.value()); - e = finder.find(&hmap, l, &reachedEnd, &outsideMaf, true); + EHHFinder finder(hmap.snpDataSize(), 0, 1000, cutoff.value(), minMAF.value(), (double) scale.value(), maxExtend.value()); + if (binom.value()) + e = finder.find(&hmap, l, &reachedEnd, &outsideMaf, true); + else + e = finder.find(&hmap, l, &reachedEnd, &outsideMaf, true); e.printEHH(&hmap); std::cout << "iHS: " << log(e.iHH_0/e.iHH_1) << std::endl; std::cout << "MAF: " << (double)e.num/(double)hmap.snpLength() << std::endl; diff --git a/src/main-ihs.cpp b/src/main-ihs.cpp index 7a704e7..7d4c342 100644 --- a/src/main-ihs.cpp +++ b/src/main-ihs.cpp @@ -45,9 +45,10 @@ int main(int argc, char** argv) Argument minMAF('f', "minmaf", "Minimum allele frequency (default: 0.05)", false, false, 0.05); Argument binfac('b', "bin", "Number of frequency bins for iHS normalization (default: 50)", false, false, 50); Argument scale('s', "scale", "Gap scale parameter in bp, used to scale gaps > scale parameter as in Voight, et al.", false, false, 20000); + Argument binom('a', "binom", "Use binomial coefficients rather than frequency squared for EHH", true, false); Argument maxExtend('e', "max-extend", "Maximum distance in bp to traverse when calculating EHH (default: 0 (disabled))", false, false, 0); Argument outfile('o', "out", "Output file", false, false, "out.txt"); - ArgParse argparse({&help, &version, &hap, &map, &outfile, &cutoff, &minMAF, &scale, &maxExtend, &binfac}, "Usage: ihsbin --map input.map --hap input.hap [--ascii] [--out outfile]"); + ArgParse argparse({&help, &version, &hap, &map, &outfile, &cutoff, &minMAF, &scale, &binfac, &maxExtend, &binom}, "Usage: ihsbin --map input.map --hap input.hap [--ascii] [--out outfile]"); if (!argparse.parseArguments(argc, argv)) { ret = 1; @@ -73,7 +74,7 @@ int main(int argc, char** argv) numSnps = HapMap::querySnpLength(hap.value().c_str()); std::cout << "Chromosomes per SNP: " << numSnps << std::endl; - calcIhs(hap.value(), map.value(), outfile.value(), cutoff.value(), minMAF.value(), (double) scale.value(), maxExtend.value(), binfac.value()); + calcIhs(hap.value(), map.value(), outfile.value(), cutoff.value(), minMAF.value(), (double) scale.value(), maxExtend.value(), binfac.value(), binom.value()); out: #if MPI_FOUND diff --git a/src/main-xpehh.cpp b/src/main-xpehh.cpp index aeaeb8b..2b954a0 100644 --- a/src/main-xpehh.cpp +++ b/src/main-xpehh.cpp @@ -47,9 +47,10 @@ int main(int argc, char** argv) Argument minMAF('f', "minmaf", "Minimum allele frequency (default: 0.05)", false, false, 0.00); Argument binfac('b', "bin", "Number of frequency bins for iHS normalization (default: 50)", false, false, 50); Argument scale('s', "scale", "Gap scale parameter in bp, used to scale gaps > scale parameter as in Voight, et al.", false, false, 20000); + Argument binom('a', "binom", "Use binomial coefficients rather than frequency squared for EHH", true, false); Argument maxExtend('e', "max-extend", "Maximum distance in bp to traverse when calculating EHH (default: 0 (disabled))", false, false, 0); Argument outfile('o', "out", "Output file", false, false, "out.txt"); - ArgParse argparse({&help, &version, &hapA, &hapB, &map, &outfile, &cutoff, &minMAF, &maxExtend, &scale, &binfac}, "Usage: xpehhbin --map input.map --hapA inputA.hap --hapB inputB.hap"); + ArgParse argparse({&help, &version, &hapA, &hapB, &map, &outfile, &cutoff, &minMAF, &scale, &binfac, &binom, &maxExtend}, "Usage: xpehhbin --map input.map --hapA inputA.hap --hapB inputB.hap"); if (!argparse.parseArguments(argc, argv)) { ret = 1; @@ -77,7 +78,7 @@ int main(int argc, char** argv) numSnps = HapMap::querySnpLength(hapB.value().c_str()); std::cout << "Haplotypes in population B: " << numSnps << std::endl; - calcXpehh(hapA.value(), hapB.value(), map.value(), outfile.value(), cutoff.value(), minMAF.value(), (double) scale.value(), maxExtend.value(), binfac.value()); + calcXpehh(hapA.value(), hapB.value(), map.value(), outfile.value(), cutoff.value(), minMAF.value(), (double) scale.value(), maxExtend.value(), binfac.value(), binom.value()); out: #if MPI_FOUND