| 1 |
#include "include/pgenlib_read.h" |
| 2 |
#include "include/pgenlib_write.h" |
| 3 |
|
| 4 |
// #define SUBSET_TEST |
| 5 |
|
| 6 |
int32_t main(int32_t argc, char** argv) { |
| 7 |
#ifdef __cplusplus |
| 8 |
using namespace plink2; |
| 9 |
#endif |
| 10 |
PglErr reterr = kPglRetSuccess; |
| 11 |
unsigned char* pgfi_alloc = nullptr; |
| 12 |
unsigned char* pgr_alloc = nullptr; |
| 13 |
unsigned char* spgw_alloc = nullptr; |
| 14 |
uintptr_t* genovec = nullptr; |
| 15 |
uintptr_t* raregeno = nullptr; |
| 16 |
uintptr_t* sample_include = nullptr; |
| 17 |
uint32_t* sample_include_cumulative_popcounts = nullptr; |
| 18 |
uint32_t* difflist_sample_ids = nullptr; |
| 19 |
FILE* outfile = nullptr; |
| 20 |
PgenHeaderCtrl header_ctrl; |
| 21 |
STPgenWriter spgw; |
| 22 |
uint32_t write_sample_ct; |
| 23 |
PgenFileInfo pgfi; |
| 24 |
PgenReader pgr; |
| 25 |
PreinitPgfi(&pgfi); |
| 26 |
PreinitPgr(&pgr); |
| 27 |
PreinitSpgw(&spgw); |
| 28 |
{ |
| 29 |
if ((argc < 3) || (argc > 6)) { |
| 30 |
fputs( |
| 31 |
"Usage:\n" |
| 32 |
"pgen_compress [-i] <input .bed or .pgen> <output filename> [sample_ct]\n" |
| 33 |
" * sample_ct is required when loading a .bed file\n" |
| 34 |
" * -i causes the index to be saved to a separate .pgen.pgi file (usually it's\n" |
| 35 |
" embedded at the front of the .pgen); this is compatible with fully\n" |
| 36 |
" sequential .pgen writing\n" |
| 37 |
"pgen_compress -u <input .pgen> <output .bed>\n" |
| 38 |
, stdout); |
| 39 |
goto main_ret_INVALID_CMDLINE; |
| 40 |
} |
| 41 |
const uint32_t write_separate_index = (argv[1][0] == '-') && (argv[1][1] == 'i') && (argv[1][2] == '\0'); |
| 42 |
const uint32_t decompress = (argv[1][0] == '-') && (argv[1][1] == 'u') && (argv[1][2] == '\0'); |
| 43 |
const uint32_t input_idx = 1 + write_separate_index + decompress; |
| 44 |
uint32_t sample_ct = 0xffffffffU; |
| 45 |
if (S_CAST(uint32_t, argc) == input_idx + 3) { |
| 46 |
if (ScanPosintDefcap(argv[input_idx + 2], &sample_ct)) { |
| 47 |
fprintf(stderr, "error: invalid sample_ct\n"); |
| 48 |
goto main_ret_INVALID_CMDLINE; |
| 49 |
} |
| 50 |
} |
| 51 |
char errstr_buf[kPglErrstrBufBlen]; |
| 52 |
uintptr_t cur_alloc_cacheline_ct; |
| 53 |
reterr = PgfiInitPhase1(argv[input_idx], nullptr, 0xffffffffU, sample_ct, &header_ctrl, &pgfi, &cur_alloc_cacheline_ct, errstr_buf); |
| 54 |
if (reterr) { |
| 55 |
fputs(errstr_buf, stderr); |
| 56 |
goto main_ret_1; |
| 57 |
} |
| 58 |
sample_ct = pgfi.raw_sample_ct; |
| 59 |
if (!sample_ct) { |
| 60 |
fprintf(stderr, "error: sample_ct == 0\n"); |
| 61 |
goto main_ret_INVALID_CMDLINE; |
| 62 |
} |
| 63 |
const uint32_t variant_ct = pgfi.raw_variant_ct; |
| 64 |
if (!variant_ct) { |
| 65 |
fprintf(stderr, "error: variant_ct == 0\n"); |
| 66 |
goto main_ret_INVALID_CMDLINE; |
| 67 |
} |
| 68 |
if (cachealigned_malloc(cur_alloc_cacheline_ct * kCacheline, &pgfi_alloc)) { |
| 69 |
goto main_ret_NOMEM; |
| 70 |
} |
| 71 |
uint32_t max_vrec_width; |
| 72 |
// todo: test block-fread |
| 73 |
reterr = PgfiInitPhase2(header_ctrl, 0, 0, 0, 0, variant_ct, &max_vrec_width, &pgfi, pgfi_alloc, &cur_alloc_cacheline_ct, errstr_buf); |
| 74 |
if (reterr) { |
| 75 |
fputs(errstr_buf, stderr); |
| 76 |
goto main_ret_1; |
| 77 |
} |
| 78 |
if (cachealigned_malloc(cur_alloc_cacheline_ct * kCacheline, &pgr_alloc)) { |
| 79 |
goto main_ret_NOMEM; |
| 80 |
} |
| 81 |
|
| 82 |
// modify this when trying block-fread |
| 83 |
reterr = PgrInit(argv[input_idx], max_vrec_width, &pgfi, &pgr, pgr_alloc); |
| 84 |
if (reterr) { |
| 85 |
fprintf(stderr, "PgrInit error %u\n", S_CAST(uint32_t, reterr)); |
| 86 |
goto main_ret_1; |
| 87 |
} |
| 88 |
|
| 89 |
if (S_CAST(uint32_t, argc) == input_idx + 3) { |
| 90 |
printf("%u variant%s detected.\n", variant_ct, (variant_ct == 1)? "" : "s"); |
| 91 |
} else { |
| 92 |
printf("%u variant%s and %u sample%s detected.\n", variant_ct, (variant_ct == 1)? "" : "s", sample_ct, (sample_ct == 1)? "" : "s"); |
| 93 |
} |
| 94 |
if (cachealigned_malloc(NypCtToVecCt(sample_ct) * kBytesPerVec, &genovec)) { |
| 95 |
goto main_ret_NOMEM; |
| 96 |
} |
| 97 |
if (decompress) { |
| 98 |
outfile = fopen(argv[3], FOPEN_WB); |
| 99 |
if (!outfile) { |
| 100 |
goto main_ret_OPEN_FAIL; |
| 101 |
} |
| 102 |
PgrSampleSubsetIndex pssi; |
| 103 |
PgrClearSampleSubsetIndex(&pgr, &pssi); |
| 104 |
const uintptr_t final_mask = (k1LU << ((sample_ct % kBitsPerWordD2) * 2)) - k1LU; |
| 105 |
const uint32_t final_widx = NypCtToWordCt(sample_ct) - 1; |
| 106 |
const uint32_t variant_byte_ct = (sample_ct + 3) / 4; |
| 107 |
fwrite("l\x1b\x01", 3, 1, outfile); |
| 108 |
for (uint32_t vidx = 0; vidx < variant_ct; ) { |
| 109 |
reterr = PgrGet(nullptr, pssi, sample_ct, vidx, &pgr, genovec); |
| 110 |
if (reterr) { |
| 111 |
fprintf(stderr, "\nread error %u, vidx=%u\n", S_CAST(uint32_t, reterr), vidx); |
| 112 |
goto main_ret_1; |
| 113 |
} |
| 114 |
PgrPlink2ToPlink1InplaceUnsafe(sample_ct, genovec); |
| 115 |
if (final_mask) { |
| 116 |
genovec[final_widx] &= final_mask; |
| 117 |
} |
| 118 |
fwrite(genovec, variant_byte_ct, 1, outfile); |
| 119 |
++vidx; |
| 120 |
if (!(vidx % 100000)) { |
| 121 |
printf("\r%u.%um variants decompressed.", vidx / 1000000, (vidx / 100000) % 10); |
| 122 |
fflush(stdout); |
| 123 |
} |
| 124 |
} |
| 125 |
if (fclose_null(&outfile)) { |
| 126 |
goto main_ret_WRITE_FAIL; |
| 127 |
} |
| 128 |
printf("\n"); |
| 129 |
goto main_ret_1; |
| 130 |
} |
| 131 |
#ifdef SUBSET_TEST |
| 132 |
// write_sample_ct = sample_ct - 3; |
| 133 |
write_sample_ct = 3; |
| 134 |
#else |
| 135 |
write_sample_ct = sample_ct; |
| 136 |
#endif |
| 137 |
// Demonstrate that, when write_separate_index is true, variant_ct_limit |
| 138 |
// can be an overestimate. |
| 139 |
// Also demonstrate automatic 8-bit -> 4-bit index compaction when |
| 140 |
// write_separate_index is true, we declare that hardcall-phase is present, |
| 141 |
// but we never write any hardcall-phase data. |
| 142 |
uint32_t max_vrec_len; |
| 143 |
reterr = SpgwInitPhase1(argv[input_idx + 1], nullptr, nullptr, write_separate_index? (variant_ct * 2) : variant_ct, write_sample_ct, 0, write_separate_index? kPgenWriteSeparateIndex : kPgenWriteBackwardSeek, write_separate_index? kfPgenGlobalHardcallPhasePresent : kfPgenGlobal0, 2, &spgw, &cur_alloc_cacheline_ct, &max_vrec_len); |
| 144 |
if (reterr) { |
| 145 |
fprintf(stderr, "compression phase 1 error %u\n", S_CAST(uint32_t, reterr)); |
| 146 |
goto main_ret_1; |
| 147 |
} |
| 148 |
if (cachealigned_malloc(cur_alloc_cacheline_ct * kCacheline, &spgw_alloc)) { |
| 149 |
goto main_ret_NOMEM; |
| 150 |
} |
| 151 |
SpgwInitPhase2(max_vrec_len, &spgw, spgw_alloc); |
| 152 |
|
| 153 |
const uint32_t max_simple_difflist_len = sample_ct / kBitsPerWordD2; |
| 154 |
const uint32_t max_returned_difflist_len = 2 * (sample_ct / kPglMaxDifflistLenDivisor); |
| 155 |
const uint32_t max_difflist_len = 2 * (write_sample_ct / kPglMaxDifflistLenDivisor); |
| 156 |
if (cachealigned_malloc(RoundUpPow2((max_returned_difflist_len + 3) / 4, kCacheline), &raregeno) || |
| 157 |
cachealigned_malloc(RoundUpPow2((sample_ct + 7) / 8, kCacheline), &sample_include) || |
| 158 |
cachealigned_malloc(RoundUpPow2((1 + (sample_ct / kBitsPerWord)) * sizeof(int32_t), kCacheline), &sample_include_cumulative_popcounts) || |
| 159 |
cachealigned_malloc(RoundUpPow2((max_returned_difflist_len + 1) * sizeof(int32_t), kCacheline), &difflist_sample_ids)) { |
| 160 |
goto main_ret_NOMEM; |
| 161 |
} |
| 162 |
#ifdef SUBSET_TEST |
| 163 |
fill_ulong_zero(BitCtToWordCt(sample_ct), sample_include); |
| 164 |
SetBit(123, sample_include); |
| 165 |
SetBit(127, sample_include); |
| 166 |
SetBit(320, sample_include); |
| 167 |
// ClearBit(123, sample_include); |
| 168 |
// ClearBit(127, sample_include); |
| 169 |
// ClearBit(320, sample_include); |
| 170 |
#else |
| 171 |
SetAllBits(sample_ct, sample_include); |
| 172 |
#endif |
| 173 |
FillCumulativePopcounts(sample_include, 1 + (sample_ct / kBitsPerWord), sample_include_cumulative_popcounts); |
| 174 |
PgrSampleSubsetIndex pssi; |
| 175 |
PgrSetSampleSubsetIndex(sample_include_cumulative_popcounts, &pgr, &pssi); |
| 176 |
for (uint32_t vidx = 0; vidx < variant_ct; ) { |
| 177 |
uint32_t difflist_common_geno; |
| 178 |
uint32_t difflist_len; |
| 179 |
reterr = PgrGetDifflistOrGenovec(sample_include, pssi, write_sample_ct, max_simple_difflist_len, vidx, &pgr, genovec, &difflist_common_geno, raregeno, difflist_sample_ids, &difflist_len); |
| 180 |
if (reterr) { |
| 181 |
fprintf(stderr, "\nread error %u, vidx=%u\n", S_CAST(uint32_t, reterr), vidx); |
| 182 |
goto main_ret_1; |
| 183 |
} |
| 184 |
if (difflist_common_geno == 0xffffffffU) { |
| 185 |
ZeroTrailingBits(write_sample_ct * 2, genovec); |
| 186 |
reterr = SpgwAppendBiallelicGenovec(genovec, &spgw); |
| 187 |
} else if (difflist_len <= max_difflist_len) { |
| 188 |
ZeroTrailingBits(2 * difflist_len, raregeno); |
| 189 |
difflist_sample_ids[difflist_len] = write_sample_ct; |
| 190 |
reterr = SpgwAppendBiallelicDifflistLimited(raregeno, difflist_sample_ids, difflist_common_geno, difflist_len, &spgw); |
| 191 |
} else { |
| 192 |
PgrDifflistToGenovecUnsafe(raregeno, difflist_sample_ids, difflist_common_geno, write_sample_ct, difflist_len, genovec); |
| 193 |
ZeroTrailingBits(write_sample_ct * 2, genovec); |
| 194 |
reterr = SpgwAppendBiallelicGenovec(genovec, &spgw); |
| 195 |
} |
| 196 |
if (reterr) { |
| 197 |
fprintf(stderr, "\ncompress/write error %u, vidx=%u\n", S_CAST(uint32_t, reterr), vidx); |
| 198 |
goto main_ret_1; |
| 199 |
} |
| 200 |
++vidx; |
| 201 |
if (!(vidx % 100000)) { |
| 202 |
printf("\r%u.%um variants compressed.", vidx / 1000000, (vidx / 100000) % 10); |
| 203 |
fflush(stdout); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
printf("\n"); |
| 208 |
|
| 209 |
reterr = SpgwFinish(&spgw); |
| 210 |
while (0) { |
| 211 |
main_ret_NOMEM: |
| 212 |
reterr = kPglRetNomem; |
| 213 |
break; |
| 214 |
main_ret_OPEN_FAIL: |
| 215 |
reterr = kPglRetOpenFail; |
| 216 |
break; |
| 217 |
main_ret_WRITE_FAIL: |
| 218 |
reterr = kPglRetWriteFail; |
| 219 |
break; |
| 220 |
main_ret_INVALID_CMDLINE: |
| 221 |
reterr = kPglRetInvalidCmdline; |
| 222 |
break; |
| 223 |
} |
| 224 |
main_ret_1: |
| 225 |
CleanupPgr(&pgr, &reterr); |
| 226 |
CleanupPgfi(&pgfi, &reterr); |
| 227 |
CleanupSpgw(&spgw, &reterr); |
| 228 |
if (pgfi_alloc) { |
| 229 |
aligned_free(pgfi_alloc); |
| 230 |
} |
| 231 |
if (pgr_alloc) { |
| 232 |
aligned_free(pgr_alloc); |
| 233 |
} |
| 234 |
if (spgw_alloc) { |
| 235 |
aligned_free(spgw_alloc); |
| 236 |
} |
| 237 |
if (genovec) { |
| 238 |
aligned_free(genovec); |
| 239 |
} |
| 240 |
if (raregeno) { |
| 241 |
aligned_free(raregeno); |
| 242 |
} |
| 243 |
if (sample_include) { |
| 244 |
aligned_free(sample_include); |
| 245 |
} |
| 246 |
if (sample_include_cumulative_popcounts) { |
| 247 |
aligned_free(sample_include_cumulative_popcounts); |
| 248 |
} |
| 249 |
if (difflist_sample_ids) { |
| 250 |
aligned_free(difflist_sample_ids); |
| 251 |
} |
| 252 |
if (outfile) { |
| 253 |
fclose(outfile); |
| 254 |
} |
| 255 |
return S_CAST(int32_t, reterr); |
| 256 |
} |