-
Notifications
You must be signed in to change notification settings - Fork 2
/
fragScaff.pl
2040 lines (1842 loc) · 70.8 KB
/
fragScaff.pl
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
#!/usr/bin/perl
use strict;
use Getopt::Std; my%opt = ();
getopts("B:s:O:E:G:q:C:p:m:P:F:M:l:c:n:t:L:Q:vS:T:fe:a:b:d:D:k:K:Rx:X:AHr:Izj:u:U:J:N:o:V:wg:", \%opt);
my$die1 = "
================================================================= fragScaff =================================================================
Version 140324.1 (180112 10X support update - beta -G X option)
Andrew Adey (adey\@ohsu.edu)
Attempts to scaffold contigs using fragment pool data. Performs best using CPT-Seq (Contiguity Preserving Transposase) libraries, but will
work using fosmid (decent) or LFR (Long Fragment Read) (poor) fragment data, though these methods are far less tested in fragScaff. It is
required that the input bam has the proper \@SQ lines, as well as \@RG lines and are aligned to the reference assembly provided as the -F
option. The bam (-B) must be reads aligned to the input assembled fasta (-F).
fragScaff.pl -B <BAM/bamParse> -F <Input fasta> <Other options>
NOTE: It is recommended to run as 'fragScaff.pl -B <BAM> -b 1 -E <node size>' to first generate the bamParse file. This file can then
be used for all subsequent stages to eliminate the need to re-parse the bam. Similarly it is recommended to run the node-node
calculations once to generate a '.links.txt' file which can then be used as option '-K' for all subsequent runs thus eliminating the
need to rerun the time-consuming node-node calculations.
UPDATE: Date: Jan. 12, 2018. Updated to allow 10X genomics bam files by detecting the CR:Z field and building the RG info in pre-processing.
Note: This has not been fully tested.
RECOMMENDED (1) 'fragScaff.pl -B <myBAM> -b 1 -E <node_size> (optional: -J <repeats.bed> -N <Nbases.bed> -o <max_node_size>)'
RUNS: (2) 'fragScaff.pl -B <myBAM.E#.bamParse> -A -O <myOUT> -t <myTHREAD#/Q>'
(3) 'fragScaff.pl -B <myBAM.E#.bamParse> -K <myOUT.fragScaff.links.txt> -F <myIN.fa> -O <myOUT2> <other options>'
This will allow you to run all pre-processing in steps (1) and (2) and then run the last stage (3) multiple times to compare
outputs and find the optimum setting for your input. Though defaults should give an optimum output.
-H Display detailed options description.
";
my$die2 = "
================================================================= fragScaff =================================================================
Version 140324.1 (180112 10X support update - beta -G X option)
Andrew Adey (adey\@ohsu.edu)
Attempts to scaffold contigs using fragment pool data. Performs best using CPT-Seq (Contiguity Preserving Transposase) libraries, but will
work using fosmid (decent) or LFR (Long Fragment Read) (poor) fragment data, though these methods are far less tested in fragScaff. It is
required that the input bam has the proper \@SQ lines, as well as \@RG lines and are aligned to the reference assembly provided as the -F
option. The bam (-B) must be reads aligned to the input assembled fasta (-F).
fragScaff.pl -B <BAM/bamParse> -F <Input fasta> <Other options>
NOTE: It is recommended to run as 'fragScaff.pl -B <BAM> -b 1 -E <node size>' to first generate the bamParse file. This file can then
be used for all subsequent stages to eliminate the need to re-parse the bam. Similarly it is recommended to run the node-node
calculations once to generate a '.links.txt' file which can then be used as option '-K' for all subsequent runs thus eliminating the
need to rerun the time-consuming node-node calculations.
UPDATE: Date: Jan. 12, 2018. Updated to allow 10X genomics bam files by detecting the CR:Z field and building the RG info in pre-processing.
Note: This has not been fully tested.
RECOMMENDED (1) 'fragScaff.pl -B <myBAM> -b 1 -E <node_size> (optional: -J <repeats.bed> -N <Nbases.bed> -o <max_node_size>)'
RUNS: (2) 'fragScaff.pl -B <myBAM.E#.bamParse> -A -O <myOUT> -t <myTHREAD#/Q>'
(3) 'fragScaff.pl -B <myBAM.E#.bamParse> -K <myOUT.fragScaff.links.txt> -F <myIN.fa> -O <myOUT2> <other options>'
This will allow you to run all pre-processing in steps (1) and (2) and then run the last stage (3) multiple times to compare
outputs and find the optimum setting for your input. Though defaults should give an optimum output.
Also note that step (3) can be run multiple times without the -F option and with -I so that it will only perform edge filtering
and graph manipulations which require the most optimization. Once the final otimized parameters are decided based on the stats
printed in the log file, it can then be run with -F to specify the input assembly fasta and without -I to produce the final
output assembly.
-H Display the following detailed options description.
---------------------- INPUT OPTIONS --------------------------------------------------------------------------------------------------------
-B [FILE] bam file (must have SQ (w/LN) & RG tags) (REQ) -O [STR] output prefix (def = -B)
(can be replaced with bamParse file) -F [FILE] fasta file from assembler output
-P [T/F/L] platform: -G [R/N/H] read group identifier:
T = CPT-Seq / 10X linked read (def) N = read name (group:read_number) (def)
F = Fosmid Pool R = RG:Z:group bam tag
L = Long Fragment Read H = after hash (name#group)
-K [FILE] previous link file. X = CR:Z:group bam tag (10X bam file - untested)
-N [FILE] Nbase bed file for input scaffolds -J [FILE] Repeatmasker bed file to exclude reads in windows
---------------------- PRUNING OPTIONS ------------------------------------------------------------------------------------------------------
-q [INT] min mapping qual (def = 10) -m [INT] min contig size to include (def = 1)
-d [FLOAT] min fraction group hit cutoff (def = 0.05) -l [INT] max number of links to use (def = 5)
-D [FLOAT] max fraction group hit cutoff (def = 0.95) -a [INT] max number of links to allow (def = 20)
-U [INT] min num group hits per node (def = 0) (will remove nodes with more)
-p [INT/A] -log10 score use minimum (def = A) -j [FLOAT] if -p A: mean links per p-bin (def = 1.25)
(set to A for auto determine) -c [INT/D] size of brach to check for split (def = D)
-r [INT] -log10 score report minimum (def = 1) (set to D to disable, experimental)
-M [INT] max score (def = 200) -u [FLOAT] combined reciprocation score multiplier (def = 2)
-g [INT/X] max size to make join (ie. one scaff must be
less than this, def = X (null))
---------------------- PLATFORM-DEPENDENT OPTIONS -------------------------------------------------------------------------------------------
-E [INT] contig end node size (T/L=5000, F=1000) -C [INT] read count threshold (T=1, F/L=20)
(Important for bamParse creation) -o [INT] max E (if dynamic) (T=10000, F/L=5000)
---------------------- OUTPUT OPTIONS -------------------------------------------------------------------------------------------------------
-n [n/N] case of N to add (def = N) -b [0/1] generate bamParse file to remove need to
-L [INT] wrap length in output fasta (def = 100) parse bam each run (...bam.bamParse)
-v print/keep intermediate files (def = no) 0 = make bamParse & continue
-f print assembly qualities in fasta file 1 = make bamParse then exit
(def = separate file) (can be run with just -b 1 -B <bam>)
-R print out link fraction overlaps (def = no) -z gzip bamParse (gzip must be command line callable)
-x [INT] min N spacer size (rec = max MP size, def = 3000) -X [INT] max N spacer size (def = 8000)
-V [INT/A] print [INT] graph files (def = 0, A for all) -w exit after printing cytoscape graph (def = no, req -V)
---------------------- PERFORMANCE OPTIONS --------------------------------------------------------------------------------------------------
-t [INT/Q] threads (def = 1) -S [INT] if -t Q/>1: number of nodes per job (def = 100)
runs with 1 during bam parsing then -T [INT] if -t Q: number of qsub jobs at a time (def = 100)
expands to multiple. The mutithread set to 'N' for no limit
portion may require a large amount of -e [STR] if -t Q: memory per job (def = 2G)
memory. Prepare for 2G each. -Q [STR] for qsub - do not manually toggle
assign to Q to qsub jobs. (best) -A exit after node all-by-all claculations
-I exit after graph manipulation
---------------------- DEPENDENCY OPTIONS ---------------------------------------------------------------------------------------------------
-k [STR] fragScaff call (def = fragScaff.pl) -s [STR] samtools call (def = samtools)
(only necessary if -t >1 or Q)
";
if (defined $opt{'H'}) {
die $die2;
}
$_ = 0 for my($pi,$gauss_x,$gauss_m,$gauss_sd,$gauss_p,$i,$ts);
$pi = 3.1415926535;
# QUAL SCORE PRUNING OPTIONS #
if (!defined $opt{'p'}) {$opt{'p'} = "A"}
if (!defined $opt{'j'}) {$opt{'j'} = 1.25}
if (!defined $opt{'r'}) {$opt{'r'} = 1}
my$max_qual = 0;
if (!defined $opt{'M'}) {$opt{'M'} = 200; $max_qual = 1E-200} else {$max_qual = (10**(-1*$opt{'M'}))}
# BEGIN QSUB CHECK #
if (defined $opt{'Q'}) {
run_qsub();
exit;
}
## END QSUB CHECK ##
# BEGIN OPTION PARSING AND INITIALIZATION #
# INPUT OPTIONS #
if (!defined $opt{'B'}) {die "\n\tPROVIDE A BAM OR BAMPARSE FILE (OPTION -B)\n$die1"}
if (!defined $opt{'F'}&&(!defined $opt{'b'} || $opt{'b'} < 0.5)&&!defined $opt{'I'}&&!defined $opt{'A'}&&(!defined $opt{'w'}&&defined $opt{'V'})) {die "\n\tPROVIDE A FASTA FILE FOR THE INPUT ASSEMBLY (OPTION -F)\n\tTHIS IS REQUIRED WHEN RUNNING W/O -b 1, -A or -I\n$die1"}
if (!defined $opt{'O'}) {$opt{'O'} = $opt{'B'}}
if (!defined $opt{'P'}) {$opt{'P'} = "T"}
if (!defined $opt{'q'}) {$opt{'q'} = 10}
if (!defined $opt{'G'}) {$opt{'G'} = "N"} elsif ($opt{'G'} !~ /[RNHX]/) {
die "
Can not determine group option (-G), please select from:
N = read name (group:read_number) (def)
R = RG:Z:group bam tag
H = after hash (name#group)
X = CR:Z:group bam tag (10X bam file)
"}
if ($opt{'P'} =~ /T/i) {
if (!defined $opt{'E'}) {$opt{'E'} = 5000}
if (!defined $opt{'o'}) {$opt{'o'} = 10000}
if (!defined $opt{'C'}) {$opt{'C'} = 1}
} elsif ($opt{'P'} =~ /L/i) {
if (!defined $opt{'E'}) {$opt{'E'} = 1000}
if (!defined $opt{'o'}) {$opt{'o'} = 5000}
if (!defined $opt{'C'}) {$opt{'C'} = 20}
} elsif ($opt{'P'} =~ /F/i) {
if (!defined $opt{'E'}) {$opt{'E'} = 1000}
if (!defined $opt{'o'}) {$opt{'o'} = 5000}
if (!defined $opt{'C'}) {$opt{'C'} = 20}
} else {
die "
Can not determine platform option (-P), please select from:
T = TC-Seq / 10X Linked Read (def)
F = Fosmid Pool
L = Long Fragment Read
"}
# CONTIG PARSING OPTIONS #
if (!defined $opt{'m'}) {$opt{'m'} = 1}
if (!defined $opt{'d'}) {$opt{'d'} = 0.05}
if (!defined $opt{'D'}) {$opt{'D'} = 0.95}
if (!defined $opt{'U'}) {$opt{'U'} = 0}
# GRAPH OPTIONS #
if (!defined $opt{'l'}) {$opt{'l'} = 5}
if (!defined $opt{'a'}) {$opt{'a'} = 20}
if (!defined $opt{'c'}) {$opt{'c'} = "D"}
if (!defined $opt{'V'}) {$opt{'V'} = 0}
# OUTPUT OPTIONS #
if (!defined $opt{'L'}) {$opt{'L'} = 100}
if (!defined $opt{'n'}) {$opt{'n'} = "N"}
if (!defined $opt{'x'}) {$opt{'x'} = 3000}
if (!defined $opt{'X'}) {$opt{'X'} = 8000}
if (!defined $opt{'g'}) {$opt{'g'} = "X"}
# PERFORMANCE OPTIONS #
if (!defined $opt{'S'}) {$opt{'S'} = 100}
if (($opt{'S'}/2) =~ /\./) {$opt{'S'}++}
if (!defined $opt{'T'}) {$opt{'T'} = 100}
if (!defined $opt{'e'}) {$opt{'e'} = "2G"}
# DEPENDENCY OPTIONS #
if (!defined $opt{'s'}) {$opt{'s'} = 'samtools'}
if (!defined $opt{'k'}) {$opt{'k'} = 'fragScaff.pl'}
# COMMON VARIABLE DEFINITIONS #
my$PFX = "";
my$dPFX = "";
my$bPFX = "";
my@P = ();
my@H = ();
my$l = "";
my$i = 0;
my$j = 0;
# LOG / WORKING DIR OPTIONS #
if (!defined $opt{'b'} || $opt{'b'} < 0.5) {
$PFX = $opt{'O'};
if (-e "$PFX.fragScaff.log") {
die "\n$PFX.fragScaff.log logfile already exists! Exiting!\n";
} else {
open LOG, ">$PFX.fragScaff.log";
select((select(LOG), $|=1)[0]); # make LOG hot
$ts = localtime(time);
print LOG "$ts\tProgram Called.\n";
if ($opt{'B'} =~ /bamParse/) {
my$bamParseE = 0;
my@H;
@H = split(/\./, $opt{'B'});
foreach (@H) {
if ($_ =~ /^E\d+$/) {$opt{'E'} = $_; $opt{'E'} =~ s/^E//; $bamParseE = 1}
}
if ($bamParseE>0) {
print LOG "\tbamParse file provided. E detected as $opt{'E'}\n";
} else {
print LOG "\tbamParse file provided. E not detected, proceeding anyway.\n";
}
}
my$linkFile = "$PFX.fragScaff.links.txt";
if (defined $opt{'K'}) {
$linkFile = $opt{'K'};
open IN, "$linkFile";
$l = <IN>; chomp $l;
if ($l =~ /^#LINKFILE/) {
@P = split(/\t/, $l);
$opt{'d'} = $P[1];
$opt{'D'} = $P[2];
$opt{'E'} = $P[3];
$opt{'r'} = $P[4];
print LOG "\tK file provided, forcing options: -d $opt{'d'}, -D $opt{'D'}, -E $opt{'E'}, -r $opt{'r'}. Expected nodeCT = $P[5]\n";
} else {
print LOG "\tK file provided, but no #LINKFILE header line! Proceeding anyway.\n";
}
close IN;
}
$dPFX = "$PFX.r$opt{'r'}.fragScaff";
$ts = localtime(time);
print LOG "$ts\tOptions: ";
foreach my$option (sort keys %opt) {
if ($option !~ /[vfRzwIAH]/) {
print LOG " -$option $opt{$option}";
}
}
foreach my$option (sort keys %opt) {
if ($option =~ /[vfRzwIAH]/) {
print LOG " -$option";
}
}
print LOG "\n";
}
if (!defined $opt{'K'}) {
$ts = localtime(time);
print LOG "$ts\tCreating temporary directory ($dPFX).\n";
if (-d "$dPFX") {
print LOG "\n$dPFX directory already exists! Exiting!\n";
die "\n$dPFX directory already exists! Exiting!\n";
} else {
system("mkdir $dPFX");
}
}
if (defined $opt{'b'}) {
my$BPoutName = "$opt{'O'}.E$opt{'E'}.o$opt{'o'}";
if (defined $opt{'J'}) {$BPoutName .= ".J"}
if (defined $opt{'N'}) {$BPoutName .= ".N"}
$bPFX = "$BPoutName.bamParse";
}
} else {
my$BPoutName = "$opt{'O'}.E$opt{'E'}.o$opt{'o'}";
if (defined $opt{'J'}) {$BPoutName .= ".J"}
if (defined $opt{'N'}) {$BPoutName .= ".N"}
$bPFX = "$BPoutName.bamParse";
open LOG, ">$bPFX.log";
select((select(LOG), $|=1)[0]);
}
## END OPTION PARSING AND INITIALIZATION ##
# BEGIN READ IN CONTIGS AND READ GROUPS #
$ts = localtime(time);
print LOG "$ts\tReading in contigs and groups ... \n";
$_ = 0 for my($groupID,$contigID,$nodeID,$length,$groupCT,$contigCT,$nodeCT);
$_ = "" for my($group_name,$contig_name);
my(@GROUP_NAME,@CONTIG_NAME,@NODE_NAME,@NODE_PARTNER_NAME,@CONTIG_LENGTH,@NODE_HITS);
my(%GROUP_ID,%CONTIG_ID,%NODE_ID);
# BEGIN RG IDENTIFICATION SUB #
my($group_found,$fieldID,$null) = (0,0,0);
sub get_rg {
$group_found = 0;
if ($opt{'G'} =~ /R/i) {
for ($fieldID = 11; $fieldID < @P; $fieldID++) {
if ($P[$fieldID] =~ /^RG:Z:/) {
$group_name = $P[$fieldID]; $group_name =~ s/^RG:Z://;
if (defined $GROUP_ID{$group_name}) {
$groupID = $GROUP_ID{$group_name};
$group_found = 1;
}
$fieldID += 999;
}
}
} elsif ($opt{'G'} =~ /N/i) {
($group_name,$null) = split(/:/, $P[0]);
if (defined $GROUP_ID{$group_name}) {
$groupID = $GROUP_ID{$group_name};
$group_found = 1;
}
} elsif ($opt{'G'} =~ /H/i) {
($null,$group_name) = split(/#/, $P[0]);
if (defined $GROUP_ID{$group_name}) {
$groupID = $GROUP_ID{$group_name};
$group_found = 1;
}
} elsif ($opt{'G'} =~ /X/i) {
for ($fieldID = 11; $fieldID < @P; $fieldID++) {
if ($P[$fieldID] =~ /^CR:Z:/) {
$group_name = $P[$fieldID]; $group_name =~ s/^CR:Z://;
if (defined $GROUP_ID{$group_name}) {
$groupID = $GROUP_ID{$group_name};
$group_found = 1;
}
$fieldID += 999;
}
}
}
}
## END RG IDENTIFICATION SUB ##
if ($opt{'B'} =~ /bamParse/) {
my($groupHits,$readHits) = ("",0);
if ($opt{'B'} =~ /gz$/) {
open BP, "zcat $opt{'B'} |";
} else {
open BP, "$opt{'B'}";
}
while ($l = <BP>) {
chomp $l;
@P = split(/\t/, $l);
if ($P[0] eq "GROUP") {
$GROUP_NAME[$P[1]] = $P[2];
$GROUP_ID{$P[2]} = $P[1];
$groupCT++;
} elsif ($P[0] eq "CONTIG") {
$CONTIG_NAME[$P[1]] = $P[2];
$CONTIG_ID{$P[2]} = $P[1];
$CONTIG_LENGTH[$P[1]] = $P[3];
$contigCT++;
} elsif ($P[0] eq "NODE") {
$NODE_NAME[$P[1]] = $P[2];
$NODE_ID{$P[2]} = $P[1];
$NODE_PARTNER_NAME[$P[1]] = $P[3];
$nodeCT++;
} elsif ($P[0] eq "NODE_HITS") {
@H = split(/,/, $P[2]);
if (@H>=2) {
foreach $groupHits (@H) {
($groupID,$readHits) = split(/:/, $groupHits);
$NODE_HITS[$P[1]]{$groupID} = $readHits;
}
}
} else {
die "\nUNRECOGNIZED FIELD IN BAMPARSE! ($P[0])\n";
}
} close BP;
$ts = localtime(time);
print LOG "$ts\tbamParse read: $contigCT contigs ($nodeCT nodes), and $groupCT groups.\n";
} else {
if (defined $opt{'b'}) {
if (defined $opt{'z'}) {
open BP, "| gzip > $bPFX";
} else {
open BP, ">$bPFX";
}
}
if ($opt{'G'} =~ /X/i) { # ADD IN 10X read group identification
$ts = localtime(time);
print LOG "$ts\t10X Genomics bam file specified. Parsing bam to collect all read groups. (beta function)\n";
open BAM, "$opt{'s'} view $opt{'B'} |";
while ($l = <BAM>) {
for ($fieldID = 11; $fieldID < @P; $fieldID++) {
if ($P[$fieldID] =~ /^CR:Z:/) {
$group_name = $P[$fieldID]; $group_name =~ s/^CR:Z://;
$fieldID += 999;
if (!defined $GROUP_ID{$group_name}) {
$GROUP_NAME[$groupID] = $group_name;
$GROUP_ID{$group_name} = $groupID;
if (defined $opt{'b'}) {print BP "GROUP\t$groupID\t$group_name\n"}
$groupID++;
}
}
}
} close BAM;
}
open HEAD, "$opt{'s'} view -H $opt{'B'} |";
while ($l = <HEAD>) {
chomp $l;
@P = split(/\t/, $l);
if ($P[0] eq "\@RG" && $opt{'G'} !~ /X/i) {
$group_name = $P[1]; $group_name =~ s/^ID://;
if ($group_name !~ /NO_MATCH/) {
if (!defined $GROUP_ID{$group_name}) {
$GROUP_NAME[$groupID] = $group_name;
$GROUP_ID{$group_name} = $groupID;
if (defined $opt{'b'}) {print BP "GROUP\t$groupID\t$group_name\n"}
$groupID++;
}
}
} elsif ($P[0] eq "\@SQ") {
$length = $P[2];
$length =~ s/\D//g;
if ($length>=$opt{'m'}) {
$contig_name = $P[1]; $contig_name =~ s/^SN://;
if (!defined $CONTIG_ID{$contig_name}) {
$CONTIG_NAME[$contigID] = $contig_name;
$CONTIG_ID{$contig_name} = $contigID;
$CONTIG_LENGTH[$contigID] = $length;
if (defined $opt{'b'}) {print BP "CONTIG\t$contigID\t$contig_name\t$length\n"}
$NODE_ID{"$contigID.L"} = $nodeID;
$NODE_NAME[$nodeID] = "$contigID.L";
$NODE_PARTNER_NAME[$nodeID] = "$contigID.R";
if (defined $opt{'b'}) {print BP "NODE\t$nodeID\t$NODE_NAME[$nodeID]\t$NODE_PARTNER_NAME[$nodeID]\n"}
$nodeID++;
$NODE_ID{"$contigID.R"} = $nodeID;
$NODE_NAME[$nodeID] = "$contigID.R";
$NODE_PARTNER_NAME[$nodeID] = "$contigID.L";
if (defined $opt{'b'}) {print BP "NODE\t$nodeID\t$NODE_NAME[$nodeID]\t$NODE_PARTNER_NAME[$nodeID]\n"}
$nodeID++;
$contigID++;
}
}
}
} close HEAD;
$groupCT = $groupID; $groupID = 0;
$contigCT = $contigID; $contigID = 0;
$nodeCT = $nodeID; $nodeID = 0;
$ts = localtime(time);
print LOG "$ts\t$contigCT contigs ($nodeCT nodes), and $groupCT groups.\n";
# BEGIN PARSE BAM #
# BEGIN EXCLUSION BED #
my%EXCLUSIONS; my$excludeWin; my%RIGHT_EXCLUSIONS;
if (defined $opt{'J'}) {
$ts = localtime(time);
if ($opt{'B'} =~ /bamParse/) {
print LOG "$ts\tExclusion windows defined (-J), but bamParse provided. Ignoring -J.\n";
} else {
print LOG "$ts\tReading in exclusion windows (-J) from $opt{'J'}\n";
$_ = 0 for my($totalExclusionWindows,$totalExclusionBases,$totalExclusionWindowsE,$totalExclusionBasesE,$wrongWindowCT);
open IN, "$opt{'J'}";
while ($l = <IN>) {
chomp $l;
@P = split(/\t/, $l);
if (!defined $CONTIG_LENGTH[$CONTIG_ID{$P[0]}]) {
$wrongWindowCT++;
} else {
if ($P[1]<$opt{'E'}||$P[2]>($CONTIG_LENGTH[$CONTIG_ID{$P[0]}]-$opt{'E'})) {
$totalExclusionWindowsE++;
$totalExclusionBasesE+=($P[2]-$P[1]);
}
if ($P[1]<$opt{'o'}||$P[2]>($CONTIG_LENGTH[$CONTIG_ID{$P[0]}]-$opt{'o'})) {
$EXCLUSIONS{$P[0]}{$P[1]} = $P[2];
if ($P[2]>($CONTIG_LENGTH[$CONTIG_ID{$P[0]}]-$opt{'o'})) {
$RIGHT_EXCLUSIONS{$P[0]}{$P[2]} = $P[1];
}
}
$totalExclusionWindows++;
$totalExclusionBases+=($P[2]-$P[1]);
}
} close IN;
print LOG "\t$totalExclusionWindows total windows ($totalExclusionBases bp)
\t$totalExclusionWindowsE windows in default nodes ($totalExclusionBasesE bp)
\t$wrongWindowCT windows do not match contigs.\n";
}
}
## END EXCLUSION BED ##
# BEGIN N-BASE BED #
my%NBASE_GAPS; my%RIGHT_NBASE_GAPS;
if (defined $opt{'N'}) {
$ts = localtime(time);
if ($opt{'B'} =~ /bamParse/) {
print LOG "$ts\tNbase windows defined (-N), but bamParse provided. Ignoring -N.\n";
} else {
print LOG "$ts\tReading in Nbase windows (-N) from $opt{'N'}\n";
$_ = 0 for my($NbaseWinCount,$NbaseWinSize,$NbaseWinCountE,$NbaseWinSizeE,$NbaseNonWindowCount);
open IN, "$opt{'N'}";
while ($l = <IN>) {
chomp $l;
@P = split(/\t/, $l);
if (!defined $CONTIG_LENGTH[$CONTIG_ID{$P[0]}]) {
$NbaseNonWindowCount++;
} else {
if ($P[1]<$opt{'E'}||$P[2]>($CONTIG_LENGTH[$CONTIG_ID{$P[0]}]-$opt{'E'})) {
$NbaseWinCountE++;
$NbaseWinSizeE+=($P[2]-$P[1]);
}
if ($P[1]<$opt{'o'}||$P[2]>($CONTIG_LENGTH[$CONTIG_ID{$P[0]}]-$opt{'o'})) {
$NBASE_GAPS{$P[0]}{$P[1]} = $P[2];
if ($P[2]>($CONTIG_LENGTH[$CONTIG_ID{$P[0]}]-$opt{'o'})) {
$RIGHT_NBASE_GAPS{$P[0]}{$P[2]} = $P[1];
}
}
$NbaseWinCount++;
$NbaseWinSize+=($P[2]-$P[1]);
}
} close IN;
print LOG "\t$NbaseWinCount total windows ($NbaseWinSize bp)
\t$NbaseWinCountE windows in default nodes ($NbaseWinSizeE bp)
\t$NbaseNonWindowCount windows do not match contigs.\n";
}
}
## END N-BASE BED ##
# BEGIN DEFINE NODE BOUNDARIES #
$ts = localtime(time);
my@LEFT_END; my@RIGHT_START; my$clippedNodeCT = 0; my$clippedBpCT = 0;
if (defined $opt{'J'} || defined $opt{'N'}) {
open NODES, ">$bPFX.reDefinedNodes.bed";
print LOG "$ts\tRedefining node boundaries to accomodate exclusions. ";
$_ = 0 for my($nodeBaseCount,$nodeNullEnd,$nodeInNull,$nodePosition,$nodeNullBasesTotal);
for ($contigID = 0; $contigID < $contigCT; $contigID++) {
$nodeBaseCount = 0; $nodePosition = 0; $nodeNullEnd = 0; $nodeNullBasesTotal = 0; $nodeInNull = 0;
while ($nodeBaseCount<$opt{'E'}&&$nodePosition<$opt{'o'}&&$nodePosition<$CONTIG_LENGTH[$contigID]) {
if (defined $NBASE_GAPS{$CONTIG_NAME[$contigID]}{$nodePosition}) {
if (($nodeInNull>0.5&&$NBASE_GAPS{$CONTIG_NAME[$contigID]}{$nodePosition}>$nodeNullEnd) || $nodeInNull<0.5) {
$nodeNullEnd = $NBASE_GAPS{$CONTIG_NAME[$contigID]}{$nodePosition};
$nodeInNull = 1;
}
}
if (defined $EXCLUSIONS{$CONTIG_NAME[$contigID]}{$nodePosition}) {
if (($nodeInNull>0.5&&$EXCLUSIONS{$CONTIG_NAME[$contigID]}{$nodePosition}>$nodeNullEnd) || $nodeInNull<0.5) {
$nodeNullEnd = $EXCLUSIONS{$CONTIG_NAME[$contigID]}{$nodePosition};
$nodeInNull = 1;
}
}
if ($nodeInNull<0.5) {
$nodeBaseCount++;
} elsif ($nodePosition>=$nodeNullEnd) {
$nodeInNull = 0;
} else {
$nodeNullBasesTotal++;
}
$nodePosition++;
}
$LEFT_END[$contigID] = $nodePosition;
if ($nodeBaseCount<$opt{'E'}) {
$clippedNodeCT++;
$clippedBpCT+=($opt{'E'}-$nodeBaseCount);
}
print NODES "$CONTIG_NAME[$contigID]\t1\t$LEFT_END[$contigID]\tSIDE=L;CONTIG_LENGTH=$CONTIG_LENGTH[$contigID];VALID_BP=$nodeBaseCount;NULL_BP=$nodeNullBasesTotal\n";
$nodeBaseCount = 0; $nodePosition = $CONTIG_LENGTH[$contigID]; $nodeNullEnd = $CONTIG_LENGTH[$contigID]; $nodeNullBasesTotal = 0; $nodeInNull = 0;
while ($nodeBaseCount<$opt{'E'}&&$nodePosition>($CONTIG_LENGTH[$contigID]-$opt{'o'})&&$nodePosition>0) {
if (defined $RIGHT_NBASE_GAPS{$CONTIG_NAME[$contigID]}{$nodePosition}) {
if (($nodeInNull>0.5&&$RIGHT_NBASE_GAPS{$CONTIG_NAME[$contigID]}{$nodePosition}<$nodeNullEnd) || $nodeInNull<0.5) {
$nodeNullEnd = $RIGHT_NBASE_GAPS{$CONTIG_NAME[$contigID]}{$nodePosition};
$nodeInNull = 1;
}
}
if (defined $RIGHT_EXCLUSIONS{$CONTIG_NAME[$contigID]}{$nodePosition}) {
if (($nodeInNull>0.5&&$RIGHT_EXCLUSIONS{$CONTIG_NAME[$contigID]}{$nodePosition}<$nodeNullEnd) || $nodeInNull<0.5) {
$nodeNullEnd = $RIGHT_EXCLUSIONS{$CONTIG_NAME[$contigID]}{$nodePosition};
$nodeInNull = 1;
}
}
if ($nodeInNull<0.5) {
$nodeBaseCount++;
} elsif ($nodePosition<=$nodeNullEnd) {
$nodeInNull = 0;
} else {
$nodeNullBasesTotal++;
}
$nodePosition--;
}
$RIGHT_START[$contigID] = $nodePosition;
if ($nodeBaseCount<$opt{'E'}) {
$clippedNodeCT++;
$clippedBpCT+=($opt{'E'}-$nodeBaseCount);
}
print NODES "$CONTIG_NAME[$contigID]\t$RIGHT_START[$contigID]\t$CONTIG_LENGTH[$contigID]\tSIDE=R;CONTIG_LENGTH=$CONTIG_LENGTH[$contigID];VALID_BP=$nodeBaseCount;NULL_BP=$nodeNullBasesTotal\n";
}
close NODES;
print LOG "$clippedNodeCT nodes with less than $opt{'E'} valid bp for a total of $clippedBpCT bp short.\n";
} else {
print LOG "$ts\tUsing default node boundaries. ";
for ($contigID = 0; $contigID < $contigCT; $contigID++) {
if ($CONTIG_LENGTH[$contigID]<$opt{'E'}) {
$LEFT_END[$contigID] = $CONTIG_LENGTH[$contigID];
$RIGHT_START[$contigID] = 1;
$clippedNodeCT++;
$clippedBpCT+=(($opt{'E'}-$CONTIG_LENGTH[$contigID])*2);
} else {
$LEFT_END[$contigID] = $opt{'E'};
$RIGHT_START[$contigID] = ($CONTIG_LENGTH[$contigID]-$opt{'E'});
}
}
print LOG "$clippedNodeCT nodes with less than $opt{'E'} bp for a total of $clippedBpCT bp short.\n";
}
%NBASE_GAPS = (); %RIGHT_EXCLUSIONS = (); %RIGHT_NBASE_GAPS = ();
## END DEFINE NODE BOUNDARIES ##
$ts = localtime(time);
print LOG "$ts\tFinding covered contig ends ...\n";
$_ = 0 for my($no_group_read_ct,$assigned_ct,$read_increment,$read_check,$read_count,
$dup_ct,$uniq_frac,$excludeFlag,$excludedReads,$readEnd,$exclude_frac,$no_group_read_frac);
my@PREV_LOC;
$read_increment = 10000000; $read_check = $read_increment;
open IN, "$opt{'s'} view -q $opt{'q'} $opt{'B'} |";
while ($l = <IN>) {
chomp $l;
@P = split(/\t/, $l);
get_rg();
if ($group_found > 0.5) {
$assigned_ct++;
$excludeFlag = 0;
if (defined $EXCLUSIONS{$P[2]}) {
$readEnd = $P[3]+length($P[9]);
foreach $excludeWin (keys %{$EXCLUSIONS{$P[2]}}) {
if ($P[3]<=$excludeWin) {
if ($readEnd>=$EXCLUSIONS{$P[2]}{$excludeWin}) {
$excludeFlag = 1;
} elsif ($readEnd>$excludeWin&&$readEnd<=$EXCLUSIONS{$P[2]}{$excludeWin}) {
if (($readEnd-$excludeWin)/length($P[9])>=0.5) {
$excludeFlag = 1;
}
}
} elsif ($P[3]<$EXCLUSIONS{$P[2]}{$excludeWin}) {
if ($readEnd<=$EXCLUSIONS{$P[2]}{$excludeWin}) {
$excludeFlag = 1;
} elsif (($readEnd-$EXCLUSIONS{$P[2]}{$excludeWin})/length($P[9])<=0.5) {
$excludeFlag = 1;
}
}
}
}
if ($excludeFlag < 0.5) {
if ($PREV_LOC[$groupID] ne "$P[2]:$P[3]") {
if (defined $CONTIG_ID{$P[2]}) {
$contigID = $CONTIG_ID{$P[2]};
if ($P[3]<=$LEFT_END[$contigID]) {
$nodeID = $NODE_ID{"$contigID.L"};
$NODE_HITS[$nodeID]{$groupID}++;
}
if (($P[3]+length($P[9]))>=$RIGHT_START[$contigID]) {
$nodeID = $NODE_ID{"$contigID.R"};
$NODE_HITS[$nodeID]{$groupID}++;
}
}
} else {
$dup_ct++;
}
$PREV_LOC[$groupID] = "$P[2]:$P[3]";
} else {
if ($PREV_LOC[$groupID] eq "$P[2]:$P[3]") {$dup_ct++}
$PREV_LOC[$groupID] = "$P[2]:$P[3]";
$excludedReads++;
}
} else {
$no_group_read_ct++;
}
$read_count++;
if ($read_count>=$read_check) {
$no_group_read_frac = sprintf("%.3f", $no_group_read_ct/$read_count);
$exclude_frac = sprintf("%.3f", $excludedReads/$assigned_ct);
$uniq_frac = sprintf("%.3f", $dup_ct/$assigned_ct);
$ts = localtime(time);
print LOG "\t$ts\t$read_check reads processed. ($no_group_read_ct ($no_group_read_frac) have no group, $excludedReads ($exclude_frac) in repeats, $dup_ct ($uniq_frac) PCR duplicates)\n";
$read_check+=$read_increment;
}
} close IN;
@PREV_LOC = ();
$no_group_read_frac = sprintf("%.3f", $no_group_read_ct/$read_count);
$exclude_frac = sprintf("%.3f", $excludedReads/$assigned_ct);
$uniq_frac = sprintf("%.3f", $dup_ct/$assigned_ct);
$ts = localtime(time);
print LOG "$ts\t$no_group_read_ct ($no_group_read_frac) reads had no group, $excludedReads ($exclude_frac) were in repeats, and $dup_ct ($uniq_frac) were PCR duplicates.\n";
if (defined $opt{'b'}) {
for ($nodeID = 0; $nodeID < $nodeCT; $nodeID++) {
my$bamParse_out = "NODE_HITS\t$nodeID\t";
foreach $groupID (keys %{$NODE_HITS[$nodeID]}) {
$bamParse_out .= "$groupID:$NODE_HITS[$nodeID]{$groupID},";
}
$bamParse_out =~ s/,$//;
print BP "$bamParse_out\n";
}
close BP;
if ($opt{'b'} > 0.5) {
$ts = localtime(time);
print LOG "$ts\tbamParse created, exiting.\n";
close LOG;
exit;
}
}
@LEFT_END = (); @RIGHT_START = ();
%EXCLUSIONS = ();
## END PARSE BAM ##
}
## END READ IN CONTIGS AND READ GROUPS ##
# BEGIN FILTER FOR REAL HITS AND MAKE MASTER FILE #
my($total_pass,$runningNodeCT) = (0,0);
my($minThresh,$maxThresh) = (-1,-1);
my$out_hits = "";
my(@NODE_INCLUSIONS,@TOTAL_PASS_CT);
my%GROUP_HIT_HIST;
$ts = localtime(time);
print LOG "$ts\tFiltering node hits ... ";
setup_node_hits();
sub setup_node_hits {
for ($nodeID = 0; $nodeID < $nodeCT; $nodeID++) {
$total_pass = 0;
foreach $groupID (keys %{$NODE_HITS[$nodeID]}) {
if ($NODE_HITS[$nodeID]{$groupID} >= $opt{'C'}) {
$NODE_INCLUSIONS[$nodeID]{$groupID} = 1;
$total_pass++;
}
}
$TOTAL_PASS_CT[$nodeID] = $total_pass;
$GROUP_HIT_HIST{$total_pass}++;
}
@NODE_HITS = ();
if (defined $opt{'v'}) {open HIST, ">$PFX.fragScaff.group_hits.hist"}
$runningNodeCT = 0;
foreach $total_pass (sort {$a<=>$b} keys %GROUP_HIT_HIST) {
$runningNodeCT+=$GROUP_HIT_HIST{$total_pass};
if (($runningNodeCT/$nodeCT)>$opt{'d'}&&$minThresh<0) {
print LOG "Min cut = $total_pass ($runningNodeCT excluded), ";
$minThresh = $total_pass;
}
if (($runningNodeCT/$nodeCT)>$opt{'D'}&&$maxThresh<0) {
print LOG "Max cut = $total_pass (".($nodeCT-$runningNodeCT)." excluded)\n";
$maxThresh = $total_pass;
}
if (defined $opt{'v'}) {print HIST "$total_pass\t$GROUP_HIT_HIST{$total_pass}\n"}
}
if (defined $opt{'v'}) {close HIST}
if (!defined $opt{'K'}) {
open MST, ">$dPFX/hits.txt";
for ($nodeID = 0; $nodeID < $nodeCT; $nodeID++) {
if ($TOTAL_PASS_CT[$nodeID]>$minThresh&&$TOTAL_PASS_CT[$nodeID]<$maxThresh&&$TOTAL_PASS_CT[$nodeID]>=$opt{'U'}) {
$out_hits = "";
foreach $groupID (keys %{$NODE_INCLUSIONS[$nodeID]}) {
$out_hits .= "$groupID,";
}
$out_hits =~ s/,$//;
print MST "$nodeID\t$TOTAL_PASS_CT[$nodeID]\t$out_hits\n";
}
}
close MST;
}
}
## END FILTER FOR REAL HITS AND MAKE MASTER FILE ##
$ts = localtime(time);
print LOG "$ts\tCalculating link scores ... nodeCT = $nodeCT\n";
# BEGIN GAUSS MASS FUNCTION SUB #
sub gauss_p {
$gauss_x = $_[0]; $gauss_m = $_[1]; $gauss_sd = $_[2];
$gauss_p = ((1/sqrt(2*$pi*($gauss_sd**2)))*exp(-1*((($gauss_x-$gauss_m)**2)/(2*($gauss_sd**2)))));
return $gauss_p;
}
## END GAUSS MASS FUNCTION SUB ##
# BEGIN EXECUTE PROBABILITY CALCULATIONS #
my($nodeID1,$nodeID2) = (0,0);
my%LOAD_FILES; my%NODE2;
if (!defined $opt{'K'}) {
if (!defined $opt{'t'} || ($opt{'t'} =~ /\d/ && $opt{'t'} < 1.5)) {
$ts = localtime(time);
print LOG "$ts\tSingle thread mode. Will be slow.\n";
pvalue_links(0,($nodeCT-1));
$LOAD_FILES{"0"} = "$dPFX/node.0.".($nodeCT-1).".txt";
} elsif ($opt{'t'} eq "Q") {
$ts = localtime(time);
print LOG "$ts\tQsubbing mode, submitting jobs. ";
open IN, "pwd |"; my$PWD = <IN>; close IN; chomp $PWD;
my$jobMax = 0;
open JOBARRAY, ">$dPFX/job_array.txt";
my(%QSUB_STATUS); my($complete_ct,$incomplete_ct,$nodeID1,$nodeID2) = (0,0,0,0);
for ($nodeID1 = 0; $nodeID1 < $nodeCT; $nodeID1+=$opt{'S'}) {
$incomplete_ct++;
$nodeID2 = $nodeID1+($opt{'S'}-1);
if ($nodeID2>=$nodeCT) {$nodeID2 = ($nodeCT-1)}
$NODE2{$nodeID1} = $nodeID2;
$QSUB_STATUS{$nodeID1} = 0;
if (defined $opt{'R'}) {
print JOBARRAY "$opt{'k'} -Q $PWD/$dPFX,$nodeCT,$nodeID1,$nodeID2 -r $opt{'r'} -M $opt{'M'} -R\n";
} else {
print JOBARRAY "$opt{'k'} -Q $PWD/$dPFX,$nodeCT,$nodeID1,$nodeID2 -r $opt{'r'} -M $opt{'M'}\n";
}
$LOAD_FILES{$jobMax} = "$dPFX/node.$nodeID1.$nodeID2.txt";
$jobMax++;
}
close JOBARRAY;
my$qsubCommand = "$PWD/$dPFX/run_array.csh";
open RUN_ARRAY, ">$qsubCommand";
print RUN_ARRAY "#/bin/bash\n#\$ -S /bin/bash\n#\$ -cwd\n#\$ -V\nCOMMAND=\$(head -n \$SGE_TASK_ID $PWD/$dPFX/job_array.txt | tail -n 1)\n\$COMMAND\n";
close RUN_ARRAY; system("chmod +x $qsubCommand");
my$jobName = "FSCF_".time;
print LOG "Job array name = $jobName, Job count = $jobMax\n";
if ($opt{'T'} eq "N") {
system("qsub -t 1-$jobMax -N $jobName -b y -l h_vmem=$opt{'e'},virtual_free=$opt{'e'} $qsubCommand \"$PWD/$dPFX/job_array.txt\"");
} else {
system("qsub -t 1-$jobMax -N $jobName -b y -l h_vmem=$opt{'e'},virtual_free=$opt{'e'} -tc $opt{'T'} $qsubCommand \"$PWD/$dPFX/job_array.txt\"");
}
while ($complete_ct<$incomplete_ct) {
$complete_ct = 0;
foreach my$checkNodeID (keys %QSUB_STATUS) {
$nodeID2 = $NODE2{$checkNodeID};
if ($QSUB_STATUS{$checkNodeID} == 0 && -e "$dPFX/node.$checkNodeID.$nodeID2.complete") {
$QSUB_STATUS{$checkNodeID} = 1;
system("rm -f $dPFX/node.$checkNodeID.$nodeID2.complete");
}
if ($QSUB_STATUS{$checkNodeID} == 1) {
$complete_ct++;
}
}
sleep(1);
if (-e "rm -f $dPFX/kill") {$complete_ct=$incomplete_ct}
}
} else {
$ts = localtime(time);
print LOG "$ts\tRunning with $opt{'t'} threads.\n";
my(%THREAD_STATUS); my($incomplete_ct,$complete_ct,$runningThreads) = (0,0,0);
($nodeID1,$nodeID2) = (0,0);
for ($nodeID1 = 0; $nodeID1 < $nodeCT; $nodeID1+=$opt{'S'}) {
$THREAD_STATUS{$nodeID1} = 0;
$nodeID2 = $nodeID1+($opt{'S'}-1);
if ($nodeID2>=$nodeCT) {$nodeID2 = ($nodeCT-1)}
$NODE2{$nodeID1} = $nodeID2;
$LOAD_FILES{$incomplete_ct} = "$dPFX/node.$nodeID1.$nodeID2.txt";
$incomplete_ct++;
}
while ($complete_ct<$incomplete_ct) {
for ($nodeID1 = 0; $nodeID1 < $nodeCT; $nodeID1+=$opt{'S'}) {
$nodeID2 = $NODE2{$nodeID1};
if (-e "$dPFX/node.$nodeID1.$nodeID2.complete") {
if ($THREAD_STATUS{$nodeID1} == 1) {
system("rm -f $dPFX/node.$nodeID1.$nodeID2.complete");
$runningThreads--;
$complete_ct++;
}
$THREAD_STATUS{$nodeID1} = 2;
} elsif ($THREAD_STATUS{$nodeID1} == 0 && $runningThreads < $opt{'t'}) {
$nodeID2 = $NODE2{$nodeID1};
if (defined $opt{'R'}) {
system("$opt{'k'} -Q $dPFX,$nodeCT,$nodeID1,$nodeID2 -r $opt{'r'} -M $opt{'M'} -R &");
} else {
system("$opt{'k'} -Q $dPFX,$nodeCT,$nodeID1,$nodeID2 -r $opt{'r'} -M $opt{'M'} &");
}
$THREAD_STATUS{$nodeID1} = 1;
$runningThreads++;
}
}
sleep(1);
if (-e "rm -f $dPFX/kill") {$complete_ct=$incomplete_ct}
}
}
$ts = localtime(time);
print LOG "$ts\tAll link pvalues calculated.\n";
} else {
$ts = localtime(time);
print LOG "$ts\tK toggled, using $opt{'K'} for links.\n";
}
## END EXECUTE PROBABILITY CALCULATIONS ##
# BEGIN PVALUE LINKS SUBROUTINE #
my(@PASS_HITS,@GROUP_LIST,@TOTAL_PASS);
my($startNode,$endNode,$nodeID1,$nodeID2,$mean_num,$stdev_num,$tally,$shared,$total,$frac,$mean,
$stdev,$pval,$side,$score,$total_links,$pair_frac,$calc_nodeID1);
my($nodeOut,$calcNodeOut) = ("","");
my%FRAC;
sub pvalue_links {
($startNode,$endNode) = (0,0);
$startNode = $_[0];
$endNode = $_[1];
(@PASS_HITS,@GROUP_LIST,@TOTAL_PASS) = ();
open MST, "$dPFX/hits.txt";
while ($l = <MST>) {
chomp $l;
@P = split(/\t/, $l);
$TOTAL_PASS[$P[0]] = $P[1];
@GROUP_LIST = split(/,/, $P[2]);
foreach $groupID (@GROUP_LIST) {
$PASS_HITS[$P[0]]{$groupID} = 1;
}
} close MST;
$_ = 0 for ($nodeID1,$nodeID2,$mean_num,$stdev_num,$tally,$shared,$total,$frac,$mean,$stdev,
$pval,$side,$total_links,$pair_frac);
$nodeOut = "";
for ($nodeID1 = $startNode; $nodeID1 <= $endNode; $nodeID1++) {
if ($TOTAL_PASS[$nodeID1]>0){$nodeOut .= calc_node($nodeID1,0)}
$nodeID1++;
if ($TOTAL_PASS[$nodeID1]>0){$nodeOut .= calc_node($nodeID1,1)}
}
open OUT, ">$dPFX/node.$startNode.$endNode.txt";
print OUT "$nodeOut"; close OUT;
}
## END PVALUE LINKS SUBROUTINE ##
# BEGIN CALC PVAL SUB #
sub calc_node {
$calc_nodeID1 = $_[0];
$side = $_[1];
$_ = 0 for ($mean_num,$stdev_num,$tally,$shared,$total,$frac,$mean,$stdev,$pval,$score,
$total_links,$pair_frac);
$calcNodeOut = "";
%FRAC = ();
for ($nodeID2 = 0; $nodeID2 < $nodeCT; $nodeID2++) {
if ($side < 0.5) {
if ($nodeID2 != $calc_nodeID1 && $nodeID2 != ($calc_nodeID1+1)) {
$shared = 0;
foreach $groupID (keys %{$PASS_HITS[$calc_nodeID1]}) {
if (defined $PASS_HITS[$nodeID2]{$groupID}) {
$shared++;
}
}
if ($shared > 0) {
$total = $TOTAL_PASS[$calc_nodeID1]+$TOTAL_PASS[$nodeID2]-$shared;
if ($total>0) {
$frac = $shared/$total;
} else {