-
Notifications
You must be signed in to change notification settings - Fork 6
/
ALNplot.c
1810 lines (1566 loc) · 54.7 KB
/
ALNplot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************************
* MIT License *
* *
* Copyright (c) 2024 Chenxi Zhou <chnx.zhou@gmail.com> *
* *
* 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. *
*********************************************************************************/
/*****************************************************************************************\
* *
* Genome Alignment Plotter *
* *
* Author: Chenxi Zhou (with an assist from Gene Myers) *
* Date : June 2024 *
* *
\*****************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <zlib.h>
#include "GDB.h"
#include "hash.h"
#include "select.h"
#include "alncode.h"
#undef DEBUG_MAKE_HASH
#undef DEBUG_READ_1ALN
#undef DEBUG_AXIS_CONF
#define MAX_XY_LEN 10000 // Max/Min plotting width/height
#define MIN_XY_LEN 100
#define MAX_LAB_LEN 20 // Max number characters for sequence names
#define MAX_LAB_FRC .2 // Max ratio of label to line plot panel
// Command line syntax and global parameter variables
static char *Usage[] =
{ "[-vSL] [-T<int(4)>] [-p[:<output:path>[.pdf]]]",
"[-l<int(100)>] [-i<float(.7)>] [-n<int(100000)>]",
"[-H<int(600)>] [-W<int>] [-f<int>] [-t<float>]",
"<alignment:path>[.1aln|.paf[.gz]]> [<selection>|<FILE> [<selection>|<FILE>]]",
};
static int VERBOSE; // -v
// static int HIGHLIGHT; // -h
// static int TRYADIAG; // -d
static int PRINTSID; // -S
static int LABELS; // ! -L
static int NTHREADS = 4; // -T
static char *OUTEPS = NULL; // -p
static int MINALEN = 100; // -a
static double MINAIDNT = 0.7; // -e
static int MAXALIGN = 100000; // -n
static int IMGHEIGH = 0; // -H
static int IMGWIDTH = 0; // -W
static int FONTSIZE = 0; // -f
static double LINESIZE = 0; // -t
// Array of segments to plot
typedef struct
{ uint8 flag;
int aread, bread;
int abpos, bbpos;
int aepos, bepos;
// int group; // segment group - e.g. chain
} Segment;
#define DEL_FLAG 0x1
#define COL_GRAY 0x2
#define COL_RED 0x4
#define COL_BLUE 0x8
#define IS_DEL(flag) ((flag)&DEL_FLAG)
#define SET_DEL(flag) ((flag)|=DEL_FLAG)
#define IS_COLOR(flag) ((flag)&0xE)
#define IS_RED(flag) ((flag)&COL_RED)
#define IS_BLUE(flag) ((flag)&COL_BLUE)
#define IS_GRAY(flag) ((flag)&COL_GRAY)
#define UNSET_COLOR(flag) ((flag)&=0xF1)
#define SET_RED(flag) (UNSET_COLOR(flag), (flag)|=COL_RED)
#define SET_BLUE(flag) (UNSET_COLOR(flag), (flag)|=COL_BLUE)
#define SET_GRAY(flag) (UNSET_COLOR(flag), (flag)|=COL_GRAY)
static Segment *segments = NULL;
static int64 nSegment = 0;
// Genome skeletons and auxiliary info
static int ISTWO; // If two GDBs are different
static GDB _AGDB, *AGDB = &_AGDB; // 1st genome skeleton with parts ...
static GDB _BGDB, *BGDB = &_BGDB; // 2nd genome skeleton with parts ...
static int NASCAFF; // Number scaffolds
static int NACONTIG; // Number contigs
static GDB_SCAFFOLD *ASCAFFS; // Scaffold record array
static GDB_CONTIG *ACONTIG; // Contig record array
static int NBSCAFF; // Number scaffolds
static int NBCONTIG; // Number contigs
static GDB_SCAFFOLD *BSCAFFS; // Scaffold record array
static GDB_CONTIG *BCONTIG; // Contig record array
static Hash_Table *AHASH; // Scaffold names hash table
static Hash_Table *BHASH; // Scaffold names hash table
static Contig_Range *ACHORD; // [0..NACONTIG) Portion of each contig to plot (or not)
static Contig_Range *BCHORD; // [0..NBCONTIG) Portion of each contig to plot (or not)
// Threading communication packet (read_1aln_block & aln_clip)
typedef struct
{ int64 beg;
int64 end;
OneFile *in;
Segment *segs;
int64 nseg;
GDB_CONTIG *bctg;
} Packet;
// Helvetica font character width
static double HELVETICA[128] =
{ 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000,
0.278, 0.278, 0.355, 0.556, 0.556, 0.889, 0.667, 0.222, 0.333, 0.333, 0.389, 0.584, 0.278, 0.333, 0.278, 0.278,
0.556, 0.556, 0.556, 0.556, 0.556, 0.556, 0.556, 0.556, 0.556, 0.556, 0.278, 0.278, 0.584, 0.584, 0.584, 0.556,
1.015, 0.667, 0.667, 0.722, 0.722, 0.667, 0.611, 0.778, 0.722, 0.278, 0.500, 0.667, 0.556, 0.833, 0.722, 0.778,
0.667, 0.778, 0.722, 0.667, 0.611, 0.722, 0.667, 0.944, 0.667, 0.667, 0.611, 0.278, 0.278, 0.278, 0.469, 0.556,
0.222, 0.556, 0.556, 0.500, 0.556, 0.556, 0.278, 0.556, 0.556, 0.222, 0.222, 0.500, 0.222, 0.833, 0.556, 0.556,
0.556, 0.556, 0.333, 0.500, 0.278, 0.556, 0.500, 0.722, 0.500, 0.500, 0.500, 0.334, 0.260, 0.334, 0.584, 0.000
};
/*******************************************************************************************
*
* Utilities
*
*******************************************************************************************/
static char *PDFTOOLS[4] = {"pstopdf", "epstopdf", "ps2pdf", "eps2pdf"};
int run_system_cmd(char *cmd, int retry)
{ int exit_code = system(cmd);
--retry;
if ((exit_code != -1 && !WEXITSTATUS(exit_code)) || !retry)
return (exit_code);
return (run_system_cmd(cmd, retry));
}
int check_executable(char *exe)
{ char cmd[4096];
int exit_code;
sprintf(cmd, "which %s 1>/dev/null 2>/dev/null", exe);
exit_code = run_system_cmd(cmd, 1);
if (exit_code == -1 || WEXITSTATUS(exit_code))
return (0);
return (1);
}
char *findPDFtool()
{ uint64 i;
for (i = 0; i < sizeof(PDFTOOLS) / sizeof(char *); i++)
if (check_executable(PDFTOOLS[i]))
return (PDFTOOLS[i]);
return (NULL);
}
static inline void expandBuffer(char **names, uint32 *n, uint32 *m, uint32 l)
{ if (*n + l > *m)
{ *m <<= 1;
*names = (char *)Realloc(*names,sizeof(char)*(*m),"Reallocating name array");
if (*names == NULL)
exit (1);
}
}
// Read next line into a buffer and return a pointer to the buffer
// the length of the line. NB: replaces '\n' with '\0'.
static char *read_line(void *input, int gzipd, int nline, char *spath)
{ static char *buffer;
static int bmax = 0;
int len;
if (bmax == 0)
{ bmax = 500;
buffer = (char *) malloc(bmax);
if (buffer == NULL)
{ fprintf(stderr,"%s: Out of memory reading %s\n",Prog_Name,spath);
exit (1);
}
}
if (gzipd)
{ if (gzgets(input,buffer,bmax) == NULL)
{ if (gzeof(input))
{ free(buffer);
bmax = 0;
return (NULL);
}
fprintf(stderr,"%s: Could not read next line, %d, of file %s\n",Prog_Name,nline,spath);
exit (1);
}
}
else
{ if (fgets(buffer,bmax,input) == NULL)
{ if (feof(input))
{ free(buffer);
bmax = 0;
return (NULL);
}
fprintf(stderr,"%s: Could not read next line, %d, of file %s\n",Prog_Name,nline,spath);
exit (1);
}
}
len = strlen(buffer);
while (buffer[len-1] != '\n')
{ bmax = ((int) (1.4*bmax)) + 100;
buffer = (char *) realloc(buffer,bmax);
if (buffer == NULL)
{ fprintf(stderr,"%s: Out of memory reading %s\n",Prog_Name,spath);
exit (1);
}
if (gzipd)
{ if (gzgets(input,buffer+len,bmax-len) == NULL)
{ if (gzeof(input))
fprintf(stderr,"%s: Last line %d of file %s does not end with new-line\n",
Prog_Name,nline,spath);
else
fprintf(stderr,"%s: Could not read next line, %d, of file %s\n",
Prog_Name,nline,spath);
exit (1);
}
}
else
{ if (fgets(buffer+len,bmax-len,input) == NULL)
{ if (feof(input))
fprintf(stderr,"%s: Last line %d of file %s does not end with new-line\n",
Prog_Name,nline,spath);
else
fprintf(stderr,"%s: Could not read next line, %d, of file %s\n",
Prog_Name,nline,spath);
exit (1);
}
}
len += strlen(buffer+len);
}
buffer[--len] = '\0';
return (buffer);
}
/*******************************************************************************************
*
* Read .1aln file creating array of segments and two GDB skeletons, AGDB & BGDB,
* and hash tables, AHASH & BHASH, of the scaffold names for each.
*
*******************************************************************************************/
void *read_1aln_block(void *args)
{ Packet *parm = (Packet *) args;
int64 beg = parm->beg;
int64 end = parm->end;
GDB_CONTIG *bctg = parm->bctg;
OneFile *in = parm->in;
Segment *segs = parm->segs;
int64 nseg = 0;
// int ngroup;
// For each alignment do
if (!oneGoto (parm->in, 'A', beg+1))
{ fprintf(stderr,"%s: Could not locate to object %lld in 1aln file\n",Prog_Name,beg+1);
exit (1);
}
oneReadLine(parm->in);
// Get the group id of the first alignment
// ngroup = -1; // oneGetGroup (parm->in, beg);
// if (ngroup < 0) ngroup = 0;
{ int64 i;
uint32 flags;
uint8 flag;
int aread, bread;
int abpos, bbpos;
int aepos, bepos;
// int group;
int diffs;
int blocksum, iid;
// Set group to be the first
// group = ngroup;
for (i = beg; i < end; i++)
{ // read i-th alignment record
if (in->lineType != 'A')
{ fprintf(stderr,"%s: Failed to be at start of alignment\n",Prog_Name);
exit (1);
}
flags = 0;
aread = oneInt(in,0);
abpos = oneInt(in,1);
aepos = oneInt(in,2);
bread = oneInt(in,3);
bbpos = oneInt(in,4);
bepos = oneInt(in,5);
diffs = 0;
while (oneReadLine(in))
if (in->lineType == 'R')
flags |= COMP_FLAG;
else if (in->lineType == 'D')
diffs = oneInt(in,0);
else if (in->lineType == 'A')
break; // stop at next A-line
else
continue; // skip trace lines
if (aepos - abpos < MINALEN || bepos - bbpos < MINALEN)
continue; // filter by segment size
blocksum = (aepos-abpos) + (bepos-bbpos);
iid = (blocksum - diffs) / 2;
if (2.*iid / blocksum < MINAIDNT)
continue; // filter by segment identity
flag = 0;
if (COMP(flags))
{ bbpos = bctg[bread].clen - bbpos;
bepos = bctg[bread].clen - bepos;
}
// add to output
segs->flag = flag;
segs->aread = aread;
segs->abpos = abpos;
segs->aepos = aepos;
segs->bread = bread;
segs->bbpos = bbpos;
segs->bepos = bepos;
// segs->group = group;
segs += 1;
nseg += 1;
// change group after this alignment
// group = ngroup;
}
}
parm->nseg = nseg;
return (NULL);
}
void read_1aln(char *oneAlnFile)
{ OneFile *input;
int64 novl;
// Initiate .1aln file reading and read header information
{ char *cpath, *tmp;
FILE *test;
char *src1_name, *src2_name;
char *spath, *tpath;
char *sptr, *eptr;
char *head;
int s, type, TSPACE;
input = open_Aln_Read(oneAlnFile,NTHREADS,&novl,&TSPACE,&src1_name,&src2_name,&cpath);
if (input == NULL)
{ fprintf(stderr,"%s: Could not open .1aln file: %s\n",Prog_Name,oneAlnFile);
exit (1);
}
test = fopen(src1_name,"r");
if (test == NULL)
{ if (*src1_name != '/')
test = fopen(Catenate(cpath,"/",src1_name,""),"r");
if (test == NULL)
{ fprintf(stderr,"%s: Could not find GDB %s\n",Prog_Name,src1_name);
exit (1);
}
tmp = Strdup(Catenate(cpath,"/",src1_name,""),"Allocating expanded name");
free(src1_name);
src1_name = tmp;
}
fclose(test);
if (src2_name != NULL)
{ test = fopen(src2_name,"r");
if (test == NULL)
{ if (*src2_name != '/')
test = fopen(Catenate(cpath,"/",src2_name,""),"r");
if (test == NULL)
{ fprintf(stderr,"%s: Could not find GDB %s\n",Prog_Name,src2_name);
exit (1);
}
tmp = Strdup(Catenate(cpath,"/",src2_name,""),"Allocating expanded name");
free(src2_name);
src2_name = tmp;
}
fclose(test);
}
free(cpath);
ISTWO = 0;
type = Get_GDB_Paths(src1_name,NULL,&spath,&tpath,0);
if (type != IS_GDB)
Create_GDB(AGDB,spath,type,0,NULL);
else
Read_GDB(AGDB,tpath);
free(spath);
free(tpath);
if (src2_name != NULL)
{ type = Get_GDB_Paths(src2_name,NULL,&spath,&tpath,0);
if (type != IS_GDB)
Create_GDB(BGDB,spath,type,0,NULL);
else
Read_GDB(BGDB,tpath);
free(spath);
free(tpath);
ISTWO = 1;
}
else
BGDB = AGDB;
free(src1_name);
free(src2_name);
NASCAFF = AGDB->nscaff;
NACONTIG = AGDB->ncontig;
ASCAFFS = AGDB->scaffolds;
ACONTIG = AGDB->contigs;
NBSCAFF = BGDB->nscaff;
NBCONTIG = BGDB->ncontig;
BSCAFFS = BGDB->scaffolds;
BCONTIG = BGDB->contigs;
AHASH = New_Hash_Table(NASCAFF,0);
head = AGDB->headers;
for (s = 0; s < NASCAFF; s++)
{ sptr = head + ASCAFFS[s].hoff;
for (eptr = sptr; *eptr != '\0'; eptr++)
if (isspace(*eptr))
break;
*eptr = '\0';
if (Hash_Lookup(AHASH,sptr) < 0)
Hash_Add(AHASH,sptr);
else
{ fprintf(stderr,"%s: Duplicate scaffold name: %s\n",Prog_Name,sptr);
exit (1);
}
}
if (ISTWO)
{ BHASH = New_Hash_Table(NBSCAFF,0);
head = BGDB->headers;
for (s = 0; s < NBSCAFF; s++)
{ sptr = head + BSCAFFS[s].hoff;
for (eptr = sptr; *eptr != '\0'; eptr++)
if (isspace(*eptr))
break;
*eptr = '\0';
if (Hash_Lookup(BHASH,sptr) < 0)
Hash_Add(BHASH,sptr);
else
{ fprintf(stderr,"%s: Duplicate scaffold name: %s\n",Prog_Name,sptr);
exit (1);
}
}
}
else
BHASH = AHASH;
}
#ifdef DEBUG_MAKE_HASH
{ int i;
fprintf(stderr,"%s: DB_A\n",Prog_Name);
fprintf(stderr,"%s: List of contigs (NACTG=%d)\n",Prog_Name,NACONTIG);
fprintf(stderr,"%s: INDEX SCAF OFFSET LENGTH\n",Prog_Name);
for (i = 0; i < NACONTIG; i++)
fprintf(stderr,"%s: %6d %6d %10lld %10lld\n",Prog_Name,i,
ACONTIG[i].scaf,ACONTIG[i].sbeg,ACONTIG[i].clen);
fprintf(stderr,"%s: List of scaffolds (NASCAFF=%d)\n",Prog_Name,NASCAFF);
for (i = 0; i < NASCAFF; i++)
fprintf(stderr,"%s: %6d %s\n",Prog_Name,i,Get_Hash_String(AHASH,i));
if (ISTWO)
{ fprintf(stderr,"%s: DB_B\n",Prog_Name);
fprintf(stderr,"%s: List of contigs (NBCTG=%d)\n",Prog_Name,NBCONTIG);
fprintf(stderr,"%s: INDEX SCAF OFFSET LENGTH\n",Prog_Name);
for (i = 0; i < NBCONTIG; i++)
fprintf(stderr,"%s: %6d %6d %10lld %10lld\n",Prog_Name,i,
BCONTIG[i].scaf,BCONTIG[i].sbeg,BCONTIG[i].clen);
fprintf(stderr,"%s: List of scaffolds (NBSCAFF=%d)\n",Prog_Name,NBSCAFF);
for (i = 0; i < NBSCAFF; i++)
fprintf(stderr,"%s: %6d %s\n",Prog_Name,i,Get_Hash_String(BHASH,i));
}
}
#endif
// Read alignment segments
{ int p;
Packet parm[NTHREADS];
pthread_t threads[NTHREADS];
// Divide .1aln into NTHREADS parts
segments = (Segment *) Malloc(sizeof(Segment)*novl, "Allocating segment array");
for (p = 0; p < NTHREADS ; p++)
{ parm[p].beg = (p * novl) / NTHREADS;
if (p > 0)
parm[p-1].end = parm[p].beg;
parm[p].segs = segments + parm[p].beg;
parm[p].nseg = 0;
parm[p].in = input + p;
parm[p].bctg = BGDB->contigs;
}
parm[NTHREADS-1].end = novl;
// Use NTHREADS to produce alignment segments for each part
for (p = 1; p < NTHREADS; p++)
pthread_create(threads+p,NULL,read_1aln_block,parm+p);
read_1aln_block(parm);
for (p = 1; p < NTHREADS; p++)
pthread_join(threads[p],NULL);
// Collect results from different part
nSegment = parm[0].nseg;
for (p = 1; p < NTHREADS; p++)
{ if (nSegment < parm[p].beg)
memmove(segments+nSegment,parm[p].segs,sizeof(Segment)*parm[p].nseg);
nSegment += parm[p].nseg;
}
if (nSegment < novl)
segments = Realloc(segments,sizeof(Segment)*nSegment,"Compacting segment arrary");
#ifdef DEBUG_READ_1ALN
fprintf(stderr, "%s: %9lld segments loaded\n",Prog_Name,novl);
fprintf(stderr, "%s: %9lld after filtering\n",Prog_Name,nSegment);
#endif
}
oneFileClose(input);
}
/*******************************************************************************************
*
* Read .paf file creating array of segments and two GDB skeletons, AGDB & BGDB
* and hash tables, AHASH & BHASH, of the scaffold names for each.
*
*******************************************************************************************/
void read_paf(char *pafAlnFile, int gzipd)
{ void *input;
int nline;
int i, naseq, maseq, nbseq, mbseq;
int index;
uint64 nsegs, msegs;
Segment *segs;
char *fptrs[11], *fptr, *eptr;
char *head;
int alen, blen, *alens, *blens;;
int aread, bread;
int abpos, bbpos;
int aepos, bepos;
// int group;
int blocksum, iid;
uint8 flag;
if (gzipd)
input = gzopen(pafAlnFile,"r");
else
input = fopen(pafAlnFile,"r");
AHASH = New_Hash_Table(1024,1);
BHASH = New_Hash_Table(1024,1);
naseq = 0;
maseq = 1024;
nbseq = 0;
mbseq = 1024;
nsegs = 0;
msegs = 4096;
alens = (int *) Malloc(sizeof(int) * maseq, "Allocating length map");
blens = (int *) Malloc(sizeof(int) * mbseq, "Allocating length map");
segments = (Segment *) Malloc(sizeof(Segment) * msegs, "Allocating segment array");
segs = segments;
nline = 1;
while ((eptr = read_line(input,gzipd,nline++,pafAlnFile)) != NULL)
{
fptr = eptr; // parse PAF line
for (i = 0; i < 11; i++)
{ while (*eptr != '\t' && *eptr != '\n' && *eptr != '\0')
eptr++;
if (eptr > fptr)
fptrs[i] = fptr;
if (*eptr == '\t')
{ *eptr = '\0';
fptr = ++eptr;
}
else
{ *eptr = '\0';
*fptr = '\0';
break;
}
}
if (i != 11)
continue;
index = Hash_Lookup(AHASH, fptrs[0]);
alen = strtol(fptrs[1], &eptr, 10);
if (index < 0)
{ alens[naseq++] = alen;
if (naseq == maseq)
{ maseq <<= 1;
alens = (int *) Realloc(alens, sizeof(int) * maseq, "Reallocating length map");
}
index = Hash_Add(AHASH, fptrs[0]);
}
aread = index;
abpos = strtol(fptrs[2], &eptr, 10);
aepos = strtol(fptrs[3], &eptr, 10);
index = Hash_Lookup(BHASH, fptrs[5]);
blen = strtol(fptrs[6], &eptr, 10);
if (index < 0)
{ blens[nbseq++] = blen;
if (nbseq == mbseq)
{ mbseq <<= 1;
blens = (int *) Realloc(blens, sizeof(int) * mbseq, "Reallocating length map");
}
index = Hash_Add(BHASH, fptrs[5]);
}
bread = index;
bbpos = strtol(fptrs[7], &eptr, 10);
bepos = strtol(fptrs[8], &eptr, 10);
if (aepos - abpos < MINALEN || bepos - bbpos < MINALEN) // filter by segment size
continue;
blocksum = (aepos-abpos) + (bepos-bbpos);
iid = strtol(fptrs[9], &eptr, 10);
if (2.*iid / blocksum < MINAIDNT) // filter by segment identity
continue;
/***
group = 0; // parse 'cn:i' tag to find segment group if exists
while (fptr)
{ eptr = fptr;
while (*eptr != '\t' && *eptr != '\n' && *eptr != '\0')
eptr++;
if (eptr - fptr > 5 && !strncmp(fptr, "cn:i:", 5))
{ group = strtol(fptr + 5, 0, 10);
break;
}
if (*eptr != '\t')
break;
fptr = ++eptr;
}
**/
flag = 0; // Add to segment array
if (*fptrs[4] == '-')
{ int tmp = bbpos;
bbpos = bepos;
bepos = tmp;
}
segs->flag = flag;
segs->aread = aread;
segs->bread = bread;
segs->abpos = abpos;
segs->aepos = aepos;
segs->bbpos = bbpos;
segs->bepos = bepos;
// segs->group = group;
nsegs += 1;
if (nsegs == msegs)
{ msegs <<= 1;
segments = (Segment *) Realloc(segments, sizeof(Segment) * msegs,
"Reallocating segment array");
}
segs = segments + nsegs;
}
if (gzipd)
gzclose(input);
else
fclose(input);
ISTWO = 1;
nSegment = nsegs;
// Contigs = Scaffolds so the underlying GDB is ...
AGDB->nscaff = AGDB->ncontig = NASCAFF = NACONTIG = naseq;
BGDB->nscaff = BGDB->ncontig = NBSCAFF = NBCONTIG = nbseq;
AGDB->scaffolds = ASCAFFS = Malloc(sizeof(GDB_SCAFFOLD)*(NASCAFF+NBSCAFF),"Allocating scaffolds");
AGDB->contigs = ACONTIG = Malloc(sizeof(GDB_CONTIG)*(NACONTIG+NBCONTIG),"Allocating contigs");
AGDB->headers = Get_Hash_String(AHASH,0);
BGDB->scaffolds = BSCAFFS = ASCAFFS + NASCAFF;
BGDB->contigs = BCONTIG = ACONTIG + NACONTIG;
BGDB->headers = Get_Hash_String(BHASH,0);
head = AGDB->headers;
for (i = 0; i < NASCAFF; i++)
{ ASCAFFS[i].slen = alens[i];
ASCAFFS[i].fctg = i;
ASCAFFS[i].ectg = i+1;
ASCAFFS[i].hoff = Get_Hash_String(AHASH,i) - head;
}
for (i = 0; i < NACONTIG; i++)
{ ACONTIG[i].clen = alens[i];
ACONTIG[i].sbeg = 0;
ACONTIG[i].boff = 0;
ACONTIG[i].scaf = i;
}
head = BGDB->headers;
for (i = 0; i < NBSCAFF; i++)
{ BSCAFFS[i].slen = blens[i];
BSCAFFS[i].fctg = i;
BSCAFFS[i].ectg = i+1;
BSCAFFS[i].hoff = Get_Hash_String(BHASH,i) - head;
}
for (i = 0; i < NBCONTIG; i++)
{ BCONTIG[i].clen = blens[i];
BCONTIG[i].sbeg = 0;
BCONTIG[i].boff = 0;
BCONTIG[i].scaf = i;
}
#ifdef DEBUG_MAKE_HASH
{ int i;
fprintf(stderr,"%s: DB_A\n",Prog_Name);
fprintf(stderr,"%s: List of contigs (NACTG=%d)\n",Prog_Name,NACONTIG);
fprintf(stderr,"%s: INDEX SCAF OFFSET LENGTH\n",Prog_Name);
for (i = 0; i < NACONTIG; i++)
fprintf(stderr,"%s: %6d %6d %10lld %10lld\n",Prog_Name,i,
ACONTIG[i].scaf,ACONTIG[i].sbeg,ACONTIG[i].clen);
fprintf(stderr,"%s: List of scaffolds (NASCAFF=%d)\n",Prog_Name,NASCAFF);
for (i = 0; i < NASCAFF; i++)
fprintf(stderr,"%s: %6d %s\n",Prog_Name,i,Get_Hash_String(AHASH,i));
if (ISTWO)
{ fprintf(stderr,"%s: DB_B\n",Prog_Name);
fprintf(stderr,"%s: List of contigs (NBCTG=%d)\n",Prog_Name,NBCONTIG);
fprintf(stderr,"%s: INDEX SCAF OFFSET LENGTH\n",Prog_Name);
for (i = 0; i < NBCONTIG; i++)
fprintf(stderr,"%s: %6d %6d %10lld %10lld\n",Prog_Name,i,
BCONTIG[i].scaf,BCONTIG[i].sbeg,BCONTIG[i].clen);
fprintf(stderr,"%s: List of scaffolds (NBSCAFF=%d)\n",Prog_Name,NBSCAFF);
for (i = 0; i < NBSCAFF; i++)
fprintf(stderr,"%s: %6d %s\n",Prog_Name,i,Get_Hash_String(BHASH,i));
}
}
#endif
free(alens);
free(blens);
}
/*******************************************************************************************
*
* Chenxi's plotting code
*
*******************************************************************************************/
static int USORT(const void *l, const void *r)
{ uint64 x = *((uint64 *) l);
uint64 y = *((uint64 *) r);
if (x > y)
return (1);
return (-1);
}
/* Currently not needed
static void axisReverse(uint64 *sarray, int64 *caxis, int64 soff, int beg, int end,
int *cbeg, int *cend)
{ int i, c, clen;
int64 coff;
coff = caxis[(uint32) sarray[beg]];
for (i = beg; i < end; i++)
{ c = (uint32) sarray[i];
soff -= caxis[c] - coff; // gap
clen = cend[c] - cbeg[c];
soff -= clen; // ctg
coff = caxis[c] + clen;
caxis[c] = soff;
}
}
*/
// Now string length of a name is capped by MAX_LAB_LEN
// If the length N exceeds the limit then
// substring name[MAX_LAB_LEN-2,N-2] is replaced by a "*"
static void addSeqName(char *names, uint32 n, uint32 m, int c0, int c1,
uint32 s, Hash_Table *hash, GDB *gdb, Contig_Range *chord, int rank,
char **_names, uint32 *_n, uint32 *_m)
{ uint32 l, n0;
int64 p;
char *name;
n0 = n;
if (PRINTSID)
{ l = Number_Digits(s+1);
expandBuffer(&names, &n, &m, l+1);
sprintf(names+n,"%u",s+1);
n += l;
}
else
{ name = Get_Hash_String(hash,s);
l = strlen(name);
expandBuffer(&names, &n, &m, l+1);
sprintf(names+n,"%s",name);
n += l;
}
if (chord[c0].beg > 0 ||
gdb->scaffolds[s].fctg != c0 || // start from first
gdb->scaffolds[s].ectg != c1+1 ||
chord[c1].end != gdb->contigs[c1].clen) // end at last
{ // partial scaffold
p = gdb->contigs[c0].sbeg + chord[c0].beg;
p += 1; // make it 1-based
l = Number_Digits(p);
l += 1;
expandBuffer(&names, &n, &m, l+1);
sprintf(names+n,"_%lld",p);
n += l;
p = gdb->contigs[c1].sbeg + chord[c1].end;
l = Number_Digits(p);
l += 1;
expandBuffer(&names, &n, &m, l+1);
sprintf(names+n,"-%lld",p);
n += l;
}
if (rank < 0)
{ // add a prime
expandBuffer(&names, &n, &m, 2);
names[n++] = '\'';
names[n] = '\0';
}
if (n > n0 + MAX_LAB_LEN) {
// string length exceed limits
l = n - n0;
if (rank < 0) {
names[n0+MAX_LAB_LEN-3] = '*';
names[n0+MAX_LAB_LEN-2] = names[n-2]; // the last character
names[n0+MAX_LAB_LEN-1] = names[n-1]; // the prime
names[n0+MAX_LAB_LEN] = '\0';
} else {
names[n0+MAX_LAB_LEN-2] = '*';
names[n0+MAX_LAB_LEN-1] = names[n-1]; // the last character
names[n0+MAX_LAB_LEN] = '\0';
}
n = n0 + MAX_LAB_LEN;
}
n++; // the null terminator
// assign_vars:
*_names = names;
*_n = n;
*_m = m;
}
static double seqNameWidth(char *names, int n)
{ int i, c;
double l, w;
w = 0;
for (i = 0; i < n; i++)
{ l = 0;
while (*names != '\0')
{ c = (uint8) (*names);
if (c < 33 || c > 126)
{ fprintf(stderr,"%s: unsupported character '%c'\n",Prog_Name,*names);
exit (1);
}
l += HELVETICA[c];
names++;
}
if (l > w) w = l;
names++;
}
return (w);
}
static double seqNameRenderWidth(char *names, int n, int64 *soff, double unit, double space)
{ int i, c;
double l, w, s;
w = 0;
for (i = 0; i < n; i++)
{ s = (i == 0 ? soff[0] : soff[i] - soff[i-1]);
if (s * unit < space)
{ names += strlen(names) + 1;
continue;
}
l = 0;
while (*names != '\0')
{ c = (uint8) (*names);
if (c < 33 || c > 126)
{ fprintf(stderr,"%s: unsupported character '%c'\n",Prog_Name,*names);
exit (1);
}
l += HELVETICA[c];
names++;
}
if (l > w) w = l;
names++;
}
return (w);
}
static double chooseFontSizeByHeight(int64 *soff, int n, double unit, double minf, double maxf)
{ int i;
double s, f;
f = maxf;
for (i = 0; i < n; i++)
{ s = (i == 0 ? soff[0] : soff[i] - soff[i-1]);
s *= unit;
if (s >= minf && s < f) f = s;
}
return (f);
}
int axisConfig(Hash_Table *hash, GDB *gdb, Contig_Range *chord,
int64 **_caxis, int64 **_saxis, char **_names, int64 *_tseq)
{ int i, j, s, r1, r2, mseq, nseq;
uint32 c0, c1, c2, nstr, mstr;
uint64 *sarray;
int64 *caxis, *saxis, tseq;
char *names;
mseq = 0;
for (i = 0; i < gdb->ncontig; i++)
if (chord[i].order >= 0)
mseq++;
sarray = (uint64 *) Malloc(sizeof(uint64)*mseq,"Allocating seq array");
saxis = (int64 *) Malloc(sizeof(int64)*mseq,"Allocating offset array");
caxis = (int64 *) Malloc(sizeof(int64)*gdb->ncontig,"Allocating offset array");
if (LABELS)
{ mstr = 2048;
names = (char *) Malloc(sizeof(char)*mstr,"Allocating name array");