forked from c-zhou/yahs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
juicer.c
1074 lines (925 loc) · 39.6 KB
/
juicer.c
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
/*********************************************************************************
* MIT License *
* *
* Copyright (c) 2021 Chenxi Zhou <[email protected]> *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
*********************************************************************************/
/********************************** Revision History *****************************
* *
* 22/06/21 - Chenxi Zhou: Created *
* *
*********************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include "khash.h"
#include "bamlite.h"
#include "ketopt.h"
#include "kvec.h"
#include "sdict.h"
#include "asset.h"
#include "version.h"
static double jc_realtime0;
enum fileTypes{NOSET, BED, BAM, BIN, PA5};
KHASH_SET_INIT_STR(str)
static int make_juicer_pre_file_from_bin(char *f, char *agp, char *fai, uint8_t mq, int scale, int count_gap, FILE *fo)
{
int ret;
FILE *fp;
uint32_t i, i0, i1, m;
uint64_t p0, p1, pair_n, pair_c, pair_u;
int64_t magic_number;
uint8_t buffer[BUFF_SIZE * 17];
CC_ERR_t err0, err1;
sdict_t *sdict = make_sdict_from_index(fai, 0);
asm_dict_t *dict = agp? make_asm_dict_from_agp(sdict, agp, 1) : make_asm_dict_from_sdict(sdict);
// check BIN file header consistency
if ((ret = file_sdict_match(f, sdict))) {
fprintf(stderr, "[E::%s] Not a valid BIN file or BIN file header does not match sequence dictionary: %d\n", __func__, ret);
fprintf(stderr, "[E::%s] Make sure no contig length threshold (YaHS option '-l') applied for the BIN file\n", __func__);
fprintf(stderr, "[E::%s] Consider using a BAM or BED file instead\n", __func__);
exit(EXIT_FAILURE);
}
fp = fopen(f, "r");
if (fp == NULL) {
fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, f);
exit(EXIT_FAILURE);
}
m = fread(&magic_number, sizeof(int64_t), 1, fp);
if (m != 1) bin_fread_error();
if (!is_valid_bin_header(magic_number)) {
fprintf(stderr, "[E::%s] not a valid BIN file\n", __func__);
exit(EXIT_FAILURE);
}
file_seek_skip_sdict(fp);
m = fread(&pair_n, sizeof(uint64_t), 1, fp);
if (m != 1) bin_fread_error();
pair_c = pair_u = 0;
while (pair_c < pair_n) {
m = fread(buffer, sizeof(uint8_t), BUFF_SIZE * 17, fp);
for (i = 0; i < m && pair_c < pair_n; i += 17, ++pair_c) {
if (*(uint8_t *) (buffer + i + 16) < mq)
continue;
err0 = sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i), *(uint32_t *) (buffer + i + 4), &i0, &p0, count_gap);
err1 = sd_coordinate_conversion(dict, *(uint32_t *) (buffer + i + 8), *(uint32_t *) (buffer + i + 12), &i1, &p1, count_gap);
if (err0 != CC_SUCCESS || err1 != CC_SUCCESS) {
// fprintf(stderr, "[W::%s] sequence not found \n", __func__);
++pair_u;
} else {
if (strcmp(dict->s[i0].name, dict->s[i1].name) <= 0)
fprintf(fo, "0\t%s\t%lu\t0\t1\t%s\t%lu\t1\n", dict->s[i0].name, p0 >> scale, dict->s[i1].name, p1 >> scale);
else
fprintf(fo, "0\t%s\t%lu\t1\t1\t%s\t%lu\t0\n", dict->s[i1].name, p1 >> scale, dict->s[i0].name, p0 >> scale);
}
}
}
fprintf(stderr, "[I::%s] %lu read pairs processed: %lu unmapped\n", __func__, pair_c, pair_u);
fclose(fp);
asm_destroy(dict);
sd_destroy(sdict);
return 0;
}
static int make_juicer_pre_file_from_bed(char *f, char *agp, char *fai, uint8_t mq, int scale, int count_gap, FILE *fo)
{
iostream_t *fp;
char *line;
char cname0[4096], cname1[4096], rname0[4096], rname1[4096];
uint32_t s0, s1, e0, e1, i0, i1;
uint64_t p0, p1, rec_c, pair_c;
uint8_t q0, q1;
int8_t buff;
CC_ERR_t err0, err1;
khash_t(str) *hmseq; // for absent sequences
khint_t k;
int absent;
hmseq = kh_init(str);
sdict_t *sdict = make_sdict_from_index(fai, 0);
asm_dict_t *dict = agp? make_asm_dict_from_agp(sdict, agp, 1) : make_asm_dict_from_sdict(sdict);
fp = iostream_open(f);
if (fp == NULL) {
fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, f);
exit(EXIT_FAILURE);
}
rec_c = pair_c = 0;
buff = 0;
while ((line = iostream_getline(fp)) != NULL) {
if (is_empty_line(line))
continue;
if (++rec_c % 1000000 == 0)
fprintf(stderr, "[I::%s] %lu million records processed, %lu read pairs \n", __func__, rec_c / 1000000, pair_c);
if (buff == 0) {
cname0[0] = rname0[0] = '\0';
s0 = e0 = UINT32_MAX;
q0 = 255;
sscanf(line, "%s %u %u %s %hhu", cname0, &s0, &e0, rname0, &q0);
++buff;
} else if (buff == 1) {
cname1[0] = rname1[0] = '\0';
s1 = e1 = UINT32_MAX;
q1 = 255;
sscanf(line, "%s %u %u %s %hhu", cname1, &s1, &e1, rname1, &q1);
if (is_read_pair(rname0, rname1)) {
buff = 0;
if (q0 < mq || q1 < mq)
continue;
err0 = sd_coordinate_conversion(dict, sd_get(sdict, cname0), s0 / 2 + e0 / 2 + (s0 & 1 && e0 & 1), &i0, &p0, count_gap);
err1 = sd_coordinate_conversion(dict, sd_get(sdict, cname1), s1 / 2 + e1 / 2 + (s1 & 1 && e1 & 1), &i1, &p1, count_gap);
if (err0 != CC_SUCCESS) {
if (err0 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname0, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname0);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname0);
}
} else if (err0 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__,
cname0, s0 / 2 + e0 / 2 + (s0 & 1 && e0 & 1));
}
} else if (err1 != CC_SUCCESS) {
if (err1 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname1, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname1);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname1);
}
} else if (err1 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__,
cname1, s1 / 2 + e1 / 2 + (s1 & 1 && e1 & 1));
}
} else {
if (strcmp(dict->s[i0].name, dict->s[i1].name) <= 0)
fprintf(fo, "0\t%s\t%lu\t0\t1\t%s\t%lu\t1\n", dict->s[i0].name, p0 >> scale, dict->s[i1].name, p1 >> scale);
else
fprintf(fo, "0\t%s\t%lu\t1\t1\t%s\t%lu\t0\n", dict->s[i1].name, p1 >> scale, dict->s[i0].name, p0 >> scale);
++pair_c;
}
} else {
buff = 1;
strcpy(cname0, cname1);
s0 = s1;
e0 = e1;
q0 = q1;
strcpy(rname0, rname1);
}
}
}
fprintf(stderr, "[I::%s] %ld read pairs processed\n", __func__, pair_c);
for (k = 0; k < kh_end(hmseq); ++k)
if (kh_exist(hmseq, k))
free((char *) kh_key(hmseq, k));
kh_destroy(str, hmseq);
iostream_close(fp);
asm_destroy(dict);
sd_destroy(sdict);
return 0;
}
static int make_juicer_pre_file_from_pa5(char *f, char *agp, char *fai, int8_t mq, int scale, int count_gap, FILE *fo)
{
iostream_t *fp;
char *line;
char cname0[4096], cname1[4096];
uint32_t x0, x1, i0, i1;
uint64_t p0, p1, rec_c, pair_c;
uint8_t q0, q1;
CC_ERR_t err0, err1;
khash_t(str) *hmseq; // for absent sequences
khint_t k;
int absent;
hmseq = kh_init(str);
sdict_t *sdict = make_sdict_from_index(fai, 0);
asm_dict_t *dict = agp? make_asm_dict_from_agp(sdict, agp, 1) : make_asm_dict_from_sdict(sdict);
fp = iostream_open(f);
if (fp == NULL) {
fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, f);
exit(EXIT_FAILURE);
}
rec_c = pair_c = 0;
while ((line = iostream_getline(fp)) != NULL) {
if (is_empty_line(line))
continue;
if (++rec_c % 1000000 == 0)
fprintf(stderr, "[I::%s] %lu million records processed, %lu read pairs \n", __func__, rec_c / 1000000, pair_c);
cname0[0] = cname1[0] = '\0';
x0 = x1 = UINT32_MAX;
q0 = q1 = 255;
sscanf(line, "%*s %s %u %s %u %hhu %hhu", cname0, &x0, cname1, &x1, &q0, &q1);
if (q0 < mq || q1 < mq)
continue;
err0 = sd_coordinate_conversion(dict, sd_get(sdict, cname0), x0, &i0, &p0, count_gap);
err1 = sd_coordinate_conversion(dict, sd_get(sdict, cname1), x1, &i1, &p1, count_gap);
if (err0 != CC_SUCCESS) {
if (err0 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname0, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname0);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname0);
}
} else if (err0 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__, cname0, x0);
}
} else if (err1 != CC_SUCCESS) {
if (err1 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname1, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname1);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname1);
}
} else if (err1 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__, cname1, x1);
}
} else {
if (strcmp(dict->s[i0].name, dict->s[i1].name) <= 0)
fprintf(fo, "0\t%s\t%lu\t0\t1\t%s\t%lu\t1\n", dict->s[i0].name, p0 >> scale, dict->s[i1].name, p1 >> scale);
else
fprintf(fo, "0\t%s\t%lu\t1\t1\t%s\t%lu\t0\n", dict->s[i1].name, p1 >> scale, dict->s[i0].name, p0 >> scale);
++pair_c;
}
}
fprintf(stderr, "[I::%s] %ld read pairs processed\n", __func__, pair_c);
for (k = 0; k < kh_end(hmseq); ++k)
if (kh_exist(hmseq, k))
free((char *) kh_key(hmseq, k));
kh_destroy(str, hmseq);
iostream_close(fp);
asm_destroy(dict);
sd_destroy(sdict);
return 0;
}
static char *parse_bam_rec(bam1_t *b, bam_header_t *h, uint8_t q, int32_t *s, int32_t *e, char **cname)
{
// 0x4 0x100 0x200 0x400 0x800
if ((b->core.flag & 0xF04) || (!b->core.flag) || b->core.qual < q)
return 0;
*cname = h->target_name[b->core.tid];
*s = b->core.pos;
*e = get_target_end(b);
return strdup(bam1_qname(b));
}
static int parse_bam_rec1(bam1_t *b, bam_header_t *h, char **cname0, int32_t *s0, char **cname1, int32_t *s1)
{
// 0x4 0x8 0x40 0x100 0x200 0x400 0x800
if ((b->core.flag & 0xF4C) || (!b->core.flag))
return 1;
*cname0 = h->target_name[b->core.tid];
*s0 = b->core.pos;
*cname1 = h->target_name[b->core.mtid];
*s1 = b->core.mpos;
return 0;
}
static int make_juicer_pre_file_from_bam(char *f, char *agp, char *fai, uint8_t mq, int scale, int count_gap, FILE *fo)
{
bamFile fp;
bam_header_t *h;
bam1_t *b;
char *cname0, *cname1, *rname0, *rname1;
int32_t s0, s1, e0, e1;
uint32_t i0, i1;
uint64_t p0, p1, rec_c, pair_c;
int8_t buff;
enum bam_sort_order so;
CC_ERR_t err0, err1;
khash_t(str) *hmseq; // for absent sequences
khint_t k;
int absent;
hmseq = kh_init(str);
sdict_t *sdict = make_sdict_from_index(fai, 0);
asm_dict_t *dict = agp? make_asm_dict_from_agp(sdict, agp, 1) : make_asm_dict_from_sdict(sdict);
fp = bam_open(f, "r");
if (fp == NULL) {
fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, f);
exit(EXIT_FAILURE);
}
h = bam_header_read(fp);
b = bam_init1();
so = bam_hrecs_sort_order(h);
cname0 = cname1 = rname0 = rname1 = 0;
s0 = s1 = e0 = e1 = 0;
i0 = i1 = 0;
p0 = p1 = 0;
rec_c = pair_c = 0;
buff = 0;
if (so == ORDER_NAME) {
// sorted by read names
while (bam_read1(fp, b) >= 0 ) {
if (++rec_c % 1000000 == 0)
fprintf(stderr, "[I::%s] %lu million records processed, %lu read pairs \n", __func__, rec_c / 1000000, pair_c);
if (buff == 0) {
rname0 = parse_bam_rec(b, h, mq, &s0, &e0, &cname0);
if (!rname0)
continue;
++buff;
} else if (buff == 1) {
rname1 = parse_bam_rec(b, h, mq, &s1, &e1, &cname1);
if (!rname1)
continue;
if (strcmp(rname0, rname1) == 0) {
buff = 0;
err0 = sd_coordinate_conversion(dict, sd_get(sdict, cname0), s0 / 2 + e0 / 2 + (s0 & 1 && e0 & 1), &i0, &p0, count_gap);
err1 = sd_coordinate_conversion(dict, sd_get(sdict, cname1), s1 / 2 + e1 / 2 + (s1 & 1 && e1 & 1), &i1, &p1, count_gap);
if (err0 != CC_SUCCESS) {
if (err0 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname0, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname0);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname0);
}
} else if (err0 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__,
cname0, s0 / 2 + e0 / 2 + (s0 & 1 && e0 & 1));
}
} else if (err1 != CC_SUCCESS) {
if (err1 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname1, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname1);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname1);
}
} else if (err1 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__,
cname1, s1 / 2 + e1 / 2 + (s1 & 1 && e1 & 1));
}
} else {
if (strcmp(dict->s[i0].name, dict->s[i1].name) <= 0)
fprintf(fo, "0\t%s\t%lu\t0\t1\t%s\t%lu\t1\n", dict->s[i0].name, p0 >> scale, dict->s[i1].name, p1 >> scale);
else
fprintf(fo, "0\t%s\t%lu\t1\t1\t%s\t%lu\t0\n", dict->s[i1].name, p1 >> scale, dict->s[i0].name, p0 >> scale);
++pair_c;
}
free(rname0);
free(rname1);
rname0 = 0;
rname1 = 0;
} else {
buff = 1;
cname0 = cname1;
s0 = s1;
e0 = e1;
free(rname0);
rname0 = rname1;
rname1 = 0;
}
}
}
} else {
// sorted by coordinates or others
if (mq > 0)
fprintf(stderr, "[W::%s] BAM file is not sorted by read name. Filtering by mapping quality %hhu suppressed \n", __func__, mq);
while (bam_read1(fp, b) >= 0 ) {
if (++rec_c % 1000000 == 0)
fprintf(stderr, "[I::%s] %lu million records processed, %lu read pairs \n", __func__, rec_c / 1000000, pair_c);
if(parse_bam_rec1(b, h, &cname0, &s0, &cname1, &s1))
continue;
err0 = sd_coordinate_conversion(dict, sd_get(sdict, cname0), s0, &i0, &p0, count_gap);
err1 = sd_coordinate_conversion(dict, sd_get(sdict, cname1), s1, &i1, &p1, count_gap);
if (err0 != CC_SUCCESS) {
if (err0 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname0, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname0);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname0);
}
} else if (err0 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__, cname0, s0);
}
} else if (err1 != CC_SUCCESS) {
if (err1 == SEQ_NOT_FOUND) {
k = kh_put(str, hmseq, cname1, &absent);
if (absent) {
kh_key(hmseq, k) = strdup(cname1);
fprintf(stderr, "[W::%s] sequence \"%s\" not found \n", __func__, cname1);
}
} else if (err1 == POS_NOT_IN_RANGE) {
fprintf(stderr, "[W::%s] sequence position \"%s:%u\" not in range \n", __func__, cname1, s1);
}
} else {
if (strcmp(dict->s[i0].name, dict->s[i1].name) <= 0)
fprintf(fo, "0\t%s\t%lu\t0\t1\t%s\t%lu\t1\n", dict->s[i0].name, p0 >> scale, dict->s[i1].name, p1 >> scale);
else
fprintf(fo, "0\t%s\t%lu\t1\t1\t%s\t%lu\t0\n", dict->s[i1].name, p1 >> scale, dict->s[i0].name, p0 >> scale);
++pair_c;
}
}
}
fprintf(stderr, "[I::%s] %lu read pairs processed\n", __func__, pair_c);
for (k = 0; k < kh_end(hmseq); ++k)
if (kh_exist(hmseq, k))
free((char *) kh_key(hmseq, k));
kh_destroy(str, hmseq);
if (rname0)
free(rname0);
if (rname1)
free(rname1);
bam_destroy1(b);
bam_header_destroy(h);
bam_close(fp);
asm_destroy(dict);
sd_destroy(sdict);
return 0;
}
static uint64_t assembly_annotation(const char *f, sdict_t *sdict, const char *out_agp, const char *out_annot,
const char *out_lift, int *scale, uint64_t max_s, uint64_t *g)
{
uint32_t i, j, c, n, m;
int *seqs;
FILE *fo_agp, *fo_annot, *fo_lift;
uint64_t genome_size, scaled_gs;
asm_dict_t *dict;
sd_seg_t seg;
fo_agp = fopen(out_agp, "w");
fo_annot = fopen(out_annot, "w");
fo_lift = fopen(out_lift, "w");
dict = make_asm_dict_from_agp(sdict, f, 1);
n = dict->u + dict->n;
seqs = (int *) calloc(n, sizeof(int));
genome_size = 0;
c = j = 0;
m = dict->s[j++].n;
for (i = 0; i < dict->u; ++i) {
seg = dict->seg[i];
fprintf(fo_agp, "assembly\t%lu\t%lu\t%u\t%s\t%s\t%u\t%u\t%s\n",
genome_size + 1, genome_size + seg.y, i + 1, agp_component_type_val(seg.t),
sdict->s[seg.c>>1].name, seg.x + 1, seg.x + seg.y, agp_orientation_val(seg.r));
fprintf(fo_annot, ">ctg%08u.1 %u %u\n", i + 1, i + 1, seg.y);
fprintf(fo_lift, "ctg%08u.1\t%u\t%u\t%u\t%s\t%s\t%u\t%u\t%s\n",
i + 1, 1, seg.y, 1, agp_component_type_val(seg.t), sdict->s[seg.c>>1].name,
seg.x + 1, seg.x + seg.y, agp_orientation_val(AGP_OT_PLUS));
if (i == m) {
m += dict->s[j++].n;
++c;
}
seqs[c++] = (int) (i + 1) * (seg.r == AGP_OT_MINUS? -1 : 1);
genome_size += seg.y;
}
for (i = 0; i < n; ++i) {
// seqs[n - 1] is always 0
if (seqs[i]) {
fprintf(fo_annot, "%d", seqs[i]);
fputc(seqs[i + 1]? ' ' : '\n', fo_annot);
}
}
fclose(fo_agp);
fclose(fo_annot);
fclose(fo_lift);
free(seqs);
asm_destroy(dict);
scaled_gs = linear_scale(genome_size, scale, max_s);
*g = genome_size;
return scaled_gs;
}
uint64_t assembly_scale_max_seq(asm_dict_t *dict, int *scale, uint64_t max_s, uint64_t *g)
{
uint32_t i;
uint64_t s;
sd_aseq_t seq;
s = 0;
for (i = 0; i < dict->n; ++i) {
seq = dict->s[i];
s = MAX(s, seq.len + seq.gap);
}
*g = s;
return linear_scale(s, scale, max_s);
}
static void print_help_pre(FILE *fp_help)
{
fprintf(fp_help, "Usage: juicer pre [options] <hic.bed>|<hic.bam>|<hic.pa5>|<hic.bin> <scaffolds.agp> <contigs.fa.fai>\n");
fprintf(fp_help, "Options:\n");
fprintf(fp_help, " -a preprocess for assembly mode\n");
fprintf(fp_help, " -q INT minimum mapping quality [10]\n");
fprintf(fp_help, " -o STR output file prefix (required for '-a' mode) [stdout]\n");
fprintf(fp_help, " --file-type STR input file type BED|BAM|PA5|BIN, file name extension is ignored if set\n");
fprintf(fp_help, " --version show version number\n");
}
static ko_longopt_t long_options[] = {
{ "file-type", ko_required_argument, 301 },
{ "seq-ctype", ko_required_argument, 302 },
{ "gap-ctype", ko_required_argument, 303 },
{ "gap-link", ko_required_argument, 304 },
{ "gap-size", ko_required_argument, 305 },
{ "help", ko_no_argument, 'h' },
{ "version", ko_no_argument, 'V' },
{ 0, 0, 0 }
};
static int main_pre(int argc, char *argv[])
{
FILE *fo;
char *fai, *agp, *agp1, *link_file, *out, *out1, *annot, *lift, *ext1, *ext2;
int mq, asm_mode;;
enum fileTypes f_type;
liftrlimit();
jc_realtime0 = realtime();
const char *opt_str = "q:ao:Vh";
ketopt_t opt = KETOPT_INIT;
int c, ret;
FILE *fp_help = stderr;
fai = agp = agp1 = link_file = out = out1 = annot = lift = 0;
mq = 10;
asm_mode = 0;
f_type = NOSET;
while ((c = ketopt(&opt, argc, argv, 1, opt_str, long_options)) >= 0) {
if (c == 'o') {
out = opt.arg;
} else if (c == 'q') {
mq = atoi(opt.arg);
} else if (c == 'a') {
asm_mode = 1;
} else if (c == 301) {
if (strcasecmp(opt.arg, "BED") == 0)
f_type = BED;
else if (strcasecmp(opt.arg, "BAM") == 0)
f_type = BAM;
else if (strcasecmp(opt.arg, "BIN") == 0)
f_type = BIN;
else if (strcasecmp(opt.arg, "PA5") == 0)
f_type = PA5;
else {
fprintf(stderr, "[E::%s] unknown file type: \"%s\"\n", __func__, opt.arg);
return 1;
}
} else if (c == 'h') {
fp_help = stdout;
} else if (c == 'V') {
puts(JUICER_VERSION);
return 0;
} else if (c == '?') {
fprintf(stderr, "[E::%s] unknown option: \"%s\"\n", __func__, argv[opt.i - 1]);
return 1;
} else if (c == ':') {
fprintf(stderr, "[E::%s] missing option: \"%s\"\n", __func__, argv[opt.i - 1]);
return 1;
}
}
if (fp_help == stdout) {
print_help_pre(stdout);
return 0;
}
if (asm_mode && !out) {
fprintf(stderr, "[E::%s] missing input: -o option is required for assembly mode (-a)\n", __func__);
return 1;
}
if (argc - opt.ind < 3) {
fprintf(stderr, "[E::%s] missing input: three positional options required\n", __func__);
print_help_pre(stderr);
return 1;
}
if (mq < 0 || mq > 255) {
fprintf(stderr, "[E::%s] invalid mapping quality threshold: %d\n", __func__, mq);
return 1;
}
uint8_t mq8;
mq8 = (uint8_t) mq;
link_file = argv[opt.ind];
agp = argv[opt.ind + 1];
fai = argv[opt.ind + 2];
if (f_type == NOSET) {
ext1 = strlen(link_file) >= 4? (link_file + strlen(link_file) - 4) : NULL;
ext2 = strlen(link_file) >= 7? (link_file + strlen(link_file) - 7) : NULL;
if (ext1 && !strcasecmp(ext1, ".bam")) f_type = BAM;
else if (ext1 && !strcasecmp(ext1, ".bin")) f_type = BIN;
else if ((ext1 && !strcasecmp(ext1, ".bed")) || (ext2 && !strcasecmp(ext2, ".bed.gz"))) f_type = BED;
else if ((ext1 && !strcasecmp(ext1, ".pa5")) || (ext2 && !strcasecmp(ext2, ".pa5.gz"))) f_type = PA5;
else {
fprintf(stderr, "[E::%s] unknown link file format. File extension .bam, .bed, .pa5 or .bin or --file-type is expected\n", __func__);
exit(EXIT_FAILURE);
}
}
if (f_type == BIN && (strcmp(link_file, "-") == 0 || *link_file == '<')) {
fprintf(stderr, "[E::%s] BIN file format from STDIN is not supported\n", __func__);
exit(EXIT_FAILURE);
}
if (out) {
out1 = (char *) malloc(strlen(out) + 35);
sprintf(out1, "%s.txt", out);
}
fo = out1 == 0? stdout : fopen(out1, "w");
if (fo == 0) {
fprintf(stderr, "[E::%s] cannot open file %s for writing\n", __func__, out);
exit(EXIT_FAILURE);
}
ret = 0;
sdict_t *sdict;
asm_dict_t *dict;
int scale;
uint64_t max_s, scaled_s;
sdict = make_sdict_from_index(fai, 0);
scale = 0;
max_s = scaled_s = 0;
agp1 = (char *) malloc(MAX(strlen(agp), out? strlen(out) : 0) + 35);
if (asm_mode) {
annot = (char *) malloc(strlen(out) + 35);
lift = (char *) malloc(strlen(out) + 35);
sprintf(agp1, "%s.assembly.agp", out);
sprintf(annot, "%s.assembly", out);
sprintf(lift, "%s.liftover.agp", out);
scaled_s = assembly_annotation(agp, sdict, agp1, annot, lift, &scale, (uint64_t) INT_MAX, &max_s);
dict = make_asm_dict_from_agp(sdict, agp1, 1);
} else {
sprintf(agp1, "%s", agp);
dict = make_asm_dict_from_agp(sdict, agp1, 1);
scaled_s = assembly_scale_max_seq(dict, &scale, (uint64_t) INT_MAX, &max_s);
}
if (f_type == BAM) {
fprintf(stderr, "[I::%s] make juicer pre input from BAM file %s\n", __func__, link_file);
ret = make_juicer_pre_file_from_bam(link_file, agp1, fai, mq8, scale, !asm_mode, fo);
} else if (f_type == BED) {
fprintf(stderr, "[I::%s] make juicer pre input from BED file %s\n", __func__, link_file);
ret = make_juicer_pre_file_from_bed(link_file, agp1, fai, mq8, scale, !asm_mode, fo);
} else if (f_type == PA5) {
fprintf(stderr, "[I::%s] make juicer pre input from PA5 file %s\n", __func__, link_file);
ret = make_juicer_pre_file_from_pa5(link_file, agp1, fai, mq8, scale, !asm_mode, fo);
} else if (f_type == BIN) {
fprintf(stderr, "[I::%s] make juicer pre input from BIN file %s\n", __func__, link_file);
ret = make_juicer_pre_file_from_bin(link_file, agp1, fai, mq8, scale, !asm_mode, fo);
}
if (asm_mode) {
fprintf(stderr, "[I::%s] genome size: %lu\n", __func__, max_s);
fprintf(stderr, "[I::%s] scale factor: %d\n", __func__, 1 << scale);
fprintf(stderr, "[I::%s] chromosome sizes for juicer_tools pre -\n", __func__);
fprintf(stderr, "PRE_C_SIZE: assembly %lu\n", scaled_s);
fprintf(stderr, "[I::%s] JUICER_PRE CMD: java -Xmx36G -jar ${juicer_tools} pre %s %s.hic <(echo \"assembly %lu\")\n",
__func__, out1, out, scaled_s);
} else {
if (scale) {
fprintf(stderr, "[W::%s] maximum scaffold length exceeds %d (=%lu)\n", __func__, INT_MAX, max_s);
fprintf(stderr, "[W::%s] using scale factor: %d\n", __func__, 1 << scale);
}
fprintf(stderr, "[I::%s] chromosome sizes for juicer_tools pre -\n", __func__);
uint32_t i;
sd_aseq_t seq;
for (i = 0; i < dict->n; ++i) {
seq = dict->s[i];
fprintf(stderr, "PRE_C_SIZE: %s %lu\n", seq.name, (seq.len + seq.gap) >> scale);
}
}
asm_destroy(dict);
sd_destroy(sdict);
if (out != 0)
fclose(fo);
if (out1)
free(out1);
if (agp1)
free(agp1);
if (annot)
free(annot);
if (lift)
free(lift);
fprintf(stderr, "[I::%s] Version: %s\n", __func__, JUICER_VERSION);
fprintf(stderr, "[I::%s] CMD: juicer", __func__);
int i;
for (i = 0; i < argc; ++i)
fprintf(stderr, " %s", argv[i]);
fprintf(stderr, "\n[I::%s] Real time: %.3f sec; CPU: %.3f sec; Peak RSS: %.3f GB\n", __func__,
realtime() - jc_realtime0, cputime(), peakrss() / 1024.0 / 1024.0 / 1024.0);
return ret;
}
static int assembly_to_agp(char *assembly, char *lift, sdict_t *sdict, FILE *fo)
{
asm_dict_t *dict;
iostream_t *fp;
char *line;
char cname[1024], c0[1024], c1[1024], *cstr;
int32_t cid, sid, fid, sign;
uint32_t i, clen, mlen, s, p, *coords;
int64_t slen;
size_t m;
kvec_t(int) segs;
dict = make_asm_dict_from_agp(sdict, lift, 1);
m = 4;
coords = (uint32_t *) malloc(sizeof(uint32_t) * m * 3);
c0[0] = c1[0] = '\0';
mlen = 0;
sid = 0;
kv_init(segs);
fp = iostream_open(assembly);
if (fp == NULL) {
fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, assembly);
exit(EXIT_FAILURE);
}
while ((line = iostream_getline(fp)) != NULL) {
if (is_empty_line(line) || line[0] != '>')
continue;
sscanf(line, "%s %d %u", cname, &cid, &clen);
cstr = strstr(cname, ":::");
if (cstr != NULL) {
cname[cstr - cname] = '\0';
}
strcpy(c1, cname + 1);
if (!strcmp(c0, c1)) {
mlen += clen;
} else {
mlen = clen;
strcpy(c0, c1);
}
if (sd_coordinate_rev_conversion(dict, asm_sd_get(dict, c1), mlen - clen, &s, &p, 0) != CC_SUCCESS) {
fprintf(stderr, "[E::%s] coordinates conversion error %s %u\n", __func__, c1, mlen - clen);
exit(EXIT_FAILURE);
}
if (clen == 0)
fprintf(stderr, "[W::%s] segment of length zero in line: %s\n", __func__, line);
if (cid > m) {
m <<= 1;
coords = (uint32_t *) realloc(coords, sizeof(uint32_t) * m * 3);
}
cid -= 1;
coords[cid * 3] = s;
coords[cid * 3 + 1] = p + 1; // 0-based to 1-based coordinates
coords[cid * 3 + 2] = clen;
}
iostream_close(fp);
fp = iostream_open(assembly);
if (fp == NULL) {
fprintf(stderr, "[E::%s] cannot open file %s for reading\n", __func__, assembly);
exit(EXIT_FAILURE);
}
while ((line = iostream_getline(fp)) != NULL) {
if (is_empty_line(line) || line[0] == '>')
continue;
segs.n = 0;
char *eptr, *fptr;
cid = strtol(line, &eptr, 10);
if (coords[(abs(cid) - 1) * 3 + 2] > 0)
kv_push(int, segs, cid);
while (*eptr && *eptr != '\n') {
cid = strtol(eptr + 1, &fptr, 10);
if (coords[(abs(cid) - 1) * 3 + 2] > 0)
kv_push(int, segs, cid);
eptr = fptr;
}
if (segs.n == 0) continue;
++sid;
fid = 0;
slen = 0;
for (i = 0; i < segs.n; ++i) {
cid = segs.a[i];
sign = cid > 0;
cid = abs(cid) - 1;
fprintf(fo, "scaffold_%d\t%ld\t%ld\t%d\t%s\t%s\t%d\t%d\t%s\n", sid, slen + 1,
slen + coords[cid * 3 + 2], ++fid, agp_component_type_val(DEFAULT_AGP_SEQ_COMPONENT_TYPE),
sdict->s[coords[cid * 3]].name, coords[cid * 3 + 1], coords[cid * 3 + 1] + coords[cid * 3 + 2] - 1,
sign? agp_orientation_val(AGP_OT_PLUS) : agp_orientation_val(AGP_OT_MINUS));
slen += coords[cid * 3 + 2];
if (i != segs.n - 1) {
fprintf(fo, "scaffold_%d\t%ld\t%ld\t%d\t%s\t%d\t%s\t%s\t%s\n", sid, slen + 1,
slen + DEFAULT_AGP_GAP_SIZE, ++fid, agp_component_type_val(DEFAULT_AGP_GAP_COMPONENT_TYPE),
DEFAULT_AGP_GAP_SIZE, agp_gap_type_val(DEFAULT_AGP_GAP_TYPE), agp_linkage_val(AGP_LG_YES),
agp_linkage_evidence_val(DEFAULT_AGP_LINKAGE_EVIDENCE));
slen += DEFAULT_AGP_GAP_SIZE;
}
}
}
iostream_close(fp);
free(coords);
kv_destroy(segs);
asm_destroy(dict);
return 0;
}
static void print_help_post(FILE *fp_help)
{
fprintf(fp_help, "Usage: juicer post [options] <review.assembly> <liftover.agp> <contigs.fa[.fai]>\n");
fprintf(fp_help, "Options:\n");
fprintf(fp_help, " -o STR output file prefix (required for scaffolds FASTA output) [stdout]\n");
fprintf(fp_help, " --seq-ctype STR AGP output sequence component type [%s]\n", agp_component_type_val(DEFAULT_AGP_SEQ_COMPONENT_TYPE));
fprintf(fp_help, " --gap-ctype STR AGP output gap component type [%s]\n", agp_component_type_val(DEFAULT_AGP_GAP_COMPONENT_TYPE));
fprintf(fp_help, " --gap-link STR AGP output gap linkage evidence [%s]\n", agp_linkage_evidence_val(DEFAULT_AGP_LINKAGE_EVIDENCE));
fprintf(fp_help, " --gap-size INT AGP output gap size between sequence component [%d]\n", DEFAULT_AGP_GAP_SIZE);
fprintf(fp_help, " --version show version number\n");
}
static int main_post(int argc, char *argv[])
{
FILE *fo;
char *fa, *fa1, *out, *out1;
liftrlimit();
jc_realtime0 = realtime();
const char *opt_str = "o:Vh";
ketopt_t opt = KETOPT_INIT;
int c, ret, is_fai;
FILE *fp_help = stderr;
sdict_t *sdict;
fa = fa1 = out = out1 = 0;
while ((c = ketopt(&opt, argc, argv, 1, opt_str, long_options)) >= 0) {
if (c == 302) {
DEFAULT_AGP_SEQ_COMPONENT_TYPE = agp_component_type_key(opt.arg);
if (DEFAULT_AGP_SEQ_COMPONENT_TYPE == AGP_CT_N ||
DEFAULT_AGP_SEQ_COMPONENT_TYPE == AGP_CT_U)
fprintf(stderr, "[W::%s] a GAP component identifier will be used for sequences: %s\n",
__func__, opt.arg);
} else if (c == 303) {
DEFAULT_AGP_GAP_COMPONENT_TYPE = agp_component_type_key(opt.arg);
if (DEFAULT_AGP_GAP_COMPONENT_TYPE != AGP_CT_N &&
DEFAULT_AGP_GAP_COMPONENT_TYPE != AGP_CT_U)
fprintf(stderr, "[W::%s] a SEQ component identifier will be used for gaps: %s\n",
__func__, opt.arg);
} else if (c == 304) {
DEFAULT_AGP_LINKAGE_EVIDENCE = agp_linkage_evidence_key(opt.arg);
} else if (c == 305) {
DEFAULT_AGP_GAP_SIZE = atoi(opt.arg);
} else if (c == 'o') {
out = opt.arg;
} else if (c == 'h') {
fp_help = stdout;
} else if (c == 'V') {
puts(JUICER_VERSION);
return 0;
} else if (c == '?') {
fprintf(stderr, "[E::%s] unknown option: \"%s\"\n", __func__, argv[opt.i - 1]);
return 1;
} else if (c == ':') {
fprintf(stderr, "[E::%s] missing option: \"%s\"\n", __func__, argv[opt.i - 1]);
return 1;
}
}
if (fp_help == stdout) {
print_help_post(stdout);
return 0;
}
if (argc - opt.ind < 3) {
fprintf(stderr, "[E::%s] missing input: three positional options required\n", __func__);
print_help_post(stderr);
return 1;
}
if (out) {
out1 = (char *) malloc(strlen(out) + 35);
sprintf(out1, "%s.FINAL.agp", out);
}
fo = out1 == 0? stdout : fopen(out1, "w");
if (fo == 0) {
fprintf(stderr, "[E::%s] cannot open file %s for writing\n", __func__, out);