-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrawbin.cpp
More file actions
5482 lines (4987 loc) · 177 KB
/
Copy pathrawbin.cpp
File metadata and controls
5482 lines (4987 loc) · 177 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//BATMAN 1.1 - added Substring search..
//BATMAN 1.10 - enhanced default output processing...
// handle N's.
// print blanks...
// --maxhits bug fixes
//{----------------------------- INCLUDE FILES -------------------------------------------------/
#include <stdio.h>
#include <string>
#include <limits.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>
#include <xmmintrin.h>
#include <emmintrin.h>
#include <map>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
//#include <dvec.h>
#include "bfix.h"
#include <getopt.h>
#include "zlib.h"
#include "assert.h"
#include "const.h"
//#include "rqindex.h"
extern "C"
{
#include "iniparser.h"
#include <time.h>
#include "MemManager.h"
#include "MiscUtilities.h"
#include "TextConverter.h"
#include "BWT.h"
}
//}----------------------------- INCLUDE FILES -------------------------------------------------/
using namespace std;
//{----------------------------- DEFINES -------------------------------------------------/
typedef map <unsigned,unsigned> HASH_PAIR;
typedef map <unsigned,unsigned char> JUNC_HASH;
#define EXTEND STRINGLENGTH/2//8
const int MAX_JUNC_LIST=500000;
const int EXTENDISLAND=10;
const int OVERLAP=10;
const int MAX_HITS_ALLOWED=2;//maximum pairs to output..
const int GAPLIMIT=7;//maximum pairs to output..
const int COVERAGE_THRESH=0;//5
const int RESIDUE=6;
const int MAXMULTIGAP=50000;
const int HIGHREPTHRES = 30;
const int EX_MIN=4;//10;//How close the junctions are..
const int REFMARK=10000;
const int MAX_REF_LINE=10000;
const int MAX_X=500;//for mouse 312
const int MAX_ONE_SIDE_HITS=5;
const int MAXSTRINGLEN=255;
const int SKIP_INIT_HIT=1;//If the full read can be mapped, dont scan for junctions ...
const int MAX_JUNC_IN_READ=100;//If the full read can be mapped, dont scan for junctions ...
enum { FULLREAD,SPLITREAD,UNIQUE,UNIQUEX,NON_UNIQUE,UNMARKED,UNEXTENDED};
#define MIS_IN_UNMAPPED 0
#define MIS_IN_RES 0
#define N_a 0
#define N_c 1
#define N_g 2
#define N_t 3
#define S_non 0
#define S_dummy 500
#define S_ag 1
#define S_gt 2
#define S_ac 3
#define S_gc 4
#define S_at 5
#define S_plus 255
#define S_gt_ag 256
#define S_gc_ag 257
#define S_at_ac 258
#define S_ct 6
#define S_ct_ac 259
#define S_ct_gc 260
#define S_gt_at 261
#define S_minus 262
#define POS_JUNC 256
#define MINUS_JUNC 259
#define INIT 100
#define LEFT 0
#define LEFTC 1
#define RIGHT 2
#define RIGHTC 3
#define FW 1
#define BW 0
#define MAX_REFGENE_OVERHANG 20 //maximum overhang allowed to determine for overhang over a putative splice.
#define INREFGENE 1
#define DONOR 2
#define ACCEPTOR 4
#define PLUS 0
#define MINUS 1
#define DELETED 2
#define EXON 4
#define COVTYPE unsigned char //what type can the maximum coverage be?
#define COVTYPEMAX UCHAR_MAX //what type can the maximum coverage be?
//#define COVTYPE unsigned short //what type can the maximum coverage be?
//#define COVTYPEMAX USHRT_MAX //what type can the maximum coverage be?
#define TAB 1
#define FQ 2
#define FA 3
#define SAINTERVAL 8
#define SAGAP_CUTOFF 5
#define SAGAP_CUTOFF_INDEX 2
#define TWOFILE 4
#define GISMODE 100
#define MAXTAG 256
#define MAXDES 500
#define PAIR_END_SEPERATOR '\t'
#define MAX_MISMATCHES_BOUND 2
#define BRANCHTHRESHOLD 80 //30 //Threshold at which to check the BWT instead of branching
#define REVFMI 1
#define FWFMI 0
#define BUFSIZE 100000
//#define MAXCOUNT 3 //30000 //Maximum number of pairs to enum...
//#define MAX_HITS_TO_STORE 200//later resolve this and MAXCOUNT conflict..
//#define MAXCOUNT MAX_HITS_TO_STORE //30000 //Maximum number of pairs to enum...
#define HEAD 0
#define TAIL 1
#define _lrotr(x, n) ((((unsigned long)(x)) >> ((int) ((n) & 31))) | (((unsigned long)(x)) << ((int) ((-(n)) & 31))))
#define _lrotl(x, n) ((((unsigned long)(x)) << ((int) ((n) & 31))) | (((unsigned long)(x)) >> ((int) ((-(n)) & 31))))
#define ror32(x, n) _lrotr(x, n)
#define rol32(x, n) _lrotl(x, n)
#define bswap32(x) (rol32((unsigned long)(x), 8) & 0x00ff00ff | ror32 ((unsigned long)(x), 8) & 0xff00ff00)
#ifdef BAT64
#define UINT uint64_t
#define UINTSIZE 64
#define MULTISTRINGLENTH 28
//#define BSWAP __builtin_bswap64
#define BSWAP bswap32
#define SEARCH_MASK 0xFFFFFFFFFFFFFF00
#define SEARCH_MASKB 0x00FFFFFFFFFFFFFF
#define SEARCH_ISOLATE_SHIFT 62
#define BSF __builtin_clzl
#define BSR __builtin_ctzl
#define INTLENGTH 64
#define BYTES_IN_UNIT UINTSIZE/8
#else
#define UINT uint32_t
#define UINTSIZE 32
#define MULTISTRINGLENTH 12
//#define BSWAP __builtin_bswap32
#define BSWAP bswap32
#define SEARCH_MASK 0xFFFFFF00
#define SEARCH_MASKB 0x00FFFFFF
#define SEARCH_ISOLATE_SHIFT 30
#define BSF __builtin_clz
#define BSR __builtin_ctz
#define INTLENGTH 32
#define BYTES_IN_UNIT UINTSIZE/8
#endif
//}----------------------------- DEFINES -------------------------------------------------/
//{----------------------------- STRUCTS -------------------------------------------------/
struct OP//Ordered pairs..
{
unsigned x;
unsigned y;
//int Motif;
};
struct OPX//Ordered pairs..
{
unsigned x;
unsigned y;
int Motif;
};
struct JStat//Junction stats..
{
int Count;
int Junc_Type;
};
struct OP_Cmp
{
bool operator()( OP OP1, OP OP2)
{
if (OP1.x == OP2.x)
{
return OP1.y< OP2.y;
}
else return OP1.x < OP2.x;
}
};
struct Out_Record
{
unsigned ID;//ID for read number..
char Strlen;
int HLength;
unsigned HLocation;
int TLength;
unsigned TLocation;
int Junc_Count;// # of junctions in this read..
char Type;
};
struct Unmapped_Record
{
int Strlen;
int Length;
unsigned Location;
//next comes string...
};
struct State
{
char State;
unsigned Location;
int Score;
};
struct Xon
{
unsigned Start;
unsigned End;
int Start_Type;
int End_Type;
};
struct Island
{
unsigned Start;
unsigned End;
Island* Next;
char Status;
};
struct Offset_Record
{
char Genome[40];
char GenomeM[40];
unsigned Offset;
FILE* Out_File;
FILE* Out_FileM;
FILE* Unmapped;
FILE* UnmappedX;
FILE* UnmappedM;
FILE* UnmappedXM;
FILE* Ref_File;
};
struct PAIR
{
unsigned Head;
unsigned Tail;
int HLevel;
int TLevel;
};
struct TAG_INFO
{
unsigned SA_Start;//start of the Sa range
unsigned Gap;//length of SA range
unsigned Block_Start;//Start of block info..
unsigned Index;//Index to SA_index
unsigned First;//first location of hit
unsigned Last;//last location of hit
unsigned Field_Length;
};
struct READ
{
char Tag_Copy[2*MAXTAG+2];
char Description[MAXDES+1];
char Tag[MAXTAG+1];
char Quality[MAXTAG+1];
char Plus[MAXTAG+1];
char NLocations[MAXTAG+1];
char Complement[MAXTAG+1];
int NCount;
unsigned char N[500];
};
struct SA
{
unsigned Start;
unsigned End;
unsigned Start_Location;// exact location of the first occurance...
unsigned End_Location;
};
struct SARANGE
{
unsigned Start;
unsigned End;
int Level;//at what relative node are we?
char Mismatches;//number of mismatches at this level
unsigned char Skip;
unsigned char Mismatch_Pos[MAX_MISMATCHES_BOUND];//BTS_PER_LOC|BTS_PER_LOC|...
unsigned Mismatch_Char;//2|2|...
unsigned Conversion_Factor;
};
struct ORIENTATION
{
char Strand;
char* String;
SARANGE TagB;//RQfactor tag..
SARANGE TagBR;//RQfactor tag..
SARANGE TagBH;//RQfactor tag..
SARANGE TagF;//Halfway tag..
SARANGE TagFR;//Halfway tag..
SARANGE TagFH;//Halfway tag..
};
struct RANGEINDEX
{
FILE* Index;
FILE* Blocks;
SA *SA_Index;
int *SA_Blocks;
unsigned Hits;
char COMPRESS;
};
typedef struct gz_stream {
z_stream stream;
int z_err; /* error code for last stream operation */
int z_eof; /* set if end of input file */
FILE *file; /* .gz file */
Byte *inbuf; /* input buffer */
Byte *outbuf; /* output buffer */
uLong crc; /* crc32 of uncompressed data */
char *msg; /* error message */
char *path; /* path name for debugging only */
int transparent; /* 1 if input file is not a .gz file */
char mode; /* 'w' or 'r' */
z_off_t start; /* start of compressed data in file (header skipped) */
z_off_t in; /* bytes into deflate or inflate */
z_off_t out; /* bytes out of deflate or inflate */
int back; /* one character push-back */
int last; /* true if push-back is last character */
} gz_stream;
//}----------------------------- STRUCTS -------------------------------------------------/*
//{----------------------------- Classes -------------------------------------------------/
typedef map <OP,JStat>::iterator map_it;
class Hash
{
public:
map <OP,JStat,OP_Cmp> Junctions;
map_it Junc_I,JJ;
map_it Last;
void Insert (OP Location,int Paring);
void Delete (OP Location);
bool Init_Iterate(OP & Location,JStat & Data);
bool Iterate(OP & Location,JStat & Data);
map_it Begin();
};
map_it Hash::Begin ()
{
return Junctions.begin();
}
void Hash::Insert (OP Location,int Paring)
{
JStat T;
assert(Paring >=255 && Paring <=262); assert(Location.x <= Location.y);
Junc_I=Junctions.find(Location);
if (Junc_I == Junctions.end()) //New entry..
{
T.Count=1;T.Junc_Type=Paring;
Junctions[Location]=T;
}
else
{
assert((Junc_I->second).Count>0 && (Junc_I->second).Junc_Type==Paring);
(Junc_I->second).Count++;
}
}
void Hash::Delete (OP Location)
{
Junc_I=Junctions.find(Location);
if (Junc_I != Junctions.end()) Junctions.erase(Junc_I);
}
bool Hash::Init_Iterate(OP & Location,JStat & Data)
{
Junc_I=Junctions.begin();
if (Junc_I == Junctions.end()) return false;
else
{
Last=Junc_I;//save last pos...
Location = Junc_I->first;
Data = Junc_I->second;
Junc_I++;
return true;
}
}
bool Hash::Iterate(OP & Location,JStat & Data)
{
if(Junc_I == Junctions.end()) return false;
Last=Junc_I;//save last pos...
Location = Junc_I->first;
Data = Junc_I->second;
Junc_I++;
return true;
}
//}----------------------------- Classes -------------------------------------------------/
//{----------------------------- FUNCTION PRTOTYPES -------------------------------------------------/*
bool Scan(unsigned & Total_Hits,unsigned & Tags_Processed,int Off,unsigned Read_ID);
char* Get_Junc_Type(int Junc_Type);
void Load_RefGene(FILE* Ref_Handle,Hash & Ref_Gene,JUNC_HASH & Donor_AT,JUNC_HASH & Donor_GT,JUNC_HASH & Donor_GC,JUNC_HASH & Donor_CT,JUNC_HASH & Acc_AC,JUNC_HASH & Acc_AG,JUNC_HASH & Acc_AT,JUNC_HASH & Acc_GC);
void Init();
void Allocate_Memory();
void Open_Files();
int Split(char* String,char Sep, char* Fields[],int Max=0);
void Load_Range_Index(char* INDFILE, char* BLKFILE,RANGEINDEX & Range_Index);
void Parse_Command_line(int argc, char* argv[]);
void Detect_Input(int & TAG_COPY_LEN,int & STRINGLENGTH,char & NORMAL_TAGS, char & PAIRING_TYPE,char & FILETYPE,int & PAIR_LENGTH_RIGHT,int & PAIR_LENGTH_LEFT);
void Convert_To_REVSA(SARANGE & Tag, char* Current_Tag);
void Get_Head_Tail(SARANGE & Head, SARANGE & Tail,unsigned d,PAIR* Pairs,int & Pairs_Index);
void Search_Small_Gap(SARANGE & Head, SARANGE & Tail, unsigned d,PAIR* Pairs,int & Pairs_Index);
void Load_Info( TAG_INFO & Tag, SARANGE & Head);
void Print_BED(Hash & Junctions,Island* Island_List, COVTYPE* Coverage,char* chromosome,char Strand);
char Two_Mismatch_Search();
void Show_Progress(unsigned Percentage);
char Search_Forwards(SARANGE & Tag,int Count,int Start,int StringLength,int & Two_Mismatches_At_End_Forward_Pointer,SARANGE* Two_Mismatches_At_End_Forward,int & Mismatches_Forward_Pointer,SARANGE* Mismatches_Forward);
char Search_Backwards(struct SARANGE & Tag,int Count,int Start,int StringLength,int & Two_Mismatches_At_End_Pointer,SARANGE* Two_Mismatches_At_End,int & Mismatches_Backward_Pointer,SARANGE* Mismatches_Backward);
void Search_Forwards_OneSA(SARANGE & Tag,int Count,int Start,int StringLength,int & Two_Mismatches_At_End_Forward_Pointer,SARANGE* Two_Mismatches_At_End_Forward,int & Mismatches_Forward_Pointer,SARANGE* Mismatches_Forward);
void Branch_Detect_Backwards (const struct SARANGE Tag,BWT *fmi,int Start);
void Branch_Detect (const struct SARANGE Tag,BWT *fmi,int Start);
unsigned Get_Location(TAG_INFO & Tag, unsigned Offset);
void Pair_Reads(SARANGE *Head_Hits,SARANGE *Tail_Hits);
int Extend_Location_Forward(SARANGE & Tag,int Start,int StringLength,int Count);
int Extend_Location_Backward(SARANGE & Tag,int Start,int StringLength,int Count);
void Verbose();
void Pack_Text(char* Current_Tag);
unsigned Get_File_Size(FILE* File);
int Location_To_Genome(unsigned & Location);
int Load_Locations(Offset_Record* Genome_Offsets,unsigned Offsets[]);
void Get_SARange_Fast( char New_Char, SARANGE & Range,BWT *fmi);
void Coverage(Offset_Record Current_Genome);
void Print_LocationX(SARANGE & Tag,char Index);
void Write_Sam(char Sign,char* Chr,unsigned Loc1,unsigned Loc2,int G1,int G2,char Code,unsigned Read_ID);
int Juncs_In_Read(OPX* Junc_List,int & Junc_List_Ptr,int Head_Start,int Head_Length,int Tail_Start, int Tail_Length);
void Find_Split_Junctions(OPX* Junc_List,int Junc_List_Ptr,Hash & Junctions,unsigned Last_ID,char* Genome);
int Scan_Minus_Motif(unsigned x,unsigned y);
int Scan_Plus_Motif(unsigned x,unsigned y);
void Mark_Exons();
void Seek_Island_Junctions(Island* Island_List,map <unsigned,unsigned char> & Donor_AT,map <unsigned,unsigned char> & Donor_GT,map <unsigned,unsigned char> & Donor_GC,map <unsigned,unsigned char> & Acc_AG,map <unsigned,unsigned char> & Acc_AC);
void Seek_Island_JunctionsM(Island* Island_List,map <unsigned,unsigned char> & Donor_CT,map <unsigned,unsigned char> & Donor_GT,map <unsigned,unsigned char> & Acc_GC,map <unsigned,unsigned char> & Acc_AC,map <unsigned,unsigned char> & Acc_AT);
void Match_Unmapped(FILE* Unmapped,Hash & Junctions,COVTYPE* Coverage,map <unsigned,unsigned char> & Acc_AG,map <unsigned,unsigned char> & Acc_AC);
void Match_UnmappedM(FILE* Unmapped,Hash & Junctions,map <unsigned,unsigned char> & Acc_GC,map <unsigned,unsigned char> & Acc_AC,map <unsigned,unsigned char> & Acc_AT);
void Match_UnmappedX(FILE* Unmapped,Hash & Junctions,map <unsigned,unsigned char> & Donor_AT,map <unsigned,unsigned char> & Donor_GT,map <unsigned,unsigned char> & Donor_GC);
void Match_UnmappedMX(FILE* Unmapped,Hash & Junctions,map <unsigned,unsigned char> & Donor_GT,map <unsigned,unsigned char> & Donor_CT);
char Search_Forwards_Exact(SARANGE & Tag,char* Current_Tag, int Start,int StringLength);
char Extend_Right(SARANGE & Tag,char* Current_Tag,int Start,int StringLength,int Stop_Gap);
char Extend_Left(SARANGE & Tag,char* Current_Tag,int Start,int StringLength,int Stop_Gap);
char Search_Backwards_Exact(SARANGE & Tag,char* Current_Tag,int Start,int StringLength);
char Scan_Half_ExtendR(char* Current_Tag, ORIENTATION & O,unsigned Read_ID);
char Scan_Half_ExtendL(char* Current_Tag, ORIENTATION & O,unsigned Read_ID);
char Scan_Half_ExtendLX(char* Current_Tag, ORIENTATION & O,unsigned Read_ID);
char Guess_Orientation(int Off);
char Load_Tail (char* Current_Tag, SARANGE & Tag);
char Load_Head (char* Current_Tag, ORIENTATION & O,SARANGE & Tag);
char Read_Tag(READ & Head,READ & Tail);
unsigned Location(SARANGE & Tag, char Index);
unsigned Get_File_Size(FILE* File);
unsigned Get_Block_Start(unsigned SAValue,unsigned & M);
gzFile File_OpenZ(const char* File_Name,const char* Mode);
BWT* Load_Indexes(char *BWTINDEX,char *OCCFILE, char *SAFILE);
void UnLoad_Indexes();
FILE* File_Open(const char* File_Name,const char* Mode);
unsigned Splicing_Signal(Island* Current_Island, Island* Next_Island);
void Merge_Island(Island* Prev_Island, Island* Current_Island);
bool Loop_For_Junctions(map <unsigned,unsigned char>::iterator &Start, map<unsigned,unsigned char>::iterator & End, unsigned char Sig, int Pairing, int x, int y, char* Read, Hash & Junctions, int Direction);
//}----------------------------- FUNCTION PRTOTYPES -------------------------------------------------/*
//{---------------------------- GLOBAL VARIABLES -------------------------------------------------
char Hit_Mask=0;
unsigned Sum=0;
unsigned T=0;
char debug;
char const *Int_To_Junc[10];
string Genome_String;
State* Can_Acceptor;
State* Can_Donor;
MMPool* mmPool;
Offset_Record Genome_Offsets[80];
gzFile Input_File;
gzFile Mate_File;
FILE* Junc_Log;
FILE* Input_FileO;
FILE* F;
READ Head, Tail;
RANGEINDEX Range_Index;
ORIENTATION Orientation[4];//orientation info...
BWT* revfmi,*fwfmi;
SARANGE Cache_SF[MAXTAG+1];
SARANGE Cache_SB[MAXTAG+1];
SARANGE Branch_Ranges[4];
SARANGE Temp_Branch_Ranges[4];
SARANGE* Head_Hits_Pos;//[30];
SARANGE* Head_Hits_Neg;//[30];
SARANGE* Tail_Hits_Neg;//[30];
SARANGE* Tail_Hits_Pos;//[30];
SARANGE* Two_Mismatches_At_EndP;//stores 2-mismatches whose last mismatch occurs at the last nuce..
SARANGE* Two_Mismatches_At_EndC;//stores 2-mismatches whose last mismatch occurs at the last nuce..
SARANGE* Two_Mismatches_At_End_ForwardP;//stores 2-mismatches whose last mismatch occurs at the last nuce..
SARANGE* Two_Mismatches_At_End_ForwardC;//stores 2-mismatches whose last mismatch occurs at the last nuce..
SARANGE* Mismatches_BackwardP;//stores possible 2-mismatches
SARANGE* Mismatches_BackwardC;//stores possible 2-mismatches
SARANGE* Mismatches_ForwardP;//stores possible 2-mismatches
SARANGE* Mismatches_ForwardC;//stores possible 2-mismatches
SARANGE* FSSStack;
SARANGE* BMStack;
SARANGE* BMHStack;
PAIR* Pairs;
FILE* Original_File;
FILE* SAM;
FILE* BigBed;
FILE* Junctions_File;
FILE* Map_File;
//--------------------------- Range Index Gloals ---------------
SA* SA_Index;
int* SA_Blocks;
unsigned Hits;
char COMPRESS;
//--------------------------- Range Index Gloals ---------------
char First_Pass=TRUE;
char COUNT_LF_MOTIF=TRUE;
char MAPMODE=TRUE;
char USEREFGENE=FALSE;
char PROCESSMODE=TRUE;
char NORMAL_TAGS;
char MINIMUMINDEX=FALSE;
char PAIRING_TYPE;
char FILETYPE;
char BUILDREF=FALSE;
char MINIMIZE_GAP=FALSE;
char WRITE_SPLITREAD=FALSE;
char DUMP_ALL_JUNC=FALSE;
char Half_Intact;
char Char_To_CodeC[256];
char Char_To_Code[256];
char Packed[MAXTAG+1];
char Random_Array[]="tatacgataggacaatgtcttcgaagcccacgcggtaagccggtcattgcggttgtgcgaacactatcagcctcgctgcatggttaccctgggtggataggacgtttgcccgacattttgacacgcataaaaggtctgtagtgggggtggcacaccataaaccctggggcggctccacgatcgtaaaatcctgcgatctg";
char JunctionFile[]="junctions.bed";
char Wiggles[]="coverage.bed";
char MapFile[]="mapfile";
char Patternfile_Count=0;//number of query files (head /tail)
char Try[4];
int HITS;
int EXON_JOIN_THRESHOLD=10;//500;//76;
int PAIR_LENGTH_RIGHT,PRRH,PRLH;
int PAIR_LENGTH_LEFT,PLRH,PLLH;
int TAG_COPY_LEN;
int STRINGLENGTH,LH,RH,RHQR,RHQL,STRINGLENGTHO;
int MAX_TAGS_TO_PROCESS;
int MIS_IN_INITMAP=2;
int MIS_SPLICE=2;
int SAGAP_CUTOFF_T,SAGAP_CUTOFF_H;
int RQFACTOR=18;//length of indexed rqindex string..
int MAXHITS=1;
int ARRAY_BOUND,END_BOUND;
int Two_Mismatches_At_End_PointerP;
int Two_Mismatches_At_End_PointerC;
int Mismatches_Forward_PointerP;
int Mismatches_Forward_PointerC;
int Mismatches_Backward_PointerP;
int Mismatches_Backward_PointerC;
int Two_Mismatches_At_End_Forward_PointerP=0;
int Two_Mismatches_At_End_Forward_PointerC=0;
int Genome_Count;int COUNT=0;int COUNT_IN_EXT=0;//int COUNT=2;int COUNT_IN_EXT=3;
int MIN_SUPPORT=0; //Minimum number of reads needed to cover a base...
int MAX_HITS_TO_STORE=200;//later resolve this and MAXCOUNT conflict..
int MAXCOUNT=MAX_HITS_TO_STORE; //30000 //Maximum number of pairs to enum...
unsigned File_Size;
unsigned Dummy[]={0,0};
unsigned EXONGAP=1000;
unsigned SOURCELENGTH;
unsigned Branch_Characters[4];
unsigned Temp_BC[4];
unsigned Conversion_Factor;
unsigned CONVERSION_FACTOR;//factor for rqindex..
unsigned Offsets[80];
const char* Code_To_Char="acgt";const char* Code_To_CharCAPS="ACGT";const char* Code_To_CharC="tgca";const char* Code_To_CharCAPSC="TGCA";
char* JUNCTIONFILE=JunctionFile;
char* WIGGLEFILE=Wiggles;
char* MAPFILE=MapFile;
char* PATTERNFILE;
char* PATTERNFILE1;
char* BWTFILE ;
char* OCCFILE ;
char* SAFILE;
char* REVBWTINDEX;
char* REVOCCFILE;
char* REVSAFILE;
char* LOCATIONFILE;
char* INDFILE;
char* BLKFILE;
char* BINFILE;
char* SORTEDRANGEFILE;
char* RANGEFILE;
char* Source;//buffer for command line processing...
char* Current_Tag;
unsigned char* Original_Text;//encoded genome file...
unsigned Orig_Text;
Out_Record* Buffer;
Out_Record Unique;
//}---------------------------- GLOBAL VARIABLES -------------------------------------------------
//{---------------------------- Command Line -------------------------------------------------
option Long_Options[]=
{
{"help",0,NULL,'h'},
{"query",1,NULL,'q'},
{0,0,0,0}
};
//}---------------------------- Command Line -------------------------------------------------
int main(int argc, char* argv[])
{
unsigned Total_Hits=0,Tags_Processed=0,Tag_Count=0;
time_t Start_Time,End_Time;
unsigned Number_of_Tags=1000,Average_Length;
unsigned Progress=0;
char Pass_Count;
unsigned Read_ID=0;
char FULLMATCHONLY=FALSE;//TRUE;
SARANGE Tag;
Parse_Command_line(argc,argv);
if(mkdir("Raw_Out",S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && errno != EEXIST) {printf("Cannot create Temp directory..\n");exit(-1);};
Genome_Count=Load_Locations(Genome_Offsets,Offsets);
Init();
Verbose();
time(&Start_Time);
time_t Maptime;
if(MAPMODE)
{
printf("======================]\r[");//progress bar....
if (NORMAL_TAGS) Pass_Count=1; else Pass_Count=2;//Number of passes to make..
while (Read_Tag(Head,Tail))
{
char Head_Tail=0;
if (MAX_TAGS_TO_PROCESS && Tag_Count >= MAX_TAGS_TO_PROCESS) break;
while (Head_Tail<Pass_Count)//loop head, then tail
{
if(Head_Tail) Head=Tail;
Head_Tail++;
Tag_Count++;
Progress++;
if (Progress==Number_of_Tags)
{
if (MAX_TAGS_TO_PROCESS)
{
Number_of_Tags=(MAX_TAGS_TO_PROCESS)/20;
Progress=0;
Show_Progress(Tag_Count*100/MAX_TAGS_TO_PROCESS);
}
else
{
off64_t Current_Pos=ftello64(Input_FileO);
Average_Length=Current_Pos/Tag_Count+1;//+1 avoids divide by zero..
Number_of_Tags=(File_Size/Average_Length)/20;
Progress=0;
Show_Progress(Current_Pos*100/File_Size);
}
}
Read_ID++;
if(!Scan(Total_Hits,Tags_Processed,0,Read_ID))
{
int olds=STRINGLENGTH;
int oldi=MIS_IN_INITMAP;
int oldc=COUNT_IN_EXT;
MIS_IN_INITMAP=0;
STRINGLENGTH=50;
//COUNT_IN_EXT=1;
Scan(Total_Hits,Tags_Processed,0,Read_ID);
//Scan(Total_Hits,Tags_Processed,0,Read_ID);
{
Read_ID++;
int oldso=STRINGLENGTHO;
int o=STRINGLENGTHO-50;
STRINGLENGTHO=STRINGLENGTH=50;
Scan(Total_Hits,Tags_Processed,o,Read_ID);
//Scan(Total_Hits,Tags_Processed,o,Read_ID);
{
Read_ID++;
STRINGLENGTHO=oldso;
Scan(Total_Hits,Tags_Processed,18,Read_ID);//19
}
STRINGLENGTHO=oldso;
//left split..
}
STRINGLENGTH=olds;
MIS_IN_INITMAP=oldi;
COUNT_IN_EXT=oldc;
}
}
}
UnLoad_Indexes();
printf("\r[++++++++100%%+++++++++]\n");//progress bar....
time(&End_Time);
Maptime=difftime(End_Time,Start_Time);
}
if (PROCESSMODE)
{
for(int i=0;i<Genome_Count;i++)
{
Genome_Offsets[i].Offset=Genome_Offsets[i+1].Offset;
printf("Processing Chromosome %s...\n",Genome_Offsets[i].Genome);
Orig_Text= Offsets[i];
Coverage(Genome_Offsets[i]);
}
}
printf("Average %d\n",Sum);
printf("%u / %u Tags/Hits",Tag_Count,Tags_Processed+Total_Hits);
time(&End_Time);printf("\n Time Taken - %.0lf Seconds ..\n ",difftime(End_Time,Start_Time));
printf("\n Time Taken - %.0lf Seconds ..\n ",Maptime);
}
/*
* === FUNCTION ======================================================================
* Name: Guess_Orientation
* Description: Try to guess the strand of the read...
* Scanning is started from Read[Off,...,Off+STRINGLENGTH]
* return 0 for head, 1 for tail
* if TAGRQF.Start=0, read mapped ...
* =====================================================================================
*/
char Guess_Orientation(int Off)
{
char Half_OK_Read=FALSE;
char Half_OK_Compl=TRUE;
SARANGE TagF,TagFR,TagFH;//Tag,Tag for RQFactor, Tag for LH.
SARANGE TagFC,TagFCR,TagFCH;
SARANGE TagB,TagBR,TagBH;//Tag,Tag for RQFactor, Tag for LH.
SARANGE TagBC,TagBCR,TagBCH;
Mismatches_Forward_PointerP=0;//init pointers..
Mismatches_Forward_PointerC=0;
Mismatches_Backward_PointerP=0;
Mismatches_Backward_PointerC=0;
Two_Mismatches_At_End_PointerP=0;
Two_Mismatches_At_End_PointerC=0;
Two_Mismatches_At_End_Forward_PointerP=0;
Two_Mismatches_At_End_Forward_PointerC=0;
HITS=0;Orientation[0].Strand=0;
TagF.Start=1;TagF.End=SOURCELENGTH;TagF.Skip=0;TagF.Mismatches=0;TagF.Mismatch_Char=0;
TagFC.Level=TagB.Level=TagBC.Level=0;
TagFC.Start=TagB.Start=TagBC.Start=0;
Try[LEFT]=0;Try[RIGHT]=0;
Try[LEFTC]=0;Try[RIGHTC]=0;
Current_Tag=Head.Tag+Off;
Cache_SF[RQFACTOR].Start=0;
Cache_SF[LH].Start=0;
char zfound=0,zcfound=0;Hit_Mask=0;
if((zfound=Search_Forwards_Exact(TagF,Head.Tag+Off,1,STRINGLENGTH)) )//search + for exact..
{
if(TagF.Start==TagF.End || TagF.Skip)//Unique hit..
{
Print_LocationX(TagF,FW);
HITS++;
//return TRUE;//exact match...
}
else
{
HITS+=TagF.End-TagF.Start+1;
}
//else
{
//return TRUE;//exact match...
TagFR=Cache_SF[RQFACTOR];//save important sa ranges..
TagFH=Cache_SF[LH];
Try[LEFT]=1;//check feasibility of extentions..
TagB.Start=1;TagB.End=SOURCELENGTH;TagB.Skip=0;TagB.Mismatches=0;TagB.Mismatch_Char=0;
Search_Backwards_Exact(TagB,Head.Tag+Off,STRINGLENGTH,STRINGLENGTH);
Try[RIGHT]=1;
TagBH=Cache_SB[RH];
TagBR=Cache_SB[RQFACTOR];
Hit_Mask=TRUE;
}
}
//else //search - for exact...
{
TagFR=Cache_SF[RQFACTOR];//save important sa ranges..
TagFH=Cache_SF[LH];
Cache_SF[RQFACTOR].Start=0;
Cache_SF[LH].Start=0;
Try[LEFT]=(TagF.Level >= LH);//check feasibility of extentions..
TagFC.Start=1;TagFC.End=SOURCELENGTH;TagFC.Skip=0;TagFC.Mismatches=0;TagFC.Mismatch_Char=0;Current_Tag=Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off);
if((zcfound=Search_Forwards_Exact(TagFC,Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off),1,STRINGLENGTH)))//search complement for exact...
{
if(TagFC.Start==TagFC.End || TagFC.Skip)//Unique hit..
{
Print_LocationX(TagFC,FW);
HITS++;
//return TRUE;//exact match..
}
else
{
HITS+=TagFC.End-TagFC.Start+1;
}
//else
{
TagFCR=Cache_SF[RQFACTOR];//save important sa ranges..
TagFCH=Cache_SF[LH];
Try[LEFTC]=1;//check feasibility of extentions..
TagBC.Start=1;TagBC.End=SOURCELENGTH;TagBC.Skip=0;TagBC.Mismatches=0;TagBC.Mismatch_Char=0;
Search_Backwards_Exact(TagBC,Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off),STRINGLENGTH,RH+1);
Try[RIGHTC]=1;
TagBCH=Cache_SB[RH];
TagBCR=Cache_SB[RQFACTOR];
Hit_Mask=TRUE;
}
}
//if(!Hit_Mask)
{
TagFCR=Cache_SF[RQFACTOR];//save important sa ranges..
TagFCH=Cache_SF[LH];
Try[LEFT]=(TagF.Level >= LH);//check feasibility of extentions..
Try[LEFTC]=(TagFC.Level >=LH);
if ( MIS_IN_INITMAP && (Try[LEFT] || Try[LEFTC]) && !(zcfound ||zfound) )//left half intact..
{
if (TagF.Level > TagFC.Level)//guess orientation..
{
SARANGE Tag=TagFH;Current_Tag=Head.Tag+Off;Tag.Level=1;
if(Search_Forwards(Tag,1,LH+1,RH,Two_Mismatches_At_End_Forward_PointerP,Two_Mismatches_At_End_ForwardP,Mismatches_Forward_PointerP,Mismatches_ForwardP)) HITS++;//return TRUE;
if (Try[LEFTC])//complement is also long..
{
SARANGE Tag=TagFCH;Current_Tag=Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off);Tag.Level=1;
if(Search_Forwards(Tag,1,LH+1,RH,Two_Mismatches_At_End_Forward_PointerC,Two_Mismatches_At_End_ForwardC,Mismatches_Forward_PointerC,Mismatches_ForwardC)) HITS++;//return TRUE;
}
else {Half_Intact=FALSE;Orientation[1].Strand=0;}//there are breaks in both halves..
}
else
{
SARANGE Tag=TagFCH;Current_Tag=Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off);Tag.Level=1;
if(Search_Forwards(Tag,1,LH+1,RH,Two_Mismatches_At_End_Forward_PointerC,Two_Mismatches_At_End_ForwardC,Mismatches_Forward_PointerC,Mismatches_ForwardC)) HITS++;//return TRUE;
if (Try[LEFT])//complement is also long..
{
SARANGE Tag=TagFH;Current_Tag=Head.Tag+Off;Tag.Level=1;
if(Search_Forwards(Tag,1,LH+1,RH,Two_Mismatches_At_End_Forward_PointerP,Two_Mismatches_At_End_ForwardP,Mismatches_Forward_PointerP,Mismatches_ForwardP)) HITS++;// return TRUE;
}
else {Half_Intact=FALSE;Orientation[1].Strand=0;}//There are breaks in both halves..
}
}
}
}
//else//break in left half..
if(1)// && !(zfound || zcfound))
{
TagB.Start=1;TagB.End=SOURCELENGTH;TagB.Skip=0;TagB.Mismatches=0;TagB.Mismatch_Char=0;
Search_Backwards_Exact(TagB,Head.Tag+Off,STRINGLENGTH,STRINGLENGTH);
if (Try[RIGHT]=(TagB.Level >= RH))
{
TagBH=Cache_SB[RH];
TagBR=Cache_SB[RQFACTOR];
//do one mismatch...
if (MIS_IN_INITMAP)
{
SARANGE Tag=TagBH;Current_Tag=Head.Tag+Off;Tag.Level=1;
if (Search_Backwards(Tag,1,LH,LH,Two_Mismatches_At_End_PointerP,Two_Mismatches_At_EndP,Mismatches_Backward_PointerP,Mismatches_BackwardP)) HITS++;//return TRUE;
}
}
TagBC.Start=1;TagBC.End=SOURCELENGTH;TagBC.Skip=0;TagBC.Mismatches=0;TagBC.Mismatch_Char=0;
Search_Backwards_Exact(TagBC,Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off),STRINGLENGTH,STRINGLENGTH);
if (Try[RIGHTC]=(TagBC.Level >= RH))
{
TagBCH=Cache_SB[RH];
TagBCR=Cache_SB[RQFACTOR];
//do one mismatch...
if (MIS_IN_INITMAP)
{
SARANGE Tag=TagBCH;Current_Tag=Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off);Tag.Level=1;
if (Search_Backwards(Tag,1,LH,LH,Two_Mismatches_At_End_PointerC,Two_Mismatches_At_EndC,Mismatches_Backward_PointerC,Mismatches_BackwardC)) HITS++;//return TRUE;
}
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------------
//do two mismatch 2|0..
//int MaxF=(TagF.Level>TagFC.Level) ? TagF.Level : TagFC.Level;
//int MaxB=(TagB.Level>TagBC.Level) ? TagB.Level : TagBC.Level;
int MaxF=(TagF.Level>TagB.Level) ? TagF.Level : TagB.Level;
int MaxB=(TagBC.Level>TagFC.Level) ? TagBC.Level : TagFC.Level;
if(MaxF>MaxB)//guess orientation...
{
Orientation[0].Strand='+';
Orientation[0].String=Head.Tag+Off;
Orientation[0].TagF=TagF;
Orientation[0].TagFR=TagFR;
Orientation[0].TagFH=TagFH;
Orientation[0].TagB=TagB;
Orientation[0].TagBR=TagBR;
Orientation[0].TagBH=TagBH;
Orientation[1].Strand='-';
Orientation[1].String=Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off);
Orientation[1].TagF=TagFC;
Orientation[1].TagFR=TagFCR;
Orientation[1].TagFH=TagFCH;
Orientation[1].TagB=TagBC;
Orientation[1].TagBR=TagBCR;
Orientation[1].TagBH=TagBCH;
}
else
{
Orientation[1].Strand='+';
Orientation[1].String=Head.Tag+Off;
Orientation[1].TagF=TagF;
Orientation[1].TagFR=TagFR;
Orientation[1].TagFH=TagFH;
Orientation[1].TagB=TagB;
Orientation[1].TagBR=TagBR;
Orientation[1].TagBH=TagBH;
Orientation[0].Strand='-';
Orientation[0].String=Head.Complement+(STRINGLENGTHO-STRINGLENGTH-Off);
Orientation[0].TagF=TagFC;
Orientation[0].TagFR=TagFCR;
Orientation[0].TagFH=TagFCH;
Orientation[0].TagB=TagBC;
Orientation[0].TagBR=TagBCR;
Orientation[0].TagBH=TagBCH;
}
if (HITS) {return TRUE;}
if (MIS_IN_INITMAP < 2 ) return FALSE;
if(!(zcfound || zfound)) return Two_Mismatch_Search();
//---------------------------------------------------------------------------------------------------------------------------------------------------
//Should have found all exact, 1 mismatches and 2|0 mismatches...
}
//{-------------------------------------- Coverage -------------------------------------------------------------------
void Coverage(Offset_Record Current_Genome)
{
Hash Junctions;
Hash Unmapped_Junctions;
OP JPair;