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

DEploid-dev / DEploid / 062add94-a5b5-4fd4-a562-e213ecc8da7c

20 Jan 2025 05:00PM UTC coverage: 86.049% (-0.1%) from 86.156%
062add94-a5b5-4fd4-a562-e213ecc8da7c

push

circleci

web-flow
Merge pull request #377 from DEploid-dev/shajoezhu-patch-1

Update config.yml

5545 of 6444 relevant lines covered (86.05%)

5298085.9 hits per line

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

77.46
/src/dEploidIO.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
#include <ctime>
26
#include <iterator>
27
#include <cassert>        // assert
28
#include "utility.hpp"    // normailize by sum
29
#include "dEploidIO.hpp"
30
#include "ibd.hpp"
31
#include "lasso/src/dEploidLasso.hpp"
32

33
DEploidIO::DEploidIO() {
31✔
34
    this->init();
31✔
35
}
31✔
36

37

38
DEploidIO::DEploidIO(const std::string &arg) {
1✔
39
    this->init();
1✔
40
    std::istringstream iss(arg);
1✔
41
    copy(std::istream_iterator<std::string>(iss),
2✔
42
         std::istream_iterator<std::string>(),
1✔
43
         std::back_inserter(argv_));
44
    this->argv_i = argv_.begin();
1✔
45
    this->core();
1✔
46
}
1✔
47

48

49
DEploidIO::DEploidIO(int argc, char *argv[]) {
127✔
50
    this->init();
127✔
51
    argv_ = std::vector<std::string>(argv + 1, argv + argc);
127✔
52
    this->argv_i = argv_.begin();
127✔
53
    this->core();
127✔
54
}
967✔
55

56

57
DEploidIO::~DEploidIO() {
80✔
58
    if (this->isCopied()){
80✔
59
        return;
3✔
60
    }
61
    if ( this->excludedMarkers != NULL ) {
77✔
62
        delete this->excludedMarkers;
8✔
63
    }
64
    if ( this->vcfReaderPtr_ != NULL ) {
77✔
65
        delete this->vcfReaderPtr_;
10✔
66
    }
67
    if ( this->panel != NULL ) {
77✔
68
        delete panel;
33✔
69
    }
70
}
560✔
71

72

73
void DEploidIO::core() {
128✔
74
    if ( argv_.size() == 0 ) {
128✔
75
        this->set_help(true);
76
        return;
2✔
77
    }
78

79
    this->reInit(); // Reset to defalt values before parsing
126✔
80
    this->parse();
126✔
81

82
    if ( this->help() || version() ) {
83✔
83
        return;
84
    }
85

86
    this->checkInput();
74✔
87
    this->finalize();
65✔
88
}
89

90

91
void DEploidIO::init() {
285✔
92
    this->setDoPrintLassoPanel(false);
93
    this->setIsCopied(false);
94
    this->setDoExportRecombProb(false);
95

96
    this->setCompressVcf(false);
97
    this->setInitialPropWasGiven(false);
98
    this->setInitialHapWasGiven(false);
99
    this->initialProp.clear();
100
    this->setPleaseCheckInitialP(true);
101
    this->setExcludeSites( false );
102
    this->excludedMarkers = NULL;
285✔
103
    this->panel = NULL;
285✔
104
    this->set_help(false);
105
    this->setVersion(false);
106
    this->setUsePanel(true);
107
    this->prefix_ = "pf3k-dEploid";
285✔
108
    this->setKStrainWasSetByHap(false);
109
    this->setKStrainWasSetByProp(false);
110

111
    this->setDoUpdateProp( true );
112
    this->setDoUpdatePair( true );
113
    this->setDoUpdateSingle( true );
114
    this->setDoExportPostProb( false );
115
    this->setDoLsPainting( false );
116
    this->setDoIbdPainting( false );
117
    this->setDoIbdViterbiPainting(false);
118
    this->setUseIBD( false );
119
    this->setUseIbdOnly(false);
120
    this->setUseLasso( false );
121
    this->setUseBestPractice(false);
122
    this->setInferBestPracticeP(true);
123
    this->setInferBestPracticeHap(true);
124
    this->setDoExportSwitchMissCopy ( true );
125
    this->setDoAllowInbreeding( false );
126
    this->useConstRecomb_ = false;
285✔
127
    this->setForbidCopyFromSame( false );
128
    this->constRecombProb_ = 1.0;
285✔
129
    this->averageCentimorganDistance_ = 15000.0;
285✔
130
    this->setScalingFactor(100.0);
131
    this->setParameterG(20.0);
132
    this->setIBDSigma(20.0);
133
    this->setUseVcf(false);
134
    this->setUseVcfSample(false);
135
    this->setExtractPlafFromVcf(false);
136
    this->vcfSampleName_ = "";
285✔
137
    this->vcfReaderPtr_ = NULL;
285✔
138
    this->setDoExportVcf(false);
139
    this->setDoComputeLLK(false);
140
    this->setVqslod(8.0);
141
    this->setLassoMaxNumPanel(100);
142

143
    this->kStrain_.init(5);  // From DEploid-Lasso, set default K to 4.
144
    this->mcmcBurn_.init(0.5);
145
    this->mcmcMachineryRate_.init(5);
146
    this->missCopyProb_.init(0.01);
147
    this->nMcmcSample_.init(800);
148
    this->precision_.init(8);
149
    this->randomSeed_.init((unsigned)0);
150
    this->parameterSigma_.init(5.0);
151

152

153
    #ifdef COMPILEDATE
154
        compileTime_ = COMPILEDATE;
285✔
155
    #else
156
        compileTime_ = "";
157
    #endif
158

159
    #ifdef DEPLOIDVERSION
160
        dEploidGitVersion_ = DEPLOIDVERSION;
285✔
161
    #else
162
        dEploidGitVersion_ = "";
163
    #endif
164

165
    #ifdef DEPLOIDVERSION
166
        lassoGitVersion_ = LASSOVERSION;
285✔
167
    #else
168
        lassoGitVersion_ = "";
169
    #endif
170

171

172
}
285✔
173

174

175
void DEploidIO::setBestPracticeParameters() {
1✔
176
    this->kStrain_.setBest(4);
177
    this->nMcmcSample_.setBest(500);
178
    this->randomSeed_.setBest(1);
179
    this->parameterSigma_.setBest(1.6);
180
    this->mcmcBurn_.setBest(.67);
181
    this->mcmcMachineryRate_.setBest(8);
182
}
3✔
183

184
void DEploidIO::getTime( bool isStartingTime ) {
126✔
185
    time_t now = time(0);
126✔
186
    // convert now to string form
187
    char* dt = ctime(&now);
126✔
188
    if ( isStartingTime ) {
126✔
189
        startingTime_ = string(dt);
252✔
190
    } else {
191
        endTime_ = string(dt);
×
192
    }
193
}
126✔
194

195

196
void DEploidIO::reInit() {
126✔
197
    this->init();
126✔
198
    this->refFileName_.clear();
199
    this->altFileName_.clear();
200
    this->plafFileName_.clear();
201
    this->panelFileName_.clear();
202
    this->excludeFileName_.clear();
203
    this->getTime(true);
126✔
204
}
126✔
205

206

207
void DEploidIO::finalize() {
65✔
208
    if (useBestPractice()){
65✔
209
        this->setBestPracticeParameters();
210
    }
211

212
    if ( this->doIbdPainting() || this->doComputeLLK() || this->doIbdViterbiPainting() ) {
65✔
213
        if (!initialPropWasGiven()) {
2✔
214
            throw InitialPropUngiven("");
×
215
        }
216
    }
217

218
    if ( this->useIBD() && this->kStrain_.getValue() == 1) {
68✔
219
        throw InvalidK();
2✔
220
    }
221

222
    if ( this->compressVcf() && !this->doExportVcf() ) {
63✔
223
        throw VcfOutUnSpecified("");
2✔
224
    }
225

226
    if ( !this->randomSeed_.useDefault() ) {
62✔
227
        this->randomSeed_.init((unsigned)(time(0)));
×
228
    }
229

230
    if ( this->excludeSites() ) {
62✔
231
        excludedMarkers = new ExcludeMarker();
16✔
232
        excludedMarkers->readFromFile(excludeFileName_.c_str());
8✔
233
    }
234

235
    if ( useVcf() ) { // read vcf files, and parse it to refCount and altCount
62✔
236
        this->vcfReaderPtr_ = new VcfReader (vcfFileName_, vcfSampleName_,
45✔
237
            extractPlafFromVcf_);
35✔
238
        if ( this->excludeSites() ) {
10✔
239
            this->vcfReaderPtr_->findAndKeepMarkers (excludedMarkers);
5✔
240
        }
241
        this->vcfReaderPtr_->finalize(); // Finalize after remove variantlines
10✔
242
        this->refCount_ = this->vcfReaderPtr_->refCount;
10✔
243
        this->altCount_ = this->vcfReaderPtr_->altCount;
10✔
244
    } else {
245
        TxtReader ref;
27✔
246
        ref.readFromFile(refFileName_.c_str());
247
        if ( this->excludeSites() ) {
25✔
248
            ref.findAndKeepMarkers( excludedMarkers );
3✔
249
        }
250
        this->refCount_ = ref.info_;
25✔
251

252
        TxtReader alt;
25✔
253
        alt.readFromFile(altFileName_.c_str());
254
        if ( this->excludeSites() ) {
25✔
255
            alt.findAndKeepMarkers( excludedMarkers );
3✔
256
        }
257
        this->altCount_ = alt.info_;
25✔
258
    }
27✔
259

260
    this->nLoci_ = refCount_.size();
35✔
261

262
    if ( this->nLoci_ != this->altCount_.size() ) {
35✔
263
        throw LociNumberUnequal( this->altFileName_ );
3✔
264
    }
265

266
    if (extractPlafFromVcf()){
34✔
267
        this->plaf_ = this->vcfReaderPtr_->plaf;
4✔
268
        this->chrom_ = this->vcfReaderPtr_->chrom_;
4✔
269
        this->position_ = this->vcfReaderPtr_->position_;
4✔
270
        this->indexOfChromStarts_ = this->vcfReaderPtr_->indexOfChromStarts_;
4✔
271
    } else {
272
        TxtReader plaf;
30✔
273
        plaf.readFromFile(plafFileName_.c_str());
274
        if ( this->excludeSites() ) {
30✔
275
            plaf.findAndKeepMarkers( excludedMarkers );
4✔
276
        }
277
        this->plaf_ = plaf.info_;
30✔
278
        this->chrom_ = plaf.chrom_;
30✔
279
        this->position_ = plaf.position_;
30✔
280
        this->indexOfChromStarts_ = plaf.indexOfChromStarts_;
30✔
281
        if ( this->nLoci_ != this->plaf_.size() ) {
30✔
282
            throw LociNumberUnequal( this->plafFileName_ );
3✔
283
        }
284
    }
30✔
285

286
    (void)removeFilesWithSameName();
33✔
287

288
    this->readPanel();
33✔
289
    IBDpathChangeAt = vector <double>(this->nLoci());
66✔
290
    finalIBDpathChangeAt = vector <double>(this->nLoci());
66✔
291

292
    siteOfTwoSwitchOne = vector <double>(this->nLoci());
66✔
293
    siteOfTwoMissCopyOne = vector <double>(this->nLoci());
66✔
294
    siteOfTwoSwitchTwo = vector <double>(this->nLoci());
66✔
295
    siteOfTwoMissCopyTwo = vector <double>(this->nLoci());
66✔
296
    siteOfOneSwitchOne = vector <double>(this->nLoci());
66✔
297
    siteOfOneMissCopyOne = vector <double>(this->nLoci());
66✔
298

299
    finalSiteOfTwoSwitchOne = vector <double>(this->nLoci());
66✔
300
    finalSiteOfTwoMissCopyOne = vector <double>(this->nLoci());
66✔
301
    finalSiteOfTwoSwitchTwo = vector <double>(this->nLoci());
66✔
302
    finalSiteOfTwoMissCopyTwo = vector <double>(this->nLoci());
66✔
303
    finalSiteOfOneSwitchOne = vector <double>(this->nLoci());
66✔
304
    finalSiteOfOneMissCopyOne = vector <double>(this->nLoci());
33✔
305
}
33✔
306

307

308
void DEploidIO::removeFilesWithSameName() {
33✔
309
    //strExportProp = this->prefix_ + ".prop";
310
    //strExportLLK = this->prefix_ + ".llk";
311
    //strExportHap = this->prefix_ + ".hap";
312

313
    //if (this->useIbdOnly()) {
314
        //strIbdExportProp = this->prefix_ + ".prop";
315
        //strIbdExportLLK = this->prefix_ + ".llk";
316
        //strIbdExportHap = this->prefix_ + ".hap";
317
    //} else {
318
        //strIbdExportProp = this->prefix_ + ".ibd.prop";
319
        //strIbdExportLLK = this->prefix_ + ".ibd.llk";
320
        //strIbdExportHap = this->prefix_ + ".ibd.hap";
321
        strIbdExportProbs = this->prefix_ + ".ibd.probs";
33✔
322
        strIbdExportViterbi = this->prefix_ + ".viterbi";
66✔
323
    //}
324

325

326
    strExportLog =  this->prefix_ + ((this->doLsPainting()) ? ".painting":"") + ".log";
96✔
327
    strExportRecombProb = this->prefix_ + ".recomb";
33✔
328

329
    strExportExtra = this->prefix_ + ".extra";
66✔
330

331
    if ( this->doLsPainting() == false ) {
33✔
332
        if (this->useIBD()) {
333
            //remove(strIbdExportProp.c_str());
334
            //remove(strIbdExportLLK.c_str());
335
            //remove(strIbdExportHap.c_str());
336
        }
337
        //remove(strExportLLK.c_str());
338
        //remove(strExportHap.c_str());
339
        //remove(strExportVcf.c_str());
340
        //remove(strExportProp.c_str());
341
        remove(strExportExtra.c_str());
30✔
342
        remove(strIbdExportProbs.c_str());
30✔
343
        remove(strIbdExportViterbi.c_str());
30✔
344
    }
345

346
    if (this->doLsPainting() || this->doExportPostProb() ) {
33✔
347
        if (this->useIBD()) {
3✔
348
            strIbdExportSingleFwdProbPrefix = this->prefix_ + ".ibd.single";
×
349
            for ( size_t i = 0; i < this->kStrain_.getValue() ; i++ ) {
×
350
                string tmpStrExportSingleFwdProb = strIbdExportSingleFwdProbPrefix + to_string(i);
×
351
                remove(tmpStrExportSingleFwdProb.c_str());
×
352
            }
353
            strIbdExportPairFwdProb = this->prefix_ + ".ibd.pair";
×
354
            remove(strIbdExportPairFwdProb.c_str());
×
355
        }
356
        strExportSingleFwdProbPrefix = this->prefix_ + ".single";
3✔
357
        for ( size_t i = 0; i < this->kStrain_.getValue() ; i++ ) {
36✔
358
            string tmpStrExportSingleFwdProb = strExportSingleFwdProbPrefix + to_string(i);
30✔
359
            remove(tmpStrExportSingleFwdProb.c_str());
15✔
360
        }
361
        strExportPairFwdProb = this->prefix_ + ".pair";
6✔
362
        remove(strExportPairFwdProb.c_str());
3✔
363
    }
364
    remove(strExportLog.c_str());
33✔
365
    remove(strExportRecombProb.c_str());
33✔
366

367
}
33✔
368

369

370
void DEploidIO::parse () {
126✔
371
    do {
372
        if (*argv_i == "-ref") {
549✔
373
            if ( this->useVcf() ) {
73✔
374
                throw ( FlagsConflict((*argv_i) , "-vcf") );
3✔
375
            }
376
            this->readNextStringto ( this->refFileName_ ) ;
72✔
377
        } else if (*argv_i == "-alt") {
476✔
378
            if ( this->useVcf() ) {
73✔
379
                throw ( FlagsConflict((*argv_i) , "-vcf") );
3✔
380
            }
381
            this->readNextStringto ( this->altFileName_ ) ;
72✔
382
        } else if (*argv_i == "-vcf") {
403✔
383
            if ( this->refFileName_.size() > 0 || this->altFileName_.size() > 0 ) {
42✔
384
                throw ( FlagsConflict((*argv_i) , "-ref or -alt") );
3✔
385
            }
386
            this->setUseVcf(true);
387
            this->readNextStringto ( this->vcfFileName_ ) ;
41✔
388
        } else if (*argv_i == "-sample") {
361✔
389
            if (useVcf() == false){
4✔
390
                throw(FlagsOrderIncorrect((*argv_i) , "-vcf") );
×
391
            }
392
            this->setUseVcfSample(true);
393
            this->readNextStringto ( this->vcfSampleName_ ) ;
4✔
394
        } else if (*argv_i == "-plafFromVcf") {
357✔
395
            if (useVcf() == false){
5✔
396
                throw(FlagsOrderIncorrect((*argv_i) , "-vcf") );
×
397
            }
398
            this->setExtractPlafFromVcf(true);
399
        } else if (*argv_i == "-vcfOut") {
352✔
400
            this->setDoExportVcf (true);
401
        } else if (*argv_i == "-plaf") {
350✔
402
            if ( this->extractPlafFromVcf() ) {
107✔
403
                throw ( FlagsConflict((*argv_i) , "-plafFromVcf") );
3✔
404
            }
405
            this->readNextStringto ( this->plafFileName_ ) ;
106✔
406
        } else if (*argv_i == "-panel") {
243✔
407
            if ( this->usePanel() == false ) {
77✔
408
                throw ( FlagsConflict((*argv_i) , "-noPanel") );
3✔
409
            }
410
            this->readNextStringto ( this->panelFileName_ ) ;
76✔
411
        } else if (*argv_i == "-noPanel") {
166✔
412
            if ( usePanel() && this->panelFileName_.size() > 0 ) {
32✔
413
                throw ( FlagsConflict((*argv_i) , "-panel") );
3✔
414
            }
415
            if ( doExportPostProb() ) {
31✔
416
                throw ( FlagsConflict((*argv_i) , "-exportPostProb") );
3✔
417
            }
418
            if ( doAllowInbreeding() ) {
30✔
419
                throw ( FlagsConflict((*argv_i) , "-inbreeding") );
×
420
            }
421
            if ( useBestPractice() ) {
30✔
422
                throw ( FlagsConflict((*argv_i) , "-best") );
3✔
423
            }
424
            this->setUsePanel(false);
425
            this->setDoExportSwitchMissCopy ( false );
426
        } else if (*argv_i == "-exclude") {
134✔
427
            this->setExcludeSites( true );
428
            this->readNextStringto ( this->excludeFileName_ ) ;
9✔
429
        } else if (*argv_i == "-o") {
125✔
430
            this->readNextStringto ( this->prefix_ ) ;
28✔
431
        } else if ( *argv_i == "-p" ) {
97✔
432
            //this->precision_ = readNextInput<size_t>() ;
433
            this->precision_.setUserDefined(readNextInput<size_t>());
3✔
434
        } else if ( *argv_i == "-k" ) {
94✔
435
            this->kStrain_.setUserDefined(readNextInput<size_t>());
8✔
436

437
            if ( this->kStrainWasSetByHap() && this->kStrain_.getValue() != this->initialHap[0].size() ) {
5✔
438
                string hint = string(" k = ") + to_string(this->kStrain_.getValue()) + ", " + this->initialHapFileName_ + " suggests otherwise";
×
439
                throw NumOfPropNotMatchNumStrain(hint);
×
440
            }
441

442
            if ( this->initialPropWasGiven() && this->kStrain_.getValue() != initialProp.size() ) {
5✔
443
                string hint = string(" k = ") + to_string(kStrain_.getValue()) + ", flag -initialP suggests otherwise";;
2✔
444
                throw NumOfPropNotMatchNumStrain(hint);
3✔
445
            }
446

447
        } else if ( *argv_i == "-nSample" ) {
86✔
448
            this->nMcmcSample_.setUserDefined(readNextInput<size_t>());
3✔
449
        } else if ( *argv_i == "-burn" ) {
83✔
450
            this->mcmcBurn_.setUserDefined(readNextInput<double>());
2✔
451
            if ( this->mcmcBurn_.getValue() < 0 || this->mcmcBurn_.getValue() > 1) {
2✔
452
                throw ( OutOfRange ("-burn", *argv_i) );
3✔
453
            }
454
        } else if ( *argv_i == "-miss" ) {
81✔
455
            this->missCopyProb_.setUserDefined(readNextInput<double>());
1✔
456
            if ( this->missCopyProb_.getValue() < 0 || this->missCopyProb_.getValue() > 1) {
1✔
457
                throw ( OutOfRange ("-miss", *argv_i) );
3✔
458
            }
459
        } else if ( *argv_i == "-c" ) {
80✔
460
            this->scalingFactor_ = readNextInput<double>() ;
×
461
        } else if ( *argv_i == "-G" ) {
80✔
462
            this->setParameterG(readNextInput<double>());
×
463
        } else if ( *argv_i == "-vqslod" ) {
80✔
464
            this->setVqslod(readNextInput<double>());
×
465
        } else if ( *argv_i == "-sigma" ) {
80✔
466
            this->parameterSigma_.setUserDefined(readNextInput<double>());
1✔
467
        } else if ( *argv_i == "-ibdSigma" ) {
79✔
468
            this->setIBDSigma(readNextInput<double>());
×
469
        } else if ( *argv_i == "-lassoMaxPanel" ) {
79✔
470
            this->setLassoMaxNumPanel(readNextInput<size_t>());
×
471
        } else if ( *argv_i == "-recomb" ) {
79✔
472
            this->constRecombProb_ = readNextInput<double>();
1✔
473
            this->useConstRecomb_ = true;
1✔
474
            if ( this->constRecombProb_ < 0 || this->constRecombProb_ > 1) {
1✔
475
                throw ( OutOfRange ("-recomb", *argv_i) );
3✔
476
            }
477
        } else if ( *argv_i == "-printRecomb" ) {
78✔
478
            this->setDoExportRecombProb( true );
479
        } else if ( *argv_i == "-forbidSame" ) {
78✔
480
            this->setForbidCopyFromSame( true );
481
        } else if ( *argv_i == "-rate" ) {
78✔
482
            this->mcmcMachineryRate_.setUserDefined(readNextInput<size_t>());
3✔
483
        } else if ( *argv_i == "-forbidUpdateProp" ) {
75✔
484
            this->setDoUpdateProp( false );
485
        } else if ( *argv_i == "-forbidUpdateSingle" ) {
69✔
486
            this->setDoUpdateSingle( false );
487
        } else if ( *argv_i == "-forbidUpdatePair" ) {
65✔
488
            this->setDoUpdatePair( false );
489
        } else if ( *argv_i == "-inbreeding" ) {
63✔
490
            if ( this->usePanel() == false ) {
2✔
491
                throw ( FlagsConflict((*argv_i) , "-noPanel") );
×
492
            }
493
            this->setDoAllowInbreeding( true );
494
            this->setDoExportPostProb( true );
495
        } else if ( *argv_i == "-exportPostProb" ) {
61✔
496
            if ( useBestPractice() ) {
2✔
497
                throw ( FlagsConflict((*argv_i) , "-best") );
×
498
            }
499
            if ( this->usePanel() == false ) {
2✔
500
                throw ( FlagsConflict((*argv_i) , "-noPanel") );
3✔
501
            }
502
            this->setDoExportPostProb( true );
503
        } else if ( *argv_i == "-painting" ) {
59✔
504
            if ( this->usePanel() == false ) {
3✔
505
                throw ( FlagsConflict((*argv_i) , "-noPanel") );
×
506
            }
507
            if ( this->initialHapWasGiven() == true ) {
3✔
508
                throw ( FlagsConflict((*argv_i) , "-initialHap") );
×
509
            }
510
            this->readNextStringto ( this->initialHapFileName_ ) ;
3✔
511
            this->setDoLsPainting( true );
512
            this->readInitialHaps();
3✔
513
        } else if ( *argv_i == "-ibd" ) {
56✔
514
            if ( useBestPractice() ) {
7✔
515
                throw ( FlagsConflict((*argv_i) , "-best") );
3✔
516
            }
517
            if ( useLasso() == true ) {
6✔
518
                throw ( FlagsConflict((*argv_i) , "-lasso") );
3✔
519
            }
520
            this->setUseIBD(true);
521
        } else if (*argv_i == "-best") {
49✔
522
            if ( doExportPostProb() ) {
9✔
523
                throw ( FlagsConflict((*argv_i) , "-exportPostProb") );
×
524
            }
525
            if ( usePanel() == false ) {
9✔
526
                throw ( FlagsConflict((*argv_i) , "-noPanel") );
3✔
527
            }
528
            if ( useIBD() == true ) {
8✔
529
                throw ( FlagsConflict((*argv_i) , "-ibd") );
3✔
530
            }
531
            if ( useLasso() == true ) {
7✔
532
                throw ( FlagsConflict((*argv_i) , "-lasso") );
3✔
533
            }
534
            this->setUseBestPractice(true);
535
        } else if (*argv_i == "-bestKonly") {
40✔
536
            this->setInferBestPracticeP(false);
537
            this->setInferBestPracticeHap(false);
538
        } else if (*argv_i == "-bestPonly") {
40✔
539
            this->setInferBestPracticeHap(false);
540
        } else if ( *argv_i == "-ibdonly" ) {
40✔
541
            if ( useBestPractice() ) {
×
542
                throw ( FlagsConflict((*argv_i) , "-best") );
×
543
            }
544
            this->setUseIBD(true);
545
            this->setUseIbdOnly(true);
546
        } else if ( *argv_i == "-lasso" ) {
40✔
547
            if ( useIBD() == true ) {
7✔
548
                throw ( FlagsConflict((*argv_i) , "-ibd") );
3✔
549
            }
550
            if ( useBestPractice() ) {
6✔
551
                throw ( FlagsConflict((*argv_i) , "-best") );
3✔
552
            }
553
            if ( doExportPostProb() ) {
5✔
554
                throw ( FlagsConflict((*argv_i) , "-exportPostProb") );
×
555
            }
556
            this->setUseLasso(true);
557
            this->setDoUpdateProp(false);
558
        } else if ( *argv_i == "-writePanel" ) {
33✔
559
            this->setDoPrintLassoPanel(true);
560
        } else if ( *argv_i == "-computeLLK" ) {
33✔
561
            this->setDoComputeLLK( true );
562
        } else if ( *argv_i == "-ibdPainting" ) {
33✔
563
            if ( useBestPractice() ) {
2✔
564
                throw ( FlagsConflict((*argv_i) , "-best") );
×
565
            }
566
            this->setDoIbdPainting( true );
567
        } else if ( *argv_i == "-ibdViterbi" ) {
31✔
568
            this->setDoIbdViterbiPainting( true );
569
        } else if ( *argv_i == "-skipCheckingInitialP" ) {
31✔
570
            this->setPleaseCheckInitialP(false);
571
        } else if ( *argv_i == "-initialP" ) {
31✔
572
            this->readInitialProportions();
12✔
573
            this->setInitialPropWasGiven( true );
574

575
            // If the k was set manually, check
576
            if ( this->kStrain_.useUserDefined() && this->kStrain_.getValue() != initialProp.size() ) {
10✔
577
                string hint = string(" k = ") + to_string(kStrain_.getValue());
2✔
578
                throw NumOfPropNotMatchNumStrain(hint);
3✔
579
            }
580

581
            // If the k was set by initial Hap, check
582
            if ( this->kStrainWasSetByHap() && this->kStrain_.getValue() != this->initialProp.size() ) {
9✔
583
                string hint = string(" k = ") + to_string(this->kStrain_.getValue()) + ", " + this->initialHapFileName_ + " suggests otherwise";
×
584
                throw NumOfPropNotMatchNumStrain(hint);
×
585
            }
586

587
            this->kStrain_.init(this->initialProp.size());
588
        } else if ( *argv_i == "-initialHap" ) {
19✔
589
            if ( this->doLsPainting() == true ) {
2✔
590
                throw ( FlagsConflict((*argv_i) , "-painting") );
×
591
            }
592
            this->readNextStringto ( this->initialHapFileName_ ) ;
2✔
593
            this->setInitialHapWasGiven(true);
594
            this->readInitialHaps();
2✔
595
        } else if ( *argv_i == "-seed") {
17✔
596
            this->randomSeed_.setUserDefined(readNextInput<size_t>());
5✔
597
        } else if ( *argv_i == "-z" ) {
12✔
598
            this->setCompressVcf(true);
599
        } else if ( *argv_i == "-h" || *argv_i == "-help") {
18✔
600
            this->set_help(true);
601
        } else if ( *argv_i == "-v" || *argv_i == "-version") {
8✔
602
            this->setVersion(true);
603
        } else {
604
            throw ( UnknowArg((*argv_i)) );
3✔
605
        }
606
    } while ( ++argv_i != argv_.end());
506✔
607
}
83✔
608

609

610
void DEploidIO::checkInput() {
74✔
611

612
    if ( this->refFileName_.size() == 0 && this->useVcf() == false ) {
74✔
613
        throw FileNameMissing ( "Ref count" );}
6✔
614
    if ( this->altFileName_.size() == 0 && this->useVcf() == false ) {
71✔
615
        throw FileNameMissing ( "Alt count" );}
4✔
616
    if ( this->plafFileName_.size() == 0 && extractPlafFromVcf() == false ) {
69✔
617
        throw FileNameMissing ( "PLAF" );}
2✔
618
    if ( usePanel() && this->panelFileName_.size() == 0 && !this->doIbdPainting() && !this->doComputeLLK() ) {
68✔
619
        throw FileNameMissing ( "Reference panel" );}
2✔
620
    if ( this->initialPropWasGiven() && ( abs(sumOfVec(initialProp) - 1.0) > 0.00001 ) && this->pleaseCheckInitialP() ) {
75✔
621
        throw SumOfPropNotOne ( to_string(sumOfVec(initialProp)) );}
6✔
622
    if ( this->initialPropWasGiven() ) {
623
        if ( this->kStrain_.useUserDefined() == true ) {
624
        } else {
625
            // set k strain by proportion length
626
        }
627
    }
628
    if (this->useBestPractice() && (!this->usePanel())){
65✔
629
        throw FlagsConflict("-best" , string("-noPanel. Reference panel is") +
×
630
            string("required for using best-practices."));
×
631
    }
632
}
65✔
633

634

635
void DEploidIO::readInitialProportions() {
12✔
636
    string tmpFlag = *argv_i;
637
    ++argv_i;
638
    if (argv_i == argv_.end() || (*argv_i)[0] == '-' ) {
12✔
639
            throw NotEnoughArg (tmpFlag);
6✔
640
    }
641

642
    do {
643
        try {
644
            double tmp = convert<double>(*argv_i);
39✔
645
            this->initialProp.push_back(tmp);
39✔
646
        } catch (const WrongType &e) {
×
647
            --argv_i;
648
            break;
649
        }
×
650
        ++argv_i;
651
    } while ( argv_i != argv_.end() && (*argv_i)[0] != '-' );
39✔
652
    --argv_i;
653

654
    return;
10✔
655
}
656

657

658
void DEploidIO::readNextStringto( string &readto ) {
413✔
659
    string tmpFlag = *argv_i;
660
    ++argv_i;
661
    if (argv_i == argv_.end() || (*argv_i)[0] == '-' ) {
413✔
662
        throw NotEnoughArg(tmpFlag);
18✔
663
    }
664
    readto = *argv_i;
665
}
407✔
666

667

668

669
std::ostream& operator<< (std::ostream& stream, const DEploidIO& dEploidIO) {
×
670
  for (std::string arg : dEploidIO.argv_) stream << " " << arg;
×
671
  return stream;
×
672
}
673

674

675
void DEploidIO::readInitialHaps() {
5✔
676
    assert( this->initialHap.size() == 0 );
677
    InitialHaplotypes initialHapToBeRead;
678
    initialHapToBeRead.readFromFile(this->initialHapFileName_.c_str());
5✔
679

680
    assert (this->initialHap.size() == 0 );
681
    this->initialHap = initialHapToBeRead.content_;
5✔
682

683
    if ( this->kStrain_.useUserDefined() && this->kStrain_.getValue()!= initialHapToBeRead.truePanelSize() ) {
5✔
684
        string hint = string(" k = ") + to_string(this->kStrain_.getValue()) + ", " + this->initialHapFileName_ + " suggests otherwise";
×
685
        throw NumOfPropNotMatchNumStrain(hint);
×
686
    }
687

688
    if ( this->kStrainWasSetByProp() && this->kStrain_.getValue() != initialHapToBeRead.truePanelSize() ) {
5✔
689
        string hint = string(" k = ") + to_string(kStrain_.getValue()) + ", flag -initialP suggests otherwise";;
×
690
        throw NumOfPropNotMatchNumStrain(hint);
×
691
    }
692

693
    this->kStrain_.init(initialHapToBeRead.truePanelSize());
694
    this->setKStrainWasSetByHap(true);
695
}
5✔
696

697

698
vector <double> DEploidIO::computeExpectedWsafFromInitialHap() {
1✔
699
    // Make this a separate function
700
    // calculate expected wsaf
701
    vector <double> expectedWsaf (this->initialHap.size(), 0.0);
1✔
702
    for ( size_t i = 0; i < this->initialHap.size(); i++ ) {
595✔
703
        assert( kStrain_.getValue() == this->initialHap[i].size() );
704
        for ( size_t k = 0; k < this->kStrain_.getValue(); k++) {
3,564✔
705
            expectedWsaf[i] += this->initialHap[i][k] * finalProp[k];
2,970✔
706
        }
707
        assert ( expectedWsaf[i] >= 0 );
708
        //assert ( expectedWsaf[i] <= 1.0 );
709
    }
710
    return expectedWsaf;
1✔
711
}
712

713

714
void DEploidIO::computeLLKfromInitialHap() {
×
715
    for ( auto const& value: this->initialProp ) {
×
716
        this->finalProp.push_back(value);
×
717
    }
718

719
    vector <double> expectedWsaf = computeExpectedWsafFromInitialHap();
×
720
    if (expectedWsaf.size() != this->refCount_.size()) {
×
721
        throw LociNumberUnequal("Hap length differs from data!");
×
722
    }
723
    vector <double> llk = calcLLKs ( this->refCount_, this->altCount_, expectedWsaf, 0, expectedWsaf.size(), this->scalingFactor());
×
724
    this->llkFromInitialHap_ = sumOfVec(llk);
×
725
}
×
726

727

728

729

730
void DEploidIO::readPanel() {
33✔
731
    if ( this->usePanel() == false ) {
33✔
732
        return;
733
    }
734
    if ( this->doIbdPainting() || this->doComputeLLK() ) {
33✔
735
        return;
736
    }
737

738
    panel = new Panel();
62✔
739
    panel->readFromFile(this->panelFileName_.c_str());
31✔
740
    if ( this->excludeSites() ) {
31✔
741
        panel->findAndKeepMarkers( this->excludedMarkers );
8✔
742
    }
743

744
    panel->computeRecombProbs( this->averageCentimorganDistance(), this->parameterG(), this->useConstRecomb(), this->constRecombProb(), this->forbidCopyFromSame() );
31✔
745
    panel->checkForExceptions( this->nLoci(), this->panelFileName_ );
62✔
746
}
747

748

749
DEploidIO::DEploidIO(const DEploidIO &cpFrom) {
5✔
750
    this->setIsCopied(true);
751
    this->setDoExportRecombProb(cpFrom.doExportRecombProb());
752
    this->setCompressVcf(cpFrom.compressVcf());
753
    this->setInitialPropWasGiven(cpFrom.initialPropWasGiven());
754
    this->setInitialHapWasGiven(cpFrom.initialHapWasGiven());
755
    this->initialProp = vector <double> (cpFrom.initialProp.begin(),
10✔
756
                                         cpFrom.initialProp.end());
757
    this->setPleaseCheckInitialP(cpFrom.pleaseCheckInitialP());
758
    this->setExcludeSites(cpFrom.excludeSites());
759
    this->excludedMarkers = cpFrom.excludedMarkers;
5✔
760
    this->panel = cpFrom.panel;
5✔
761
    this->set_help(cpFrom.help());
762
    this->setVersion(cpFrom.version());
763
    this->setUsePanel(cpFrom.usePanel());
764
    this->prefix_ = cpFrom.prefix_;
5✔
765
    this->setKStrainWasSetByHap(cpFrom.kStrainWasSetByHap());
766
    this->setKStrainWasSetByProp(cpFrom.kStrainWasSetByProp());
767

768
    this->setDoUpdateProp(cpFrom.doUpdateProp());
769
    this->setDoUpdatePair(cpFrom.doUpdatePair());
770
    this->setDoUpdateSingle(cpFrom.doUpdateSingle());
771
    this->setDoExportPostProb(cpFrom.doExportPostProb());
772
    this->setDoLsPainting(cpFrom.doLsPainting());
773
    this->setDoIbdPainting(cpFrom.doIbdPainting());
774
    this->setUseIBD(cpFrom.useIBD());
775
    this->setUseIbdOnly(cpFrom.useIbdOnly());
776
    this->setUseLasso(cpFrom.useLasso());
777
    this->setDoExportSwitchMissCopy(cpFrom.doExportSwitchMissCopy());
778
    this->setDoAllowInbreeding(cpFrom.doAllowInbreeding());
779
    this->useConstRecomb_ = cpFrom.useConstRecomb();
5✔
780
    this->setForbidCopyFromSame(cpFrom.forbidCopyFromSame());
781
    this->constRecombProb_ = cpFrom.constRecombProb();
5✔
782
    this->averageCentimorganDistance_ = cpFrom.averageCentimorganDistance();
5✔
783
    this->setScalingFactor(cpFrom.scalingFactor());
784
    this->setParameterG(cpFrom.parameterG());
785
    this->setIBDSigma(cpFrom.ibdSigma());
786
    this->setUseVcf(cpFrom.useVcf());
787
    this->vcfReaderPtr_ = cpFrom.vcfReaderPtr_;
5✔
788
    this->setDoExportVcf(cpFrom.doExportVcf());
789
    this->setDoComputeLLK(cpFrom.doComputeLLK());
790
    this->setNLoci(cpFrom.nLoci());
791
    this->refCount_ = vector <double> (cpFrom.refCount_.begin(),
10✔
792
                                       cpFrom.refCount_.end());
793
    this->altCount_ = vector <double> (cpFrom.altCount_.begin(),
10✔
794
                                       cpFrom.altCount_.end());
795
    this->plaf_ = vector <double> (cpFrom.plaf_.begin(),
10✔
796
                                   cpFrom.plaf_.end());
797
    this->chrom_ = vector <string> (cpFrom.chrom_.begin(),
10✔
798
                                   cpFrom.chrom_.end());
5✔
799
    this->position_ = vector < vector <int> > (cpFrom.position_.begin(),
10✔
800
                                   cpFrom.position_.end());
5✔
801
    this->indexOfChromStarts_ = vector <size_t> (cpFrom.indexOfChromStarts_.begin(),
10✔
802
                                   cpFrom.indexOfChromStarts_.end());
803
    this->setVqslod(cpFrom.vqslod());
804
    this->setLassoMaxNumPanel(cpFrom.lassoMaxNumPanel());
805
    //this->strExportProp = cpFrom.strExportProp;
806
    //this->strExportLLK = cpFrom.strExportLLK;
807
    //this->strExportHap = cpFrom.strExportHap;
808
    //this->strIbdExportProp = cpFrom.strIbdExportProp;
809
    //this->strIbdExportLLK = cpFrom.strIbdExportLLK;
810
    //this->strIbdExportHap = cpFrom.strIbdExportHap;
811

812
    this->kStrain_.makeCopy(cpFrom.kStrain_);
813
    this->precision_.makeCopy(cpFrom.precision_);
814
    this->missCopyProb_ .makeCopy(cpFrom.missCopyProb_);
815
    this->randomSeed_.makeCopy(cpFrom.randomSeed_);
816
    this->nMcmcSample_.makeCopy(cpFrom.nMcmcSample_);
817
    this->parameterSigma_.makeCopy(cpFrom.parameterSigma_);
818
    this->mcmcBurn_.makeCopy(cpFrom.mcmcBurn_);
819
    this->mcmcMachineryRate_.makeCopy(cpFrom.mcmcMachineryRate_);
820
}
5✔
821

822

823
void DEploidIO::getIBDprobsIntegrated(vector < vector <double> > &prob) {
5✔
824
    if (prob.size() !=  this->nLoci()) {
5✔
825
        throw InvalidInput("Invalid probabilities! Check size!");
×
826
    }
827

828
    assert(this->ibdProbsIntegrated.size() == 0);
829

830
    for (size_t i = 0; i < prob[0].size(); i++) {
28✔
831
        this->ibdProbsIntegrated.push_back(0.0);
23✔
832
    }
833

834
    for ( size_t siteIndex = 0; siteIndex < this->nLoci(); siteIndex++ ) {
2,620✔
835
        for (size_t i = 0; i < prob[siteIndex].size(); i++) {
15,567✔
836
            this->ibdProbsIntegrated[i] += prob[siteIndex][i];
12,952✔
837
        }
838
    }
839
    normalizeBySum(this->ibdProbsIntegrated);
5✔
840
}
5✔
841

842

843
void DEploidIO::computeEffectiveKstrain(vector <double> proportion) {
×
844
    double tmpSumSq = 0.0;
845
    for (double p : proportion) {
×
846
        tmpSumSq += p * p;
×
847
    }
848
    this->effectiveKstrain_ = 1.0 / tmpSumSq;
×
849
}
×
850

851

852
void DEploidIO::computeInferredKstrain(vector <double> proportion) {
×
853
    this->inferredKstrain_ = 0;
×
854
    for (double p : proportion) {
×
855
        if ( p > 0.01 ) {
×
856
            this->inferredKstrain_ += 1;
×
857
        }
858
    }
859
}
×
860

861

862
void DEploidIO::computeAdjustedEffectiveKstrain() {
×
863
    this->adjustedEffectiveKstrain_ = this->effectiveKstrain_;
×
864
    if ( (this->inferredKstrain_ == 2) & (ibdProbsIntegrated.size() == 2)) {
×
865
        if ( this->ibdProbsIntegrated[1] > 0.95 ) {
×
866
            this->adjustedEffectiveKstrain_ = 1;
×
867
        }
868
    }
869
}
×
870

871

872
vector <double> DEploidIO::lassoComputeObsWsaf(size_t segmentStartIndex, size_t nLoci) {
56✔
873
    vector <double> ret(nLoci, 0.0);
56✔
874
    for ( size_t i = 0; i < nLoci; i++) {
2,104✔
875
        size_t idx = i + segmentStartIndex;
2,048✔
876
        ret[i] = this->altCount_[idx] / (this->refCount_[idx] + this->altCount_[idx] + 0.00000000000001);
2,048✔
877
    }
878
    return ret;
56✔
879
}
880

881

882
vector < vector <double> > DEploidIO::lassoSubsetPanel(size_t segmentStartIndex, size_t nLoci) {
56✔
883
    vector < vector <double> > ret;
884
    for ( size_t i = 0; i < nLoci; i++) {
2,104✔
885
        size_t idx = i + segmentStartIndex;
2,048✔
886
        ret.push_back(this->panel->content_[idx]);
2,048✔
887
    }
888
    return ret;
56✔
889
}
×
890

891

892
void DEploidIO::dEploidLasso() {
4✔
893
    for ( size_t chromi = 0 ; chromi < this->indexOfChromStarts_.size(); chromi++ ) {
60✔
894
        size_t start = this->indexOfChromStarts_[chromi];
56✔
895
        size_t length = this->position_[chromi].size();
896
        size_t end = start + length;
56✔
897
        vector <double> wsaf = lassoComputeObsWsaf(start, length);
56✔
898
        vector < vector <double> > tmpPanel = lassoSubsetPanel(start, length);
56✔
899
        DEploidLASSO dummy(tmpPanel, wsaf, 250);
56✔
900

901
        vector <string> newHeader;
902
        for (size_t i = 0; i < min(dummy.choiceIdx.size(), lassoMaxNumPanel()); i++) {
112✔
903
            newHeader.push_back(panel->header_[dummy.choiceIdx[i]]);
56✔
904
        }
905
        newHeader.push_back("3d7");
112✔
906

907
        vector < vector <double> > newPanel;
908
        for (size_t i = 0; i < dummy.reducedPanel.size(); i++) {
2,104✔
909
            vector <double> tmpRow;
910
            for (size_t j = 0; j < min(dummy.choiceIdx.size(), lassoMaxNumPanel()); j++) {
4,096✔
911
                tmpRow.push_back(dummy.reducedPanel[i][j]);
2,048✔
912
            }
913
            tmpRow.push_back(static_cast<int>(0));
2,048✔
914
            newPanel.push_back(tmpRow);
2,048✔
915
        }
916

917
        Panel * tmp = new Panel(vecFromTo(this->panel->pRec_, start, end),
56✔
918
                            vecFromTo(this->panel->pRecEachHap_, start, end),
112✔
919
                            vecFromTo(this->panel->pNoRec_, start, end),
112✔
920
                            vecFromTo(this->panel->pRecRec_, start, end),
112✔
921
                            vecFromTo(this->panel->pRecNoRec_, start, end),
112✔
922
                            vecFromTo(this->panel->pNoRecNoRec_, start, end),
112✔
923
                            newPanel,
924
                            this->panel->header_);
56✔
925
        lassoPanels.push_back(tmp);
56✔
926
        lassoPlafs.push_back(vecFromTo(plaf_, start, end));
56✔
927
        lassoRefCount.push_back(vecFromTo(refCount_, start, end));
56✔
928
        lassoAltCount.push_back(vecFromTo(altCount_, start, end));
112✔
929
        if (this->doPrintLassoPanel()) {
56✔
930
            this->writePanel(tmp, chromi, newHeader);
×
931
        }
932
    }
56✔
933
    this->finalProp.clear();
4✔
934
    for ( auto const& value: this->initialProp ) {
6✔
935
        this->finalProp.push_back(value);
2✔
936
    }
937
}
4✔
938

939

940
void DEploidIO::trimVec(vector <double> &vec, vector <size_t> &idx) {
6✔
941
    vector <double> ret;
942
    for (auto const& value : idx){
1,926✔
943
        ret.push_back(vec[value]);
1,920✔
944
    }
945
    //return ret;
946
    vec.clear();
947
    for (auto const& value : ret){
1,926✔
948
        vec.push_back(value);
1,920✔
949
    }
950
}
6✔
951

952

953
void DEploidIO::trimming(vector <size_t> & trimmingCriteria) {
2✔
954
    this->trimVec(this->refCount_, trimmingCriteria);
2✔
955
    this->trimVec(this->altCount_, trimmingCriteria);
2✔
956
    this->trimVec(this->plaf_, trimmingCriteria);
2✔
957

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

960
    vector <string> oldChrom = vector <string> (chrom_.begin(), chrom_.end());
2✔
961
    this->chrom_.clear();
2✔
962

963
    vector < vector < int > > oldposition = this->position_;
2✔
964
    this->position_.clear();
965

966
    for (size_t chromI = 0; chromI < oldChrom.size(); chromI++) {
30✔
967
        size_t hapIndex = indexOfChromStarts_[chromI];
28✔
968
        vector <int> newTrimmedPos;
969
        for (size_t posI = 0; posI < oldposition[chromI].size(); posI++) {
1,162✔
970
            if (std::find(trimmingCriteria.begin(),trimmingCriteria.end(), hapIndex)
1,134✔
971
                    != trimmingCriteria.end()){
972
                if (newTrimmedPos.size() == 0) {
640✔
973
                    this->chrom_.push_back(oldChrom[chromI]);
28✔
974
                }
975
                newTrimmedPos.push_back(oldposition[chromI][posI]);
640✔
976
            }
977

978
            hapIndex++;
1,134✔
979
        }
980
        this->position_.push_back(newTrimmedPos);
28✔
981
    }
982

983
    this->indexOfChromStarts_.clear();
2✔
984
    assert(indexOfChromStarts_.size() == 0);
985
    this->indexOfChromStarts_.push_back((size_t)0);
2✔
986
    for (size_t tmpChrom = 0;
2✔
987
            indexOfChromStarts_.size() < this->chrom_.size(); tmpChrom++ ) {
28✔
988
        indexOfChromStarts_.push_back(
989
            indexOfChromStarts_.back()+this->position_[tmpChrom].size());
26✔
990
    }
991
    assert(indexOfChromStarts_.size() == this->chrom_.size());
992
}
2✔
993

994

995

996
void DEploidIO::trimmingHalf(vector <size_t> & trimmingCriteria) {
×
997
    this->trimVec(this->refCount_, trimmingCriteria);
×
998
    this->trimVec(this->altCount_, trimmingCriteria);
×
999
    this->trimVec(this->plaf_, trimmingCriteria);
×
1000

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

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

1006
    vector < vector < int > > oldposition = this->position_;
×
1007
    this->position_.clear();
1008

1009
    for (size_t chromI = 0; chromI < oldChrom.size(); chromI++) {
×
1010
        if (chromI > 10) {
×
1011
        //if (chromI%2 == 0) {
1012
            size_t hapIndex = indexOfChromStarts_[chromI];
×
1013
            vector <int> newTrimmedPos;
1014
            for (size_t posI = 0; posI < oldposition[chromI].size(); posI++) {
×
1015
                if (std::find(trimmingCriteria.begin(),trimmingCriteria.end(), hapIndex)
×
1016
                        != trimmingCriteria.end()){
1017
                    if (newTrimmedPos.size() == 0) {
×
1018
                        this->chrom_.push_back(oldChrom[chromI]);
×
1019
                    }
1020
                    newTrimmedPos.push_back(oldposition[chromI][posI]);
×
1021
                }
1022

1023
                hapIndex++;
×
1024
            }
1025
            this->position_.push_back(newTrimmedPos);
×
1026
        }
1027
    }
1028

1029
    this->indexOfChromStarts_.clear();
×
1030
    assert(indexOfChromStarts_.size() == 0);
1031
    this->indexOfChromStarts_.push_back((size_t)0);
×
1032
    for (size_t tmpChrom = 0;
×
1033
            indexOfChromStarts_.size() < this->chrom_.size(); tmpChrom++ ) {
×
1034
        indexOfChromStarts_.push_back(
1035
            indexOfChromStarts_.back()+this->position_[tmpChrom].size());
×
1036
    }
1037
    assert(indexOfChromStarts_.size() == this->chrom_.size());
1038
}
×
1039

1040

1041
void DEploidIO::computeObsWsaf() {
×
1042
    assert(this->obsWsaf_.size() == 0);
1043
    for ( size_t i = 0; i < this->nLoci(); i++) {
×
1044
        this->obsWsaf_.push_back(this->altCount_[i] /
×
1045
            (this->refCount_[i] + this->altCount_[i] + 0.00000000000001));
×
1046
    }
1047
    assert(this->obsWsaf_.size() == this->nLoci());
1048
}
×
1049

1050

1051
void DEploidIO::dEploidLassoTrimfirst() {  // This is trimming using VQSLOD
1✔
1052
    if (vcfReaderPtr_ == NULL){
1✔
1053
        panel = new Panel (*panel);
×
1054
        panel->computeRecombProbs(this->averageCentimorganDistance(), this->parameterG(), true, 0.0000001, this->forbidCopyFromSame());
×
1055
        this->dEploidLasso();
×
1056
        this->setIsCopied(false);
1057
        this->excludedMarkers = NULL;
×
1058
        return;
×
1059
    }
1060

1061
    this->vcfReaderPtr_->findLegitSnpsGivenVQSLOD(this->vqslod());
1✔
1062
    this->trimming(this->vcfReaderPtr_->legitVqslodAt);
1✔
1063

1064
    panel = new Panel (*panel);
1✔
1065
    panel->computeRecombProbs(this->averageCentimorganDistance(), this->parameterG(), true, 0.0000001, this->forbidCopyFromSame());
1✔
1066
    panel->findAndKeepMarkersGivenIndex(this->vcfReaderPtr_->legitVqslodAt);
1✔
1067
    this->dEploidLasso();
1✔
1068
    this->setIsCopied(false);
1069
    this->excludedMarkers = NULL;
1✔
1070
    this->vcfReaderPtr_ = NULL;
1✔
1071
}
1072

1073

1074

1075
void DEploidIO::dEploidLassoFullPanel() {
×
1076
    // Filter SNPs first, restrict to wsaf > 0 don't work ...
1077
    this->vcfReaderPtr_->findLegitSnpsGivenVQSLODHalf(this->vqslod());
×
1078
    //this->vcfReaderPtr_->findLegitSnpsGivenVQSLOD(this->vqslod());
1079
    //this->vcfReaderPtr_->findLegitSnpsGivenVQSLODandWsfGt0(this->vqslod());
1080

1081
    //this->trimming(this->vcfReaderPtr_->legitVqslodAt);
1082
    this->trimmingHalf(this->vcfReaderPtr_->legitVqslodAt);
×
1083
    this->computeObsWsaf();
×
1084

1085
    Panel tmpPanel(*panel);
×
1086
    tmpPanel.computeRecombProbs(this->averageCentimorganDistance(), this->parameterG(), true, 0.0000001, this->forbidCopyFromSame());
×
1087
    //tmpPanel.findAndKeepMarkersGivenIndex(this->vcfReaderPtr_->legitVqslodAt);
1088
    tmpPanel.findAndKeepMarkersGivenIndexHalf(this->vcfReaderPtr_->legitVqslodAt);
×
1089
    DEploidLASSO dummy(tmpPanel.content_, this->obsWsaf_, 250);
×
1090

1091
    for (size_t i = 0; i < dummy.choiceIdx.size(); i++) {
1092
        dout << i << " " << dummy.devRatio[i]<<endl;
1093
    }
1094
    //size_t maxNumPanel = 10;
1095
    // Use the first 10 strains in the panel
1096
    vector <string> newHeader;
1097
    for (size_t i = 0; i < min(dummy.choiceIdx.size(), lassoMaxNumPanel()); i++) {
×
1098
        newHeader.push_back(panel->header_[dummy.choiceIdx[i]]);
×
1099
    }
1100
newHeader.push_back("3d7");
×
1101
//cout <<newHeader.size()<<endl;
1102

1103
    vector < vector <double> > newPanel;
1104
    for (size_t i = 0; i < dummy.reducedPanel.size(); i++) {
×
1105
        vector <double> tmpRow;
1106
        for (size_t j = 0; j < min(dummy.choiceIdx.size(), lassoMaxNumPanel()); j++) {
×
1107
            tmpRow.push_back(dummy.reducedPanel[i][j]);
×
1108
        }
1109
tmpRow.push_back(static_cast<int>(0));
×
1110
        newPanel.push_back(tmpRow);
×
1111
    }
1112

1113
    panel = new Panel(tmpPanel.pRec_,
×
1114
                      tmpPanel.pRecEachHap_,
1115
                      tmpPanel.pNoRec_,
1116
                      tmpPanel.pRecRec_,
1117
                      tmpPanel.pRecNoRec_,
1118
                      tmpPanel.pNoRecNoRec_,
1119
                      newPanel,
1120
                      newHeader);
×
1121

1122
    this->setIsCopied(false);
1123
    this->excludedMarkers = NULL;
×
1124
    this->vcfReaderPtr_ = NULL;
×
1125
}
×
1126

1127

1128
void DEploidIO::ibdTrimming() {
1✔
1129
    if (vcfReaderPtr_ == NULL){
1✔
1130
        panel = new Panel(*panel);
×
1131
        this->setIsCopied(false);
1132
        this->excludedMarkers = NULL;
×
1133
        return;
×
1134
    }
1135
    // Filter SNPs first
1136
    //cout << "here" <<endl;
1137
    this->vcfReaderPtr_->findLegitSnpsGivenVQSLOD(this->vqslod());
1✔
1138
    //cout << "stop here" <<endl;
1139
    this->trimming(this->vcfReaderPtr_->legitVqslodAt);
1✔
1140
    panel = new Panel(*panel);
1✔
1141
    this->panel->findAndKeepMarkersGivenIndex(
1✔
1142
                                        this->vcfReaderPtr_->legitVqslodAt);
1✔
1143

1144
    this->setIsCopied(false);
1145
    this->excludedMarkers = NULL;
1✔
1146
    this->vcfReaderPtr_ = NULL;
1✔
1147
}
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