-
Notifications
You must be signed in to change notification settings - Fork 3
/
PretextMap.cpp
2120 lines (1804 loc) · 60.2 KB
/
PretextMap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2019 Ed Harry, Wellcome Sanger Institute
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.
*/
#define String_(x) #x
#define String(x) String_(x)
#define PretextMap_Version "PretextMap Version " String(PV)
#include "Header.h"
#include <math.h>
global_variable
u08
Licence[] = R"lic(
Copyright (c) 2019 Ed Harry, Wellcome Sanger Institute
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.
)lic";
global_variable
u08
ThirdParty[] = R"thirdparty(
Third-party software and resources used in this project, along with their respective licenses:
libdeflate (https://github.com/ebiggers/libdeflate)
Copyright 2016 Eric Biggers
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.
mpc (https://github.com/orangeduck/mpc)
Licensed Under BSD
Copyright (c) 2013, Daniel Holden All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of
the authors and should not be interpreted as representing official policies, either
expressed or implied, of the FreeBSD Project.
stb_sprintf (https://github.com/nothings/stb/blob/master/stb_sprintf.h)
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
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 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.
stb_dxt (https://github.com/nothings/stb/blob/master/stb_dxt.h)
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
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 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.
)thirdparty";
global_variable
u08
Status_Marco_Expression_Sponge = 0;
global_variable
char
Message_Buffer[1024];
#define PrintError(message, ...) \
{ \
stbsp_snprintf(Message_Buffer, 512, message, ##__VA_ARGS__); \
stbsp_snprintf(Message_Buffer + 512, 512, "[PretextMap error] :: %s\n", Message_Buffer); \
fprintf(stderr, "%s", Message_Buffer + 512); \
} \
Status_Marco_Expression_Sponge = 0
#define PrintStatus(message, ...) \
{ \
stbsp_snprintf(Message_Buffer, 512, message, ##__VA_ARGS__); \
stbsp_snprintf(Message_Buffer + 512, 512, "[PretextMap status] :: %s\n", Message_Buffer); \
fprintf(stdout, "%s", Message_Buffer + 512); \
} \
Status_Marco_Expression_Sponge = 0
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wreserved-id-macro"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wextra-semi-stmt"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wconditional-uninitialized"
#pragma GCC diagnostic ignored "-Wdouble-promotion"
#pragma GCC diagnostic ignored "-Wpadded"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#define STB_SPRINTF_IMPLEMENTATION
#include "stb_sprintf.h"
#pragma clang diagnostic pop
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wpadded"
#include "mpc/mpc.h"
#pragma clang diagnostic pop
global_variable
memory_arena
Working_Set;
global_variable
thread_pool *
Thread_Pool;
#include "LineBufferQueue.cpp"
global_variable
threadSig
Number_of_Contigs = 0;
global_variable
volatile u64
Total_Genome_Length = 0;
global_variable
threadSig
Number_of_Header_Lines = 0;
global_function
void
IncramentNumberOfHeaderLines()
{
__sync_fetch_and_add(&Number_of_Header_Lines, 1);
}
global_function
void
DecrementNumberHeaderLines()
{
__sync_fetch_and_sub(&Number_of_Header_Lines, 1);
}
struct
contig_preprocess_node
{
u64 length;
u32 pad;
u32 hashedName;
u32 name[16];
contig_preprocess_node *next;
};
struct
contig
{
u64 length;
u64 previousCumulativeLength;
u32 name[16];
};
#define Contig_Hash_Table_Size 2609
#define Contig_Hash_Table_Seed 6697139165994551568
global_function
u08 *
PushStringIntoIntArray(u32 *intArray, u32 arrayLength, u08 *string, u08 stringTerminator = '\0')
{
u08 *stringToInt = (u08 *)intArray;
u32 stringLength = 0;
while(*string != stringTerminator && stringLength < (arrayLength << 2))
{
*(stringToInt++) = *(string++);
++stringLength;
}
while (stringLength & 3)
{
*(stringToInt++) = 0;
++stringLength;
}
for ( u32 index = (stringLength >> 2);
index < arrayLength;
++index )
{
intArray[index] = 0;
}
return(string);
}
global_function
u32
GetHashedContigName(u32 *name, u32 nInts)
{
return(FastHash32((void *)name, (u64)(nInts * 4), Contig_Hash_Table_Seed) % Contig_Hash_Table_Size);
}
struct
contig_hash_table_node
{
u32 index;
u32 pad;
contig_hash_table_node *next;
};
global_variable
contig *
Contigs = 0;
global_variable
contig_hash_table_node **
Contig_Hash_Table = 0;
global_function
void
InitiateContigHashTable()
{
ForLoop(Contig_Hash_Table_Size)
{
*(Contig_Hash_Table + index) = 0;
}
}
global_function
void
InsertContigIntoHashTable(u32 index, u32 hash, contig_hash_table_node **table = Contig_Hash_Table)
{
contig_hash_table_node *node = table[hash];
contig_hash_table_node *nextNode = node ? node->next : 0;
while (nextNode)
{
node = nextNode;
nextNode = node->next;
}
contig_hash_table_node *newNode = PushStruct(Working_Set, contig_hash_table_node);
newNode->index = index;
newNode->next = 0;
(node ? node->next : table[hash]) = newNode;
}
global_function
u32
ContigHashTableLookup(u32 *name, u32 nameLength, contig **cont, contig *contigs = Contigs, contig_hash_table_node **table = Contig_Hash_Table)
{
u32 result = 1;
contig_hash_table_node *node = table[GetHashedContigName(name, nameLength)];
if (node)
{
while (!AreNullTerminatedStringsEqual(name, (contigs + node->index)->name, nameLength))
{
if (node->next)
{
node = node->next;
}
else
{
result = 0;
break;
}
}
if (result)
{
*cont = contigs + node->index;
}
}
else
{
result = 0;
}
return(result);
}
global_variable
volatile contig_preprocess_node *
First_Contig_Preprocess_Node = 0;
global_variable
volatile contig_preprocess_node *
Current_Contig_Preprocess_Node = 0;
global_variable
volatile memory_arena_snapshot
Contig_Preprocess_SnapShot;
global_variable
mutex
Working_Set_rwMutex;
global_variable
u32
Min_Map_Quality = 10;
global_variable
u32
Max_Image_Depth = 15;
#define Min_Image_Depth 10
#define Number_of_LODs (Max_Image_Depth - Min_Image_Depth + 1)
#define Pixel_Resolution(depth) (1 << (depth))
global_variable
volatile u16***
Images;
global_variable
u32**
Image_Histograms;
global_variable
f32**
Image_Norm_Scalars;
global_function
void
InitaliseImages()
{
Images = PushArray(Working_Set, volatile u16**, Number_of_LODs);
Image_Histograms = PushArray(Working_Set, u32*, Number_of_LODs);
Image_Norm_Scalars = PushArray(Working_Set, f32*, Number_of_LODs);
for ( u32 depth = Max_Image_Depth, imIndex = 0;
depth >= Min_Image_Depth;
--depth, ++imIndex )
{
Images[imIndex] = PushArray(Working_Set, volatile u16*, Pixel_Resolution(depth));
Image_Histograms[imIndex] = PushArray(Working_Set, u32, 1 << 8);
memset(Image_Histograms[imIndex], 0, (1 << 8) * sizeof(u32));
Image_Norm_Scalars[imIndex] = PushArray(Working_Set, f32, Pixel_Resolution(depth));
memset(Image_Norm_Scalars[imIndex], 0, Pixel_Resolution(depth) * sizeof(f32));
ForLoop(Pixel_Resolution(depth))
{
Images[imIndex][index] = PushArray(Working_Set, volatile u16, Pixel_Resolution(depth) - index);
ForLoop2(Pixel_Resolution(depth) - index)
{
Images[imIndex][index][index2] = 0;
}
}
}
}
global_function
void
AddReadPairToImage(u64 read1, u64 read2)
{
f64 factor = 1.0 / (f64)Total_Genome_Length;
u32 pixel1 = (u32)((f64)read1 * factor * (f64)Pixel_Resolution(Max_Image_Depth));
u32 pixel2 = (u32)((f64)read2 * factor * (f64)Pixel_Resolution(Max_Image_Depth));
u32 min = Min(pixel1, pixel2);
u32 max = Max(pixel1, pixel2);
volatile u16 *pixel = &Images[0][min][max - min];
#ifndef _WIN32
volatile u16 oldVal = __atomic_fetch_add(pixel, 1, 0);
#else
volatile u16 oldVal = __atomic_fetch_add((volatile unsigned long *)pixel, 1, 0);
#endif
if (oldVal == u16_max)
{
#ifndef _WIN32
__atomic_store(pixel, &oldVal, 0);
#else
__atomic_store((volatile unsigned long *)pixel, (volatile unsigned long *)&oldVal, 0);
#endif
}
}
global_variable
volatile u64
Total_Reads_Processed;
global_variable
volatile u64
Total_Good_Reads;
enum
file_type
{
sam,
pairs,
undet
};
global_variable
file_type
File_Type = undet;
#define StatusPrintEvery 10000
global_function
void
ProcessBodyLine(void *in)
{
line_buffer *buffer = (line_buffer *)in;
u08 *line = buffer->line;
ForLoop(buffer->nLines)
{
u64 nLinesTotal = __atomic_add_fetch(&Total_Reads_Processed, 1, 0);
while (*line++ != '\t') {}
u32 len = 1;
u32 flags;
if (File_Type == sam)
{
while (*++line != '\t') ++len;
flags = StringToInt(line, len);
}
else
{
--line;
flags = 0x1;
}
if ((flags < 128) && (flags & 0x1) && !(flags & 0x4) && !(flags & 0x8))
{
u32 contigName1[16];
line = PushStringIntoIntArray(contigName1, ArrayCount(contigName1), ++line, '\t');
len = 0;
while (*++line != '\t') ++len;
u64 relPos1 = StringToInt64(line, len);
u32 mapq;
if (File_Type == sam)
{
len = 0;
while (*++line != '\t') ++len;
mapq = StringToInt(line, len);
}
else
{
mapq = Min_Map_Quality;
}
if (mapq >= Min_Map_Quality)
{
if (File_Type == sam) while (*++line != '\t') {}
u32 sameContig = 0;
u32 contigName2[16];
if (*++line == '=')
{
sameContig = 1;
++line;
}
else
{
line = PushStringIntoIntArray(contigName2, ArrayCount(contigName2), line, '\t');
}
len = 0;
while (*++line != '\t') ++len;
u64 relPos2 = StringToInt64(line, len);
contig *cont = 0;
ContigHashTableLookup(contigName1, ArrayCount(contigName1), &cont);
if (cont)
{
u64 cummLen1 = cont->previousCumulativeLength;
u64 read1 = cummLen1 + relPos1;
u64 cummLen2 = 0;
if (sameContig)
{
cummLen2 = cummLen1;
}
else
{
cont = 0;
ContigHashTableLookup(contigName2, ArrayCount(contigName2), &cont);
if (cont)
{
cummLen2 = cont->previousCumulativeLength;
}
}
if (cont)
{
u64 read2 = cummLen2 + relPos2;
AddReadPairToImage(read1, read2);
__atomic_add_fetch(&Total_Good_Reads, 1, 0);
}
}
}
}
if ((nLinesTotal % StatusPrintEvery) == 0)
{
char buff[128];
memset((void *)buff, ' ', 80);
buff[80] = 0;
printf("\r%s", buff);
if (File_Type == sam)
stbsp_snprintf(buff, sizeof(buff), "[PretextMap status] :: %$u reads processed, %$u read-pairs mapped", nLinesTotal, Total_Good_Reads);
else
stbsp_snprintf(buff, sizeof(buff), "[PretextMap status] :: %$u read-pairs mapped", nLinesTotal);
printf("\r%s", buff);
fflush(stdout);
}
while (*line++ != '\n') {}
}
AddLineBufferToQueue(Line_Buffer_Queue, buffer);
}
global_variable
contig *
Filter_Include_Contigs = 0;
global_variable
contig *
Filter_Exclude_Contigs = 0;
global_variable
contig_hash_table_node **
Filter_Include_Hash_Table = 0;
global_variable
contig_hash_table_node **
Filter_Exclude_Hash_Table = 0;
global_function
void
CreateFilters(const char *includeFilterString, const char *excludeFilterString)
{
mpc_parser_t *name = mpc_new("name");
mpc_parser_t *syntax = mpc_new("syntax");
mpca_lang (MPCA_LANG_DEFAULT,
" name : /[0-9A-Za-z!#$%&\\+\\.\\/:;\\?@^_|~\\-][0-9A-Za-z!#$%&\\*\\+\\.\\/:;=\\?@^_|~\\-]*/;"
" syntax : /^/ <name> (',' <name>)* /$/;",
name, syntax, NULL
);
mpc_result_t result;
if (includeFilterString)
{
if (mpc_parse("include filter", includeFilterString, syntax, &result))
{
mpc_ast_t *syntaxTree = (mpc_ast_t *)result.output;
u32 nToAdd = 0;
ForLoop((u32)syntaxTree->children_num)
{
if (strstr(syntaxTree->children[index]->tag, "name")) ++nToAdd;
}
if (nToAdd)
{
u32 buff[16];
Filter_Include_Contigs = PushArray(Working_Set, contig, nToAdd);
Filter_Include_Hash_Table = PushArray(Working_Set, contig_hash_table_node*, Contig_Hash_Table_Size);
ForLoop(Contig_Hash_Table_Size)
{
*(Filter_Include_Hash_Table + index) = 0;
}
u32 count = 0;
ForLoop((u32)syntaxTree->children_num)
{
if (strstr(syntaxTree->children[index]->tag, "name"))
{
PushStringIntoIntArray(buff, ArrayCount(buff), (u08 *)syntaxTree->children[index]->contents);
contig *cont = Filter_Include_Contigs + count;
ForLoop2(ArrayCount(buff))
{
cont->name[index2] = buff[index2];
}
InsertContigIntoHashTable(count++, GetHashedContigName(buff, ArrayCount(buff)), Filter_Include_Hash_Table);
}
}
}
mpc_ast_delete((mpc_ast_t *)result.output);
}
else
{
PrintError(mpc_err_string(result.error));
mpc_err_delete(result.error);
exit(EXIT_FAILURE);
}
}
if (excludeFilterString)
{
if (mpc_parse("exclude filter", excludeFilterString, syntax, &result))
{
mpc_ast_t *syntaxTree = (mpc_ast_t *)result.output;
u32 nToAdd = 0;
ForLoop((u32)syntaxTree->children_num)
{
if (strstr(syntaxTree->children[index]->tag, "name")) ++nToAdd;
}
if (nToAdd)
{
u32 buff[16];
Filter_Exclude_Contigs = PushArray(Working_Set, contig, nToAdd);
Filter_Exclude_Hash_Table = PushArray(Working_Set, contig_hash_table_node*, Contig_Hash_Table_Size);
ForLoop(Contig_Hash_Table_Size)
{
*(Filter_Exclude_Hash_Table + index) = 0;
}
u32 count = 0;
ForLoop((u32)syntaxTree->children_num)
{
if (strstr(syntaxTree->children[index]->tag, "name"))
{
PushStringIntoIntArray(buff, ArrayCount(buff), (u08 *)syntaxTree->children[index]->contents);
contig *cont = Filter_Exclude_Contigs + count;
ForLoop2(ArrayCount(buff))
{
cont->name[index2] = buff[index2];
}
InsertContigIntoHashTable(count++, GetHashedContigName(buff, ArrayCount(buff)), Filter_Exclude_Hash_Table);
}
}
}
mpc_ast_delete((mpc_ast_t *)result.output);
}
else
{
PrintError(mpc_err_string(result.error));
mpc_err_delete(result.error);
exit(EXIT_FAILURE);
}
}
mpc_cleanup(2, name, syntax);
}
global_function
u32
FilterSequenceName(u32 *name, u32 nameLength)
{
u32 returnResult = 1;
contig *tmp;
if (Filter_Include_Hash_Table)
{
returnResult = ContigHashTableLookup(name, nameLength, &tmp, Filter_Include_Contigs, Filter_Include_Hash_Table);
}
if (Filter_Exclude_Hash_Table)
{
returnResult = returnResult && !ContigHashTableLookup(name, nameLength, &tmp, Filter_Exclude_Contigs, Filter_Exclude_Hash_Table);
}
return(returnResult);
}
global_function
void
ProcessHeaderLine(u08 *line)
{
u32 buff32[16];
u32 buffIndex = 0;
u08 *buff = (u08 *)buff32;
if (File_Type == sam)
{
while (*line != '\t')
{
++line;
}
line += 4;
}
else
{
line += 12;
}
while (*line != '\t')
{
*buff++ = *line++;
++buffIndex;
}
while (buffIndex & 3)
{
*buff++ = 0;
++buffIndex;
}
for ( u32 index = (buffIndex >> 2);
index < ArrayCount(buff32);
++index )
{
buff32[index] = 0;
}
if (FilterSequenceName(buff32, ArrayCount(buff32)))
{
line += (File_Type == sam ? 4 : 1);
u32 count = 1;
while (*++line != '\n' && *line != '\t')
{
++count;
}
u64 length = StringToInt64(line, count);
u32 hash = GetHashedContigName(buff32, ArrayCount(buff32));
LockMutex(Working_Set_rwMutex);
if (!First_Contig_Preprocess_Node)
{
TakeMemoryArenaSnapshot(&Working_Set, (memory_arena_snapshot *)&Contig_Preprocess_SnapShot);
}
contig_preprocess_node *node = PushStruct(Working_Set, contig_preprocess_node);
if (!First_Contig_Preprocess_Node)
{
First_Contig_Preprocess_Node = node;
}
else
{
Current_Contig_Preprocess_Node->next = node;
}
node->length = length;
node->hashedName = hash;
node->next = 0;
ForLoop(ArrayCount(buff32))
{
node->name[index] = buff32[index];
}
Current_Contig_Preprocess_Node = node;
++Number_of_Contigs;
UnlockMutex(Working_Set_rwMutex);
}
DecrementNumberHeaderLines();
}
enum
sort_by
{
sortByLength,
sortByName,
noSort
};
enum
sort_order
{
ascend,
descend
};
global_variable
sort_by
Sort_By = sortByLength;
global_variable
sort_order
Sort_Order = descend;
global_variable
threadSig
Finishing_Header = 0;
global_function
void
FinishProcessingHeader()
{
if (__atomic_fetch_add(&Finishing_Header, 1, 0) == 0)
{
while (Number_of_Header_Lines) {}
if (!Number_of_Contigs)
{
PrintError("0 sequences to map to");
exit(EXIT_FAILURE);
}
u32 *indexes = PushArray(Working_Set, u32, Number_of_Contigs);
u32 *tmpSpace = PushArray(Working_Set, u32, Number_of_Contigs);
contig_preprocess_node **nodes = PushArray(Working_Set, contig_preprocess_node*, Number_of_Contigs);
u32 tmpIndex = 0;
TraverseLinkedList((contig_preprocess_node *)First_Contig_Preprocess_Node, contig_preprocess_node)
{
nodes[tmpIndex] = node;
indexes[tmpIndex] = tmpIndex;
++tmpIndex;
Total_Genome_Length += node->length;
}
u32 hist_[512];
u32 *hist = (u32 *)hist_;
u32 *nextHist = hist + 256;
for ( u32 countIndex = 0;
countIndex < (Sort_By == noSort ? 0 : (Sort_By == sortByLength ? 2 : (ArrayCount(First_Contig_Preprocess_Node->name))));
++countIndex )
{
ForLoop(255)
{
hist[index] = 0;
}
ForLoop(Number_of_Contigs)
{
u32 val = Sort_By == sortByLength ? (u32)(((*(nodes+indexes[index]))->length >> (32 * countIndex)) & 0xffffffff) : (*(nodes+indexes[index]))->name[ArrayCount(First_Contig_Preprocess_Node->name) - 1 - countIndex];
u32 histVal = Sort_Order == descend ? 255 - (val & 0xff) : (val & 0xff);
++hist[histVal];
}
for ( u32 shift = 0;
shift < 24;
shift += 8 )
{
u32 total = 0;
ForLoop(255)
{
u32 tmp = hist[index];
hist[index] = total;
total += tmp;
nextHist[index] = 0;
}
hist[255] = total;
ForLoop(Number_of_Contigs)
{
u32 val = Sort_By == sortByLength ? ((u32)(((*(nodes+indexes[index]))->length >> (32 * countIndex)) & 0xffffffff) >> shift) : ((*(nodes+indexes[index]))->name[ArrayCount(First_Contig_Preprocess_Node->name) - 1 - countIndex] >> shift);
u32 histVal = Sort_Order == descend ? 255 - (val & 0xff) : (val & 0xff);
u32 nextHistVal = Sort_Order == descend ? 255 - ((val >> 8) & 0xff): ((val >> 8) & 0xff);
u32 newIndex = hist[histVal]++;
tmpSpace[newIndex] = indexes[index];
++nextHist[nextHistVal];
}
u32 *tmp = tmpSpace;
tmpSpace = indexes;
indexes = tmp;
tmp = nextHist;
nextHist = hist;
hist = tmp;
}
u32 total = 0;
ForLoop(255)
{
u32 tmp = hist[index];
hist[index] = total;
total += tmp;
}
hist[255] = total;
ForLoop(Number_of_Contigs)
{
u32 val = Sort_By == sortByLength ? ((u32)(((*(nodes+indexes[index]))->length >> (32 * countIndex)) & 0xffffffff) >> 24) : ((*(nodes+indexes[index]))->name[ArrayCount(First_Contig_Preprocess_Node->name) - 1 - countIndex] >> 24);
u32 histVal = Sort_Order == descend ? 255 - (val & 0xff) : (val & 0xff);
u32 newIndex = hist[histVal]++;
tmpSpace[newIndex] = indexes[index];
}
if (Sort_By == sortByLength)
{
indexes = tmpSpace;
}
else
{
u32 *tmp = tmpSpace;
tmpSpace = indexes;
indexes = tmp;
}
}
u32 **names = PushArray(Working_Set, u32*, Number_of_Contigs);
u64 *lengths = PushArray(Working_Set, u64, Number_of_Contigs);
u32 *hashes = PushArray(Working_Set, u32, Number_of_Contigs);
ForLoop(Number_of_Contigs)
{
contig_preprocess_node *node = nodes[indexes[index]];
lengths[index] = node->length;
hashes[index] = node->hashedName;
names[index] = PushArray(Working_Set, u32, ArrayCount(node->name));
ForLoop2(ArrayCount(node->name))
{
names[index][index2] = node->name[index2];
}