-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPretextView.cpp
11651 lines (9833 loc) · 448 KB
/
PretextView.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) 2021 Ed Harry, Wellcome Sanger Institute
Copyright (c) 2024 Shaoheng Guan, 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 my_String_(x) #x
#define my_String(x) my_String_(x)
#define PretextView_Version "PretextViewAI Version " my_String(PV) // PV is defined in the CMakeLists.txt
#define PretextView_Title "PretextViewAI " my_String(PV) " - Wellcome Sanger Institute"
#include <aisort.h> // place this before add Header.h to avoid macro conflict
#include <Header.h>
#ifdef DEBUG
#include <errno.h>
#endif // DEBUG
#include "utilsPretextView.h"
#include "auto_curation_state.h"
#include "TextureLoadQueue.cpp" //
#include "ColorMapData.cpp" // add color maps
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
#pragma GCC diagnostic ignored "-Wpadded"
#pragma GCC diagnostic ignored "-Wreserved-id-macro"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wdouble-promotion"
#pragma GCC diagnostic ignored "-Wcomma"
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
#pragma GCC diagnostic ignored "-Wshorten-64-to-32"
#pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion"
#define FONTSTASH_IMPLEMENTATION
#include "fontstash.h"
#define GLFONTSTASH_IMPLEMENTATION
#include "gl3corefontstash.h"
#pragma clang diagnostic pop
#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 "-Wreserved-id-macro"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#pragma GCC diagnostic ignored "-Wcast-align"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wdouble-promotion"
#pragma GCC diagnostic ignored "-Wpadded"
#pragma GCC diagnostic ignored "-Wimplicit-int-conversion"
#define STBI_ONLY_PNG
#ifndef DEBUG
#define STBI_ASSERT(x)
#endif // debug
#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif // STB_IMAGE_IMPLEMENTATION
#ifndef STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#endif // STB_IMAGE_WRITE_IMPLEMENTATION
#pragma clang diagnostic pop
#include "Resources.cpp"
#include "genomeData.h"
#include "showWindowData.h"
std::string shader_source_dir = getResourcesPath() + "/src/shaderSource/";
// Contact_Matrix->shaderProgram
global_variable
std::string
VertexSource_Texture = readShaderSource(shader_source_dir + "contactMatrixVertex.shader");
// Contact_Matrix->shaderProgram
global_variable
std::string
FragmentSource_Texture = readShaderSource( shader_source_dir + "contactMatrixFragment.shader");
global_variable
std::string
VertexSource_Flat = readShaderSource( shader_source_dir + "flatVertex.shader");
global_variable
std::string
FragmentSource_Flat = readShaderSource( shader_source_dir + "flatFragment.shader");
global_variable
std::string
VertexSource_EditablePlot = readShaderSource( shader_source_dir + "editVertex.shader");
global_variable
std::string
FragmentSource_EditablePlot = readShaderSource( shader_source_dir + "editFragment.shader");
// https://blog.tammearu.eu/posts/gllines/
global_variable
std::string
GeometrySource_EditablePlot = readShaderSource( shader_source_dir + "editGeometry.shader");
global_variable
std::string
VertexSource_UI = readShaderSource( shader_source_dir + "uiVertex.shader");
global_variable
std::string
FragmentSource_UI = readShaderSource( shader_source_dir + "uiFragment.shader");
#define UI_SHADER_LOC_POSITION 0
#define UI_SHADER_LOC_TEXCOORD 1
#define UI_SHADER_LOC_COLOR 2
global_variable
memory_arena
Working_Set;
global_variable
thread_pool *
Thread_Pool;
global_variable
u32
Redisplay = 0;
global_variable
s32
Window_Width, Window_Height, FrameBuffer_Width, FrameBuffer_Height;
global_variable
struct nk_vec2
Screen_Scale;
global_variable
theme
Current_Theme;
global_variable
u08 *
Theme_Name[THEME_COUNT];
global_function
void
SetTheme(struct nk_context *ctx, enum theme theme);
global_variable
f32
bgcolor[][4] =
{
{0.2f, 0.6f, 0.4f, 1.0f}, // original
{0.922f, 0.635f, 0.369f, 1.0f}, // jasper (orange)
{0.62f, 0.482f, 0.71f, 1.0f}, // heather (lavender)
{0.29f, 0.545f, 0.659f, 1.0f}, // Carolina (blue)
{1.0f, 1.0f, 1.0f, 1.0f}, // white
{0.765f, 0.765f, 0.765f, 1.0f}, // Grey
{0.0f, 0.0f, 0.0f, 1.0f} // Black
};
global_variable const char *
bg_color[] =
{
"Classic",
"Jasper",
"Heather",
"Carolina",
"White",
"Grey",
"Black"};
global_variable
u08
active_bgcolor = 0;
struct
metaoutline
{
u08 on;
u08 color;
};
struct metaoutline global_meta_outline = {1, 0};
global_variable metaoutline *
meta_outline = &global_meta_outline;
global_variable
u08
default_metadata_colorProfile = 0;
global_variable
u08
meta_data_curcolorProfile = default_metadata_colorProfile;
global_variable
f32
meta_dataColors[][3] =
{
{1.666f, 2.666f, 3.666f}, // original
{2.666f, 1.666f, 3.666f},
{3.666f, 2.666f, 1.666f},
{5.666f, 3.666f, 2.666f}};
global_variable const char *
metaColors[] =
{
"Color set 1",
"Color set 2",
"Color set 3",
"Color set 4"};
global_variable const char *
outlineColors[] =
{
"No-Outline",
"Black-Outline",
"White-Outline"};
global_variable
u08
activeGraphColour = 0;
global_variable
nk_colorf
graphColors[] =
{
{0.1f, 0.8f, 0.7f, 1.0f}, // Default
{1.0f, 0.341f, 0.2f, 1.0f}, // Vermillion
{0.137f, 0.451f, 0.882f, 1.0f}, // essence (vibrant blue)
{0.706f, 0.0f, 1.0f, 1.0f} // Drystorm (purple)
};
global_variable const char *
colour_graph[] =
{
"Default",
"Vermillion",
"Blue",
"Purple"};
global_variable
nk_colorf
DefaultGraphColour = graphColors[activeGraphColour];
global_variable
nk_context *
NK_Context;
global_function
void
ChangeSize(s32 width, s32 height)
{
glViewport(0, 0, width, height);
}
global_function
void
UpdateScreenScale()
{
Screen_Scale.x = (f32)FrameBuffer_Width / (f32)Window_Width;
Screen_Scale.y = (f32)FrameBuffer_Height / (f32)Window_Height;
SetTheme(NK_Context, Current_Theme);
}
global_function
void
GLFWChangeFrameBufferSize(GLFWwindow *win, s32 width, s32 height)
{
(void)win;
ChangeSize(width, height);
Redisplay = 1;
FrameBuffer_Height = height;
FrameBuffer_Width = width;
UpdateScreenScale();
}
global_function
void
GLFWChangeWindowSize(GLFWwindow *win, s32 width, s32 height)
{
(void)win;
Window_Width = width;
Window_Height = height;
UpdateScreenScale();
}
global_variable
device *
NK_Device;
global_variable
nk_font_atlas *
NK_Atlas;
global_variable
nk_font *
NK_Font;
global_variable
nk_font *
NK_Font_Browser;
global_variable
color_maps *
Color_Maps;
#ifdef Internal
global_variable
quad_data *
Texture_Tile_Grid;
global_variable
quad_data *
QuadTree_Data;
#endif
struct
CustomColorMapOrder
{
u32 nMaps;
u32 order[Number_of_Color_Maps];
};
global_variable
CustomColorMapOrder
userColourMapOrder;
global_variable
s32
useCustomOrder = 0;
global_variable
quad_data *
Grid_Data;
global_variable
quad_data *
Contig_ColourBar_Data;
global_variable
quad_data *
Scaff_Bar_Data;
global_variable
quad_data *
Edit_Mode_Data;
global_variable
quad_data *
Label_Box_Data;
global_variable
quad_data *
Scale_Bar_Data;
global_variable
quad_data *
Tool_Tip_Data;
global_variable
quad_data *
Waypoint_Data;
global_variable
ui_shader *
UI_Shader;
global_variable
flat_shader *
Flat_Shader;
global_variable
contact_matrix *
Contact_Matrix;
global_variable
threadSig
Texture_Ptr = 0; // used to define which texture is currently being processed, the data is saved in Current_Loaded_Texture
global_variable
volatile texture_buffer *
Current_Loaded_Texture = 0; // define the global texture buffer
global_variable
u32
Texture_Resolution;
global_variable
u32
Number_of_MipMaps;
global_variable
u32
Number_of_Textures_1D;
global_variable
u32
Number_of_Pixels_1D;
global_variable
u32
Bytes_Per_Texture;
global_variable
texture_buffer_queue *
Texture_Buffer_Queue;
global_variable
pointd
Mouse_Move;
global_variable
tool_tip Tool_Tip_Move;
global_variable
edit_pixels
Edit_Pixels;
global_variable
point3f
Camera_Position;
global_variable
FONScontext *
FontStash_Context;
#define Grey_Background {0.569f, 0.549f, 0.451f, 1.0f}
#define Yellow_Text_Float {0.941176471f, 0.725490196f, 0.058823529f, 1.0f}
#define Red_Text_Float {0.941176471f, 0.039215686f, 0.019607843f, 1.0f}
#define Green_Float {0.3f, 0.6f, 0.0f, 0.2f}
#define Red_Float {0.6f, 0.3f, 0.0f, 0.2f}
#define Blue_Float {0.0f, 0.3f, 0.6f, 0.2f}
#define Red_Full {1.0f, 0.0f, 0.0f, 1.0f}
#define Blue_Full {0.0f, 0.0f, 1.0f, 1.0f}
global_variable
ui_colour_element_bg *
Contig_Name_Labels;
global_variable
ui_colour_element_bg *
Scale_Bars;
global_variable
ui_colour_element_bg *
Tool_Tip;
global_variable
u32
Waypoints_Always_Visible = 1;
global_variable
u08
Scaffs_Always_Visible = 1;
global_variable
u08
MetaData_Always_Visible = 1;
global_variable
ui_colour_element *
Grid;
global_variable
ui_colour_element *
Contig_Ids;
#ifdef Internal
global_variable
ui_colour_element *
Tiles;
global_variable
ui_colour_element *
QuadTrees;
#endif
global_variable
edit_mode_colours *
Edit_Mode_Colours;
global_variable
waypoint_mode_data *
Waypoint_Mode_Data;
global_variable
meta_mode_data *
Scaff_Mode_Data;
global_variable
meta_mode_data *
MetaData_Mode_Data;
global_variable
meta_mode_data *
Extension_Mode_Data;
struct selected_sequence_cover_countor
{
s32 original_contig_index;
s32 idx_within_original_contig;
f64 end_time;
f64 label_last_seconds = 5.0f;
u08 plotted;
f32 contig_start; // ratio
f32 contig_end; // ratio
s32 map_loc; // in pixel
void clear()
{
original_contig_index = -1;
idx_within_original_contig = -1;
end_time = -1.;
plotted = false;
contig_start = -1.0;
contig_end = -1.0;
map_loc = -1;
};
void set(
s32 original_contig_index_,
s32 idx_within_original_contig_,
f64 current_time_,
f32 contig_start_,
f32 contig_end_,
s32 map_loc_
)
{
this->clear();
original_contig_index = original_contig_index_;
idx_within_original_contig = idx_within_original_contig_;
end_time = current_time_ + label_last_seconds;
plotted = 0;
contig_start = contig_start_;
contig_end = contig_end_;
map_loc = map_loc_;
}
};
global_variable
selected_sequence_cover_countor
Selected_Sequence_Cover_Countor;
global_variable
u32
UI_On = 0;
global_variable
global_mode
Global_Mode = mode_normal;
#define Edit_Mode (Global_Mode == mode_edit)
#define Normal_Mode (Global_Mode == mode_normal)
#define Waypoint_Edit_Mode (Global_Mode == mode_waypoint_edit)
#define Scaff_Edit_Mode (Global_Mode == mode_scaff_edit)
#define MetaData_Edit_Mode (Global_Mode == mode_meta_edit)
#define Extension_Mode (Global_Mode == mode_extension)
#define Select_Sort_Area_Mode (Global_Mode == mode_selectExclude_sort_area)
global_variable
s32
Font_Normal = FONS_INVALID;
global_variable
s32
Font_Bold = FONS_INVALID;
global_function
void
ClampCamera()
{
Camera_Position.z = my_Min(my_Max(Camera_Position.z, 1.0f), ((f32)(Pow2((Number_of_MipMaps + 1)))));
Camera_Position.x = my_Min(my_Max(Camera_Position.x, -0.5f), 0.5f);
Camera_Position.y = my_Min(my_Max(Camera_Position.y, -0.5f), 0.5f);
}
global_function
void
ZoomCamera(f32 dir)
{
f32 scrollFactor = 1.1f;
scrollFactor += dir > 0.0f ? dir : -dir;
Camera_Position.z *= (dir > 0.0f ? scrollFactor : (1.0f / scrollFactor));
ClampCamera();
}
global_variable
u32
Loading = 0;
global_variable
u32
Yahs_sorting = 0;
global_variable s32 ai_sort_button = 0;
global_variable s32 yahs_sort_button = 0;
global_function
s32
RearrangeMap(u32 pixelFrom, u32 pixelTo, s32 delta, u08 snap = 0, bool update_contigs_flag=true);
global_function
void
InvertMap(u32 pixelFrom, u32 pixelTo, bool update_contigs_flag=true);
global_variable
u32
Global_Edit_Invert_Flag = 0;
global_variable
u08
Scaff_Painting_Flag = 0;
global_variable
u08
Scaff_FF_Flag = 0;
global_variable
u32
Scaff_Painting_Id = 0;
global_variable
original_contig *
Original_Contigs;
global_variable
u32
Number_of_Original_Contigs;
global_variable
contigs *
Contigs;
global_function
u08
IsContigInverted(u32 index)
{
return(Contigs->contigInvertFlags[index >> 3] & (1 << (index & 7))); // 所以这个32位的数:后三位表示1所在的位数,其他的表示编号
// return(Contigs->contigInvertFlags[index / 8] & (1 << (index % 8))); // check if contig is inverted
}
global_function
void
setContactMatrixVertexArray(contact_matrix* Contact_Matrix_, bool copy_flag=false, bool regenerate_flag=false);
global_function
void
prepare_before_copy(f32 *original_control_points);
global_function
void
restore_settings_after_copy(const f32 *original_control_points);
#define Max_Number_of_Contigs 4096
global_variable
meta_data *
Meta_Data; // meta data tags for each contig
global_variable
u32
MetaData_Active_Tag = 0;
global_variable
u08
MetaData_Edit_State = 0;
global_variable
u16
sortMetaEdits;
global_variable
const char *
Default_Tags[] =
{
"Haplotig",
"Unloc",
"X",
"Y",
"Z",
"W",
"HAP1",
"HAP2",
"Target",
"Contaminant",
"Singleton",
"X1",
"X2",
"Y1",
"Y2",
"Z1",
"Z2",
"W1",
"W2",
"I",
"II",
"III",
"IV",
"V",
"U",
"B1",
"B2",
"B3"
};
global_variable
map_state *
Map_State;
#include "copy_texture.h"
// define the struct to store the ai model mask
std::unique_ptr<AiModel> ai_model = nullptr;
global_variable auto auto_curation_state=AutoCurationState();
global_function
void
UpdateContigsFromMapState() // todo reading 从map的状态更新contigs
{
u32 lastScaffID = Map_State->scaffIds[0]; // 第一个scaff的编号
u32 scaffId = lastScaffID ? 1 : 0; //
u32 lastId = Map_State->originalContigIds[0]; // 第一个像素点对应的id
u32 lastCoord = Map_State->contigRelCoords[0]; // 第一个像素点的局部坐标
u32 contigPtr = 0;
u32 length = 0;
u32 startCoord = lastCoord;
u08 inverted = Map_State->contigRelCoords[1] < lastCoord; // 判断是不是反转的
Map_State->contigIds[0] = 0;
u32 pixelIdx = 0;
ForLoop(Number_of_Original_Contigs) (Original_Contigs + index)->nContigs = 0; // 将每一个contig的 片段数目 置为零
ForLoop(Number_of_Pixels_1D - 1) // 遍历每一个像素点 更新 Original_Contigs, Contigs
// 遍历完之后,contigPtr为214,但是Number_of_Original_Contigs = 218
{
if (contigPtr == Max_Number_of_Contigs) break; // 确保 contigPtr 不超出最大contig的数值
++length; // current fragment length
pixelIdx = index + 1; // 像素点编号, 加一因为第一个已经用来初始化了
u32 id = Map_State->originalContigIds[pixelIdx]; // 像素点的 contig id
u32 coord = Map_State->contigRelCoords[pixelIdx]; // 像素点的局部坐标
if (id != lastId || (inverted && coord != (lastCoord - 1)) || (!inverted && coord != (lastCoord + 1))) // 如果不是一个连续片段
{
Original_Contigs[lastId].contigMapPixels[Original_Contigs[lastId].nContigs] = pixelIdx - 1 - (length >> 1); // update Original_Contigs: contigMapPixels
Original_Contigs[lastId].nContigs++; // update Original_Contigs: nContigs, contigMapPixels
contig *last_cont = Contigs->contigs + contigPtr; // 获取上一个contig的指针, 并且给contigPtr + 1
contigPtr++;
last_cont->originalContigId = lastId; // 更新这个片段的id
last_cont->length = length; // 更新长度
last_cont->startCoord = startCoord; // 更新开头为当前片段在该contig上的局部坐标 endCoord = startCoord + length - 1
last_cont->metaDataFlags = Map_State->metaDataFlags + pixelIdx - 1; // Finished (shaoheng): memory problem: assign the pointer to the cont->metaDataFlags, the original is nullptr, the let this ptr point to the last pixel of the contig
u32 thisScaffID = Map_State->scaffIds[pixelIdx - 1]; // 上一个像素点对应的 scaffid
last_cont->scaffId = thisScaffID ? ((thisScaffID == lastScaffID) ? (scaffId) : (++scaffId)) : 0; // 如果存在scaffid则(判断是不是同一个scaff,如果是则继续用scaffid,否则++scaffid),否则为0
lastScaffID = thisScaffID; // 更新
// 余数表示8数的第几位,如果未反向则对应位为0,若反向则对应位为1
if (IsContigInverted(contigPtr - 1)) // 判断上一个contig是否反向
{ // 位操作更新contigflag
// 每8个片段的正反采用一个u08表示,每一个bit表示一个正反,余数表示这八个中的第几个,如果没有反向则对应位为0
if (!inverted) Contigs->contigInvertFlags[(contigPtr - 1) >> 3] &= ~(1 << ((contigPtr - 1) & 7));
}
else
{ // 如果反向则额对应位的bit为1
if (inverted) Contigs->contigInvertFlags[(contigPtr - 1) >> 3] |= (1 << ((contigPtr - 1) & 7)); // 如果反向
}
startCoord = coord; // 当前片段开始的坐标
length = 0; // 当前片段长度清零0
if (pixelIdx < (Number_of_Pixels_1D - 1)) inverted = Map_State->contigRelCoords[pixelIdx + 1] < coord; // 更新inverted
}
// 更新上一个id和局部坐标
Map_State->contigIds[pixelIdx] = (u32)contigPtr; // 像素点对应的 片段id 修改为当前的统计得到片段id
lastId = id; // 更新上一个像素点的id
lastCoord = coord; // 更新上一个像素点的局部坐标
}
if (contigPtr < Max_Number_of_Contigs) // contigptr 小于 Number_of_Original_Contigs
// 更新最后一个contig的最后一个片段信息
{
(Original_Contigs + lastId)->contigMapPixels[(Original_Contigs + lastId)->nContigs++] = pixelIdx - 1 - (length >> 1);
++length;
contig *cont = Contigs->contigs + contigPtr++;
cont->originalContigId = lastId;
cont->length = length;
cont->startCoord = startCoord;
cont->metaDataFlags = Map_State->metaDataFlags + pixelIdx - 1;
u32 thisScaffID = Map_State->scaffIds[pixelIdx];
cont->scaffId = thisScaffID ? ((thisScaffID == lastScaffID) ? (scaffId) : (++scaffId)) : 0;
if (IsContigInverted(contigPtr - 1))
{
if (!inverted) Contigs->contigInvertFlags[(contigPtr - 1) >> 3] &= ~(1 << ((contigPtr - 1) & 7));
}
else
{
if (inverted) Contigs->contigInvertFlags[(contigPtr - 1) >> 3] |= (1 << ((contigPtr - 1) & 7));
}
}
Contigs->numberOfContigs = contigPtr;
}
global_function
void
AddMapEdit(s32 delta, pointui finalPixels, u32 invert);
global_function
void
RebuildContig(u32 pixel)
{
for (;;)
{
u32 contigId = Map_State->contigIds[pixel];
u32 origContigId = Map_State->originalContigIds[pixel];
u32 top = (u32)pixel;
while (top && (Map_State->contigIds[top - 1] == contigId)) --top;
u32 bottom = pixel;
while ((bottom < (Number_of_Pixels_1D - 1)) && (Map_State->contigIds[bottom + 1] == contigId)) ++bottom;
if (IsContigInverted(contigId))
{
InvertMap(top, bottom);
AddMapEdit(0, {top, bottom}, 1);
continue;
}
u08 fragmented = 0;
ForLoop(Number_of_Pixels_1D)
{
if ((Map_State->contigIds[index] != contigId) && (Map_State->originalContigIds[index] == origContigId))
{
fragmented = 1;
break;
}
}
if (fragmented)
{
u32 contigTopCoord = Map_State->contigRelCoords[top];
if (contigTopCoord)
{
u32 otherPixel = 0;
ForLoop(Number_of_Pixels_1D)
{
if ((Map_State->originalContigIds[index] == origContigId) && (Map_State->contigRelCoords[index] == (contigTopCoord - 1)))
{
otherPixel = index;
break;
}
}
u08 invert = !otherPixel || (Map_State->contigIds[otherPixel - 1] != Map_State->contigIds[otherPixel]);
u32 otherPixel2 = otherPixel;
if (invert)
{
while ((otherPixel < (Number_of_Pixels_1D - 1)) && (Map_State->contigIds[otherPixel + 1] == Map_State->contigIds[otherPixel2])) ++otherPixel;
}
else
{
while (otherPixel2 && (Map_State->contigIds[otherPixel2 - 1] == Map_State->contigIds[otherPixel])) --otherPixel2;
}
s32 delta = (s32)top - (s32)otherPixel;
if (delta > 0) --delta;
else delta = (s32)top - (s32)otherPixel2;
pointui finalPixels = {(u32)((s32)otherPixel2 + delta), (u32)((s32)otherPixel + delta)};
RearrangeMap(otherPixel2, otherPixel, delta);
if (invert) InvertMap(finalPixels.x, finalPixels.y);
AddMapEdit(delta, finalPixels, invert);
}
else
{
u32 contigBottomCoord = Map_State->contigRelCoords[bottom];
u32 otherPixel = 0;
ForLoop(Number_of_Pixels_1D)
{
if ((Map_State->originalContigIds[index] == origContigId) && (Map_State->contigRelCoords[index] == (contigBottomCoord + 1)))
{
otherPixel = index;
break;
}
}
u08 invert = (otherPixel == (Number_of_Pixels_1D - 1)) || (Map_State->contigIds[otherPixel + 1] != Map_State->contigIds[otherPixel]);
u32 otherPixel2 = otherPixel;
if (!invert)
{
while ((otherPixel < (Number_of_Pixels_1D - 1)) && (Map_State->contigIds[otherPixel + 1] == Map_State->contigIds[otherPixel2])) ++otherPixel;
}
else
{
while (otherPixel2 && (Map_State->contigIds[otherPixel2 - 1] == Map_State->contigIds[otherPixel])) --otherPixel2;
}
s32 delta = (s32)bottom - (s32)otherPixel2;
if (delta < 0) ++delta;
else delta = (s32)bottom - (s32)otherPixel;
pointui finalPixels = {(u32)((s32)otherPixel2 + delta), (u32)((s32)otherPixel + delta)};
RearrangeMap(otherPixel2, otherPixel, delta);
if (invert) InvertMap(finalPixels.x, finalPixels.y);
AddMapEdit(delta, finalPixels, invert);
}
continue;
}
else break;
}
}
struct
map_edit
{
u32 finalPix1;
u32 finalPix2;
s32 delta;