-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.cpp
1786 lines (1656 loc) · 52.5 KB
/
model.cpp
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
// /*************************************************************************
// *
// * Baylor College of Medicine CONFIDENTIAL
// * __________________
// *
// * [2016] - [20**] Baylor College of Medicine & Yongtao Guan
// * All Rights Reserved.
// *
// * NOTICE: All information contained herein is, and remains
// * the property of Baylor College of Medicine and the creator.
// * The intellectual and technical concepts contained
// * herein are proprietary to Baylor College of Medicine and the creator
// * and may be covered by U.S. and Foreign Patents, patents in process,
// * and are protected by trade secret or copyright law.
// * Dissemination of this information or reproduction of this material
// * is strictly forbidden unless prior written permission is obtained
// * from Baylor College of Medicine.
#include <fstream>
#include <iostream>
#include <array>
#include <unordered_map>
#include <string.h>
#include <zlib.h>
#include <string>
#include <cmath>
#include <complex>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <sys/vfs.h>
#include <sys/time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_permutation.h>
#include "gsl/gsl_rng.h"
#include "gsl/gsl_multimin.h"
#include "gsl/gsl_blas.h"
#include "gsl/gsl_linalg.h"
#include "gsl/gsl_randist.h"
#include "gsl/gsl_permute.h"
using namespace std;
#define dd 2
void operation(double a[][dd], int, double);
void compute_stats_unequal_datasize(vector<string> &vfn, string fout, string method, int withgc, int ncol, int n_bin, string bin_file);
void compute_stats2_unequal_datasize(vector<string> &vfn1, vector<string> &vfn2, string fout, string method, int ncol, int n_bin, string bin_file);
void adio(string, double*, int *);
int compare_doubles(const void * a, const void *b);
int compare_doubles_2D(const void *pa, const void *pb);
double * select_quantiles(double * xx, long nx, int ng);
double ** select_quantiles_all(double ** xx, long nx, int ng, int ncol);
int quickselect(double * brk, int ng, double num);
double hellinger(unordered_map<string, double> wx, unordered_map<string, double> wy);
double cosine(unordered_map<string, double> wx, unordered_map<string, double> wy);
void quantify_reads(string fin, string fout, int dim, int w, int step, int nm, int, int, double alpha);
void regress_gc(string fin, string fout);
void regress_gc_residual(string fin, string beta_file, string fout);
void regress_gc_beta(string fin);
//global gsl variable in use;
const gsl_rng_type * gslType;
gsl_rng * gsl_r;
//global log file;
FILE * flog;
//global length;
int seqLen;
double now()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.;
}
int main(int argc, char ** argv)
{
string fin;
string fin2;
string fout;
string fnlog("\0");
string method("h2");
vector<string> vfn;
vector<string> vfn2;
int dim = 75;
int w = dim; //sliding window size
int step = w; //sliding window step
int nm = 1;
double alpha = 1.0;
int adaptor_size = 0;
int cad = 0;
int cad2 = 0;
int qtf = 0;
int rgc = 0;
int rgc_beta = 0;
int rgc_res = 0;
int tphred = 20;
int withgc = 0; //toggle for whether to compute ad distance for gc contents.
int ncol=4;
int n_bin=15;
string bin_file = "";
string beta_file = "";
unsigned long seed = time(NULL);
for(int i = 1; i < argc; i++)
{
string str;
string opt;
if(i>1 && argv[i][0] != '-')
continue;
if(i==1 && argv[i][0] == '-')
{
printf("./nubeam cad or ./nubeam qtf \n");
exit(0);
}
str.assign(argv[i]);
opt.assign(str, 0, str.length());
if(str.compare("cad") == 0) {
cad = 1;
}
else if(str.compare("cad2") == 0) {
cad2 = 1;
}
else if(str.compare("qtf") == 0) {
qtf = 1;
}
else if(str.compare("rgc") == 0) {
rgc = 1;
}
else if(str.compare("rgc_beta") == 0) {
rgc_beta = 1;
}
else if(str.compare("rgc_res") == 0) {
rgc_res = 1;
}
else if(str.compare("-h") == 0) {
if(qtf == 1)
{
printf("./nubeam quad [-idonwh]\n");
printf("compute quadriples for reads in (gzipped) fastq format.\n");
printf("produces prefix.quad.gz (gc content is within) and prefix.quad.log.\n");
printf("-a : adaptor size (default 0)\n");
printf("-i : input filename\n");
printf("-o : output prefix\n");
printf("-d : dimension, or length of the reads (default d=75).\n");
printf("-n : number of missing nucleotide allowed.\n");
printf("-w : sliding window size (default w=d).\n");
printf("-S : sliding window step (default S=w).\n");
printf("-f : value, plus 33 is the PHRED quality value of fastq reads.\n");
printf("-h : print this help\n");
}
else if(cad == 1)
{
printf("./nubeam cad [-iomsh]\n");
printf("compute pariwise distances of a set; the inputs are nubeam qtf outputs.\n");
printf("produces prefix.cad.log.\n");
printf("-i : specifies input file which is output of nubeam.\n");
printf("-o : output prefix (prefix.log contains pairwise distance matrix)\n");
printf("-m : choice of methods: h2, cos.\n");
printf("-s : sampling s many entries to perform test\n");
printf("-c : designating the number of columns of scores\n");
printf("-b : designating the number of bins per column of scores\n");
// printf("-a : allow negative entres or not.\n");
printf("-h : print this help\n");
}
else if(cad2 == 1)
{
printf("./nubeam cad2 [-ijomsh]\n");
printf("compute pariwise cross distances between two sets; the inputs are nubeam qtf outputs.\n");
printf("produces prefix.cad2.log.\n");
printf("-i : specifies input file of first set, which is output of nubeam.\n");
printf("-j : specifies input file of second set, which is output of nubeam.\n");
printf("-o : output prefix (prefix.log contains pairwise distance matrix)\n");
printf("-m : choice of methods: h2, ad, ks, ad1.\n");
printf("-s : sampling s many entries to perform test\n");
printf("-c : designating the number of columns of scores\n");
printf("-b : designating the number of bins per column of scores\n");
printf("-bf : the file describing how to partition the bins\n");
// printf("-a : allow negative entres or not.\n");
printf("-h : print this help\n");
}
else if(rgc == 1)
{
printf("./nubeam nogc [-ioh]\n");
printf("regress out gc contents from read quantification and output residuals.\n");
printf("produces prefix.nogc.gz and prefix.nogc.log.\n");
printf("-i : input file name.\n");
printf("-o : output prefix.\n");
printf("-h : print this help\n");
}
else if(rgc_beta == 1)
{
printf("./nubeam rgc_beta [-ioh]\n");
printf("perform regression on gc contents from read quantification and output regression coefficients.\n");
printf("produces prefix.beta.log.\n");
printf("-i : input file name.\n");
printf("-o : output prefix.\n");
printf("-h : print this help\n");
}
else if(rgc_res == 1)
{
printf("./nubeam rgc_res [-ioh beta]\n");
printf("regress out gc contents from read quantification and output residuals.\n");
printf("produces prefix.nogc.gz and prefix.nogc.log.\n");
printf("-i : input file name.\n");
printf("-beta : beta file name.\n");
printf("-o : output prefix.\n");
printf("-h : print this help\n");
}
else {
printf("./nubeam [qtf, rgc_beta, rgc_res, cad, cad2]\n");
printf("For example, use ./nubeam qtf -h for more options.\n");
}
exit(0);
}
else if (str.compare("-a") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
adaptor_size = atoi(argv[i+1]);
}
else if (str.compare("-in") == 0 || str.compare("-i") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
fin.clear();
fin.assign(argv[i+1]);
vfn.push_back(fin);
}
else if (str.compare("-j") == 0 ) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
fin2.clear();
fin2.assign(argv[i+1]);
vfn2.push_back(fin2);
}
else if (str.compare("-out") == 0 || str.compare("-o") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
fout.clear();
fout.assign(argv[i+1]);
fnlog.assign(fout);
}
else if (str.compare("-d") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
dim = w = step = atoi(argv[i+1]);
}
else if (str.compare("-method") == 0 || str.compare("-m") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
method.clear();
method.assign(argv[i+1]);
if(method.compare("h2") != 0 && method.compare("cos") !=0)
{
printf("wrong augument after option.\n");
exit(0);
}
}
else if (str.compare("-nm") == 0 || str.compare("-n") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
nm = atoi(argv[i+1]);
}
else if (str.compare("-w") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
w = step = atoi(argv[i+1]);
}
else if (str.compare("-S") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
step = atoi(argv[i+1]);
}
else if (str.compare("-f") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
tphred = atoi(argv[i+1]);
}
else if (str.compare("-R") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
seed = atol(argv[i+1]);
}
else if (str.compare("-withgc") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
withgc = atoi(argv[i+1]);
}
else if (str.compare("-c") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
ncol = atoi(argv[i+1]);
}
else if (str.compare("-b") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
if(!isdigit(argv[i+1][0]))
{
printf("wrong augument after option.\n");
exit(0);
}
n_bin = atoi(argv[i+1]);
}
else if (str.compare("-bf") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
bin_file.assign(argv[i+1]);
}
else if (str.compare("-beta") == 0) {
if(argv[i+1] == NULL || argv[i+1][0] == '-') continue;
beta_file.assign(argv[i+1]);
}
else
{
fprintf(stderr,"Bad option %s\n", argv[i]);
exit(0);
}
}
gsl_rng_env_setup();
gslType = gsl_rng_default;
gsl_r = gsl_rng_alloc (gslType);
gsl_rng_set(gsl_r, seed);
//set up random number generator;
if(fnlog.size()==0)
{
char temp[1000];
sprintf(temp, "%ld", seed);
fnlog.assign(temp);
}
seqLen = dim;
if(qtf) {
fnlog.append(".quad.log");
printf("Log file name: %s \n", fnlog.c_str());
flog = fopen(fnlog.c_str(), "w");
if(flog == NULL)
printf("can't open log file %s to write, proceed anyway.", fnlog.c_str());
quantify_reads(fin, fout, dim, w, step, nm, adaptor_size, tphred, alpha);
fclose(flog);
return 1;
}
if(rgc) {
fnlog.append(".nogc.log");
printf("Log file name: %s \n", fnlog.c_str());
flog = fopen(fnlog.c_str(), "w");
if(flog == NULL)
printf("can't open log file %s to write, proceed anyway.", fnlog.c_str());
regress_gc(fin, fout);
fclose(flog);
return 1;
}
if(rgc_beta) {
fnlog.append(".beta.log");
printf("Log file name: %s \n", fnlog.c_str());
flog = fopen(fnlog.c_str(), "w");
if(flog == NULL) {
printf("can't open log file %s to write.", fnlog.c_str());
exit(0);
}
regress_gc_beta(fin);
fclose(flog);
return 1;
}
if(rgc_res) {
fnlog.append(".nogc.log");
printf("Log file name: %s \n", fnlog.c_str());
flog = fopen(fnlog.c_str(), "w");
if(flog == NULL)
printf("can't open log file %s to write, proceed anyway.", fnlog.c_str());
regress_gc_residual(fin, beta_file, fout);
fclose(flog);
return 1;
}
if(cad) {
fnlog.append(".cad.log");
printf("Log file name: %s \n", fnlog.c_str());
flog = fopen(fnlog.c_str(), "w");
if(flog == NULL)
printf("can't open log file %s to write, proceed anyway.", fnlog.c_str());
compute_stats_unequal_datasize(vfn, fout, method, withgc, ncol, n_bin, bin_file);
fclose(flog);
return 1;
}
if(cad2) {
fnlog.append(".cad2.log");
printf("Log file name: %s \n", fnlog.c_str());
flog = fopen(fnlog.c_str(), "w");
if(flog == NULL)
printf("can't open log file %s to write, proceed anyway.", fnlog.c_str());
compute_stats2_unequal_datasize(vfn, vfn2, fout, method, ncol, n_bin, bin_file);
fclose(flog);
return 1;
}
return 0;
}
void quantify_reads(string fin, string fout, int dim, int w, int step, int nm, int adaptor, int tphred, double alpha)
{
fprintf(flog, "### quad -d %d -w %d -s %d -n %d -a %d -A %f -o %s -i %s \n", dim, w, step, nm, adaptor, alpha, fout.c_str(), fin.c_str());
map<int, char> i2c;
i2c[0] = 'A';
i2c[1] = 'T';
i2c[2] = 'C';
i2c[3] = 'G';
map<char, int> c2i;
c2i['A'] = 0;
c2i['T'] = 1;
c2i['C'] = 2;
c2i['G'] = 3;
c2i['N'] = -1;
map<char, char> c2c;
c2c['A'] = 'T';
c2c['T'] = 'A';
c2c['C'] = 'G';
c2c['G'] = 'C';
c2c['N'] = 'N';
map<char, char> c2cp;
c2cp['A'] = 'C';
c2cp['T'] = 'G';
c2cp['C'] = 'A';
c2cp['G'] = 'T';
c2cp['N'] = 'N';
string f1(fout);
f1.append(".quad.gz");
gzFile fp1 = gzopen(f1.c_str(), "wb");
if(fp1 == NULL)
{
printf("can't open %s file to write\n", fout.c_str());
exit(0);
}
gzFile fgz = NULL;
fgz = gzopen(fin.c_str(), "rb");
if(fgz == NULL) {
printf("can't open %s file to read. \n", fin.c_str());
exit(0);
}
char * first = new char[1024];
char * tline = new char[1024];
char * tscore = new char[1024];
gzgets(fgz, first, 1024);
gzgets(fgz, tline, 1024);
gzgets(fgz, tscore, 1024);
gzgets(fgz, tscore, 1024);
int c1, c2, c3;
c1=c2=c3=0;
while (tline != NULL)
{
char * line = tline + adaptor;
char * score = tscore + adaptor;
int skip = 0;
dim = strlen(line) - 1;
if((dim + 1) < (w + adaptor))
skip = 1;
if(skip == 0) {
int temp = 0;
for(int k = 0; k < dim; k++)
temp += (int) (toupper(line[k]) == 'N');
if(temp > nm)
skip = 2;
}
if(skip == 0) {
int lowscore = 0;
for(int k = 0; k < dim; k++)
{
int phred = (int) score[k] - 33;
if(phred < tphred)
lowscore ++;
if(lowscore > 1) break;
}
if(lowscore > 1)
skip = 3;
}
if(skip == 0) {
for(int s = 0; s <= (dim-w); s+=step)
{
string zz("");
zz.append(line, s, w);
// write gc contents.
int nc[4];
int na = 0;
memset(nc, 0, 4*sizeof(int));
// start the for loop
for (int k = 0; k < w; k++) {
// all capital letters
zz[k] = toupper(zz[k]);
// get GC contents
int t1=c2i[zz[k]];
if(t1>=0) nc[t1]++;
else na++;
}
// reverse complement of zz
string zz_rc(w, 'N');
for (int k = 0; k < w; k++) {
// get reverse complement
zz_rc[k] = c2c[zz[w - 1 - k]];
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz.at(k) == 'A'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz.at(k) == 'T'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz.at(k) == 'C'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz.at(k) == 'G'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
gzprintf(fp1, "%d %d\n", nc[0]+nc[1], nc[2]+nc[3]);
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz_rc.at(k) == 'A'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz_rc.at(k) == 'T'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz_rc.at(k) == 'C'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
{
double prod[dd][dd] = {{1,0},{0,1}};
for(unsigned k = 0; k < zz.size(); k++) {
operation(prod, (int) (zz_rc.at(k) == 'G'), alpha);
}
gzprintf(fp1, "%lf ", log(prod[0][0] + sqrt(3) * prod[0][1] + M_SQRT2 * prod[1][0] + sqrt(5) * prod[1][1]));
}
gzprintf(fp1, "%d %d\n", nc[0]+nc[1], nc[2]+nc[3]);
}
}
else if (skip == 1)
c1++;
else if (skip == 2)
c2++;
else if (skip == 3)
c3++;
char * ret = Z_NULL;
ret = gzgets(fgz, first, 1024);
if(ret == Z_NULL) break;
ret = gzgets(fgz, tline, 1024);
if(ret == Z_NULL) break;
ret = gzgets(fgz, tscore, 1024);
if(ret == Z_NULL) break;
ret = gzgets(fgz, tscore, 1024);
if(ret == Z_NULL) break;
}
fprintf(flog, " %d was skipped due to insufficient length \n", c1);
fprintf(flog, " %d was skipped due to missing rate \n", c2);
fprintf(flog, " %d was skipped due to low phred \n", c3);
gzclose(fp1);
gzclose(fgz);
delete[] tline;
delete[] first;
delete[] tscore;
}
void operation(double prod[][dd], int yes, double aa)
{
if(yes == 1) {
prod[0][1] += aa*prod[0][0];
prod[1][1] += aa*prod[1][0];
}
else {
prod[0][0] += aa*prod[0][1];
prod[1][0] += aa*prod[1][1];
}
}
int compare_doubles(const void * a, const void * b)
{
return ((int) (*(double *) a > *(double *) b));
}
int compare_doubles_2D(const void *pa, const void *pb) {
const double *a = *(const double **)pa;
const double *b = *(const double **)pb;
if(a[4] > b[4])
return 1;
else if(a[4] < b[4])
return -1;
else
return 0;
}
double * select_quantiles(double * xx, long nx, int ng)
{
double * temp = new double[nx];
std::copy(xx, xx + nx, temp);
double * brk = new double[ng+1]; // breaks
qsort((double *) temp, nx, sizeof(double), compare_doubles);
for (int j = 0; j < ng; j++) {
brk[j] = temp[(nx / ng) * j];
}
// brk[ng] = temp[nx - 1] + 0.1;
brk[ng] = INFINITY;
brk[0] = -1 * INFINITY;
delete[] temp;
return(brk);
}
double ** select_quantiles_all(double ** xx, long nx, int ng, int ncol)
{
double ** brk = new double*[ncol]; //for each of columns
for (int i = 0; i < ncol; i++) {
brk[i] = new double[ng+1]; // breaks
qsort((double *) xx[i], nx, sizeof(double), compare_doubles);
for (int j = 0; j < ng; j++) {
brk[i][j] = xx[i][(nx / ng) * j];
}
brk[i][ng] = xx[i][nx - 1] + 0.1;
}
return(brk);
}
int quickselect(double * brk, int ng, double num)
{
// ng number of bins, num the number to be placed in a certain bin
// brk, the break points of bin for a single dimension
int start = 0;
int end = ng - 1;
while (start < end) {
int middle = start + ceil((end - start) / 2.0);
if (num < brk[middle]) {
end = middle - 1;
}
else {
start = middle;
}
}
return(start);
}
double hellinger(unordered_map<string, double> wx, unordered_map<string, double> wy) // wx and wy are unordered hash maps
{
/* calculate the pair-wise distance for two samples */
double res = 0;
double temp = 0;
unordered_map<string, double>:: iterator itr;
for (itr = wx.begin(); itr != wx.end(); itr++)
{
// itr works as a pointer to pair<string, double>
// type itr->first stores the key part and
// itr->second stroes the value part
if (wy.find(itr->first) != wy.end()) {
temp = sqrt((itr->second) * wy[itr->first]);
res += temp;
}
}
res = sqrt(1 - res);
return(res);
}
double cosine(unordered_map<string, double> wx, unordered_map<string, double> wy) // wx and wy are unordered hash maps
{
/* calculate the pair-wise distance for two samples */
double res = 0;
double temp1 = 0;
double temp2 = 0;
double temp3 = 0;
unordered_map<string, double>:: iterator itr;
for (itr = wx.begin(); itr != wx.end(); itr++)
{
// itr works as a pointer to pair<string, double>
// type itr->first stores the key part and
// itr->second stroes the value part
if (wy.find(itr->first) == wy.end()) {
double x = itr->second;
temp2 += x * x;
}
else {
double x = itr->second;
double y = wy[itr->first];
temp1 += x * y;
temp2 += x * x;
temp3 += y * y;
}
}
for (itr = wy.begin(); itr != wy.end(); itr++)
{
// string key = itr->first;
if (wx.find(itr->first) == wx.end()) {
double y = itr->second;
temp3 += y * y;
}
}
res = temp1 / (sqrt(temp2) * sqrt(temp3));
res = 1 - res;
res *= 0.5;
return(res);
}
long long int filerow(string f1)
{
gzFile fp = gzopen(f1.c_str(), "rb");
if(fp == NULL) {
printf(" can't open file, %s \n", f1.c_str());
exit(0);
}
long optimalSize = 4 * 1024 * 1024;
char * buf = new char[optimalSize+1];
long long int dim1 = 0;
while(1) {
long byte_read = gzread(fp, buf, optimalSize);
for (int i = 0; i < byte_read; i++)
{
if(buf[i] =='\n')
dim1++;
}
if(gzeof(fp))
break;
}
delete[] buf;
gzclose(fp);
return(dim1);
}
void regress_gc_beta(string fin)
{ const int np = 3; // regressor;
const int ncol = 4; //nubeam columns;
long long int dim1 = filerow(fin);
printf("File %s contains %lld lines \n", fin.c_str(), dim1);
gsl_matrix * xty=gsl_matrix_alloc(np, ncol);
gsl_matrix_set_zero(xty);
gsl_matrix * U = gsl_matrix_alloc(np,np);
gsl_matrix_set_zero(U);
printf("Begin to scan the file. \n");
{
gzFile fp = gzopen(fin.c_str(), "rb");
if(fp == NULL) {
printf(" can't open file, %s \n", fin.c_str());
exit(0);
}
// open score and gc file;
char * line1 = new char[1024];
double * xx = new double[np];
double * yy = new double[ncol];
for (long long int r = 0; r < dim1; r++) {
gzgets(fp, line1, 1024);
char * tok1 = strtok(line1, " \t");
yy[0] = atof(tok1);
for (int j = 1; j < ncol; j++)
{
tok1=strtok(NULL, " \t");
if(tok1 != NULL)
yy[j] = atof(tok1);
} //first four entries are scores;
for (int j = 0; j < 2; j++)
{
tok1=strtok(NULL, " \t");
if(tok1 != NULL)
xx[j] = log(1.0+atof(tok1));
// xx[j] = log(100 * atof(tok1));
} //remaining two entries are counts of AT and CG.
xx[np-1] = 1;
for (int i = 0; i < np; i++)
for (int j = 0; j < np; j++)
{
double temp = xx[i] * xx[j] + gsl_matrix_get(U, i, j);
gsl_matrix_set(U, i, j, temp);
}
for (int i = 0; i < np; i++)
for (int j = 0; j < ncol; j++)
{
double temp = xx[i] * yy[j] + gsl_matrix_get(xty, i, j);
gsl_matrix_set(xty, i, j, temp);
}
}
delete[] line1;
delete[] xx;
delete[] yy;
gzclose(fp);
}
printf("Finished scanning the file. Begin to calculate regression coefficients.\n");
gsl_matrix * V = gsl_matrix_alloc(np,np);
gsl_vector * S = gsl_vector_alloc(np);
gsl_vector * work = gsl_vector_alloc(np);
for(int i = 0; i < np; i++)
for (int j = i+1; j < np; j++)
gsl_matrix_set(U, i, j, gsl_matrix_get(U, j, i));
//fill the upper;
gsl_linalg_SV_decomp(U, V, S, work);
gsl_matrix * mb= gsl_matrix_alloc(np,ncol);
for (int c = 0; c < ncol; c++)
{
gsl_vector_view vxy = gsl_matrix_column(xty, c);
gsl_vector_view gb = gsl_matrix_column(mb, c);
gsl_linalg_SV_solve(U, V, S, &vxy.vector, &gb.vector);
}
gsl_matrix_free(xty);
gsl_matrix_free(U);
gsl_matrix_free(V);
gsl_vector_free(S);
gsl_vector_free(work);
printf("Obtained regression coefficients.\n");
for(int i = 0; i < np; i++)
{
for(int c = 0; c < ncol; c++)
fprintf(flog, "%.10lf ", gsl_matrix_get(mb, i, c));
fprintf(flog, "\n");
}
gsl_matrix_free(mb);
}
void regress_gc_residual(string fin, string beta_file, string fout)
{ const int np = 3; // regressor;
const int ncol = 4; //nubeam columns;
long long int dim1 = filerow(fin);
printf("File %s contains %lld lines \n", fin.c_str(), dim1);
fprintf(flog, "File %s contains %lld lines \n", fin.c_str(), dim1);
fout.append(".nogc.gz");
gzFile pout = gzopen(fout.c_str(), "wb");
if(pout == NULL) {
printf(" can't open file, %s \n", fout.c_str());
exit(0);
}//output file ready;
printf("Output file %s prepared.\n", fout.c_str());
fprintf(flog, "Output file %s prepared.\n", fout.c_str());
gsl_matrix * mb= gsl_matrix_alloc(np,ncol);
gzFile myfile = gzopen(beta_file.c_str(), "rb");
char * line = new char[1024];
for (int i = 0; i < np; i++) {
gzgets(myfile, line, 1024);
char * tok = strtok(line, " ");
int j = 0;
while (tok != NULL && j < ncol)
{
gsl_matrix_set(mb, i, j, atof(tok));
tok = strtok(NULL, " ");
j++;
}
}
delete[] line;
gzclose(myfile);
printf("Obtained the regression coefficients.\n");
for(int i = 0; i < np; i++)
{
for(int c = 0; c < ncol; c++)
printf("%lf ", gsl_matrix_get(mb, i, c));
printf("\n");
}
printf("Begin to scan the file and obtain residuals.\n");
fprintf(flog, "Begin to scan the file and obtain residuals.\n");
{
gzFile fp = gzopen(fin.c_str(), "rb");
if(fp == NULL) {
printf(" can't open file, %s \n", fin.c_str());
exit(0);
}
// open score and gc contents file;
char * line1 = new char[1024];
double * xx = new double[np];
double * yy = new double[ncol];
for (long long int r = 0; r < dim1; r++) {
gzgets(fp, line1, 1024);
char * tok1 = strtok(line1, " ");
yy[0] = atof(tok1);
for (int j = 1; j < ncol; j++)
{
tok1=strtok(NULL, " \t");
if(tok1 != NULL)
yy[j] = atof(tok1);
}//read a line from fp1;
for (int j = 0; j < 2; j++)
{
tok1=strtok(NULL, " \t");
if(tok1 != NULL)
xx[j] = log(1.0+atof(tok1));
// xx[j] = log(100 * atof(tok1));
}//read a line from fp1;
xx[np-1] = 1;
for (int c = 0; c < ncol; c++)
{
double temp = 0;
for (int j = 0; j < np; j++)
temp += xx[j] * gsl_matrix_get(mb, j, c);
yy[c] -= temp;
gzprintf(pout, "%lf ", yy[c]);
}
gzprintf(pout, "\n");
}
delete[] line1;
delete[] xx;
delete[] yy;
gzclose(fp);
}
printf("Obtained residuals.\n");
fprintf(flog, "Obtained residuals.\n");
gzclose(pout);
gsl_matrix_free(mb);
}
void regress_gc(string fin, string fout)
{ //this reads two files twice, but significantly reduced memory footprint.
const int np = 3; // regressor;
const int ncol = 4; //nubeam columns;
long long int dim1 = filerow(fin);
printf("File %s contains %lld lines \n", fin.c_str(), dim1);
fprintf(flog, "File %s contains %lld lines \n", fin.c_str(), dim1);
fout.append(".nogc.gz");