This repository has been archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
alviewcore.cpp
11870 lines (10173 loc) · 372 KB
/
alviewcore.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
/*
Written by Richard Finney, 2015-2016, National Cancer Institute, National Institutes of Health
This file "alviewcore.cpp" contains the core alview routines.
This is *** MOSTLY *** public domain ... EXCEPTION: note GOTOH code by Peter Clote.
Clote's GOTOH (optimized Smith-Watterman) code is only for non-commercial users.
To deal with this : just keep keep #define GOTOH=0 to disable it OR re-engineer it if you are commercial outfit.
Otherwise, do whatever you want! NIH assumes no liabilities or responsibilities. Use at your own risk.
#Compile for Webserver on Linux ...
g++ -DUNIX -DWEB_SERVER=1 -UQT_GUI -UCOMMAND_LINE -DNATIVE -Wall -o alview alviewcore.cpp alvmisc.cpp -lz -Wall -I/h1/finneyr/samtools-0.1.18/ /h1/finneyr/samtools-0.1.18/libbam.a -lm -lz
# notes ...
#Target specific options:
# -m64 Generate 64bit x86-64 code
# -minline-all-stringops Inline all known string operations
# -mno-align-stringops Do not align destination of the string operations
# -momit-leaf-frame-pointer Omit the frame pointer in leaf functions
Compile for command line Using NEW samtools 1.0 ( after august 2014) on linux
gcc -Wall -DSAMTOOLS1=1 -DUNIX=1 -DCMD_LINE=1 -o alview alviewcore.cpp -I. \
-I/h1/finneyr/samtools-1.0/ \
-I/h1/finneyr/samtools-1.0/htslib-1.0/ \
-I/h1/finneyr/samtools-1.0/htslib-1.0/htslib/ \
-L/h1/finneyr/samtools-1.0/ \
/h1/finneyr/samtools-1.0/libbam.a \
/h1/finneyr/samtools-1.0/htslib-1.0/libhts.a \
-lbam -lm -lz -lgd -lpthread -lstdc++
Compile for command line Using old samtools - compile for command line on linux ...
****** note, can't mix c and cpp effortlessley
cp alvmisc.c alvmisc.cpp
gcc -Wall -DUNIX=1 -DCMD_LINE=1 -o alview_cmdline_linux alvmisc.cpp alviewcore.cpp -I. -I/data/nextgen/finneyr/samtools-0.1.18/ -L/data/nextgen/finneyr/samtools-0.1.18/ -lbam -lm -lz -lstdc++
Compile for command line cygwin (USE SAMTOOLS 0.18)
cp alvmisc.c alvmisc.cpp
gcc -Wall -DUNIX=1 -DCMD_LINE=1 -o alview_cmdline_cygwin alvmisc.cpp alviewcore.cpp -I. -Ic:/rich/samtools-0.1.18 -Lc:/rich/samtools-0.1.18 -lbam -lm -lz -lstdc++
Example command line run (cygwin) :
G/alview_cmdline_cygwin.exe c:/rich/BAMS/RW1.BWA-aln.RG.MKDUP.bam x.png chr17:7512444-7519536 hg19
Compile command line for *Windows* cl.exe compiler (visual C), *note* usage of hacked samtools library
cl -Ox /MD -c -DWIN32=1 -DCMD_LINE=1 -I sam01832/ -I zlib32/ -I . alviewcore.cpp
cl -Ox /MD -c -DWIN32=1 -DCMD_LINE=1 -I sam01832/ -I zlib32/ -I . alvmisc.cpp
link /MACHINE:x86 /OUT:alview_cmd_win.exe Alvmisc.obj alviewcore.obj sam01832/my.lib zlib32/zlib.lib
todo: (soon !)
Prev Next
guard against too big mallocs
multi image operations
divorce from hg18, hg19
fix these messages
[bam_parse_region] fail to determine the sequence name.
example web usage in Excel ...
=HYPERLINK("https://cgwb-test.nci.nih.gov:8443/cgi-bin/alview?position="&D1&":"&E1-100&"-"&E1+100&"&iw=1000&ih=400&file="&A1,"n")
*/
/*
***************** ---------- define these on compile line , example gcc -DWEB_SERVER=1
#define UNIX 0
#define WEB_SERVER 0
#define USE_MYSQL 0
#define CMD_LINE 0
#define QT_GUI 0
*/
#define GOTOH 0
// gotoh turns on Peter Clotes GOTOH code It is limited to noncommercial use. You can re-engineer it for commercial use if you really need it.
// You may TURN ON GOTOH if you are non-commerical
// WxWidgets version , possible someday but not implemented for now ...
#define WX_GUI 0
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#if 0 // def WIN32 // i think some version of msvc didn't get this right
#define unlink _unlink
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "alview.h"
#ifdef WIN32
// windows wacky-ness
#include "WINWACKY/mman.h"
#include <time.h>
#include <WINWACKY/dirent.h>
#include <io.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif
#ifdef UNIX
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#endif
#if MAC
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <dirent.h>
#endif
#ifdef QT_GUI
#ifdef WIN32
#if 0
*** boutell gd graphics library had been removed ***
// needed for gd.h stuff ...
#define NONDLL 1
#define BGDWIN32 1
#include "../libgd-2.1.0_64/rpf/gd.h"
#include "../libgd-2.1.0_64/rpf/gdfontl.h"
#endif
#else
#undef WIN32
#include <gd.h>
#include <gdfontl.h>
#endif
#include "interface.h" // routines to make work on a GUI
#else
#define USE_GD 0 // got rid of gd graphics library - using custom implementation now
#if USE_GD
#include <gd.h> // gd library
gdImagePtr im;
#else
#endif
#define MAXBUFF 20000
#define MAXSMALLBUFF 2000
int snp_call_flag = 0;
#ifdef CMD_LINE
int snp_call_spot = 0; // genomic location
int snp_call_dnaat = 0;
char snp_call_referece = ' ';
char snp_call_chr[MAXBUFF];
int snp_call_A_cnt = 0;
int snp_call_C_cnt = 0;
int snp_call_G_cnt = 0;
int snp_call_T_cnt = 0;
int snp_call_Ins_cnt = 0;
int snp_call_Del_cnt = 0;
#endif
char gchr[MAXBUFF]; // global chromsome string
int gs; // global start genomic cooordinate
int ge; // global end genomic cooordinate
int gdiff; // start - end = gs - ge
int menu; // main menu
int pickmenu;
char pickmenustring[MAXBUFF];
char menustring[MAXBUFF];
char forcefilecgiarg[MAXBUFF];
char pickq[MAXBUFF];
char bamname[MAXBUFF];
char filecgiarg[MAXBUFF];
char jutlfn[MAXBUFF]; // "just use this long file name"
char position[MAXBUFF];
char op[MAXBUFF];
int width,height;
struct image_type image;
struct image_type *im; // = ℑ
#endif
#define NEEDSQL 0
// needsql is for my tcga and target stuff. private access only. sadly. they really should make this data public
#define FAVEBACKGROUNDCOLOR "F8F2E4"
int ih = 350; // Image Height, 350 picked as default
int iw = 1000; // Image Width, 1000 picked as default
char blds[512]; // "genomic build" string : examples "hg19", "hg38"
char fn_bam[FILENAME_MAX]; // bam file name
int did_content_type = 0; // for web services , "started output" , this means printed "Content-type: text/html\n\n" to output for html rendering
int inited_colors_flag = 0; // used to init colors for graphics
char CONFIGFILE[] = "alview.conf";
char URL[512];
char HTDOCSDIR[512];
char CGIDIRIFNECESSARY[512];
// old char FULL_PATH_TO_TTF[512]; // "/usr/X11R6/lib/X11/fonts/TTF/luximb.ttf" ?
char GENOMEDATADIR[512]; // eample /home/rfinney/MD/ALV/release/GENOMEDATA will append blds (build) string
void freedom_for_memory(void);
void free_filez(void);
#if WIN32
void jdebug(const char *s) // for use in debuging
{
// V--- if this set to 0 (zero) to turn off debug
#if 0
// ^--- this thing here - if it is set to 1, then it writes to a file called "x"
static FILE *debugfp = (FILE *)0;
static int cnt = 0;
char fn[2048];
// int i = 0;
// char m[2048];
strcpy(fn,"x");
if (!debugfp) debugfp = fopen(fn,"a");
if (debugfp == (FILE *)0) { return; }
fprintf(debugfp,"%d:%s\n",cnt++,s);
fflush(debugfp);
// fclose(debugfp);
#endif
return;
}
#else
void jdebug(const char *s) // for use in debuging on internet
{
#if 1
// ^^^ TO TURN OFF IF SET THIS TO ONE
// fprintf(stderr,"%s\n",s); fflush(stderr);
#else
register int i;
time_t timer;
char fn[256];
char m[512];
FILE *fp;
/* printf("DEBUG: in jdebug %s <br>\n",s); printf("%s\n",s); */
#ifdef WEB_SERVER
strcpy(fn,"/tmp/x");
#else
strcpy(fn,"x");
#endif
fp = fopen(fn,"a");
if (fp == (FILE *)0) { return; }
timer=time(NULL);
sprintf(m,"%s",asctime(localtime(&timer)));
for (i=0;m[i];i++) if (m[i] == '\n') {m[i]=(char)0; break; }
fprintf(fp,"[%s]%s\n",m,s);
fclose(fp);
return;
#endif
}
#endif
#ifdef MAC
//strlcpy already defined
#else
size_t strlcpy(char *dst, char *src, size_t siz)
{
register char *d = dst;
register const char *s = src;
register size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0)
break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0)
*d = '\0'; /* NUL-terminate dst */
while (*s++)
;
}
return(s - src - 1); /* count does not include NUL */
}
#endif
#define NCI_RED "A90101"
// nice red color associated with NCI brand
#ifdef UNIX
#include <sys/types.h>
#include <unistd.h>
#include <libgen.h>
#endif
char cwd[FILENAME_MAX];
char ced[FILENAME_MAX];
#ifdef WIN32
char hacked_ced[FILENAME_MAX]; // windows - for current executing directory
char hacked_ced_for_sprintf[FILENAME_MAX]; // windows - for URL
#endif
void full_path_to_current_and_executing_directory(char cwd[], char ced[])
{
#ifdef WIN32
// Windows:
int i,j;
int bytes = 0;
TCHAR cExePath[FILENAME_MAX];
char t[FILENAME_MAX];
TCHAR cCurrentPath[FILENAME_MAX];
strcpy(cwd,".");
strcpy(ced,".");
cExePath[0] = (DWORD)0;
cCurrentPath[0] = (DWORD)0;
#if 0
// worked on qt
bytes = GetModuleFileName(NULL, cExePath, FILENAME_MAX);
if(bytes == 0) { } // return -1;
else
{
// dos size_t wcstombs( char *mbstr, const wchar_t *wcstr, size_t count);
// unix size_t wcstombs(char *S, const wchar_t *PWC, size_t N); #include <stdlib.h>
bytes = wcstombs(ced,cExePath,bytes);
}
bytes = GetCurrentDirectory(FILENAME_MAX,cCurrentPath);
bytes = wcstombs(cwd,cCurrentPath,bytes);
#endif
bytes = GetModuleFileName(NULL, t, FILENAME_MAX);
if(bytes == 0) { } // return -1;
strcpy(ced,t);
bytes = GetCurrentDirectory(FILENAME_MAX,t);
strcpy(cwd,t);
char drive[FILENAME_MAX];
char dir[FILENAME_MAX];
char fname[FILENAME_MAX];
char ext[FILENAME_MAX];
_splitpath(ced,drive,dir,fname,ext);
#if QT_GUI
sprintf(ced,"%s\%s",drive,dir);
#else
sprintf(ced,"%s\\%s",drive,dir);
#endif
strcpy(hacked_ced,ced);
for (i=0;ced[i];i++)
{
if (ced[i] == '\\') hacked_ced[i] = '/';
else hacked_ced[i] = ced[i];
}
hacked_ced[i] = (char)0;
for (j=i=0;ced[i];i++)
{
if (ced[i] == '\\') { hacked_ced_for_sprintf[j++] = '\\'; hacked_ced_for_sprintf[j++] = '\\'; }
else hacked_ced_for_sprintf[j++] = ced[i];
}
hacked_ced_for_sprintf[j] = (char)0;
return;
#endif
#ifdef UNIX
// Linux:
char szTmp[32];
char tmps[FILENAME_MAX];
int bytes;
int len = FILENAME_MAX;
sprintf(szTmp, "/proc/%d/exe", getpid());
bytes = readlink(szTmp, ced, len);
if (bytes >= 0)
ced[bytes] = '\0';
strcpy(tmps,ced);
char *z = dirname(tmps);
if (z) strcpy(ced,z);
getcwd(cwd,FILENAME_MAX);
return;
#endif
}
#if 0 // needed for really old compilers
long ftell(FILE *stream);
off_t ftello(FILE *stream);
#endif
#ifdef WIN32
off_t ftello(FILE *stream);
#endif
/// --- samtools includes ...
#ifdef WIN32
#include "winwacky.h"
#endif
#if NATIVE
// #include "minibam.h"
#include "alview.h"
#else
#endif
#include "bam.h"
#include "sam.h"
#include "kstring.h"
#if USE_MYSQL
// dont worry about this unless you want to hook up your own database.
// Regardless you probably don't want to deal with UCSC cruft and their pathological fear of the stanadard library and endless piles of abstration .
#include "/usr/local/mysql-standard-5.0.27-linux-x86_64-icc-glibc23/include/mysql.h"
// #define NEEDCONFIG 1
#include "cbiitcommon.c"
#include "common.h"
#include "linefile.h"
#include "hash.h"
#include "cheapcgi.h"
#include "htmshell.h"
#include "obscure.h"
#include "web.h"
#include "cart.h"
#include "hdb.h"
#include "dbDb.h"
#include "hgFind.h"
#include "hCommon.h"
#include "hui.h"
#include "customTrack.h"
#include "../lib/cbiit.h"
#endif
int black, white, green, red, blue, orange, gray, yellow, purple,pink, lightgreen,lightgray, lightpink, lightblue, lightbrown,cyan;
int maroon;
int verylightgray;
int grays[10];
int grays2[10];
int schemered;
int schemecyan;
int schemeblue;
int schemegreen;
int schemeyellow;
int schemewhite;
int schemepurple;
int dna_a ;
int dna_c ;
int dna_g ;
int dna_t ;
char dna_a_s[24];
char dna_c_s[24];
char dna_g_s[24];
char dna_t_s[24];
char dna_I_s[24]; // insert
char dna_D_s[24]; // delete
void ImageColorAllocate(int *color,unsigned char R,unsigned char G,unsigned char B)
{
unsigned char *z;
z = (unsigned char *)color;
*(z+0) = R;
*(z+1) = G;
*(z+2) = B;
return;
}
void init_colors(void)
{
#if 0 // USE_GD
if (inited_colors_flag == 0)
{
red = gdImageColorAllocate(im, 255, 0, 0);
green = gdImageColorAllocate(im, 0, 255, 0);
blue = gdImageColorAllocate(im, 0, 0, 255);
cyan = gdImageColorAllocate(im, 0x5, 0xeb,0xf9); // 05ebf9
lightgreen = gdImageColorAllocate(im, 64, 125, 64);
black = gdImageColorAllocate(im, 0, 0, 0);
purple = gdImageColorAllocate(im, 150, 0, 255);
orange = gdImageColorAllocate(im, 255, 100 , 0 );
// 9f005f ff5f00 bfff00 003fbf
strcpy(dna_a_s,"9f005f");
strcpy(dna_c_s,"ff5f00");
strcpy(dna_g_s,"bfff00");
strcpy(dna_t_s,"003fbf");
strcpy(dna_I_s,"00ffff"); // cyan insert
strcpy(dna_D_s,"ffb000"); // yellow delete
dna_a = gdImageColorAllocate(im,0x9f,0x00,0x5f );
dna_c = gdImageColorAllocate(im,0xff,0x5f,0x00 );
dna_g = gdImageColorAllocate(im,0xbf,0xff,0x00 );
dna_t = gdImageColorAllocate(im,0x00,0x3f,0xbf );
pink = gdImageColorAllocate(im, 0xfd, 0x8b , 0xe9 ); // pink = gdImageColorAllocate(im, 200, 100 , 100 );
lightblue = gdImageColorAllocate(im, 170, 170, 255);
lightbrown = gdImageColorAllocate(im, 0xEE, 0xC5, 0x91); // A67D3D = 166 , 125, 61 // EEC591
white = gdImageColorAllocate(im, 255, 255, 255);
gray = gdImageColorAllocate(im, 0xc0,0xc0,0xc0);
lightgray = gdImageColorAllocate(im, 0xd0,0xd0,0xd0);
lightpink = gdImageColorAllocate(im, 0xf1,0xcd,0xee); // f1cdee
yellow = gdImageColorAllocate(im, 0xff, 0xb0 , 0x00 );// fffb00
maroon = gdImageColorAllocate(im, 0xd1, 0x34 , 0x00 );
schemepurple = gdImageColorAllocate(im, 0xea, 0xc7 , 0xeb );
schemewhite = gdImageColorAllocate(im, 0xff, 0xff , 0xff );
schemered = gdImageColorAllocate(im, 0xFF, 0x00 , 0x00 );
schemecyan = gdImageColorAllocate(im, 0x00, 0xff , 0xf0 );
schemeblue = gdImageColorAllocate(im, 0x00, 0xce , 0xf3 );
schemegreen = gdImageColorAllocate(im, 0x39, 0xe6 , 0x00 );
schemeyellow = gdImageColorAllocate(im, 0xff, 0xb0 , 0x00 );// fffb00
verylightgray = gdImageColorAllocate(im, 0xf8,0xf8,0xf8);
grays[0] = gdImageColorAllocate(im, 0xff,0xff,0xff);
grays[1] = gdImageColorAllocate(im, 0xe0,0xe0,0xe0);
grays[2] = gdImageColorAllocate(im, 0xc0,0xc0,0xc0);
grays[3] = gdImageColorAllocate(im, 0xa0,0xa0,0xa0);
grays[4] = gdImageColorAllocate(im, 0x80,0x80,0x80);
grays[5] = gdImageColorAllocate(im, 0x60,0x60,0x60);
grays[6] = gdImageColorAllocate(im, 0x40,0x40,0x40);
grays[7] = gdImageColorAllocate(im, 0x20,0x20,0x20);
grays[8] = gdImageColorAllocate(im, 0x10,0x10,0x10);
grays[9] = gdImageColorAllocate(im, 0x00,0x00,0x00);
grays2[0] = gdImageColorAllocate(im, 255,255,255);
grays2[1] = gdImageColorAllocate(im, 245,245,245);
grays2[2] = gdImageColorAllocate(im, 235,235,235);
grays2[3] = gdImageColorAllocate(im, 225,225,225);
grays2[4] = gdImageColorAllocate(im, 215,215,215);
grays2[5] = gdImageColorAllocate(im, 205,205,205);
grays2[6] = gdImageColorAllocate(im, 195,195,195);
grays2[7] = gdImageColorAllocate(im, 185,185,185);
grays2[8] = gdImageColorAllocate(im, 175,175,175);
grays2[9] = gdImageColorAllocate(im, 165,165,165);
inited_colors_flag = 1;
}
#endif
// our own custom implemention that does not use GD graphics library, but uses our custom implementation ...
if (inited_colors_flag == 0)
{
ImageColorAllocate(&red, 255, 0, 0);
ImageColorAllocate(&green, 0, 255, 0);
ImageColorAllocate(&blue, 0, 0, 255);
ImageColorAllocate(&cyan, 0x5, 0xeb,0xf9); // 05ebf9
ImageColorAllocate( &lightgreen,64, 125, 64);
ImageColorAllocate( &black,0, 0, 0);
ImageColorAllocate( &purple,150, 0, 255);
ImageColorAllocate( &orange, 255, 100 , 0 );
// 9f005f ff5f00 bfff00 003fbf
strcpy(dna_a_s,"9f005f"); // nucleotides acgt ...
strcpy(dna_c_s,"ff5f00");
strcpy(dna_g_s,"bfff00");
strcpy(dna_t_s,"003fbf");
strcpy(dna_I_s,"00ffff"); // cyan insert
strcpy(dna_D_s,"ffb000"); // yellow delete
ImageColorAllocate(&dna_a,0x9f,0x00,0x5f );
ImageColorAllocate(&dna_c,0xff,0x5f,0x00 );
ImageColorAllocate(&dna_g,0xbf,0xff,0x00 );
ImageColorAllocate(&dna_t,0x00,0x3f,0xbf );
ImageColorAllocate(&pink, 0xfd, 0x8b , 0xe9 ); // pink = ImageColorAllocate( 200, 100 , 100 );
ImageColorAllocate(&lightblue, 170, 170, 255);
ImageColorAllocate(&lightbrown, 0xEE, 0xC5, 0x91); // A67D3D = 166 , 125, 61 // EEC591
ImageColorAllocate(& white, 255, 255, 255);
ImageColorAllocate(& gray, 0xc0,0xc0,0xc0);
ImageColorAllocate(& lightgray, 0xd0,0xd0,0xd0);
ImageColorAllocate(& lightpink, 0xf1,0xcd,0xee); // f1cdee
ImageColorAllocate(& yellow, 0xff, 0xb0 , 0x00 );// fffb00
ImageColorAllocate(& maroon, 0xd1, 0x34 , 0x00 );
ImageColorAllocate(& schemepurple, 0xea, 0xc7 , 0xeb );
ImageColorAllocate(&schemewhite , 0xff, 0xff , 0xff );
ImageColorAllocate(&schemered ,0xFF, 0x00 , 0x00 );
ImageColorAllocate(&schemecyan ,0x00, 0xff , 0xf0 );
ImageColorAllocate(&schemeblue ,0x00, 0xce , 0xf3 );
ImageColorAllocate(&schemegreen ,0x39, 0xe6 , 0x00 );
ImageColorAllocate(&schemeyellow ,0xff, 0xb0 , 0x00 );// fffb00
ImageColorAllocate(&verylightgray, 0xf8,0xf8,0xf8);
ImageColorAllocate(& grays[0] , 0xff,0xff,0xff);
ImageColorAllocate(& grays[1] , 0xe0,0xe0,0xe0);
ImageColorAllocate(& grays[2] , 0xc0,0xc0,0xc0);
ImageColorAllocate(& grays[3] , 0xa0,0xa0,0xa0);
ImageColorAllocate(& grays[4] , 0x80,0x80,0x80);
ImageColorAllocate(& grays[5] , 0x60,0x60,0x60);
ImageColorAllocate(& grays[6] , 0x40,0x40,0x40);
ImageColorAllocate(& grays[7] , 0x20,0x20,0x20);
ImageColorAllocate(& grays[8] , 0x10,0x10,0x10);
ImageColorAllocate(& grays[9] , 0x00,0x00,0x00);
ImageColorAllocate(&grays2[0], 255,255,255);
ImageColorAllocate(&grays2[1], 245,245,245);
ImageColorAllocate(&grays2[2], 235,235,235);
ImageColorAllocate(&grays2[3], 225,225,225);
ImageColorAllocate(&grays2[4], 215,215,215);
ImageColorAllocate(&grays2[5], 205,205,205);
ImageColorAllocate(&grays2[6], 195,195,195);
ImageColorAllocate(&grays2[7], 185,185,185);
ImageColorAllocate(&grays2[8], 175,175,175);
ImageColorAllocate(&grays2[9], 165,165,165);
inited_colors_flag = 1;
}
return;
}
char refflat_d_fn[FILENAME_MAX];
char refflat_o_fn[FILENAME_MAX];
char snp_fn[FILENAME_MAX];
// --- SNP annotation track ...
struct snp_type // ****** input and output snp records as packed in this order !!!! >>>
{
unsigned int s; // start 4 bytes
unsigned int e; // end 4 bytes
unsigned char chrindex; // 1 char index - chromosome index
char mask; // info on sift , bit 1 = is sift , bit2 = nsfp origin
char rs[12]; // null terminated RS dbsnp string
};
/// add up the packed bytes in above struct ... get 22 bytes
#define SIZE_SNPTYPE 22
static int snpcmp(const void *aarg, const void *barg)
{
struct snp_type *a;
struct snp_type *b;
a = (struct snp_type *)aarg;
b = (struct snp_type *)barg;
if (a->chrindex < b->chrindex ) return -1;
else if (a->chrindex > b->chrindex ) return 1;
if (a->s <b->s ) return -1;
else if (a->s >b->s ) return 1;
return 0;
}
static FILE *fp_snp = (FILE *)0;
static long int last_snp_fseek_place;
static struct snp_type temp_snp_space;
char junque[512];
struct snp_type *binary_search_snp_file(struct snp_type *match_me, long int lo, long int hi)
{
long int seekto;
long int mid;
int k;
// char m[5120];
// sprintf(m," in binary_search_snp_file() - start - fp_snp is %p, lo=%ld , hi=%ld",fp_snp,lo,hi); jdebug(m);
if (fp_snp == (void *)0)
{
fprintf(stderr,"ERROR: in binary_search_snp_file() - fp_snp is NULL\n");
return((struct snp_type *)0);
}
// printf("dbg in binary_search_snp_file() chr=%s loc=%d last_snp_fseek_place=%ld lo=%ld hi=%ld\n",match_me->chr,match_me->s,last_snp_fseek_place,lo,hi);
if (lo > hi)
{
// fprintf(stderr,"ERROR: can't find [%s] cuz lo > hi %ld and %ld in binary_search_snp_file\n",match_me,lo,hi);
return (struct snp_type *)0;
}
if (lo==hi)
{
seekto = (SIZE_SNPTYPE * lo);
if (fseek(fp_snp,(size_t) seekto , SEEK_SET) < 0)
{
fprintf(stderr,"ERROR SEEK to %ld in binary_search_snp_file lo=%ld\n",seekto,lo);
return (struct snp_type *)0;
}
last_snp_fseek_place = seekto;
// -- must make portable, regardless of struct packing
fread(&temp_snp_space.s,sizeof(unsigned int ),1,fp_snp); // 4 bytes - start
fread(&temp_snp_space.e,sizeof(unsigned int ),1,fp_snp); // 4 bytes - end
fread(&temp_snp_space.chrindex,1,1,fp_snp); // 1 bytes - chrindex
fread(&temp_snp_space.mask,1,1,fp_snp); // 1 bytes - mask field
fread(&temp_snp_space.rs,12,1,fp_snp); // 12 bytes - rs with null termination
if (snpcmp(&temp_snp_space,match_me) == 0)
{
// fprintf(stderr,"GOT binary_search_snp_file lo=%ld hi=%ld %s\n",lo,hi,temp_snp_space); fflush(stderr);
return &temp_snp_space;
}
else
{
return (struct snp_type *)0;
}
}
mid = (lo + hi) / 2 ;
//printf("in search mid=%d \n",mid); fflush(stdout);
seekto = (SIZE_SNPTYPE * mid);
//printf("in search seekto=%ld [ which is mid=%d * 2] \n",seekto,mid); fflush(stdout);
if (fseek(fp_snp,(size_t) seekto , SEEK_SET) < 0)
{
fprintf(stderr,"SEEK to %ld ERROR in binary_search_snp_file, mid=%ld\n",seekto,mid);
exit(0);
}
last_snp_fseek_place = seekto;
fread( &temp_snp_space, 1 , SIZE_SNPTYPE , fp_snp );
k = snpcmp(&temp_snp_space,match_me);
if (k == 0) // unlikely !!!
{
//printf("in search found [%s]at %d (2)\n",match_me,mid); fflush(stdout);
return &temp_snp_space;
}
// if (strcmp(temp_snp_space.chr,match_me->chr) == 0)
if (temp_snp_space.chrindex == match_me->chrindex)
{
if ((match_me->s >= temp_snp_space.e) && (match_me->e < temp_snp_space.e))
return &temp_snp_space;
}
if (k > 0)
{
return binary_search_snp_file(match_me, lo, mid-1);
}
else
{
return binary_search_snp_file(match_me, mid+1, hi);
}
}
static long int snp_fixed_hi;
static void setup_snp(char *blds)
{
int error;
char m[1024];
if (fp_snp) return; // already opened
#ifdef WIN32
fp_snp = fopen(snp_fn,"rb");
#else
fp_snp = fopen(snp_fn,"r");
#endif
error = errno;
if (fp_snp == (FILE *)0)
{
sprintf(m,"ERROR: snp_fn=\"%s\" in setup_snp(), errno=%d",snp_fn,errno);
jdebug(m);
return;
}
fseek(fp_snp,(size_t) 0 , SEEK_END);
snp_fixed_hi = ftell(fp_snp)/SIZE_SNPTYPE;
sprintf(m,"END setup_snp filename=[%s], SIZE_SNPTYPE=%d fpd=%p, snp_fixed_hi=%ld",snp_fn,SIZE_SNPTYPE,fp_snp,snp_fixed_hi); jdebug(m);
return;
}
static void close_snp(void)
{
if (fp_snp) fclose(fp_snp);
fp_snp = (FILE *)0;
}
static char prevchr[16] = "";
static unsigned int prevsnpindex;
static unsigned char chr2index(char *s)
{
unsigned char ret = 0xff;
if (strcmp(s,prevchr) == 0) return prevsnpindex;
if (strcmp(s,"1") == 0) { ret = 0; }
else if (strcmp(s,"2") == 0) { ret = 1; }
else if (strcmp(s,"3") == 0) { ret = 2; }
else if (strcmp(s,"4") == 0) { ret = 3; }
else if (strcmp(s,"5") == 0) { ret = 4; }
else if (strcmp(s,"6") == 0) { ret = 5; }
else if (strcmp(s,"7") == 0) { ret = 6; }
else if (strcmp(s,"8") == 0) { ret = 7; }
else if (strcmp(s,"9") == 0) { ret = 8; }
else if (strcmp(s,"10") == 0) { ret = 9; }
else if (strcmp(s,"11") == 0) { ret = 10; }
else if (strcmp(s,"12") == 0) { ret = 11; }
else if (strcmp(s,"13") == 0) { ret = 12; }
else if (strcmp(s,"14") == 0) { ret = 13; }
else if (strcmp(s,"15") == 0) { ret = 14; }
else if (strcmp(s,"16") == 0) { ret = 15; }
else if (strcmp(s,"17") == 0) { ret = 16; }
else if (strcmp(s,"18") == 0) { ret = 17; }
else if (strcmp(s,"19") == 0) { ret = 18; }
else if (strcmp(s,"20") == 0) { ret = 19; }
else if (strcmp(s,"21") == 0) { ret = 20; }
else if (strcmp(s,"22") == 0) { ret = 21; }
else if (strcmp(s,"X") == 0) { ret = 22; }
else if (strcmp(s,"Y") == 0) { ret = 23; }
else if (strcmp(s,"M") == 0) { ret = 24; }
else if (strcmp(s,"chr1") == 0) { ret = 0; }
else if (strcmp(s,"chr2") == 0) { ret = 1; }
else if (strcmp(s,"chr3") == 0) { ret = 2; }
else if (strcmp(s,"chr4") == 0) { ret = 3; }
else if (strcmp(s,"chr5") == 0) { ret = 4; }
else if (strcmp(s,"chr6") == 0) { ret = 5; }
else if (strcmp(s,"chr7") == 0) { ret = 6; }
else if (strcmp(s,"chr8") == 0) { ret = 7; }
else if (strcmp(s,"chr9") == 0) { ret = 8; }
else if (strcmp(s,"chr10") == 0) { ret = 9; }
else if (strcmp(s,"chr11") == 0) { ret = 10; }
else if (strcmp(s,"chr12") == 0) { ret = 11; }
else if (strcmp(s,"chr13") == 0) { ret = 12; }
else if (strcmp(s,"chr14") == 0) { ret = 13; }
else if (strcmp(s,"chr15") == 0) { ret = 14; }
else if (strcmp(s,"chr16") == 0) { ret = 15; }
else if (strcmp(s,"chr17") == 0) { ret = 16; }
else if (strcmp(s,"chr18") == 0) { ret = 17; }
else if (strcmp(s,"chr19") == 0) { ret = 18; }
else if (strcmp(s,"chr20") == 0) { ret = 19; }
else if (strcmp(s,"chr21") == 0) { ret = 20; }
else if (strcmp(s,"chr22") == 0) { ret = 21; }
else if (strcmp(s,"chrX") == 0) { ret = 22; }
else if (strcmp(s,"chrY") == 0) { ret = 23; }
else if (strcmp(s,"chrM") == 0) { ret = 24; }
else
{
fprintf(stderr,"ERROR - invalid chromosome [%s]\n",s);
prevchr[0] = (char)0;
return 0xff;
}
strcpy(prevchr,s);
prevsnpindex = ret;
return ret;
}
static void index2chr(int idx, char *chrarg)
{
if (idx == 0) { strcpy(chrarg,"chr1"); return; }
if (idx == 1) { strcpy(chrarg,"chr2"); return; }
if (idx == 2) { strcpy(chrarg,"chr3"); return; }
if (idx == 3) { strcpy(chrarg,"chr4"); return; }
if (idx == 4) { strcpy(chrarg,"chr5"); return; }
if (idx == 5) { strcpy(chrarg,"chr6"); return; }
if (idx == 6) { strcpy(chrarg,"chr7"); return; }
if (idx == 7) { strcpy(chrarg,"chr8"); return; }
if (idx == 8) { strcpy(chrarg,"chr9"); return; }
if (idx == 9) { strcpy(chrarg,"chr10"); return; }
if (idx == 10) { strcpy(chrarg,"chr11"); return; }
if (idx == 11) { strcpy(chrarg,"chr12"); return; }
if (idx == 12) { strcpy(chrarg,"chr13"); return; }
if (idx == 13) { strcpy(chrarg,"chr14"); return; }
if (idx == 14) { strcpy(chrarg,"chr15"); return; }
if (idx == 15) { strcpy(chrarg,"chr16"); return; }
if (idx == 16) { strcpy(chrarg,"chr17"); return; }
if (idx == 17) { strcpy(chrarg,"chr18"); return; }
if (idx == 18) { strcpy(chrarg,"chr19"); return; }
if (idx == 19) { strcpy(chrarg,"chr20"); return; }
if (idx == 20) { strcpy(chrarg,"chr21"); return; }
if (idx == 21) { strcpy(chrarg,"chr22"); return; }
if (idx == 22) { strcpy(chrarg,"chrX"); return; }
if (idx == 23) { strcpy(chrarg,"chrY"); return; }
if (idx == 24) { strcpy(chrarg,"chrM"); return; }
strcpy(chrarg,"ERR");
return;
}
static void paint_snp_annot( char khrarg[] , unsigned int loc1 , unsigned int loc2 , char stuff[])
{
struct snp_type ss;
char tmps[512];
double d;
int x1,x2;
int y1,y2;
int len;
double local_pxwidth;
unsigned char uc;
int readcnt = 0;
int kickout = 0;
size_t stat;
long int spot;
struct snp_type f;
struct snp_type *z;
int yoffset = 1;
char chrlesschr[2048];
char m[2048];
stuff[0] = (char)0;
if (fp_snp == (FILE *)0)
{
sprintf(m,"ERROR - in paint_snp_annot() - snp file is not initialized"); jdebug(m);
return;
}
y1 = ih - 3 - yoffset;
y2 = ih - 7 - yoffset;
len = loc2 - loc1;
local_pxwidth = (double)iw /(double)(len);
sprintf(m,"in paint_snp_annot START: khrarg=%s loc1=%u loc2=%u, fp_snp = %p , len=%d",khrarg,loc1,loc2,fp_snp,len); jdebug(m);
strcpy(stuff,"unknown_snp");
memset(&f,0,sizeof(struct snp_type));
// --- strip "chr" off
if (strncmp(khrarg,"chr",3) == 0)
strcpy(chrlesschr,&khrarg[3]);
else
strcpy(chrlesschr,khrarg);
uc = chr2index(chrlesschr);
if (uc == 0xff) return;
f.chrindex = uc;
f.s = loc1;
f.e = loc2;
// sprintf(m," in paint_snp_annot, before binary_search_snp_file() - start - fp_snp is %p , lo=0, hi=",fp_snp,snp_fixed_hi); jdebug(m);
z = binary_search_snp_file(&f,0,snp_fixed_hi);
if (z)
{
// if ((strcmp(z->chr,khrarg) == 0) && ((loc1-1) == z->s))
if ((z->chrindex = uc) && (loc1 == z->s))
{
// return; NNNNNOOOOOOOO ... do NOT return. We just want to get near it
}
}
sprintf(m,"in paint_snp_annot(), after binary_search_snp_file() z=%p khrarg=%s loc=%d last_snp_fseek_place=%ld",z,khrarg,loc1,last_snp_fseek_place); jdebug(m);
spot = last_snp_fseek_place;
// rewind a little;
spot = spot - (100 * SIZE_SNPTYPE);
if (spot < 0) spot = 0;
fseek(fp_snp,spot,SEEK_SET);
// sprintf(m,"before while loc1=%u loc2=%u, spot=%ld, last_snp_fseek_place=%ld",loc1,loc2,spot,last_snp_fseek_place); jdebug(m);
kickout = readcnt = 0;
while (1)
{
// -- must make portable, regardless of struct packing
// sprintf(m,"before fread s, a=%p fp_snp=%p",&ss.s, fp_snp); jdebug(m);
// sprintf(m,"before fread e, a=%p fp_snp=%p", &ss.e, fp_snp); jdebug(m);
#if 0
// need some debug
stat = fread(&ss.s,sizeof(unsigned int ),1,fp_snp); // 4 bytes - start
error = errno;
if (stat != 1) { sprintf(m,"ERROR: cant read s errno=%d",error); jdebug(m); break; }
stat = fread(&ss.e,sizeof(unsigned int ),1,fp_snp); // 4 bytes - start
error = errno;
if (stat != 1) { sprintf(m,"ERROR: cant read e errno=%d %p,%ld,1,%p",error,&ss.e,sizeof(unsigned int),fp_snp); jdebug(m); break; }
stat = fread(&ss.chrindex,1,1,fp_snp); // 1 bytes - chrindex
error = errno;
if (stat != 1) { sprintf(m,"ERROR: cant read chrindex errno=%d",error); jdebug(m); break; }
stat = fread(&ss.mask,1,1,fp_snp); // 1 bytes - mask field
error = errno;
if (stat != 1) { sprintf(m,"ERROR: cant read mask errno=%d",error); jdebug(m); break; }
stat = fread(&ss.rs,12,1,fp_snp); // 12 bytes - rs with null termination
error = errno;
if (stat != 1) { sprintf(m,"ERROR: cant read rs errno=%d",error); jdebug(m); break; }
// sprintf(m,"after freads"); jdebug(m);
#else
// __________ WARNING ON WINDOWS !!!! file must be open binary or Bill Gates will overflow your buffer and *** make you program in Visual Basic ***
stat = fread(&ss.s,sizeof(unsigned int ),1,fp_snp); // 4 bytes - start
if (stat != 1) break;
stat = fread(&ss.e,sizeof(unsigned int ),1,fp_snp); // 4 bytes - start