-
Notifications
You must be signed in to change notification settings - Fork 0
/
varfiles.hpp
1006 lines (905 loc) · 33.9 KB
/
varfiles.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 Anthony J. Greenberg
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/// Read and write genetic variant files
/** \file
* \author Anthony J. Greenberg
* \copyright Copyright (c) 2017 Anthony J. Greenberg
* \version 0.1
*
* Definitions and interface documentation for classes that read and write various genetic variant file formats.
*
* Currently supported formats:
* - _plink_ BED
* - _plink_ TPED
* - VCF
* - Hapmap (.hmp.txt)
*
*
*/
#ifndef varfiles_hpp
#define varfiles_hpp
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <cstdint>
#include <limits>
#include "populations.hpp"
using std::fstream;
using std::string;
using std::vector;
using std::unordered_map;
using std::numeric_limits;
namespace sampFiles {
// Forward declarations
class VarFile;
class GbinFile;
class GbinFileI;
class GbinFileO;
class BedFile;
class BedFileI;
class BedFileO;
class GtxtFile;
class GtxtFileI;
class GtxtFileO;
class TpedFile;
class TpedFileI;
class TpedFileO;
class VcfFile;
class VcfFileI;
class VcfFileO;
class HmpFile;
class HmpFileI;
class HmpFileO;
/** \brief Buffer size
*
* Size of the buffer for reading files text files. I use it in functions that count the number of lines, for example. The buffer size (10M) is optimized for a MacBook Pro with an SSD. Other systems may perform better with a different value (e.g., if you have a spinning drive and more RAM you may want to experiemtn with increasing it).
*
*/
static const size_t BUF_SIZE = 10485760;
/// Machine \f$\epsilon\f$
const double EPS = numeric_limits<double>::epsilon();
/// pi
const double PI = 3.14159265358979323846264338328;
/** \brief Base variant file class
*
* Abstract base class for all the input/output formats. Cannot be initialized directly.
*/
class VarFile {
protected:
/// Variant file stream
fstream _varFile;
/// Default constructor (protected)
VarFile() {_varFile.exceptions(fstream::badbit); };
public:
/// Copy constructor
VarFile(const VarFile &in) = default;
/// Copy assignment
VarFile &operator=(const VarFile &in) = default;
/// Move constructor
VarFile(VarFile &&in) = default;
/// Move assignment
VarFile &operator=(VarFile &&in) = default;
/// Destructor
~VarFile(){if (_varFile.is_open()) _varFile.close(); };
/// \brief Open stream
virtual void open() = 0;
/// Close stream
virtual void close() = 0;
};
/** \brief Generic binary file base class
*
* Sets up streams for binary files. No support for header lines.
*
*/
class GbinFile : public VarFile {
protected:
/// File name
string _fileName;
/// Number of elements in a row
size_t _nCols;
/// Size of each element in bytes
size_t _elemSize;
public:
/// Default constructor
GbinFile() : VarFile(), _nCols{0}, _elemSize{sizeof(char)} {};
/** \brief Constructor with file name
*
* Throws ``Number of elements not divisible by the number of columns'' if the total number of elements is not divisible by the number of elements in a column.
*
* \param[in] fileName file name
* \param[in] nCols number of columns, or elements in a row
* \param[in] elemSize size of each element in bytes
*
*/
GbinFile(const string &fileName, const size_t &nCols, const size_t &elemSize) : VarFile(), _fileName{fileName}, _nCols{nCols}, _elemSize{elemSize} {};
/// Copy constructor
GbinFile(const GbinFile &in) = default;
/// Copy assignment
GbinFile &operator=(const GbinFile &in) = default;
/// Move constructor
GbinFile(GbinFile &&in) = default;
/// Move assignment
GbinFile &operator=(GbinFile &&in) = default;
/// Destructor
~GbinFile(){};
/// \brief Open stream (does nothing)
virtual void open() {};
/// Close stream
virtual void close();
};
/** \brief Binary file input class
*
* Reads binary files.
*
*/
class GbinFileI : GbinFile {
protected:
/** \brief Get number of rows in the binary file
*
* Requires knowledge of the number of elements in a row and their size in bytes. Throws a _string_ object ``Number of elements not divisible by row size'' if the total number of elements in the file is not divisible by the product of the number of columns and element size.
*
* \return number of rows
*/
virtual uint64_t _numLines();
public:
/// Default constructor
GbinFileI() : GbinFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including extension
* \param[in] nCols number of columns, or elements in a row
* \param[in] elemSize size of each element in bytes
*
*/
GbinFileI(const string &fileName, const size_t &nCols, const size_t &elemSize) : GbinFile(fileName, nCols, elemSize) {};
/// Copy constructor
GbinFileI(const GbinFileI &in) = default;
/// Copy assignment
GbinFileI &operator=(const GbinFileI &in) = default;
/// Move constructor
GbinFileI(GbinFileI &&in) = default;
/// Move assignment
GbinFileI &operator=(GbinFileI &&in) = default;
/// Destructor
~GbinFileI(){};
/// \brief Open stream to read
void open();
/** \brief Sample rows and save to a binary file
*
* Sample \f$n\f$ lines without replacement from the file represented by the current object and save to the `out` object. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of rows in the file.
*
* \param[in] out output object
* \param[in] n number of rows to sample
*
*/
void sample(GbinFileO &out, const uint64_t &n);
/// Number of rows in the object
uint64_t nlines() { return _numLines(); };
};
/** \brief Generic binary file output class
*
* Writes binary files.
*
*/
class GbinFileO : public GbinFile {
friend class GbinFileI;
protected:
public:
/// Default constructor
GbinFileO() : GbinFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including extension
* \param[in] nCols number of columns, or elements in a row
* \param[in] elemSize size of each element in bytes
*
*/
GbinFileO(const string &fileName, const size_t &nCols, const size_t &elemSize) : GbinFile(fileName, nCols, elemSize) {};
/// Copy constructor
GbinFileO(const GbinFileO &in) = default;
/// Copy assignment
GbinFileO &operator=(const GbinFileO &in) = default;
/// Move constructor
GbinFileO(GbinFileO &&in) = default;
/// Move assignment
GbinFileO &operator=(GbinFileO &&in) = default;
/// Destructor
~GbinFileO(){};
/// \brief Open stream to write
void open();
};
/** \brief BED file base class
*
* Sets up streams for the auxiliary files.
*/
class BedFile : public GbinFile {
protected:
/// Corresponding .fam file stream
fstream _famFile;
/// Corresponding .bim file stream
fstream _bimFile;
/// File name stub (minus the extension)
string _fileStub;
/** \brief Genotype bit masks
*
* Used to isolate each of the four genotypes (moving from the last bit pair) from the .bed two-bit genotype coding.
*
*/
static const vector<char> _masks;
/** \brief Genotype bit tests
*
* Used to test each of the four genotypes (moving from the last bit pair) from the .bed two-bit genotype coding for the possible states.
*
*/
static const unordered_map<char, string> _tests;
public:
/// Default constructor
BedFile();
/** \brief File name constructor
*
* \param[in] stubName file name minus the extension
*/
BedFile(const string &stubName);
/// Copy constructor
BedFile(const BedFile &in) = default;
/// Copy assignment
BedFile &operator=(const BedFile &in) = default;
/// Move constructor
BedFile(BedFile &&in) = default;
/// Move assignment
BedFile &operator=(BedFile &&in) = default;
/// Destructor
~BedFile();
/// Open stream (does nothing)
virtual void open() {};
/// Close stream
void close();
};
/** \brief BED file input class
*
* Reads BED files and the auxiliary files that come with them (.fam and .bim) as necessary. Only the SNP-major version is supported.
*
*/
class BedFileI : public BedFile {
protected:
/** \brief Get number of lines in the `_bimFile`
*
* Assumes Unix-like line endings. The result is equal to the number of SNPs.
*
* \return number of lines in `_bimFile`
*/
uint64_t _numLines();
/** \brief Get number of lines in the `_famFile`
*
* Assumes Unix-like line endings. The result is equal to the number of individuals.
*
* \return number of lines in `_famFile`
*/
uint64_t _famLines();
/** \brief Copy the .fam file and count number of lines
*
* Assumes Unix-like line endings. The result is equal to the number of individuals. The current object's .fam file is copied to the provided file stream, which should be open for raading. If not, the function throws a _string_ object ``Output .fam filestream not open''.
*
* \param[in] fam .fam file stream
*
* \return number of lines in `_famFile`
*/
uint64_t _famLines(fstream &fam);
/** \brief Between-SNP linkage disequilibrium (LD)
*
* Calculates two LD statistics (\f$ r^2 \f$ and \f$ D' \f$) between two SNPs from a BED file. Missing values are ignored. If there are fewer than three haplotypes with data present at both loci, the return values are -9. This value is also returned if one of the loci is monomorphic after taking out missing data at the other SNP.
* Minor (not necessarily derived) allele counts are also reported to enable downstream filtering. Note that the populations are assumed diploid and the counts are of haploid chromosomes (i.e. one homozygote yields count of 2).
*
* \param[in] snp1 first SNP
* \param[in] snp2 second SNP
* \param[in] N length of the genotype vector in bytes (four genotypes per byte)
* \param[in] pad number of bit pairs of padding in the last byte
* \param[out] rSq the \f$ r^2 \f$ estimate
* \param[out] Dprime the \f$ D' \f$ estimate
* \param[out] dcnt1 minor allele count at locus 1
* \param[out] dcnt2 minor allele count at locus 2
*
*/
void _ld(const char *snp1, const char *snp2, const size_t &N, const unsigned short &pad, double &rSq, double &Dprime, double &dcnt1, double &dcnt2);
/** \brief Between-SNP LD within populations
*
* Calculates two LD statistics (\f$ r^2 \f$ and \f$ D' \f$) between two SNPs from a BED file. Missing values are ignored. If there are fewer than three haplotypes with data present at both loci, the return values are -9. This value is also returned if one of the loci is monomorphic after taking out missing data at the other SNP.
* Minor (not necessarily derived) allele counts are also reported to enable downstream filtering. Note that the populations are assumed diploid and the counts are of haploid chromosomes (i.e. one homozygote yields count of 2). The values are calculted within each population as indicated by the `PopIndex` object. The results are returned in the supplied vectors, which are assumed to be of correct size. Since this is an internal function unexposed to the user, this is not chaecked to save on compuation steps.
* Care must be taken that the `char` arrays passed to the function have lengths compatible with the number of individuals indexed by `PopIndex`. This is not checked.
*
* \param[in] snp1 first SNP
* \param[in] snp2 second SNP
* \param[in] popID population index
* \param[out] rSq vector of \f$ r^2 \f$ estimates
* \param[out] Dprime vector of \f$ D' \f$ estimates
* \param[out] dcnt1 vector of minor allele counts at locus 1
* \param[out] dcnt2 vector of minor allele counts at locus 2
*
*/
void _ld(const char *snp1, const char *snp2, const PopIndex &popID, vector<double> &rSq, vector<double> &Dprime, vector<double> &dcnt1, vector<double> &dcnt2);
public:
/// Default constructor
BedFileI() : BedFile() {};
/** \brief File name constructor
*
* \param[in] stubName file name minus the extension
*/
BedFileI(const string &stubName) : BedFile(stubName) {};
/// Copy constructor
BedFileI(const BedFileI &in) = default;
/// Copy assignment
BedFileI &operator=(const BedFileI &in) = default;
/// Move constructor
BedFileI(BedFileI &&in) = default;
/// Move assignment
BedFileI &operator=(BedFileI &&in) = default;
/// Destructor
~BedFileI(){};
/// \brief Open stream to read
void open();
/** \brief Sample SNPs and save to BED file
*
* Sample \f$n\f$ SNPs without replacement from the file represented by the current object and save to the `out` object. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of SNPs in the file.
*
* \param[in] out output object
* \param[in] n number of SNPs to sample
*
*/
void sample(BedFileO &out, const uint64_t &n);
/** \brief Linkage disequilibrium among sampled sites
*
* Samples sequential pairs of SNPs and calculates two LD measures (\f$ r^2 \f$ and \f$ D' \f$). Saves to a file with the same name as the one preceding the .bed etc extensions, but adds _LD.tsv at the end. Each line is tab-delimited with the chromosome number (from the .bim file), between-SNP distance, non-reference allele count for each SNP, \f$ r^2 \f$, and \f$ D' \f$.
* Missing data are ignored (only pairwise-complete observations are included). If one of the SNPs is monomorphic or if the total number of pairwise present genotypes is fewer than three (exclusive), the LD measures are returned as -9 to indicate missing values.
*
* \param[in] n number of SNP pairs to sample
*/
void sampleLD(const uint64_t &n);
/** \brief LD among sampled sites within populations
*
* Samples sequential pairs of SNPs and calculates two LD measures (\f$ r^2 \f$ and \f$ D' \f$) within populations indicated by `PopIndex`. Saves to a file with the same name as the one preceding the .bed etc extensions, but adds _LD.tsv at the end. Each line is tab-delimited with the chromosome number (from the .bim file), between-SNP distance, non-reference allele count for each SNP, \f$ r^2 \f$, and \f$ D' \f$.
* Missing data are ignored (only pairwise-complete observations are included). If one of the SNPs is monomorphic or if the total number of pairwise present genotypes is fewer than three (exclusive), the LD measures are returned as -9 to indicate missing values.
*
* \param[in] popID population index
* \param[in] n number of SNP pairs to sample
*/
void sampleLD(const PopIndex &popID, const uint64_t &n);
/// Number of SNPs in the object
uint64_t nsnp() { return _numLines(); };
/// Number of individuals in the object
uint64_t nindiv() { return _famLines(); };
};
/** \brief BED file output class
*
* Writes to BED files and the auxiliary files that come with them (.fam and .bim) as necessary. Data are written in the SNP-major format.
*
*/
class BedFileO : public BedFile {
friend class BedFileI;
protected:
public:
/// Default constructor
BedFileO() : BedFile() {};
/** \brief File name constructor
*
* \param[in] stubName file name minus the extension
*/
BedFileO(const string &stubName) : BedFile(stubName) {};
/// Copy constructor
BedFileO(const BedFileO &in) = default;
/// Copy assignment
BedFileO &operator=(const BedFileO &in) = default;
/// Move constructor
BedFileO(BedFileO &&in) = default;
/// Move assignment
BedFileO &operator=(BedFileO &&in) = default;
/// Destructor
~BedFileO(){};
/// \brief Open stream to write
void open();
};
/** \brief Generic text file base class
*
* Sets up streams for text files. If specified, the first line can be considered a header and treated separately.
*
*/
class GtxtFile : public VarFile {
protected:
/// File name
string _fileName;
/// Is there a header?
bool _head;
public:
/// Default constructor
GtxtFile() : VarFile(), _head{false} {};
/** \brief Constructor with file name
*
* \param[in] fileName file name
*
*/
GtxtFile(const string &fileName) : VarFile(), _fileName{fileName}, _head{false} {};
/** \brief Constructor with file name and header indicator
*
* \param[in] fileName file name
* \param[in] head header presence
*
*/
GtxtFile(const string &fileName, const bool &head) : VarFile(), _fileName{fileName}, _head{head} {};
/// Copy constructor
GtxtFile(const GtxtFile &in) = default;
/// Copy assignment
GtxtFile &operator=(const GtxtFile &in) = default;
/// Move constructor
GtxtFile(GtxtFile &&in) = default;
/// Move assignment
GtxtFile &operator=(GtxtFile &&in) = default;
/// Destructor
~GtxtFile(){};
/// \brief Open stream (does nothing)
virtual void open() {};
/// Close stream
virtual void close();
};
/** \brief Text file input class
*
* Reads text files, skipping or copying the header as necessary.
*
*/
class GtxtFileI : GtxtFile {
protected:
/** \brief Get number of rows in the text file.
*
* Assumes Unix-like line endings. Header, if present, is not counted. Is overriden in some, but not all, derived classes.
*
* \return number of rows
*/
virtual uint64_t _numLines();
public:
/// Default constructor
GtxtFileI() : GtxtFile() {};
/** \brief File name constructor with header specification
*
* \param[in] fileName file name including extension
*/
GtxtFileI(const string &fileName) : GtxtFile(fileName) {};
/** \brief File name constructor with header specification
*
* \param[in] fileName file name including extension
* \param[in] head header presence
*/
GtxtFileI(const string &fileName, const bool &head) : GtxtFile(fileName, head) {};
/// Copy constructor
GtxtFileI(const GtxtFileI &in) = default;
/// Copy assignment
GtxtFileI &operator=(const GtxtFileI &in) = default;
/// Move constructor
GtxtFileI(GtxtFileI &&in) = default;
/// Move assignment
GtxtFileI &operator=(GtxtFileI &&in) = default;
/// Destructor
~GtxtFileI(){};
/// \brief Open stream to read
void open();
/** \brief Sample rows and save to a text file
*
* Sample \f$n\f$ lines without replacement from the file represented by the current object and save to the `out` object. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of rows in the file.
*
* \param[in] out output object
* \param[in] n number of rows to sample
* \param[in] headSkip skip header? Ignored if there is no header
*
*/
void sample(GtxtFileO &out, const uint64_t &n, const bool &headSkip);
/** \brief Sample rows and save export to a vector of strings
*
* Sample \f$n\f$ rows without replacement from the file represented by the current object and output a vector of strings. Each field separated by the specified delimiter is stored as an element of the vector. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of rows in the file. The output vector is erased if it is not empty.
*
* \param[in] n number of SNPs to sample
* \param[in] headSkip skip header? Ignored if there is no header
* \param[in] delim field delimiter
* \param[out] out output vector
*
*/
void sample(const uint64_t &n, const bool &headSkip, const char &delim, vector<string> &out);
/// Number of SNPs in the object
uint64_t nlines() { return _numLines(); };
};
/** \brief Generic text file output class
*
* Writes text files.
*
*/
class GtxtFileO : public GtxtFile {
friend class GtxtFileI;
protected:
public:
/// Default constructor
GtxtFileO() : GtxtFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including the extension
*/
GtxtFileO(const string &fileName) : GtxtFile(fileName) {};
/** \brief File name constructor with header specification
*
* \param[in] fileName file name including the extension
* \param[in] head header presence
*/
GtxtFileO(const string &fileName, const bool &head) : GtxtFile(fileName, head) {};
/// Copy constructor
GtxtFileO(const GtxtFileO &in) = default;
/// Copy assignment
GtxtFileO &operator=(const GtxtFileO &in) = default;
/// Move constructor
GtxtFileO(GtxtFileO &&in) = default;
/// Move assignment
GtxtFileO &operator=(GtxtFileO &&in) = default;
/// Destructor
~GtxtFileO(){};
/// \brief Open stream to write
void open();
};
/** \brief TPED file base class
*
* Sets up the stream for the corresponding .tfam file.
*/
class TpedFile : public GtxtFile {
protected:
/// Corresponding .tfam file stream
fstream _tfamFile;
/// File name stub (minus the extension)
string _fileStub;
public:
/// Default constructor
TpedFile() : GtxtFile() {_tfamFile.exceptions(fstream::badbit); };
/** \brief File name constructor
*
* \param[in] stubName file name minus the extension
*/
TpedFile(const string &stubName) : GtxtFile(stubName + ".tped"), _fileStub{stubName} {_tfamFile.exceptions(fstream::badbit); }; // no headers in .tped
/// Copy constructor
TpedFile(const TpedFile &in) = default;
/// Copy assignment
TpedFile &operator=(const TpedFile &in) = default;
/// Move constructor
TpedFile(TpedFile &&in) = default;
/// Move assignment
TpedFile &operator=(TpedFile &&in) = default;
/// Destructor
~TpedFile();
/// Open stream (does nothing)
virtual void open() {};
/// Close stream
void close();
};
/** \brief TPED file input class
*
* Reads TPED files and the corresponding .tfam files as necessary.
*
*/
class TpedFileI : public TpedFile {
protected:
/** \brief Get number of lines in the `_tfamFile`
*
* Assumes Unix-like line endings. The result is equal to the number of individuals. The `_tfamFile` should already be open for reading.
*
* \return number of lines in `_tfamFile`
*/
uint64_t _famLines();
/** \brief Copy the .tfam file and count number of lines
*
* Assumes Unix-like line endings. The result is equal to the number of individuals. The current object's .tfam file is copied to the provided file stream, which should already be open for writing. If not, the function throws a _string_ object ``Output .fam filestream not open''.
*
* \param[in] fam .tfam file stream
*
* \return number of lines in `_tfamFile`
*/
uint64_t _famLines(fstream &fam);
/** \brief Copy the .tfam file
*
* The current object's .tfam file is copied to the provided file stream, which should already be open for writing. If not, the function throws a _string_ object ``Output .fam filestream not open''.
*
* \param[in] fam .tfam file stream
*
*/
void _famCopy(fstream &fam);
/** \brief Get number of rows in the text file.
*
* Assumes Unix-like line endings. Header, if present, is not counted. Is overriden in some, but not all, derived classes.
*
* \return number of rows
*/
uint64_t _numLines();
public:
/// Default constructor
TpedFileI() : TpedFile() {};
/** \brief File name constructor
*
* \param[in] stubName file name minus the extension
*/
TpedFileI(const string &stubName) : TpedFile(stubName) {};
/// Copy constructor
TpedFileI(const TpedFileI &in) = default;
/// Copy assignment
TpedFileI &operator=(const TpedFileI &in) = default;
/// Move constructor
TpedFileI(TpedFileI &&in) = default;
/// Move assignment
TpedFileI &operator=(TpedFileI &&in) = default;
/// Destructor
~TpedFileI(){};
/// \brief Open stream to read
void open();
/** \brief Sample SNPs and save to BED file
*
* Sample \f$n\f$ SNPs without replacement from the file represented by the current object and save to the `out` object. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of SNPs in the file.
*
* \param[in] out output object
* \param[in] n number of SNPs to sample
*
*/
void sample(TpedFileO &out, const uint64_t &n);
/// Number of SNPs in the object
uint64_t nsnp() { return _numLines(); };
/// Number of individuals in the object
uint64_t nindiv() { return _famLines(); };
};
/** \brief TPED file output class
*
* Writes to TPED files and the corresponding .tfam files as necessary. Data are written in the SNP-major format.
*
*/
class TpedFileO : TpedFile {
friend class TpedFileI;
protected:
public:
/// Default constructor
TpedFileO() : TpedFile() {};
/** \brief File name constructor
*
* \param[in] stubName file name minus the extension
*/
TpedFileO(const string &stubName) : TpedFile(stubName) {};
/// Copy constructor
TpedFileO(const TpedFileO &in) = default;
/// Copy assignment
TpedFileO &operator=(const TpedFileO &in) = default;
/// Move constructor
TpedFileO(TpedFileO &&in) = default;
/// Move assignment
TpedFileO &operator=(TpedFileO &&in) = default;
/// Destructor
~TpedFileO(){};
/// \brief Open stream to write
void open();
};
/** \brief VCF file base class
*
* Sets up streams for VCF files. Any accompanying .idx files are ignored.
*
*/
class VcfFile : public GtxtFile {
protected:
public:
/// Default constructor
VcfFile() : GtxtFile() {};
/** \brief Constructor with file name
*
* \param[in] fileName file name
*
*/
VcfFile(const string &fileName) : GtxtFile(fileName) {};
/// Copy constructor
VcfFile(const VcfFile &in) = default;
/// Copy assignment
VcfFile &operator=(const VcfFile &in) = default;
/// Move constructor
VcfFile(VcfFile &&in) = default;
/// Move assignment
VcfFile &operator=(VcfFile &&in) = default;
/// Destructor
~VcfFile(){};
/// \brief Open stream (does nothing)
void open() {};
/// Close stream
void close();
};
/** \brief VCF file input class
*
* Reads VCF files, skipping or copying the header as necessary; .idx files are ignored.
*
*/
class VcfFileI : public VcfFile {
protected:
/** \brief Get number of SNPs in the VCF file.
*
* Assumes Unix-like line endings. Header is not counted.
*
* \return number of SNPs
*/
uint64_t _numLines();
public:
/// Default constructor
VcfFileI() : VcfFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including extension
*/
VcfFileI(const string &fileName) : VcfFile(fileName) {};
/// Copy constructor
VcfFileI(const VcfFileI &in) = default;
/// Copy assignment
VcfFileI &operator=(const VcfFileI &in) = default;
/// Move constructor
VcfFileI(VcfFileI &&in) = default;
/// Move assignment
VcfFileI &operator=(VcfFileI &&in) = default;
/// Destructor
~VcfFileI(){};
/// \brief Open stream to read
void open();
/** \brief Sample SNPs and save to VCF file
*
* Sample \f$n\f$ SNPs without replacement from the file represented by the current object and save to the `out` object. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of SNPs in the file.
*
* \param[in] out output object
* \param[in] n number of SNPs to sample
*
*/
void sample(VcfFileO &out, const uint64_t &n);
/// Number of SNPs in the object
uint64_t nsnp() { return _numLines(); };
};
/** \brief VCF file output class
*
* Writes VCF files.
*
*/
class VcfFileO : public VcfFile {
friend class VcfFileI;
protected:
public:
/// Default constructor
VcfFileO() : VcfFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including the extension
*/
VcfFileO(const string &fileName) : VcfFile(fileName) {};
/// Copy constructor
VcfFileO(const VcfFileO &in) = default;
/// Copy assignment
VcfFileO &operator=(const VcfFileO &in) = default;
/// Move constructor
VcfFileO(VcfFileO &&in) = default;
/// Move assignment
VcfFileO &operator=(VcfFileO &&in) = default;
/// Destructor
~VcfFileO(){};
/// \brief Open stream to write
void open();
};
/** \brief Hapmap (HMP) file base class
*
* Sets up streams for HMP files.
*
*/
class HmpFile : public GtxtFile {
protected:
public:
/// Default constructor
HmpFile() : GtxtFile() {};
/** \brief Constructor with file name
*
* \param[in] fileName file name
*
*/
HmpFile(const string &fileName) : GtxtFile(fileName) {};
/// Copy constructor
HmpFile(const HmpFile &in) = default;
/// Copy assignment
HmpFile &operator=(const HmpFile &in) = default;
/// Move constructor
HmpFile(HmpFile &&in) = default;
/// Move assignment
HmpFile &operator=(HmpFile &&in) = default;
/// Destructor
~HmpFile(){};
/// \brief Open stream (does nothing)
virtual void open() {};
/// Close stream
virtual void close();
};
/** \brief HMP file input class
*
* Reads HMP files, skipping or copying the header as necessary.
*
*/
class HmpFileI : HmpFile {
protected:
/** \brief Get number of SNPs in the HMP file.
*
* Assumes Unix-like line endings. Header is not counted.
*
* \return number of SNPs
*/
uint64_t _numLines();
public:
/// Default constructor
HmpFileI() : HmpFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including extension
*/
HmpFileI(const string &fileName);
/// Copy constructor
HmpFileI(const HmpFileI &in) = default;
/// Copy assignment
HmpFileI &operator=(const HmpFileI &in) = default;
/// Move constructor
HmpFileI(HmpFileI &&in) = default;
/// Move assignment
HmpFileI &operator=(HmpFileI &&in) = default;
/// Destructor
~HmpFileI(){};
/// \brief Open stream to read
void open();
/** \brief Sample SNPs and save to HMP file
*
* Sample \f$n\f$ SNPs without replacement from the file represented by the current object and save to the `out` object. Uses Vitter's \cite vitter87a method. Number of samples has to be smaller that the number of SNPs in the file.
*
* \param[in] out output object
* \param[in] n number of SNPs to sample
*
*/
void sample(HmpFileO &out, const uint64_t &n);
/// Number of SNPs in the object
uint64_t nsnp() { return _numLines(); };
};
/** \brief HMP file output class
*
* Writes HMP files.
*
*/
class HmpFileO : public HmpFile {
friend class HmpFileI;
protected:
public:
/// Default constructor
HmpFileO() : HmpFile() {};
/** \brief File name constructor
*
* \param[in] fileName file name including the extension
*/
HmpFileO(const string &fileName) : HmpFile(fileName) {};
/// Copy constructor
HmpFileO(const HmpFileO &in) = default;
/// Copy assignment
HmpFileO &operator=(const HmpFileO &in) = default;
/// Move constructor
HmpFileO(HmpFileO &&in) = default;
/// Move assignment
HmpFileO &operator=(HmpFileO &&in) = default;
/// Destructor
~HmpFileO(){};
/// \brief Open stream to write
void open();
};
}