-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistmake.c
More file actions
2259 lines (1871 loc) · 53.4 KB
/
Copy pathhistmake.c
File metadata and controls
2259 lines (1871 loc) · 53.4 KB
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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <nicklib.h>
#include <globals.h>
#include <getpars.h>
#include <nicksam.h>
#include "admutils.h"
#include "mcmcpars.h"
#include "mcio.h"
#include "popsubs.h"
#include "cigsubs.h"
#define WVERSION "1850"
// printcount added
// meandepth added
// depth > 100 counted as 100
// rgonly added
// single-stranded UDG added
// badrgname added
// rgcount added
// printbadrgs added
// printrghit added
// rgnone count added
// singlebam mode compulsory (bamname: sampname:)
// snp alleles must be set valid
// readdepth added (big output file) ;
// maxfullpops 1000
// very long reads trapped
// MAXPOPS 5000
// code cleaned for O2
// new db format (4 fields) upward compatible
// readedge added
// rgcats added
// polymask added (shop 1000G polymorphic fa)
// rgsplit added
// killcpg added
// cpgonly added
// minlen, maxlen added
// rgcount added
// printread added (use with maxread)
// deaminhistname added (Daniel request)
// chrom 90 (MT) supported
// countbasepos added
// cadjustname added
// maxcl fixed (calcudg)
// empty hists not written
// mqclip
// deaminhistname fixed for indels
// more careful memory allocation
// processdups added
// countbases added (used by damagescore in drfancy )
// forcereg added
// killsnps added
/**
For single chromosome organisms with non-integer chromosome names
forcereg: <name>
allows one to pretend the chromosome is 1 for bam and reference.fa processing
It would be easy to add a feature to map chromosomes 1,2,... to names.
Similar change made to pullit.
killsnps: YES applies a mask to reference from snpfile. Note that histmake output
will now depend on snpname: <snpfile>
*/
int ***LC, ***RC ;
long ***cbases ; // numrg X 2 X 12
int mapreg = YES ;
#define MAXFL 50
#define MAXSSS 8192
#define MAXPOPS 5000
#define MAXRG 50000
#define MAXL 150
#define MINL 8
int pulldownmode = NO ;
int pullmode = NO ;
int printtable = NO ;
int *rgpops, numrg ;
char **iublist = NULL ;
int flipstrand = NO ; // debug/test only
int printrd = NO ;
int posbasestats = NO ;
char *cadjustname = NULL ;
int cadjstackbase = NO ;
// YES => base dist is stack dependent
int ultima = NO ;
int bqclip = 999 ;
int mqclip = 999 ;
long xindels[2] ;
int pubcigar = 0 ;
long badcigar = 0 ;
int lomap = 10, loqual = 20, ispair = NO ;
int *threshlo, *threshhi, *quallo ;
char *iubpops = NULL ;
int numiub = 0 ;
int printcount = NO ;
int usechimp = NO ;
int rgsplit = NO ;
char *dstag = "ds:f:" ;
char *rctag = "rc:i:" ;
char *iubfile = NULL ;
char *dbfile = NULL ;
char *dbbam = NULL ;
char *samxtra = NULL ;
char *readbam = NULL ;
char *bamname = NULL, *sampname = NULL ;
char *readdepthname = NULL ;
char *samtoolspath="samtools" ;
char *deaminhistname = NULL ;
FILE *deaminfile = NULL;
int singlebam = YES ;
// int maxread = 1000*1000*1000 ; // debug
long maxread = -1 ;
char xregname[10], yregname[10] ;
char *parname = NULL ;
char *xtag = NULL ;
char *oldlogfilename = NULL ;
int rgonly = NO ; // yes prints rg group and quits
int usepmdscore = NO ; // on read. Just effects readbam
int flatbase = NO ;
int processdups = NO ;
double pmdlo = -999 ;
double pmdhi = 999 ;
int minlen = MINL ;
int maxlen = MAXL ;
char xregname[10], yregname[10], mtregname[10] ;
char *udgmode = NULL ;
int udgtype = -1 ;
int calcudgtype = YES ;
int udgverbose = NO ;
extern int checksizemode ;
// the variables below should not be needed
char *omode = "eigenstrat" ; // compulsory as we only support
extern int packmode ; //!< flag - input {is not,is} in packed mode
extern char *packgenos ; //!< packed genotype data (packit.h)
extern char *packepath ;
extern long packlen; //!< allocated size of packgenos data space
extern long rlen; //!< number of bytes in packgenos space that each SNP's data occupies
char *trashdir = "/var/tmp" ;
extern int verbose ;
int qtmode = NO ;
Indiv **indivmarkers;
SNP **snpmarkers = NULL ;
int numsnps, numindivs ;
int hasrggroup = YES ;
int basewindow = 0 ;
long nreject, nindelx, nindel ;
int readsumm = 0, readsummc = 0 ;
long cpgmiss = 0 ;
char *genotypename = NULL ;
char *snpname = NULL ;
char *indoutfilename = NULL ;
char *snpoutfilename = NULL ;
char *genooutfilename = NULL ;
char *indivname = NULL ;
int checkmode = NO ;
char **samplist ;
int nsamps ;
int minchrom = 1 ;
int maxchrom = 24 ;
int xchrom = -1 ;
char *reffasta = NULL ;
char *outputname = NULL ;
FILE *ofile ;
double fakespacing = 0.0 ;
char unknowngender = 'U' ;
char *regname = NULL ;
char **iublist ;
char *regstring = NULL ;
int reglen ;
char *chimpstring = NULL;
int chimplen ;
char *rstring ;
char *poplistname = NULL ;
char **poplist ;
char **rawpoplist ;
char **fullpoplist ;
char *badrgname = NULL ;
char **bad ;
int nbadrg = 0 ;
int npops=0, nfpops = 0, ret ;
int seed = 0 ;
char *fasta ;
char *histfilename = NULL ;
char *ref[1] ; // ref seq name
char *chimpref[1] ; // chimp seq name
char *polyref[1] ; // poly seq name
char *polystring = NULL;
char *polyname = "polymask" ;
int polylen ;
int polymask = YES ;
int killcpg = NO ;
int cpgonly = NO ;
int stopcount = NO ;
READ **readlist, *readpt, *readptx, *oldreadpt ;
THRESH *pthresh, ptx ;
HIST ***bighistlist ;
HIST ***bighistlistu ;
HIST **bighist, *histpt ;
int maxbighistlen = 500000, bighistlen ;
int hd1 = 101, hd2=101, hd3 =2, hd4 = 50 ;
int ****htable ;
int *ptoax, ppwk[200], numpp, readedge = 10 ;
int maxmq=0, maxbq = 0 ;
char *rgcatsname = NULL ;
int numrgcats = 1, numrgc = 0 ;
int rgnone = 0 ;
char **rgclist ;
int *rgcats, *rgcount, rgmiss = 0 ;
char *refname = "Href" ;
int *cat2rg, *rg2cat ;
char *forcereg = NULL ;
int killsnps = NO ;
void readcommands(int argc, char **argv) ;
long setgenoblank (SNP **snpmarkers, int numsnps, int numindivs) ;
int readfa1(char *faname, char **pfasta, int *flen) ;
char *myfai_fetch(faidx_t *fai, char *reg, int *plen) ;
int mksamplist(char **samplist, Indiv **indivmarkers, int numindivs) ;
void loadreg(char *reg) ;
void mkpop (char **rawpops, char **pops, int n) ;
void calccount(int ***countarr, int firstsnp, int lastsnp) ;
void fixreg(int chrom) ;
int getgt(int *aa) ;
int stripindivs(Indiv **indivmarkers, int numindivs) ;
int getfalist(char **poplist, int npops, char *dbfile, char **iublist) ;
int checkread(READ *readpt) ;
int setudgtype(char *mode) ;
void clearhist(HIST *hpt) ;
void counthist(READ *readpt, HIST **bighist) ;
void clear4D(int ****table, int x1, int x2, int x3, int x4, int val) ;
void dumphist (char *histfilename, HIST *** bigh, int ultmode) ;
void zalh(int hd1, int hd2, int hd3, int hd4) ;
void makergcats(char *rrr, char *bamname) ;
int qcpg1(int pos, char *regstring, int reglen) ;
int countsam(int first, int last) ;
int callsam() ;
int ptox(int p) ;
void crackx(long *bb, HIST *hpt) ;
char *udgm(int ds, int tt) ;
void calcudg(HIST **bighist, int *stype, int *utype) ;
void deaminhist(HIST **bighist, FILE *fff) ;
void countbasepos () ;
void setthreshx(THRESH *pthresh) ;
int testconchi(double *a, int m, int n) ;
FILE *samstream ;
int main(int argc, char **argv)
{
int i, j, k, d, n, kk, g, s, t, tt, ccheck ;
int pos, isnp, jsnp, gval ;
SNP *cupt ;
Indiv *indx ;
char *chimpname = "Chimp" ;
int numvind, nignore, numrisks = 1 ;
int chrom, lastchrom = -1 ;
int firstsnp, rlen, mlen ;
int lastsnp, lensnp ;
int ***countarr, depth ;
char ss[MAXSTR] ;
int *dsum, *dnum, *dcover ;
int isok, t1, t2 ;
double meandepth, ymem ;
FILE *readdfff = NULL, *fjunk ;
int mq, bq, pp, ww[100] ;
int a, b ;
int d1, d2, d3, d4 ;
int a1, a2, a3, a4 ;
double **xx ;
int **snppos, *fpos, *lpos ;
double y0, y1, yy ;
cputime(0) ;
calcmem(0) ;
ofile = stdout;
packmode = YES ;
ref[0] = polyref[0] = NULL ;
readcommands(argc, argv) ;
if (polyref[0] != NULL) polymask = YES ;
if (parname == NULL) {
printf("version: %s\n", WVERSION) ;
return 0 ;
}
printf("executable: %s\n", argv[0]) ;
printf("version: %s\n", WVERSION) ;
printnl() ;
printcmdline(argc, argv) ;
printf("cwd: %s\n", getcwd(ss, MAXSTR)) ;
printnl() ;
setdump(coredump) ;
setref(&reffasta, iubfile, "Href") ;
ref[0] = strdup(reffasta) ;
printf("reference: %s\n", reffasta) ;
if (histfilename == NULL) fatalx("no histfile!\n") ;
printf("histfilename: %s\n", histfilename) ;
if (forcereg) printf("forcereg: %s\n", forcereg) ;
if (killsnps) {
numsnps = getsnps(snpname, &snpmarkers, 0, NULL, &nignore, numrisks) ;
}
hd1 = MIN(mqclip+1, hd1) ;
hd2 = MIN(bqclip+1, hd2) ;
hd4 = 2*readedge + 1 ;
maxbighistlen = hd1*hd2*hd3*hd4 ;
++maxbighistlen ;
if (bqclip < 900) printf("bqclip: %d\n", bqclip) ;
if (mqclip < 900) printf("mqclip: %d\n", mqclip) ;
if (udgverbose) printf("udgverbose set\n") ;
nreject = nindelx = nindel = 0 ;
if (cadjustname != NULL) posbasestats = YES ;
if (basewindow > 0) {
t = basewindow % 2 ;
if (t==0) ++basewindow ; // force odd
}
if (samxtra != NULL) printf("samxtra: %s\n", samxtra) ;
readdepthname = NULL ;
printf("histmake: version: %s\n", WVERSION) ;
if (cpgonly) killcpg = NO ;
if (outputname != NULL) openit(outputname, &ofile, "w") ;
singlebam = YES ;
if (ultima) printf("ultima mode set!\n") ;
if (seed == 0) seed = filehash(parname) ;
SRAND(seed) ;
printf("seed: %d\n", seed) ;
if (reffasta != NULL) printf("reference fasta: %s\n", reffasta) ;
if (numchrom<0) numchrom = 22 ;
if (numchrom != 22) printf("numchrom: %d\n", numchrom) ;
sprintf(xregname, "%d", numchrom+1) ;
sprintf(yregname, "%d", numchrom+2) ;
if (readdepthname != NULL) openit(readdepthname, &readdfff, "w") ;
minlen = MAX(minlen, 2*readedge) ;
numindivs = 1 ;
xindels[0] = xindels[1] = 0 ;
ZALLOC(samplist, numindivs, char *) ;
if (sampname == NULL) fatalx("no sampname!\n") ;
samplist[0] = strdup(sampname) ;
nsamps = 1 ;
poplist = samplist ;
if ((singlebam) && (nsamps != 1)) fatalx("singlebam set but nsamps != 1\n") ;
if ((singlebam) && (sampname == NULL)) fatalx("singlebam set but no sampname\n") ;
if ((bamname == NULL) && (sampname != NULL)) fatalx("sampname set but not bamname\n") ;
t = openit_trap(bamname, &fjunk, "r") ;
if (t==NO) fatalx("failed to open %s\n", bamname) ;
fclose(fjunk) ;
sprintf(xregname, "%d", numchrom+1) ;
sprintf(yregname, "%d", numchrom+2) ;
sprintf(mtregname, "%d", 90) ;
printf("samplist:\n") ;
printstrings(samplist, nsamps) ;
printf("numsnps: %d numindivs: %d\n", numsnps, numindivs) ;
fflush(stdout) ;
if (polymask) {
getfalist(&polyname, 1, iubfile, polyref) ; // ref[0] is now reference fasta ;
printf("polymask: %s\n", polyref[0]) ;
}
ZALLOC(LC, 2, int **) ;
ZALLOC(RC, 2, int **) ;
for (s=0; s <= 1; ++s) {
LC[s] = initarray_2Dint(4,4,0) ;
RC[s] = initarray_2Dint(4,4,0) ;
}
npops = 1 ;
ZALLOC(poplist, npops, char *) ;
ZALLOC(rawpoplist, npops, char *) ;
ZALLOC(fullpoplist, MAXPOPS, char *) ;
if (sampname == NULL) {
fatalx("no sampname!\n") ;
npops = loadlist(rawpoplist, poplistname) ;
}
else {
fullpoplist[0] = poplist[0] = rawpoplist[0] = strdup(sampname) ;
}
ZALLOC(dsum, numindivs, int) ;
ZALLOC(dnum, numindivs, int) ;
ZALLOC(dcover, numindivs, int) ;
// must fix indivmarkers for missings.
pthresh = &ptx ;
setthreshx(pthresh) ;
ZALLOC(rgpops, MAXRG, int) ;
nbadrg = 0 ;
if (badrgname != NULL) {
nbadrg = numlines(badrgname) ;
ZALLOC(bad, nbadrg, char *) ;
nbadrg = loadlist(bad, badrgname) ;
}
numrg = 0 ;
if (rgsplit) {
rgcatsname = mytemp("rggroups") ;
if (bamname == NULL) fatalx("no bamname!\n") ;
makergcats(rgcatsname, bamname) ;
}
numrgcats = 1 ;
if (rgcatsname != NULL) {
t = numlines(rgcatsname) ;
ZALLOC(rgclist, t, char *) ;
ZALLOC(rgcats, t, int) ;
rgmiss = 0 ;
xx = initarray_2Ddouble(1, t, -1) ;
numrgc = getxxnames(&rgclist, xx, t, 1, rgcatsname) ;
fixit(rgcats, xx[0], numrgc) ;
free2D(&xx, 1) ;
ivmaxmin(rgcats, numrgc, &numrgcats, NULL) ;
++numrgcats ;
ZALLOC(rgcount, numrgcats, int) ;
ZALLOC(cat2rg, numrgcats, int) ;
ZALLOC(rg2cat, numrgc, int) ;
printnl() ;
printf("rgcats: %d\n", numrgcats) ;
hasrggroup = YES ;
for (k=0; k< numrgc; ++k) {
t = rgcats[k] ;
cat2rg[t] = k ;
rg2cat[k] = t ;
}
for (k=0; k< numrgc; ++k) {
printf("%30s %3d\n", rgclist[k], rgcats[k]) ;
}
printnl() ;
}
else {
hasrggroup = NO ;
ZALLOC(rgcount, numrgcats, int) ;
ZALLOC(cat2rg, numrgcats, int) ;
ZALLOC(rg2cat, numrgc, int) ;
}
if (rgonly) {
printf("rgonly set\n") ;
printf("##end of histmake\n") ;
return 0 ;
}
countarr = NULL; lensnp = -1 ;
t = 3 ;
ZALLOC(readlist, t, READ *) ;
ZALLOC(readlist[0], 1, READ) ;
readpt = readlist[0] ;
cleanread(readpt) ;
ZALLOC(readlist[1], 1, READ) ;
readptx = readlist[1] ;
cleanread(readptx) ;
ZALLOC(readlist[2], 1, READ) ;
oldreadpt = readlist[2] ;
cleanread(oldreadpt) ;
t = 2*readedge + 1 ; if (t>50) fatalx("read window too big\n") ;
ZALLOC(bighistlist, numrgcats, HIST **) ;
ZALLOC(cbases, numrgcats, long **) ;
for (j=0; j<numrgcats; ++j) {
cbases[j] = initarray_2Dlong(2, 12, 1) ;
ZALLOC(bighistlist[j], maxbighistlen, HIST *) ;
bighist = bighistlist[j] ;
for (k = 0; k< maxbighistlen; ++k) {
ZALLOC(bighist[k], 1, HIST) ;
histpt = bighist[k] ;
clearhist(histpt) ;
}
}
ZALLOC(bighistlistu, numrgcats, HIST **) ;
for (j=0; j<numrgcats; ++j) {
ZALLOC(bighistlistu[j], maxbighistlen, HIST *) ;
bighist = bighistlistu[j] ;
for (k = 0; k< maxbighistlen; ++k) {
ZALLOC(bighist[k], 1, HIST) ;
histpt = bighist[k] ;
clearhist(histpt) ;
}
}
t = 0 ;
ivclear(ppwk, -9999, 200) ;
ptoax = ppwk+100 ; // index can be negative
for (k=0; k<readedge; ++k) {
pp = k + 1 ;
ww[t] = pp ;
++t ;
}
for (k=0; k<readedge; ++k) {
pp = -(k + 1) ;
ww[t] = pp ;
++t ;
}
pp = 99 ;
ww[t] = pp ;
++t ;
numpp = t ;
for (a=0; a<numpp; ++a) {
pp = ww[a] ;
ptoax[pp] = a ;
}
hd4 = 2*readedge+2 ;
zalh(hd1, hd2, hd3, hd4) ;
t = 0 ;
clear4D(htable, hd1, hd2, hd3, hd4, -1) ;
for (mq=0; mq<hd1; ++mq) {
for (bq=0; bq<hd2; ++bq) {
for (s=0; s<=1; ++s) {
for (a=0; a<numpp; ++a) {
pp = ww[a] ;
htable[mq][bq][s][a] = t ;
for (j=0; j<numrgcats; ++j) {
bighist = bighistlist[j] ;
histpt = bighist[t] ;
histpt -> mq = mq ;
histpt -> bq = bq ;
histpt -> strand = s ;
histpt -> pp = pp ;
bighist = bighistlistu[j] ;
histpt = bighist[t] ;
histpt -> mq = mq ;
histpt -> bq = bq ;
histpt -> strand = s ;
histpt -> pp = pp ;
}
++t ;
if (t>=maxbighistlen) fatalx("overflow (bighist)\n") ;
}
}
}
}
bighistlen = t ;
printf("bighistlen: %6d max: %6d\n", bighistlen, maxbighistlen) ;
if (xchrom > 0) minchrom = maxchrom = xchrom ;
for (chrom=minchrom; chrom <= maxchrom; ++chrom) {
sprintf(ss, "%d", chrom) ;
if (forcereg) strcpy(ss, forcereg) ;
loadreg(ss) ;
fixreg(chrom) ;
printf("calling sam\n") ; fflush(stdout) ;
callsam() ;
printf("called sam\n") ; fflush(stdout) ;
n = countsam(1, reglen) ;
printf("reg: %s reads: %d\n", regname, n) ; fflush(stdout) ;
printf("chrom %d processed\n", chrom) ;
fflush(stdout) ;
}
if (numrg>0) {
printbadrgs() ;
printrghit() ;
if (rgnone>0) printf("*** reads with no RG group: %d\n", rgnone) ;
}
countbasepos() ;
deaminfile = stdout ;
if (deaminhistname != NULL) openit(deaminhistname, &deaminfile, "w") ;
for (k=0; k<numrgcats; ++k) {
for (j=0; j<=1; ++j) {
printf("cbases rcat: %d strand: %d\n", k, j) ;
printlmat(cbases[k][j], 3, 4) ;
}
}
dumphist(histfilename, bighistlist, ultima) ;
if (deaminhistname != NULL) {
fclose(deaminfile) ;
deaminfile = NULL ;
deaminhistname = NULL ;
}
printf("badcigars: %ld\n", badcigar) ;
y0 = xindels[0] ;
y1 = xindels[1] ;
yy = y1 / (y0 + y1 + 1.0e-6) ;
printf("indel count (reject, no indel, indel)" ) ;
printf(" %ld", nreject) ;
printf(" %ld", nindelx) ;
printf(" %ld", nindel) ;
if (cpgonly || killcpg) printf(" cpgmiss: %ld", cpgmiss) ;
printnl() ;
printf("indel rate: %9.3f\n", yy) ;
ymem = calcmem(1)/1.0e6 ;
printf("##end of histmake: %12.3f seconds cpu %12.3f Mbytes in use\n", cputime(1), ymem) ;
return 0 ;
ymem = calcmem(1)/1.0e6 ;
printf("##end of histmake: %12.3f seconds cpu %12.3f Mbytes in use\n", cputime(1), ymem) ;
return 0 ;
}
int getgt(int *aa)
{
int depth ;
double pref ;
depth = intsum(aa, 2) ;
if (depth==0) return -1 ;
if (aa[0]==aa[1]) return 2*ranmod(2) ;
if (aa[0]==0) return 0 ;
if (aa[1]==0) return 2 ;
pref = (double) aa[0] / (double) depth ;
if (prob1(pref)==YES) return 2 ;
return 0 ;
}
void fixreg(int chrom)
// reference set OK except on target snps
{
char *treg = NULL ;
int k, len, pos ;
SNP *cupt ;
// lower case used to mask bases not in snpname
len = strlen(regstring) ;
for (k=0; k<numsnps; ++k) {
cupt = snpmarkers[k] ;
if (cupt -> chrom != chrom) continue ;
pos = snpmarkers[k] -> physpos ;
if (pos <= 0) continue ;
if (pos >= len) continue ;
regstring[pos-1] = '?' ;
}
}
void readcommands(int argc, char **argv)
{
int i,haploid=0;
phandle *ph ;
char str[5000] ;
char *tempname ;
char *tparams ;
int n ;
int targc;
char *targv[5] ;
while ((i = getopt (argc, argv, "r:p:t:cvVs")) != -1) {
switch (i)
{
case 'p':
parname = strdup(optarg) ;
break;
case 't':
xtag = strdup(optarg) ;
break;
case 'v':
printf("version: %s\n", WVERSION) ;
break;
case 'c':
checkmode = YES ;
break;
case 'V':
verbose = YES ;
break;
case 's':
seed = seednum() ; // random seed; No exact replay
break;
case 'r':
oldlogfilename = strdup(optarg) ;
randomname(&tempname) ;
tparams = mytemp(tempname) ;
mkparamfile(oldlogfilename, tparams) ;
parname = strdup(tparams) ;
break ;
case '?':
printf ("Usage: bad params.... \n") ;
fatalx("bad params\n") ;
}
}
if (parname == NULL) {
printf("no parameters!\n") ;
return ;
}
printf("##parameter file: %s\n", parname) ;
ph = openpars(parname) ;
dostrsub(ph) ;
fixpars(ph, xtag) ;
fixpars(ph, basename(argv[0])) ;
getstring(ph, "bamname:", &bamname) ;
getstring(ph, "cramname:", &bamname) ;
getstring(ph, "sampname:", &sampname) ;
getstring(ph, "refname:", &refname) ;
getstring(ph, "referencefile:", &reffasta) ;
getint(ph, "minchrom:", &minchrom) ;
getint(ph, "maxchrom:", &maxchrom) ;
getint(ph, "chrom:", &xchrom) ;
getint(ph, "pmdscore:", &usepmdscore) ;
getint(ph, "rgonly:", &rgonly) ;
getint(ph, "flipstrand:", &flipstrand) ;
getlong(ph, "maxread:", &maxread) ;
getint(ph, "seed:", &seed) ;
getint(ph, "numchrom:", &numchrom) ;
getint(ph, "calcudgtype:", &calcudgtype) ;
getint(ph, "printread:", &printrd) ;
getint(ph, "printrd:", &printrd) ;
getint(ph, "posbasestats:", &posbasestats) ;
getint(ph, "flatbase:", &flatbase) ;
getint(ph, "ultima:", &ultima) ;
getint(ph, "indelaware:", &ultima) ;
getint(ph, "bqclip:", &bqclip) ;
getint(ph, "mqclip:", &mqclip) ;
getint(ph, "pisubcigar:", &pubcigar) ;
getint(ph, "lomap:", &lomap) ;
getint(ph, "loqual:", &loqual) ;
getint(ph, "pair:", &ispair) ;
getint(ph, "ispair:", &ispair) ;
getint(ph, "readsumm:", &readsumm) ;
getint(ph, "processdups:", &processdups) ;
getint(ph, "coredump:", &coredump);
getint(ph, "cadjstackbase:", &cadjstackbase) ;
getint(ph, "udgverbose:", &udgverbose) ;
getdbl(ph, "pmdlo:", &pmdlo) ;
getdbl(ph, "pmdhi:", &pmdhi) ;
getstring(ph, "fadatabase:", &iubfile) ;
getstring(ph, "samxtra:", &samxtra) ;
getstring(ph, "samtools:", &samtoolspath) ;
getstring(ph, "samtoolspath:", &samtoolspath) ;
getstring(ph, "readbam:", &readbam) ;
getstring(ph, "badrgname:", &badrgname) ;
getstring(ph, "readdepthname:", &readdepthname) ;
getstring(ph, "ref:", &reffasta) ;
getstring(ph, "polyname:", &polyname) ;
getstring(ph, "polymaskfile:", &polyref[0]) ;
getstring(ph, "histfilename:", &histfilename) ;
getstring(ph, "deaminhistname:", &deaminhistname) ;
getstring(ph, "deaminhist:", &deaminhistname) ;
getstring(ph, "rgcats:", &rgcatsname) ;
getstring(ph, "rgcatsname:", &rgcatsname) ;
getint(ph, "readedge:", &readedge) ;
getint(ph, "killcpg:", &killcpg) ;
getint(ph, "cpgonly:", &cpgonly) ;
getint(ph, "polymask:", &polymask) ;
getint(ph, "rgsplit:", &rgsplit) ;
getint(ph, "minlen:", &minlen) ;
getint(ph, "maxlen:", &maxlen) ;
getint(ph, "minlength:", &minlen) ;
getint(ph, "maxlength:", &maxlen) ;
getint(ph, "basewindow:", &basewindow) ;
getint(ph, "mapreg:", &mapreg) ;
getstring(ph, "cadjustname:", &cadjustname) ;
getstring(ph, "cadjname:", &cadjustname) ;
getlongstring(ph, "samxtra:", &samxtra) ;
getstring(ph, "forcereg:", &forcereg) ; // all reads forced to use this reg. For prokaryotes
getint(ph, "killsnps:", &killsnps) ; // kill reference on snps
getstring(ph, "snpname:", &snpname) ; // needed if killsnps set YES
writeparsx(ph) ;
closepars(ph) ;
fflush(stdout) ;
}
int readfa1(char *faname, char **pfasta, int *flen)
{
faidx_t *fai ;
int k, len, t ;
char *ttfasta, *qqfasta ;
char ssreg[20] ;
int ntry = 0, itry ;
int ismt ;
if (pfasta != NULL) freestring(pfasta) ;
*flen = 0 ;
*pfasta = NULL ;
if (faname == NULL) {
return 0;
}
fai = fai_load(faname) ;
strcpy(ssreg, regname) ;
ismt = NO ;
t = strcmp(regname, xregname) ; if ((t==0) && (mapreg)) strcpy(ssreg, "X") ;
t = strcmp(regname, yregname) ; if ((t==0) && (mapreg)) strcpy(ssreg, "Y") ;
t = strcmp(regname, mtregname) ; if ((t==0) && (mapreg)) strcpy(ssreg, "MT") ;
if (t==0) ismt = YES ;
ttfasta = myfai_fetch(fai, ssreg, &len) ;
*flen = len ;
if (ttfasta == NULL) {
printf ("***warning: no hetfa for %s :: %s\n", faname, ssreg) ;
return len ;
}
if (ismt) {
// pretend that MT is linear of twice length
ZALLOC(qqfasta, 2*len+1, char) ;
strcpy(qqfasta, ttfasta) ;
strcpy(qqfasta+len, ttfasta) ;
*pfasta = strdup(qqfasta) ;
len = 2*len ;
freestring(&qqfasta) ;
}
else {
*pfasta = strdup(ttfasta) ;
}
freestring(&ttfasta) ;
return len ;
}
char *myfai_fetch(faidx_t *fai, char *reg, int *plen)
{
char *treg, *s ;
treg = strdup(reg) ;
if (fai==NULL) fatalx("(my_fai_fetch): fai NULL\n") ;
s = fai_fetch(fai, treg, plen) ;
if (*plen > 0) {
free(treg) ;
return s ;
}
free(treg) ;
return NULL ;
}
void clearfainfo(FATYPE *fapt, int mode)
{
if (mode==1) {
freestring(&fapt->faname) ;
freestring(&fapt->alias) ;
freestring(&fapt->famask) ;
fapt -> fai = fapt -> faimask = NULL ;
}
freestring(&fapt -> rstring) ;
freestring(&fapt -> mstring) ;
freestring(&fapt -> regname) ;
fapt -> lopos = fapt -> hipos = -1 ;
fapt -> len = 0 ;
fapt -> mlen = 0 ;
fapt -> rlen = 0 ;
}
void printfapt(FATYPE *fapt)
{
int k, np = 0, mlen = fapt -> mlen ;
char cc ;
printf("fapt: %s %s\n", fapt->faname, fapt->alias) ;
printf("%x %d %d n", fapt -> fai, fapt -> lopos, fapt -> hipos) ;
printf("len: %d rlen: %d\n", fapt -> len, fapt -> rlen) ;
printf("mask: %s %d\n", fapt -> famask, fapt -> mlen) ;
printnl() ;
fflush(stdout) ;
}