-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.c
2726 lines (2656 loc) · 115 KB
/
generic.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
#include "generic.h"
#include <stdio.h>
int8_t seq_comp_table[16] = { 0, 8, 4, 12, 2, 10, 9, 14, 1, 6, 5, 13, 3, 11, 7, 15 };
//Debug Tools
typedef struct Node {
int number;
int count;
struct Node* next;
} Node;
Node* head = NULL;
// Function to search for a number in the list and update its count
Node* searchAndUpdate(Node* head, int number) {
Node* current = head;
while (current != NULL) {
if (current->number == number) {
current->count++;
return head;
}
current = current->next;
}
// Number not found, create a new node and add it to the list
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->number = number;
newNode->count = 1;
newNode->next = head;
return newNode;
}
// Function to display the occurrence count of numbers in the list
void displayList(Node* head) {
Node* current = head;
printf("Number\tCount\n");
while (current != NULL) {
printf("%d\t%d\n", current->number, current->count);
current = current->next;
}
}
// Function to free the memory allocated for the list
void freeList(Node* head) {
Node* current = head;
while (current != NULL) {
Node* next = current->next;
free(current);
current = next;
}
}
/* definitions of functions */
//The iteres functions.
char *get_filename_without_ext(char *filename) {
char *s;
s = malloc(strlen(filename) + 1);
strcpy(s, filename);
char *dot = strrchr(s, '.');
if(!dot || dot == s) return s;
*dot = '\0';
return s;
}
char *get_filename_ext(char *filename) {
char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
bool is_file(const char* path) {
struct stat buf;
stat(path, &buf);
return S_ISREG(buf.st_mode);
}
bool is_dir(const char* path) {
struct stat buf;
stat(path, &buf);
return S_ISDIR(buf.st_mode);
}
double cal_rpkm (unsigned long long int reads_count, unsigned long long int total_length, unsigned long long int mapped_reads_num) {
return reads_count / (mapped_reads_num * 1e-9 * total_length);
}
double cal_rpm (unsigned long long int reads_count, unsigned long long int mapped_reads_num) {
return reads_count / (mapped_reads_num * 1e-6 );
}
struct lineFile *lineFileOpen2(char *fileName, bool zTerm){
/* Open up a lineFile or die trying. */
if (is_dir(fileName))
errAbort("Error: %s is a directory not a file", fileName);
struct lineFile *lf = lineFileMayOpen(fileName, zTerm);
if (lf == NULL)
errAbort("Couldn't open %s , %s", fileName, strerror(errno));
return lf;
}
void writeReport(char *outfile, unsigned long long int *cnt, unsigned int mapQ, char *subfam){
FILE *f = mustOpen(outfile, "w");
fprintf(f, "total reads (pair): %llu\n", cnt[0]);
//fprintf(f, "read ends 1: %llu\n", cnt[0]);
//fprintf(f, "read ends 2: %llu\n", cnt[1]);
//fprintf(f, "mapped read ends 1: %llu\n", cnt[2]);
//fprintf(f, "mapped read ends 2: %llu\n", cnt[3]);
//fprintf(f, "used read ends 1: %llu\n", cnt[4]);
//fprintf(f, "used read ends 2: %llu\n", cnt[5]);
fprintf(f, "mappable reads (pair): %llu\n", cnt[6]);
//fprintf(f, "non-redundant mappable reads (pair): %llu\n", cnt[8]);
fprintf(f, "uniquely mapped reads (pair) (mapQ >= %u): %llu\n", mapQ, cnt[7]);
fprintf(f, "non-redundant uniquely mapped reads (pair): %llu\n", cnt[11]);
fprintf(f, "mapped reads (pair) overlap with repeats but discarded due to mapped to different subfamilies: %llu\n", cnt[12]);
fprintf(f, "mapped reads (pair) overlap with [%s] repeats: %llu\n", subfam, cnt[9]);
fprintf(f, "uniquely mapped reads (pair) overlap with [%s] repeats: %llu\n", subfam, cnt[10]);
carefulClose(&f);
}
void writeWigandStat(struct hash *hash, struct hash *hash1, struct hash *hash2, char *of1, char *of2, char *of3, char *of4, char *of5, unsigned long long int reads_num, unsigned long int reads_num_unique){
FILE *f1 = mustOpen(of1, "w");
FILE *f2 = mustOpen(of2, "w");
FILE *f5 = mustOpen(of5, "w");
unsigned int m;
struct hashEl *helr;
struct hashCookie cookier = hashFirst(hash);
// Write the stat file
fprintf(f1, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#subfamily", "family", "class", "consensus_length", "reads_count", "unique_reads_count", "total_length", "genome_count", "all_reads_RPKM", "all_reads_RPM", "unique_reads_RPKM", "unique_reads_RPM");
while ( (helr = hashNext(&cookier)) != NULL ) {
struct rep *or = (struct rep *) (helr->val);
fprintf(f1, "%s\t%s\t%s\t%u\t%llu\t%llu\t%llu\t%llu\t%.3f\t%.3f\t%.3f\t%.3f\n", or->name, or->fname, or->cname, or->length, or->read_count, or->read_count_unique, or->total_length, or->genome_count, cal_rpkm(or->read_count, or->total_length, reads_num), cal_rpm(or->read_count, reads_num), cal_rpkm(or->read_count_unique, or->total_length, reads_num_unique), cal_rpm(or->read_count_unique, reads_num_unique));
// Write the wig file
if (or->length != 0){
fprintf(f2, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name);
fprintf(f5, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name);
for (m = 0; m < or->length; m++){
fprintf(f2, "%u\n", (or->bp_total)[m]);
fprintf(f5, "%u\n", (or->bp_total_unique)[m]);
}
}
}
carefulClose(&f2);
carefulClose(&f1);
carefulClose(&f5);
FILE *f3 = mustOpen(of3, "w");
struct hashEl *hel3;
struct hashCookie cookier3 = hashFirst(hash1);
fprintf(f3, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#family", "class", "reads_count", "unique_reads_count", "total_length", "genome_count", "all_reads_RPKM", "all_reads_RPM", "unique_reads_RPKM", "unique_reads_RPM");
while ( (hel3 = hashNext(&cookier3)) != NULL) {
struct repfam *or = (struct repfam *) hel3->val;
fprintf(f3, "%s\t%s\t%llu\t%llu\t%llu\t%llu\t%.3f\t%.3f\t%.3f\t%.3f\n", or->fname, or->cname, or->read_count, or->read_count_unique, or->total_length, or->genome_count, cal_rpkm(or->read_count, or->total_length, reads_num), cal_rpm(or->read_count, reads_num), cal_rpkm(or->read_count_unique, or->total_length, reads_num_unique), cal_rpm(or->read_count_unique, reads_num_unique));
}
carefulClose(&f3);
FILE *f4 = mustOpen(of4, "w");
struct hashEl *hel4;
struct hashCookie cookier4 = hashFirst(hash2);
fprintf(f4, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#class", "reads_count", "unique_reads_count", "total_length", "genome_count", "all_reads_RPKM", "all_reads_RPM", "unique_reads_RPKM", "unique_reads_RPM");
while ( (hel4 = hashNext(&cookier4)) != NULL) {
struct repcla *or = (struct repcla *) hel4->val;
fprintf(f4, "%s\t%llu\t%llu\t%llu\t%llu\t%.3f\t%.3f\t%.3f\t%.3f\n", or->cname, or->read_count, or->read_count_unique, or->total_length, or->genome_count, cal_rpkm(or->read_count, or->total_length, reads_num), cal_rpm(or->read_count, reads_num), cal_rpkm(or->read_count_unique, or->total_length, reads_num_unique), cal_rpm(or->read_count_unique, reads_num_unique));
}
carefulClose(&f4);
}
void writeWigandStatCage(struct hash *hash, struct hash *hash1, struct hash *hash2, char *of1, char *of2, char *of6, char *of3, char *of4, char *of5, char *of7, unsigned long long int reads_num, unsigned long int reads_num_unique){
fprintf(stderr, "Test stats and Wig file %s. \n", of1);
FILE *ftest = fopen("asample.txt", "w");
fprintf(stderr, "Test example.stat \n");
FILE *f1;
f1 = mustOpen(of1, "w");
FILE *f2 = mustOpen(of2, "w"); // outWig_ALL_+
FILE *f5 = mustOpen(of5, "w"); // outWig_Uniq_+
FILE *f6 = mustOpen(of6, "w"); // outWig_ALL_-
FILE *f7 = mustOpen(of7, "w"); // outWig_Uniq_-
unsigned int m;
struct hashEl *helr;
struct hashCookie cookier = hashFirst(hash);
// Write the stat file
fprintf(f1, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#subfamily", "family", "class", "consensus_length", "reads_count", "unique_reads_count", "total_length", "genome_count", "all_reads_RPKM", "all_reads_RPM", "unique_reads_RPKM", "unique_reads_RPM");
while ( (helr = hashNext(&cookier)) != NULL ) {
struct rep *or = (struct rep *) (helr->val);
fprintf(f1, "%s\t%s\t%s\t%u\t%llu\t%llu\t%llu\t%llu\t%.3f\t%.3f\t%.3f\t%.3f\n", or->name, or->fname, or->cname, or->length, or->read_count, or->read_count_unique, or->total_length, or->genome_count, cal_rpkm(or->read_count, or->total_length, reads_num), cal_rpm(or->read_count, reads_num), cal_rpkm(or->read_count_unique, or->total_length, reads_num_unique), cal_rpm(or->read_count_unique, reads_num_unique));
// Write the wig file
if (or->length != 0){
fprintf(f6, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name); // outWig_ALL_-
fprintf(f7, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name); // outWig_Uniq_-
for (m = 0; m < or->length; m++){
fprintf(f6, "%u\n", (or->bp_total_minus)[m]);
fprintf(f7, "%u\n", (or->bp_total_unique_minus)[m]);
}
fprintf(f2, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name); // outWig_ALL_+
fprintf(f5, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name); // outWig_Uniq_+
for (m = 0; m < or->length; m++){
fprintf(f2, "%u\n", (or->bp_total_plus)[m]);
fprintf(f5, "%u\n", (or->bp_total_unique_plus)[m]);
}
}
}
carefulClose(&f2);
carefulClose(&f1);
carefulClose(&f5);
FILE *f3 = mustOpen(of3, "w");
struct hashEl *hel3;
struct hashCookie cookier3 = hashFirst(hash1);
fprintf(f3, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#family", "class", "reads_count", "unique_reads_count", "total_length", "genome_count", "all_reads_RPKM", "all_reads_RPM", "unique_reads_RPKM", "unique_reads_RPM");
while ( (hel3 = hashNext(&cookier3)) != NULL) {
struct repfam *or = (struct repfam *) hel3->val;
fprintf(f3, "%s\t%s\t%llu\t%llu\t%llu\t%llu\t%.3f\t%.3f\t%.3f\t%.3f\n", or->fname, or->cname, or->read_count, or->read_count_unique, or->total_length, or->genome_count, cal_rpkm(or->read_count, or->total_length, reads_num), cal_rpm(or->read_count, reads_num), cal_rpkm(or->read_count_unique, or->total_length, reads_num_unique), cal_rpm(or->read_count_unique, reads_num_unique));
}
carefulClose(&f3);
FILE *f4 = mustOpen(of4, "w");
struct hashEl *hel4;
struct hashCookie cookier4 = hashFirst(hash2);
fprintf(f4, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#class", "reads_count", "unique_reads_count", "total_length", "genome_count", "all_reads_RPKM", "all_reads_RPM", "unique_reads_RPKM", "unique_reads_RPM");
while ( (hel4 = hashNext(&cookier4)) != NULL) {
struct repcla *or = (struct repcla *) hel4->val;
fprintf(f4, "%s\t%llu\t%llu\t%llu\t%llu\t%.3f\t%.3f\t%.3f\t%.3f\n", or->cname, or->read_count, or->read_count_unique, or->total_length, or->genome_count, cal_rpkm(or->read_count, or->total_length, reads_num), cal_rpm(or->read_count, reads_num), cal_rpkm(or->read_count_unique, or->total_length, reads_num_unique), cal_rpm(or->read_count_unique, reads_num_unique));
}
carefulClose(&f4);
}
void MREwriteWigandStat(struct hash *hash, struct hash *hash1, struct hash *hash2, char *of1, char *of2, char *of3, char *of4){
FILE *f1 = mustOpen(of1, "w");
FILE *f2 = mustOpen(of2, "w");
unsigned int m;
struct hashEl *helr;
struct hashCookie cookier = hashFirst(hash);
fprintf(f1, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "#subfamily", "family", "class", "consensus_length", "covered_CpG_sites", "CpG_total_score", "total_length", "genome_count");
while ( (helr = hashNext(&cookier)) != NULL ) {
struct rep *or = (struct rep *) (helr->val);
fprintf(f1, "%s\t%s\t%s\t%u\t%u\t%.4f\t%llu\t%llu\n", or->name, or->fname, or->cname, or->length, or->cpgCount, or->cpgTotalScore, or->total_length, or->genome_count);
if (or->length != 0){
fprintf(f2, "fixedStep chrom=%s start=1 step=1 span=1\n", or->name);
for (m = 0; m < or->length; m++){
fprintf(f2, "%.4f\n", (or->cpgScore)[m]);
}
}
}
carefulClose(&f2);
carefulClose(&f1);
FILE *f3 = mustOpen(of3, "w");
struct hashEl *hel3;
struct hashCookie cookier3 = hashFirst(hash1);
fprintf(f3, "%s\t%s\t%s\t%s\t%s\t%s\n", "#family", "class", "covered_CpG_sites", "CpG_total_score", "total_length", "genome_count");
while ( (hel3 = hashNext(&cookier3)) != NULL) {
struct repfam *or = (struct repfam *) hel3->val;
fprintf(f3, "%s\t%s\t%u\t%.4f\t%llu\t%llu\n", or->fname, or->cname, or->cpgCount, or->cpgTotalScore, or->total_length, or->genome_count);
}
carefulClose(&f3);
FILE *f4 = mustOpen(of4, "w");
struct hashEl *hel4;
struct hashCookie cookier4 = hashFirst(hash2);
fprintf(f4, "%s\t%s\t%s\t%s\t%s\n", "#class", "covered_CpG_sites", "CpG_total_score", "total_length", "genome_count");
while ( (hel4 = hashNext(&cookier4)) != NULL) {
struct repcla *or = (struct repcla *) hel4->val;
fprintf(f4, "%s\t%u\t%.4f\t%llu\t%llu\n", or->cname, or->cpgCount, or->cpgTotalScore, or->total_length, or->genome_count);
}
carefulClose(&f4);
}
unsigned long long int *samFile2nodupRepbedFile(char *samfile, struct hash *chrHash, struct hash *hashRmsk, struct hash *hashRep, struct hash *hashFam, struct hash *hashCla, int isSam, unsigned int mapQ, int filter, int rmDup, int addChr) {
samfile_t *samfp;
char chr[100], prn[500], key[100], strand;
unsigned int start, end, cend, rstart, rend;
unsigned long long int *cnt = malloc(sizeof(unsigned long long int) * 5);
unsigned long long int mapped_reads_num = 0, reads_num = 0, reads_used = 0, unique_reads = 0, repeat_reads = 0;
struct hash *nochr = newHash(0), *dup = newHash(0);
if (isSam) {
if ( (samfp = samopen(samfile, "r", 0)) == 0) {
fprintf(stderr, "Fail to open SAM file %s\n", samfile);
errAbort("Error\n");
}
} else {
if ( (samfp = samopen(samfile, "rb", 0)) == 0) {
fprintf(stderr, "Fail to open BAM file %s\n", samfile);
errAbort("Error\n");
}
}
strcpy(prn, "empty");
bam1_t *b;
bam_header_t *h;
h = samfp->header;
b = bam_init1();
while ( samread(samfp, b) >= 0) {
//if ( sameString (bam1_qname(b), prn))
// continue;
reads_num++;
if ((reads_num % 10000) == 0)
fprintf(stderr, "\r* Processed reads: %llu", reads_num);
//strcpy(prn, bam1_qname(b));
//if (b->core.tid < 0)
if (b->core.flag & BAM_FUNMAP)
continue;
if (b->core.qual < mapQ)
continue;
mapped_reads_num++;
//change chr name to chr1, chr2 ...
strcpy(chr, h->target_name[b->core.tid]);
if (addChr){
if (startsWith("GL", h->target_name[b->core.tid])) {
continue;
} else if (sameWord(h->target_name[b->core.tid], "MT")) {
strcpy(chr,"chrM");
} else if (!startsWith("chr", h->target_name[b->core.tid])) {
strcpy(chr, "chr");
strcat(chr, h->target_name[b->core.tid]);
}
}
struct hashEl *he = hashLookup(nochr, chr);
if (he != NULL)
continue;
cend = (unsigned int) (hashIntValDefault(chrHash, chr, 2) - 1);
if (cend == 1){
hashAddInt(nochr, chr, 1);
warn("* Warning: reads mapped to chromosome %s will be discarded as %s not existed in the chromosome size file", chr, chr);
continue;
}
reads_used++;
start = (unsigned int) b->core.pos;
int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
end = min(cend, (unsigned int)tmpend);
strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
//remove dup first
if (rmDup == 1){
if (sprintf(key, "%s:%u:%u:%c", chr, start, end, strand) < 0)
errAbort("Mem ERROR");
struct hashEl *hel = hashLookup(dup, key);
if (hel == NULL) {
hashAddInt(dup, key, 1);
} else {
continue;
}
}
unique_reads++;
//transfer coordinates
int i, j;
unsigned int qlen = end - start;
struct binElement *hitList = NULL, *hit;
struct hashEl *hel2 = hashLookup(hashRmsk, chr);
if (hel2 != NULL) {
struct binKeeper *bs2 = (struct binKeeper *) hel2->val;
hitList = binKeeperFind(bs2, start, end);
if(hitList != NULL) {
for (hit = hitList; hit !=NULL; hit = hit->next) {
struct rmsk *ss = (struct rmsk *) hit->val;
if (filter == 0){
struct hashEl *hel3 = hashLookup(hashRep, ss->name);
if (hel3 != NULL){
struct rep *rs = (struct rep *) hel3->val;
rs->read_count++;
if (rs->length != 0){
rstart = start - ss->start;
rstart = (rstart < 0) ? 0 : rstart;
rend = rstart + qlen;
rend = (rend < ss->end) ? rend : ss->end;
for (i = rstart; i < rend; i++) {
j = i + ss->consensus_start;
if (j >= ss->consensus_end) {
break;
}
if (j >= rs->length) {
break;
}
(rs->bp_total)[j]++;
}
}
}
//fill hashFam
struct hashEl *hel4 = hashLookup(hashFam, ss->fname);
if (hel4 != NULL) {
struct repfam *fs = (struct repfam *) hel4->val;
fs->read_count++;
}
//fill hashCla
struct hashEl *hel5 = hashLookup(hashCla, ss->cname);
if (hel5 != NULL) {
struct repcla *cs = (struct repcla *) hel5->val;
cs->read_count++;
}
} else {
slNameAddHead(&(ss->sl), bam1_qname(b));
}
break;
}
repeat_reads++;
slFreeList(hitList);
}
}
}
fprintf(stderr, "\r* Processed reads: %llu\n", reads_num);
samclose(samfp);
bam_destroy1(b);
freeHash(&nochr);
freeHash(&dup);
cnt[0] = reads_num;
cnt[1] = mapped_reads_num;
cnt[2] = reads_used;
cnt[3] = unique_reads;
cnt[4] = repeat_reads;
return cnt;
}
float getCov(unsigned int aStart, unsigned int aEnd, unsigned int start, unsigned int end){
float overlap = positiveRangeIntersection((int)aStart, (int)aEnd, (int)start, (int)end);
float denominator = (float)(aEnd - aStart);
float cov = (denominator == 0) ? 0.0 : overlap/denominator;
return cov;
}
int mapped2diffSubfam(struct hash *hashRmsk, char *subfam, int nm, char *ahstring, int qlen){
char *row[100], *row2[4];
int i, nm2, start, end, num2;
struct binElement *hitList = NULL, *hit;
//an example string
//XA:Z:chr9,-69070599,36M,1;chr20,-29616939,36M,1;chr9,+68450226,36M,2;chrUn_gl000219,+94584,36M,2;chrUn_gl000241,+31782,36M,2;
//fprintf(stderr, "row %s\n", ahstring);
int numFields = chopByChar(ahstring, ';', row, ArraySize(row));
for(i=0; i<numFields; i++){
if (strlen(row[i]) > 0){
//fprintf(stderr, "row[i] %s\n", row[i]);
num2 = chopByChar(row[i], ',', row2, ArraySize(row2));
//if (num2 != 4){
// fprintf(stderr, "num2 %i\n", num2);
// exit(2);
//}
assert(num2 == 4);
nm2 = (int) strtol(row2[3], 0, 0);
if (nm2 <= nm){ // hmm..., FIXME?
start = abs((int)strtol(row2[1], 0, 0));
end = start + qlen; //FIXME
struct hashEl *hel2 = hashLookup(hashRmsk,row2[0]);
if (hel2 != NULL) {
struct binKeeper *bs2 = (struct binKeeper *) hel2->val;
hitList = binKeeperFind(bs2, start, end);
if(hitList != NULL) {
for (hit = hitList; hit !=NULL; hit = hit->next) {
struct rmsk *ss = (struct rmsk *) hit->val;
if (!sameWord(ss->name, subfam)){
return 1;
}
}
}
}
}
}
}
return 0;
}
//samFile, no s, this function is for the filter.
unsigned long long int *samFile2nodupRepbedFileNew(char *samfile, struct hash *chrHash, struct hash *hashRmsk, struct hash *hashRep, struct hash *hashFam, struct hash *hashCla, int isSam, unsigned int mapQ, int filter, int rmDup, int addChr, int discardWrongEnd, unsigned int iSize, unsigned int extension, float minCoverage, int treat, char *outbed, char *outbed_unique, int diffSubfam) {
samfile_t *samfp;
FILE *outbed_f = NULL, *outbed_unique_f = NULL;
char chr[100], key[100], strand, ahstring[2000];
int nm;
unsigned int start, end, cend, rstart, rend;
unsigned long long int *cnt = malloc(sizeof(unsigned long long int) * 13);
//unsigned long long int mapped_reads_num = 0, reads_num = 0, reads_used = 0, unique_reads = 0, repeat_reads = 0;
unsigned long long int read_end1 = 0, read_end2 = 0;
unsigned long long int read_end1_mapped = 0, read_end2_mapped = 0;
unsigned long long int read_end1_used = 0, read_end2_used = 0;
unsigned long long int reads_nonredundant = 0;
unsigned long long int reads_nonredundant_unique = 0;
unsigned long long int reads_mapped = 0;
unsigned long long int reads_mapped_unique = 0;
unsigned long long int reads_repeat = 0;
unsigned long long int reads_repeat_unique = 0;
unsigned long long int reads_diff_subfam = 0;
struct hash *nochr = newHash(0), *dup = newHash(0);
if (isSam) {
if ( (samfp = samopen(samfile, "r", 0)) == 0) {
fprintf(stderr, "Fail to open SAM file %s\n", samfile);
errAbort("Error\n");
}
} else {
if ( (samfp = samopen(samfile, "rb", 0)) == 0) {
fprintf(stderr, "Fail to open BAM file %s\n", samfile);
errAbort("Error\n");
}
}
//strcpy(prn, "empty");
if (outbed != NULL)
outbed_f = mustOpen(outbed, "w");
if (outbed_unique != NULL)
outbed_unique_f = mustOpen(outbed_unique, "w");
bam1_t *b;
bam_header_t *h;
h = samfp->header;
b = bam_init1();
while ( samread(samfp, b) >= 0) {
//if ( sameString (bam1_qname(b), prn))
// continue;
if (b->core.flag & BAM_FPAIRED) {
if (b->core.flag & BAM_FREAD1){
read_end1++;
}else{
if(treat)
read_end1++;
else
read_end2++;
}
}else{
read_end1++;
}
if (((read_end1 + read_end2) % 10000) == 0)
fprintf(stderr, "\r* Processed read ends: %llu", (read_end1 + read_end2));
//strcpy(prn, bam1_qname(b));
//if (b->core.tid < 0)
if (b->core.flag & BAM_FUNMAP)
continue;
//if (b->core.qual < mapQ)
// continue;
if (b->core.flag & BAM_FPAIRED) {
if (b->core.flag & BAM_FREAD1){
read_end1_mapped++;
}else{
if (treat)
read_end1_mapped++;
else
read_end2_mapped++;
}
}else{
read_end1_mapped++;
}
//change chr name to chr1, chr2 ...
strcpy(chr, h->target_name[b->core.tid]);
if (addChr){
if (startsWith("GL", h->target_name[b->core.tid])) {
continue;
} else if (sameWord(h->target_name[b->core.tid], "MT")) {
strcpy(chr,"chrM");
} else if (!startsWith("chr", h->target_name[b->core.tid])) {
strcpy(chr, "chr");
strcat(chr, h->target_name[b->core.tid]);
}
}
//check Ref reads mapped to existed in chromosome size file or not
struct hashEl *he = hashLookup(nochr, chr);
if (he != NULL)
continue;
cend = (unsigned int) (hashIntValDefault(chrHash, chr, 2) - 1);
if (cend == 1){
hashAddInt(nochr, chr, 1);
warn("* Warning: read ends mapped to chromosome %s will be discarded as %s not existed in the chromosome size file", chr, chr);
continue;
}
if (b->core.flag & BAM_FPAIRED) {
if (b->core.flag & BAM_FREAD1){
read_end1_used++;
}else{
if (treat)
read_end1_used++;
else
read_end2_used++;
}
}else{
read_end1_used++;
}
//get mapping location for paired-end or single-end
if (treat){
reads_mapped++;
if (b->core.qual >= mapQ)
reads_mapped_unique++;
start = (unsigned int) b->core.pos;
int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
end = min(cend, (unsigned int)tmpend);
strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
if (extension) {
if (strand == '+'){
end = min(start + extension, cend);
}else{
if (end < extension)
start = 0;
else
start = end - extension;
//start = max(end - extension, 0);
}
}
}else{
if (b->core.flag & BAM_FPAIRED) {
if (!(b->core.flag & BAM_FMUNMAP)){
if (b->core.flag & BAM_FREAD1){
if (abs(b->core.isize) > iSize || b->core.isize == 0){
continue;
}else{
reads_mapped++;
if (b->core.qual >= mapQ)
reads_mapped_unique++;
if (b->core.isize > 0){
start = (unsigned int) b->core.pos;
strand = '+';
int tmpend = start + b->core.isize;
end = min(cend, (unsigned int)tmpend);
}else{
start = (unsigned int) b->core.mpos;
strand = '-';
int tmpend = start - b->core.isize;
end = min(cend, (unsigned int)tmpend);
}
}
}else{
continue;
}
}else{
if (discardWrongEnd){
continue;
}else{
reads_mapped++;
if (b->core.qual >= mapQ)
reads_mapped_unique++;
start = (unsigned int) b->core.pos;
int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
end = min(cend, (unsigned int)tmpend);
strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
if (extension) {
if (strand == '+'){
end = min(start + extension, cend);
}else{
if (end < extension)
start = 0;
else
start = end - extension;
//start = max(end - extension, 0);
}
}
}
}
}else{
reads_mapped++;
if (b->core.qual >= mapQ)
reads_mapped_unique++;
start = (unsigned int) b->core.pos;
int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
end = min(cend, (unsigned int)tmpend);
strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
if (extension) {
if (strand == '+'){
end = min(start + extension, cend);
}else{
if (end < extension)
start = 0;
else
start = end - extension;
//start = max(end - extension, 0);
}
}
}
}
//remove dup first
if (rmDup){
//redundant only useful for unique reads
if (b->core.qual >= mapQ){
if (sprintf(key, "%s:%u:%u:%c", chr, start, end, strand) < 0)
errAbort("Mem ERROR");
}
struct hashEl *hel = hashLookup(dup, key);
if (hel == NULL) {
hashAddInt(dup, key, 1);
} else {
continue;
}
}
//reads_nonredundant++;
if (b->core.qual >= mapQ)
reads_nonredundant_unique++;
//output bed
if (outbed_f){
fprintf(outbed_f, "%s\t%u\t%u\t%s\t%i\t%c", chr, start, end, bam1_qname(b), b->core.qual, strand);
if(bam_aux_get(b, "XA")){
fprintf(outbed_f, "\t%i\t%s", bam_aux2i(bam_aux_get(b, "NM")), bam_aux2Z(bam_aux_get(b, "XA")) );
}
fprintf(outbed_f, "\n");
}
if (outbed_unique_f){
if(b->core.qual >= mapQ){
fprintf(outbed_unique_f, "%s\t%u\t%u\t%s\t%i\t%c\n", chr, start, end, bam1_qname(b), b->core.qual, strand);
}
}
//transfer coordinates
int i, j;
int index = 0, tindex = 0;
float coverage = 0.0, tcoverage = 0.0;
unsigned int qlen = end - start;
struct rmsk *ss = NULL;
struct binElement *hitList = NULL, *hit;
struct hashEl *hel2 = hashLookup(hashRmsk, chr);
if (hel2 != NULL) {
struct binKeeper *bs2 = (struct binKeeper *) hel2->val;
hitList = binKeeperFind(bs2, start, end);
if(hitList != NULL) {
for (hit = hitList; hit !=NULL; hit = hit->next) {
index++;
struct rmsk *sss = (struct rmsk *) hit->val;
float cov = getCov(start, end, sss->start, sss->end);
//fprintf(stderr, "coverage: %.2f\n", cov);
if (cov > coverage){
tindex = index;
tcoverage = cov;
}
coverage = cov;
}
if (tcoverage < minCoverage)
continue;
index = 0;
for (hit = hitList; hit !=NULL; hit = hit->next) {
index++;
if (index == tindex){
ss = (struct rmsk *) hit->val;
break;
}
}
//filter reads mapped to differetn subfam
if (diffSubfam){
//filter reads mapped to different subfamiles with same NM
if(bam_aux_get(b, "XA")){
strcpy(ahstring, bam_aux2Z(bam_aux_get(b, "XA")));
nm = bam_aux2i(bam_aux_get(b, "NM"));
if (mapped2diffSubfam(hashRmsk, ss->name, nm, ahstring, (int)qlen)){
reads_diff_subfam++;
continue;
}
}
}
if (filter == 0){
struct hashEl *hel3 = hashLookup(hashRep, ss->name);
if (hel3 != NULL){
struct rep *rs = (struct rep *) hel3->val;
rs->read_count++;
if (b->core.qual >= mapQ)
rs->read_count_unique++;
if (rs->length != 0){
rstart = start - ss->start;
rstart = (rstart < 0) ? 0 : rstart;
rend = rstart + qlen;
rend = (rend < ss->end) ? rend : ss->end;
for (i = rstart; i < rend; i++) {
j = i + ss->consensus_start;
if (j >= ss->consensus_end) {
break;
}
if (j >= rs->length) {
break;
}
(rs->bp_total)[j]++;
if (b->core.qual >= mapQ)
(rs->bp_total_unique)[j]++;
}
}
}
//fill hashFam
struct hashEl *hel4 = hashLookup(hashFam, ss->fname);
if (hel4 != NULL) {
struct repfam *fs = (struct repfam *) hel4->val;
fs->read_count++;
if (b->core.qual >= mapQ)
fs->read_count_unique++;
}
//fill hashCla
struct hashEl *hel5 = hashLookup(hashCla, ss->cname);
if (hel5 != NULL) {
struct repcla *cs = (struct repcla *) hel5->val;
cs->read_count++;
if (b->core.qual >= mapQ)
cs->read_count_unique++;
}
} else {
slNameAddHead(&(ss->sl), bam1_qname(b));
if (b->core.qual >= mapQ)
slNameAddHead(&(ss->sl_unique), bam1_qname(b));
}
reads_repeat++;
if (b->core.qual >= mapQ)
reads_repeat_unique++;
slFreeList(hitList);
}
}
}
fprintf(stderr, "\r* Processed read ends: %llu\n", (read_end1 + read_end2));
samclose(samfp);
bam_destroy1(b);
freeHash(&nochr);
freeHash(&dup);
if (outbed_f)
carefulClose(&outbed_f);
if (outbed_unique_f)
carefulClose(&outbed_unique_f);
cnt[0] = read_end1;
cnt[1] = read_end2;
cnt[2] = read_end1_mapped;
cnt[3] = read_end2_mapped;
cnt[4] = read_end1_used;
cnt[5] = read_end2_used;
cnt[6] = reads_mapped;
cnt[7] = reads_mapped_unique;
cnt[8] = reads_nonredundant;
cnt[9] = reads_repeat;
cnt[10] = reads_repeat_unique;
cnt[11] = reads_nonredundant_unique;
cnt[12] = reads_diff_subfam;
return cnt;
}
//This is Cage-specific for the filter.
unsigned long long int *samFile2nodupRepbedFileNewCage(char *samfile, struct hash *chrHash, struct hash *hashRmsk, struct hash *hashRep, struct hash *hashFam, struct hash *hashCla, int isSam, unsigned int mapQ, int filter, int rmDup, int addChr, int discardWrongEnd, unsigned int iSize, unsigned int extension, float minCoverage, int treat, char *outbed, char *outbed_unique, int diffSubfam, int optcagewindow) {
samfile_t *samfp;
FILE *outbed_f = NULL, *outbed_unique_f = NULL;
char chr[100], key[100], strand, ahstring[2000];
int nm;
unsigned int start, end, cend, rstart, rend;
unsigned long long int *cnt = malloc(sizeof(unsigned long long int) * 13);
//unsigned long long int mapped_reads_num = 0, reads_num = 0, reads_used = 0, unique_reads = 0, repeat_reads = 0;
unsigned long long int read_end1 = 0, read_end2 = 0;
unsigned long long int read_end1_mapped = 0, read_end2_mapped = 0;
unsigned long long int read_end1_used = 0, read_end2_used = 0;
unsigned long long int reads_nonredundant = 0;
unsigned long long int reads_nonredundant_unique = 0;
unsigned long long int reads_mapped = 0;
unsigned long long int reads_mapped_unique = 0;
unsigned long long int reads_repeat = 0;
unsigned long long int reads_repeat_unique = 0;
unsigned long long int reads_diff_subfam = 0;
struct hash *nochr = newHash(0), *dup = newHash(0);
if (isSam) {
if ( (samfp = samopen(samfile, "r", 0)) == 0) {
fprintf(stderr, "Fail to open SAM file %s\n", samfile);
errAbort("Error\n");
}
} else {
if ( (samfp = samopen(samfile, "rb", 0)) == 0) {
fprintf(stderr, "Fail to open BAM file %s\n", samfile);
errAbort("Error\n");
}
}
//strcpy(prn, "empty");
if (outbed != NULL)
outbed_f = mustOpen(outbed, "w");
if (outbed_unique != NULL)
outbed_unique_f = mustOpen(outbed_unique, "w");
bam1_t *b;
bam_header_t *h;
h = samfp->header;
b = bam_init1();
while ( samread(samfp, b) >= 0) {
//if ( sameString (bam1_qname(b), prn))
// continue;
if (b->core.flag & BAM_FPAIRED) {
if (b->core.flag & BAM_FREAD1){
read_end1++;
}else{
if(treat)
read_end1++;
else
read_end2++;
}
}else{
read_end1++;
}
if (((read_end1 + read_end2) % 10000) == 0)
fprintf(stderr, "\r* Processed read ends: %llu", (read_end1 + read_end2));
//strcpy(prn, bam1_qname(b));
//if (b->core.tid < 0)
if (b->core.flag & BAM_FUNMAP)
continue;
//if (b->core.qual < mapQ)
// continue;
if (b->core.flag & BAM_FPAIRED) {
if (b->core.flag & BAM_FREAD1){
read_end1_mapped++;
}else{
if (treat)
read_end1_mapped++;
else
read_end2_mapped++;
}
}else{
read_end1_mapped++;
}
//change chr name to chr1, chr2 ...
strcpy(chr, h->target_name[b->core.tid]);
if (addChr){
if (startsWith("GL", h->target_name[b->core.tid])) {
continue;
} else if (sameWord(h->target_name[b->core.tid], "MT")) {
strcpy(chr,"chrM");
} else if (!startsWith("chr", h->target_name[b->core.tid])) {
strcpy(chr, "chr");
strcat(chr, h->target_name[b->core.tid]);
}
}
//check Ref reads mapped to existed in chromosome size file or not
struct hashEl *he = hashLookup(nochr, chr);
if (he != NULL)
continue;
cend = (unsigned int) (hashIntValDefault(chrHash, chr, 2) - 1);
if (cend == 1){
hashAddInt(nochr, chr, 1);
warn("* Warning: read ends mapped to chromosome %s will be discarded as %s not existed in the chromosome size file", chr, chr);
continue;
}
if (b->core.flag & BAM_FPAIRED) {
if (b->core.flag & BAM_FREAD1){
read_end1_used++;
}else{
if (treat)
read_end1_used++;
else
read_end2_used++;
}
}else{
read_end1_used++;
}
//get mapping location for paired-end or single-end
// if (treat){
// reads_mapped++;
// if (b->core.qual >= mapQ)
// reads_mapped_unique++;
// start = (unsigned int) b->core.pos;
// int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
// end = min(cend, (unsigned int)tmpend);
// strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
// if (extension) {
// if (strand == '+'){
// end = min(start + extension, cend);
// }else{
// if (end < extension)
// start = 0;
// else
// start = end - extension;
// //start = max(end - extension, 0);
// }
// }
//
// }else{
// if (b->core.flag & BAM_FPAIRED) {
// if (!(b->core.flag & BAM_FMUNMAP)){
// if (b->core.flag & BAM_FREAD1){
// if (abs(b->core.isize) > iSize || b->core.isize == 0){
// continue;
// }else{
// reads_mapped++;
// if (b->core.qual >= mapQ)
// reads_mapped_unique++;
// if (b->core.isize > 0){
// start = (unsigned int) b->core.pos;
// strand = '+';
// int tmpend = start + b->core.isize;
// end = min(cend, (unsigned int)tmpend);
// }else{
// start = (unsigned int) b->core.mpos;
// strand = '-';
// int tmpend = start - b->core.isize;
// end = min(cend, (unsigned int)tmpend);
// }
// }
// }else{
// continue;
// }
// }else{
// if (discardWrongEnd){
// continue;
// }else{
// reads_mapped++;
// if (b->core.qual >= mapQ)
// reads_mapped_unique++;
// start = (unsigned int) b->core.pos;
// int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
// end = min(cend, (unsigned int)tmpend);
// strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
// if (extension) {
// if (strand == '+'){
// end = min(start + extension, cend);
// }else{
// if (end < extension)
// start = 0;
// else
// start = end - extension;
// //start = max(end - extension, 0);
// }
// }
// }
// }
// }else{
// reads_mapped++;
// if (b->core.qual >= mapQ)
// reads_mapped_unique++;
// start = (unsigned int) b->core.pos;
// int tmpend = b->core.n_cigar? bam_calend(&b->core, bam1_cigar(b)) : b->core.pos + b->core.l_qseq;
// end = min(cend, (unsigned int)tmpend);
// strand = (b->core.flag&BAM_FREVERSE)? '-' : '+';
// if (extension) {
// if (strand == '+'){