• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DEploid-dev / DEploid / 11416527288

19 Oct 2024 09:41AM UTC coverage: 87.329% (+87.3%) from 0.0%
11416527288

push

github

web-flow
Merge pull request #364 from DEploid-dev/363_fix_cran_init_issue

363 fix cran init issue

4 of 8 new or added lines in 3 files covered. (50.0%)

331 existing lines in 16 files now uncovered.

5803 of 6645 relevant lines covered (87.33%)

5136007.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.15
/src/dEploidIO-workflow.cpp
1
/*
2
 * dEploid is used for deconvoluting Plasmodium falciparum genome from
3
 * mix-infected patient sample.
4
 *
5
 * Copyright (C) 2016-2017 University of Oxford
6
 *
7
 * Author: Sha (Joe) Zhu
8
 *
9
 * This file is part of dEploid.
10
 *
11
 * dEploid is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25

26

27
#include <iostream>  // std::cout
28
#include "mcmc.hpp"
29
#include "dEploidIO.hpp"
30

31
void DEploidIO::workflow_lasso() {
1✔
32
    this->dEploidLasso();
1✔
33
    MersenneTwister lassoRg(this->randomSeed_.getValue());
1✔
34
    DEploidIO tmpIO(*this);
2✔
35
    vector < vector <double> > hap;
1✔
36
    for (size_t chromi = 0;
14✔
37
         chromi < this->indexOfChromStarts_.size();
15✔
38
         chromi++ ) {
39
        tmpIO.position_.clear();
40
        tmpIO.position_.push_back(this->position_.at(chromi));
14✔
41
        tmpIO.indexOfChromStarts_.clear();
42
        tmpIO.indexOfChromStarts_.push_back(0);
14✔
43
        string job = string("DEploid-Lasso learning chromosome ");
14✔
44
        job.append(this->chrom_[chromi]).append(" haplotypes");
14✔
45
        McmcSample * lassoMcmcSample = new McmcSample();
14✔
46
        McmcMachinery lassoMcmcMachinery(
47
                                    &this->lassoPlafs.at(chromi),
48
                                    &this->lassoRefCount.at(chromi),
49
                                    &this->lassoAltCount.at(chromi),
50
                                    this->lassoPanels.at(chromi),
51
                                    &tmpIO,
52
                                    job,
53
                                    "lasso",
54
                                    lassoMcmcSample,
55
                                    &lassoRg,
56
                                    false);
70✔
57
        lassoMcmcMachinery.runMcmcChain(true,   // show progress
14✔
58
                                        false);  // use IBD
59
        for (size_t snpi = 0;
567✔
60
             snpi < lassoMcmcSample->hap.size(); snpi++) {
581✔
61
             hap.push_back(vector <double> (
1,134✔
62
                                lassoMcmcSample->hap[snpi].begin(),
63
                                lassoMcmcSample->hap[snpi].end()));
64
        }
65
        delete lassoMcmcSample;
14✔
66
    }
67
    this->writeHap(hap, "lasso");
2✔
68
}
1✔
69

70

71
void DEploidIO::workflow_ibd() {
2✔
72
    if (this->useIBD()) {  // ibd
2✔
73
        McmcSample * ibdMcmcSample = new McmcSample();
1✔
74
        MersenneTwister ibdRg(this->randomSeed_.getValue());
1✔
75
        McmcMachinery ibdMcmcMachinery(&this->plaf_,
76
                                       &this->refCount_,
77
                                       &this->altCount_,
78
                                       this->panel,
79
                                       this,
80
                                       "DEploid-IBD",
81
                                       "ibd",
82
                                       ibdMcmcSample,
83
                                       &ibdRg,
84
                                       true);
3✔
85
        ibdMcmcMachinery.runMcmcChain(true,   // show progress
1✔
86
                                      true);  // use IBD
87
        delete ibdMcmcSample;
1✔
88
    }
89

90
    McmcSample * mcmcSample = new McmcSample();
2✔
91
    MersenneTwister rg(this->randomSeed_.getValue());
2✔
92
    McmcMachinery mcmcMachinery(&this->plaf_,
93
                        &this->refCount_,
94
                        &this->altCount_,
95
                        this->panel,
96
                        this,
97
                        "DEploid classic version",
98
                        "classic",  // brief
99
                        mcmcSample,
100
                        &rg,
101
                        false);  // use IBD
6✔
102
    mcmcMachinery.runMcmcChain(true,     // show progress
2✔
103
                       false);   // use IBD
104
    this->operation_paintIBD();
2✔
105
    this->writeHap(mcmcSample->hap, "final");
2✔
106
    delete mcmcSample;
2✔
107
}
2✔
108

109

110
void DEploidIO::workflow_best() {
1✔
111
    MersenneTwister rg(this->randomSeed_.getValue());
1✔
112

113
    // #############################################################
114
    // ################# DEploid-LASSO to learn K ##################
115
    // #############################################################
116

117
    vector < vector <double> > chooseKhap;
1✔
118

119
    DEploidIO toLearnK(*this);
2✔
120
    toLearnK.dEploidLassoTrimfirst();
1✔
121
    DEploidIO toLearnKtmp(*this);
2✔
122
    for (size_t chromi = 0;
14✔
123
         chromi < toLearnK.indexOfChromStarts_.size();
15✔
124
         chromi++ ) {
125
        bool tryAgain = true;
126
        while (tryAgain) {
36✔
127
          toLearnKtmp.position_.clear();
128
          toLearnKtmp.position_.push_back(toLearnK.position_.at(chromi));
22✔
129
          toLearnKtmp.indexOfChromStarts_.clear();
130
          toLearnKtmp.indexOfChromStarts_.push_back(0);
22✔
131

132
          McmcSample * lassoMcmcSample = new McmcSample();
22✔
133
          string job = string("DEploid-Lasso learning K ");
22✔
134
          job.append(toLearnK.chrom_[chromi]).append(" leanrning K");
22✔
135
          McmcMachinery lassoMcmcMachinery(
136
                                  &toLearnK.lassoPlafs.at(chromi),
137
                                  &toLearnK.lassoRefCount.at(chromi),
138
                                  &toLearnK.lassoAltCount.at(chromi),
139
                                  toLearnK.lassoPanels.at(chromi),
140
                                  &toLearnKtmp,
141
                                  job,
142
                                  job,
143
                                  lassoMcmcSample,
144
                                  &rg,
145
                                  false);
88✔
146
          lassoMcmcMachinery.runMcmcChain(true,   // show progress
22✔
147
                                          false,  // use IBD
148
                                          true,   // notInR
149
                                          false);  // averageP
150
          toLearnKtmp.initialProp = toLearnKtmp.finalProp;
22✔
151
          toLearnKtmp.setInitialPropWasGiven(true);
152
          if (toLearnKtmp.acceptRatio() != 0) {
22✔
153
            this->chooseK.appendProportions(toLearnKtmp.finalProp);
14✔
154
            tryAgain = false;
155
          } else {
156
            toLearnKtmp.setInitialPropWasGiven(false);
157
            tryAgain = true;
158
          }
159

160
          if (!tryAgain) {
161
            for (size_t snpi = 0;
320✔
162
                snpi < lassoMcmcSample->hap.size(); snpi++) {
334✔
163
                chooseKhap.push_back(vector <double> (
640✔
164
                                    lassoMcmcSample->hap[snpi].begin(),
165
                                    lassoMcmcSample->hap[snpi].end()));
166
            }
167
          }
168
          delete lassoMcmcSample;
22✔
169
        }
170
    }
171
    // Gathering haplotype information
172
    toLearnK.writeHap(chooseKhap, "chooseK");
1✔
173

174
    // Gathering proportion information
175
    this->initialProp.clear();
1✔
176
    vector <double> initialP = this->chooseK.chosenP();
1✔
177
    for (auto const& value : initialP) {
5✔
178
        if (value > 0.01) {
4✔
179
            this->initialProp.push_back(value);
2✔
180
        }
181
    }
182
    normalizeBySum(this->initialProp);
1✔
183
    this->kStrain_.init(this->initialProp.size());
1✔
184
    this->setInitialPropWasGiven(true);
185

186
    if (this->inferBestPracticeP()) {
1✔
187
      if (this->initialProp.size() > 1) {
1✔
188
        // #############################################################
189
        // ###################### DEploid-IBD   ########################
190
        // #############################################################
191
        McmcSample * ibdMcmcSample = new McmcSample();
1✔
192
        DEploidIO tmpIO2(*this);
2✔
193
        tmpIO2.ibdTrimming();
1✔
194
        McmcMachinery ibdMcmcMachinery(&tmpIO2.plaf_,
195
                              &tmpIO2.refCount_,
196
                              &tmpIO2.altCount_,
197
                              tmpIO2.panel,
198
                              &tmpIO2,
199
                              string("DEploid-IBD learning proportion"),
2✔
200
                              string("ibd"),
1✔
201
                              ibdMcmcSample,
202
                              &rg,
203
                              true);
2✔
204
        ibdMcmcMachinery.runMcmcChain(true,   // show progress
1✔
205
                                      true);  // use IBD
206
        // if (this->useIbdOnly()) {
207
            // tmpIO.paintIBD();
208
            // this->finalProp = tmpIO.initialProp;
209
        // }
210

211
        tmpIO2.operation_paintIBD();
1✔
212

213
        vector <double> initialP;
214
        for (auto const& value : tmpIO2.finalProp) {
3✔
215
            if (value > 0.01) {
2✔
216
                initialP.push_back(value);
2✔
217
            }
218
        }
219
        cout << endl;
220
        this->initialProp = initialP;
1✔
221
        this->finalProp = initialP;
1✔
222
        this->kStrain_.init(initialP.size());
1✔
223
        this->setInitialPropWasGiven(true);
224
        this->setDoUpdateProp(false);
225
        delete ibdMcmcSample;
1✔
226
      } else {
UNCOV
227
        this->finalProp.clear();
×
UNCOV
228
        this->finalProp.push_back(1.0);
×
229
        this->kStrain_.init(1);
230
      }
231
    }
232

233
    if (this->inferBestPracticeHap()) {
1✔
234
    // #################################################################
235
    // ###################### DEploid-LASSO ############################
236
    // #################################################################
237
    // *** Frist split the reference panel etc
238
      this->dEploidLasso();
1✔
239

240
      DEploidIO dEploidLassoIO(*this);
2✔
241
      dEploidLassoIO.initialProp = this->initialProp;
1✔
242
      dEploidLassoIO.setDoUpdateProp(false);
243
      dEploidLassoIO.setInitialPropWasGiven(true);
244
      dEploidLassoIO.kStrain_.init(this->kStrain_.getValue());
245
      vector < vector <double> > hap;
1✔
246
      for (size_t chromi = 0;
14✔
247
           chromi < this->indexOfChromStarts_.size();
15✔
248
           chromi++ ) {
249
          dEploidLassoIO.position_.clear();
250
          dEploidLassoIO.position_.push_back(
14✔
251
                                      this->position_.at(chromi));
252
          dEploidLassoIO.indexOfChromStarts_.clear();
253
          dEploidLassoIO.indexOfChromStarts_.push_back(0);
14✔
254

255
          McmcSample * lassoMcmcSample = new McmcSample();
14✔
256
          string job = string("DEploid-Lasso learning chromosome ");
14✔
257
          job.append(this->chrom_[chromi]).append(" haplotypes");
14✔
258
          McmcMachinery lassoMcmcMachinery(
259
                                    &this->lassoPlafs.at(chromi),
260
                                    &this->lassoRefCount.at(chromi),
261
                                    &this->lassoAltCount.at(chromi),
262
                                    this->lassoPanels.at(chromi),
263
                                    &dEploidLassoIO,
264
                                    job,
265
                                    job,
266
                                    lassoMcmcSample,
267
                                    &rg,
268
                                    false);
56✔
269
          lassoMcmcMachinery.runMcmcChain(true,   // show progress
14✔
270
                                          false);  // use IBD
271
          // *** Append haplotypes to the end
272
          for (size_t snpi = 0;
567✔
273
               snpi < lassoMcmcSample->hap.size(); snpi++) {
581✔
274
              hap.push_back(vector <double> (
1,134✔
275
                                  lassoMcmcSample->hap[snpi].begin(),
276
                                  lassoMcmcSample->hap[snpi].end()));
277
          }
278
          delete lassoMcmcSample;
14✔
279
      }
280
      this->writeHap(hap, "final");
1✔
281
      this->writeVcf(hap, dEploidLassoIO.initialProp, "final");
2✔
282
    }
283

284
    if (this->inferBestPracticeP() & (this->initialProp.size() > 1)) {
1✔
285
        this->operation_paintIBD();
1✔
286
    }
287
}
1✔
288

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc