-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jrna2Psy.pm
executable file
·2404 lines (1907 loc) · 64 KB
/
Jrna2Psy.pm
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
# please read the __README included with this distribution prior to using this code
# jrna - scripts copyright Matt Yoder, 2004, gnu public license applies
package Jrna;
use vars qw($VERSION);
$VERSION = '0.777';
use strict;
use warnings;
use FindBin qw($Bin); # sets $Bin to the root directory
srand ();
####################################
# Jrna package variables (globals) #
####################################
my $TIME = localtime($^T);
my $rootdir = $Bin;
#ultimately in stem descriptor obj?
my @bonds; # array of compensatory bond changes for stem regions bonds[non-prime block number][basepairindex][statistic], not to be confused with @bond!!, used in displaying stems on the webpage output
my %sindex; # should be a member of a stems descriptor object
my %stems; # key is a block index that points to the prime end
# mtrx obj?
my @mx; # $mx[terminal][block][0] -> the actuall data in the matrix
my %ters; # terminal hash #->name
# mtrx meta data obj?
my @deschead; # description headers $deschead[block][0] - first row; [1] - second row
my @nbd = ();
my @nbd1 = (); # [0] - block contents; [1] - column start; [2] - column end (needs to be eliminated at some point)
my $defaultcolstart = 20; # the first column length (taxa+whitespace), used various places, should be nuked
# i/o
my @fileroot = ("blkd", "mx", "sl", "stm", "bpf"); # path and filename roots for the various interleave output in out_mx
my %out_modes = ( # not implemented - future global "formating" reference
"raw", 0,
"nexus", 1,
"phylip", 2,
"fasta", 3, # not implemented
"tnt", 4,
"mrbayes", 5,
"phase", 6,
"poy", 7,
"inaase", 8,
"css", 9,
"xml", 10 #not implemented
);
# alphabet obj?
#0 1 2 3 4 5 6 7
my @rna = qw(A U G C N - ? o); #(index order is important-> should be a hash likely) - double check this on reporting - is N out of place?
my @iupac = qw(A C T G U R Y S W K M B D H V N \. \-); # used as a regex to search against, thus \. \- (merge to IUPAC object)
my %basepairs = ( # hash of all possible base-pair combinations for aucg?-no, where o is other
"aa", 0, "au", 1, "ag", 2, "ac", 3, "an", 4, "a-", 5, "a?", 6, "ao", 7,
"ua", 8, "uu", 9, "ug", 10, "uc", 11, "un", 12, "u-", 13, "u?", 14, "uo", 15,
"ga", 16, "gu", 17, "gg", 18, "gc", 19, "gn", 20, "g-", 21, "g?", 22, "go", 23,
"ca", 24, "cu", 25, "cg", 26, "cc", 27, "cn", 28, "c-", 29, "c?", 30, "co", 31,
"na", 32, "nu", 33, "ng", 34, "nc", 35, "nn", 36, "n-", 37, "n?", 38, "no", 39,
"-a", 40, "-u", 41, "-g", 42, "-c", 43, "-n", 44, "--", 45, "-?", 46, "-o", 47,
"?a", 48, "?u", 49, "?g", 50, "?c", 51, "?n", 52, "?-", 53, "??", 54, "?o", 55,
"oa", 56, "ou", 57, "og", 58, "oc", 59, "on", 60, "o-", 61, "o?", 62, "oo", 63
);
# colum obj ultimately
my @ca;
# misc globals to clean up
my ($cur_block, $stemtot, $numpairsinstems, $totalstemlength ) = (0) x 4;
my $orig_interleave = interleave->new; # an interleave object for the original interleave -> sink to mx object ultimately
# ---------------------------------------------------------------------------------------------
sub calc_colstats () { # column stats algorithms
my $slice = shift;
$slice->describe;
my (@ccol); # reset the descriptive array
my ($a, $c, $g, $n, $u, $dash, $question, $other) = (0) x 8;
# build the columns (remove to seperate fn)
foreach my $x ($slice->loop("Blocks")) {
if ($nbd[$x][3] == 0) { # only examine bracketed blocks
for (my $p=0; $p<$nbd[$x][0]; $p++) { # loop through all positions in the string
foreach my $y ($slice->loop("Taxa")) { # loop through the taxa - remember smaller subsets may have all dashes, while larger don't
$mx[$y][$x][0] =~ /.{$p}(.)/; # get the character a position $p
$ccol[$nbd[$x][6]+$p] .= $1; # concat
}
}
}
}
foreach my $blk ($slice->loop("Blocks")) { # loop through the blocks matrix
if ($nbd[$blk][3]==0) { # only examine bracketed blocks
for (my $i = $nbd[$blk][6]; $i < $nbd[$blk][7]+1; $i++) { # loop through all positions in the string
$a = ($ccol[$i] =~ tr/A//);
$c = ($ccol[$i] =~ tr/C//);
$g = ($ccol[$i] =~ tr/G//);
$u = ($ccol[$i] =~ tr/T|U//);
$n = ($ccol[$i] =~ tr/N//);
$dash = ($ccol[$i] =~ tr/-//);
$question = ($ccol[$i] =~ tr/\?//);
$other = $slice->total("Taxa") - ($a+$c+$g+$n+$u+$dash+$question); # $t
#print " $other ";
#$sum = $other + $a+$c+$g+$t+$n+$u+$dash+$question;
#store values in a matrix for later use;
$ca[$i][0] = $a;
$ca[$i][1] = $u;
$ca[$i][2] = $g;
$ca[$i][3] = $c;
$ca[$i][4] = $n;
$ca[$i][5] = $dash;
$ca[$i][6] = $question;
$ca[$i][7] = $other;
for (my $z=0; $z<8; $z++) { # zero out undefs for stats purposes, there is probably a better way to do this
$ca[$i][$z] = 0 unless defined $ca[$i][$z];
}
#DEBUG
# print "$blk : $i : $ca[$i][0] $ca[$i][1] $ca[$i][2] $ca[$i][3] $ca[$i][4] $ca[$i][5] $ca[$i][6] $ca[$i][7] \n ";
}
}
}
}
sub calc_stem_bp_stats() {
my (
$slice,
$summary_mode # 0-include all taxa; 1-exclude "??" from calculations
) = @_;
#dependancies:
# prior call to stems_initialize
my ($stem1, $stem2, $mask, $curstem, $bpindex);
my $a = 0;
my @wrkstems;
my @tmp;
@bonds = (); # scope ok, reset to at each call
my $total_taxa = $slice->total("Taxa");
#bonds[non-prime block number][columposition][basepairindex][statistic], {statsitics: 0-total; 1- ?} note BP index is NOT the same as columnum!!!! has to be rebuilt because of stem expansion
foreach my $blk ($slice->loop("Blocks")) {
if (exists $stems{$blk}) {
push @wrkstems, $blk;
}
}
print "working stems: || @wrkstems |\n";
#could probably build $bonds as %bonds, but I'm not smart enought to do hashofhashofhash
foreach $curstem (@wrkstems) { # loop through block indicies
$bpindex = 0; # recalculated index
#loop through taxa
foreach my $t ($slice->loop("Taxa")) {
#get the raw data
@tmp = &str_aligned_stems($mx[$t][$curstem][0], $mx[$t][$stems{$curstem}][0], $nbd[$curstem][8], $nbd[$stems{$curstem}][8]);
$stem1 = $tmp[0];
$stem2 = $tmp[1];
$mask = $tmp[2];
#print "| $stem1, $stem2, $mask |\n";
$bpindex = 0; #recalculated index
for (my $p = 0; $p < length $mask; $p ++ ) { # loop through positions
#figure out the BP match
$mask =~ /.{$p}(.)/;
if ($1 eq '.') { # mark only bonded areas
}
else {
$stem1 =~ /.{$p}(.)/;
$a = $1;
$stem2 =~ /.{$p}(.)/;
$a .= $1;
$a = lc ($a); # perhaps not needed
# exchange non aucg-?n characters with 'o' ########
$a =~ s/(?)([^aucg\-\?n])/o/gi; #[check me]
# increment totals based on index
# print $a;
# print $basepairs{$a};
$bonds[$curstem][$bpindex][$basepairs{$a}][0]++; # note that not all values are initialized!
$bpindex++;
}
}
#print "bpindex: $bpindex\n";
}
}
# build percentage and other stats here; note: (#bonds[non-prime block number][columposition][basepairindex][statistic])
# calculate relative percentages of each base pair combination (all data mode)
foreach $curstem (@wrkstems) { #loop through block indicies
for (my $p=0; $p < $#{$bonds[$curstem]} + 1; $p++) { #loop through positions ------- $bonds[$blk][ END OF THIS INDEX] --------------
foreach $a (keys %basepairs) {
#DEBUG
my $qq_tot = 0;
if (defined $bonds[$curstem][$p][$basepairs{$a}][0]) { # only calculate % for those with > 0
if ($summary_mode == 1) {
$qq_tot = $bonds[$curstem][$p][$basepairs{'??'}][0] unless not defined $bonds[$curstem][$p][$basepairs{'??'}][0];
$bonds[$curstem][$p][$basepairs{$a}][1] = $bonds[$curstem][$p][$basepairs{$a}][0] / ($total_taxa - $qq_tot);
}
else {
$bonds[$curstem][$p][$basepairs{$a}][1] = $bonds[$curstem][$p][$basepairs{$a}][0] / ($total_taxa);
}
}
else {
$bonds[$curstem][$p][$basepairs{$a}][1] = 0;
}
}
}
}
# calculate covariation for each position/basepair
my $cvpercent = .03;
my $bpcomp1;
my $bpcomp2; #left and right strings of existing bps
my ($p1, $p2, $tmpcomp1, $tmpcomp2);
# for each stem
foreach $curstem (@wrkstems) { #loop through block indicies
#build the composition hash
for (my $p=0; $p < $#{$bonds[$curstem]} +1; $p++) { #plus 1 right here
$bpcomp1 = "";
$bpcomp2 = "";
#build "composition" strings
foreach $a (keys %basepairs) {
if ($bonds[$curstem][$p][$basepairs{$a}][1] >= $cvpercent) {
$a =~ /(.)(.)/;
$bpcomp1 .= $1;
$bpcomp2 .= $2;
}
}
# check each basepair combination (existing only?) versus bphash where presence > $cvpercent
foreach $a (keys %basepairs) {
$tmpcomp1 = $bpcomp1;
$tmpcomp2 = $bpcomp2;
if ($bonds[$curstem][$p][$basepairs{$a}][1] > $cvpercent) { # check this row
#print "$a\n";
$a =~ /(.)(.)/;
my $re3 = quotemeta($1);
my $re4 = quotemeta($2);
#print "A: (|$tmpcomp1|\t$c1\t$re3)\t\t(|$tmpcomp2|\t$c2\t$re4)\n";
$tmpcomp1 =~ s/$re3//gi;
$tmpcomp2 =~ s/$re4//gi;
$tmpcomp1 =~ s/\?|N|-|o//gi;
$tmpcomp2 =~ s/\?|N|-|o//gi;
#print "B: (|$tmpcomp1|\t$c1\t$re3)\t\t(|$tmpcomp2|\t$c2\t$re4)\n\n";
if ((length $tmpcomp1 > 0) and (length $tmpcomp2 > 0) ) {
#print "$curstem $a $p covarying!\n";
$bonds[$curstem][$p][$basepairs{$a}][2] = 1;
}
}
}
}
}
# debug
# foreach $curstem (@wrkstems) {
# print "| $curstem: $#{$bonds[$curstem]} \n";
# for (my $i=0; $i<9; $i++) { #print the first ith positions - will need to be changed to last index of $i, which = length
# for (my $j=0; $j<24; $j++) {
# print "$curstem";
# print " $j: ";
# if (defined $bonds[$curstem][$i][$j][0]) { print "$bonds[$curstem][$i][$j][0] "}
# else { print "undef "};
#
# }
# print "\n";
# }
# }
}
sub slice_calc_mx_totals () { # slice method ultimately?
# need to divide slice/non slice methods
my $slice = shift;
# returns this hash
my %tot = ( # available totals
"numbrakchars", 0, # number of bracketed characters
"numbrakblks", 0, # number of bracketed blocks
"inaaseblks", 0, # number of Inaase blocks -? what's this for?
"longestbrakblk", 0, # length longest bracketed block
"totchars", 0, # total columns
"numunbrakchars", 0 # number non-bracketed characters
);
foreach my $x ($slice->loop("Blocks")) {
if ($nbd[$x][3] == 1) { # note this doesn't include the and $nbd[$b][0] < $imaxblklen
$tot{"numbrakchars"} += $nbd[$x][0];
$tot{"numbrakblks"}++;
$tot{"inaaseblks"} = $tot{"inaaseblks"}."$nbd[$x][1]-$nbd[$x][2] "; # the complete string of inaase data
if ($tot{"longestbrakblk"} < $nbd[$x][0]) {$tot{"longestbrakblk"} = $nbd[$x][0] }
}
else {
$tot{"numunbrakchars"} += $nbd[$x][0];
}
}
$tot{"totchars"} = $tot{"numunbrakchars"} + $tot{"numbrakchars"};
return %tot;
sub slice_calc_stem_totals () {
my $slice = shift;
my %tot = (
"bdas", 0, #bdas = blocks defined as stems
"length_bdas", 0,
"num_non_prime_bdas", 0,
"num_prime_bdas", 0,
"num_complete_pairs", 0,
"num_complete_helices", 0
);
foreach my $blk ($slice->loop("Blocks")) {
if (grep ($_ == $blk, %stems)) {
$tot{"bdas"}++;
$tot{"length_bdas"} += $nbd[$blk][0];
}
if (grep ($_ == $blk, keys %stems)) {
$tot{"num_prime_bdas"}++;
if (grep ($_ == $stems{$blk}, values %stems)) {
$tot{"num_complete_pairs"} += ($nbd[$blk][8] =~ tr/\(//);
$tot{"num_complete_helices"}++;
}
}
if (grep ($_ == $blk, values %stems)) {
$tot{"num_non_prime_bdas"}++;
}
}
return %tot;
}
sub mx_colcompgraph () { # returns an array corresponding to percentage of a u g c n - ? other and a graph/logo of said composition
# dependencies: a prior call to &calc_colstats with containing block
my (
$slice,
$col, # column number
$length # graph length (in pixels/characters)
) = @_;
$length = $slice->total("Taxa") if $slice->total("Taxa") < $length;
my @out;
my $textgraph;
my @tmparray;
my @mar;
my $sta = Statistics::Descriptive::Sparse->new();
for (my $i=0; $i<8; $i++) { # why nine previously? should be 8??????
if (defined $ca[$col][$i]) { # this might need to be "if ( $ca[$col][$i] ) "
push @tmparray, $ca[$col][$i];
}
else {
push @tmparray, 0;
}
}
$sta -> add_data(@tmparray); #@{$ca[$col]}[0..8]);
my $sum = $sta->sum();
if ($sum==0) {die "sum is zero! - died in mx_colcompgraph on $col"}
my ($mores, $ones, $subtracts) = (0) x 3;
for ( my $i = 0; $i < 8; $i++) {
if (int(($ca[$col][$i] / $sum) * $length) == 0 and $ca[$col][$i] > 0) { # if there is a tiny percentage plot a least one
$out[$i] = 1;
$ones++;
}
elsif ($ca[$col][$i] == 0) {
$out[$i] = 0;
}
else {
$out[$i] = int(($ca[$col][$i]/$sum) * $length);
$mores += int(($ca[$col][$i]/$sum) * $length);
}
}
my $gh = Statistics::Descriptive::Full->new();
if ($mores+$ones-$length > 0) { #must fix length by pseudo subtracting
for (my $r = 0; $r < $mores+$ones-$length; $r++) {
$gh->add_data(@out);
$out[$gh->maxdex]--;
$gh=Statistics::Descriptive::Full->new();
}
}
elsif ($mores+$ones-$length < 0) { #must fix length by psuedo adding
for (my $r = 0; $r < abs($mores+$ones-$length); $r++) {
$gh->add_data(@out);
$out[$gh->maxdex]++;
$gh=Statistics::Descriptive::Full->new();
}
}
for (my $i=0; $i < 8; $i++) {
$textgraph .= ($rna[$i] x $out[$i]);
}
push @out, $textgraph; # just for fun, toss on another value (index is 8)
return @out;
}
sub tax_stats_bp_comp () {
# returns a two dimensional array of stats describing block composition for a given taxon/fragment
# [0 all data; 1 - bracketed only; 2- stems only; 3- non stems, non brackets]
# [0-a; 1-u; 2-g; 3-c; 4-other; 5-length]
my (
$tax_index,
$slice,
) = @_;
my @tmpstats;
my $fragment;
foreach my $blk ($slice->loop("Blocks")) { # all data
$fragment .= $mx[$tax_index][$blk][0];
}
@{$tmpstats[0]}[0..8] = &str_rnafragmentcomp($fragment);
#my %tmp = &str_unique_chars($fragment);
@{$tmpstats[0]}[9] = &str_unique_chars($fragment);
$fragment = "";
foreach my $blk ($slice->loop("Blocks")) { #bracketed only
if ($nbd[$blk][3] == 1) {
$fragment .= $mx[$tax_index][$blk][0];
}
}
@{$tmpstats[1]}[0..9] = &str_rnafragmentcomp($fragment);
#@{$tmpstats[3]}[9] = &str_unique_chars($fragment);
$fragment = "";
foreach my $blk ($slice->loop("Blocks")) { # helicies only
if (grep $_ eq $blk, %stems) {
$fragment .= $mx[$tax_index][$blk][0];
}
}
@{$tmpstats[2]}[0..9] = &str_rnafragmentcomp($fragment);
#@{$tmpstats[3]}[9] = &str_unique_chars($fragment);
$fragment = "";
foreach my $blk ($slice->loop("Blocks")) { #non-stems, non brackets
$fragment .= $mx[$tax_index][$blk][0] unless (grep $_ eq $blk, %stems);
}
@{$tmpstats[3]}[0..9] = &str_rnafragmentcomp($fragment);
#@{$tmpstats[3]}[9] = &str_unique_chars($fragment);
return @tmpstats;
}
sub out_column_stats () {
my $input = shift;
my $slice = shift;
my %params = @_;
my @tmpblks;
my $col_obj;
my $head_check = 1;
my @empty = ();
chdir($rootdir);
&io_confirmdir("analyses/stat/column/$input->{Modelroot}");
$params{'-exclude'} = \@empty if not defined $params{'-exclude'};
open (OUT, ">$input->{Modelroot}.txt") || die "couldn't open file output in out_column_stats - \n";
select (OUT);
print &str_file_header();
# make sure a global alphabet object is predefined (column object alphabet is local to object)
if ( not defined $params{'-alphabet'} ) {
my $mega;
foreach my $blk ($slice->loop("Blocks")) {
foreach my $tax ($slice->loop("Taxa")) {
$mega .= $mx[$tax][$blk][0];
}
}
my @alpha = &str_unique_chars($mega) ;
$params{'-alphabet'} = \@alpha;
print "[no alphabet was passed - using all chars present in slice: (", @{$params{'-alphabet'}}, ")]\n";
};
foreach my $blk ($slice->loop("Blocks")) {
next if $nbd[$blk][3] == 1;
foreach my $tax ($slice->loop("Taxa")) {
push @tmpblks, $mx[$tax][$blk][0];
}
my $col_obj = columns->new(@tmpblks);
if ($head_check == 1) {
$head_check = 0;
$col_obj->table(
%params,
'-offset' => $nbd[$blk][6],
'-header' => 'true'
);
}
else{
$col_obj->table(
%params,
'-offset' => $nbd[$blk][6],
'-header' => 'false' # overrides %params!
);
}
@tmpblks=();
print "\n" if $params{'-blockgaps'} eq 'true';
}
select (*STDOUT);
close OUT;
print "\nstat results saved at analyses/stat/column/",$input->{Modelroot},"/",$input->{Modelroot},".txt \n";
}
sub out_block_stats () {
my (
$input,
$slice,
$char # a regex expression, the count/stats of which will be returned
) = @_;
die "must provide a regex for out_block_stats\n" if $char eq "";
my %count;
my $c;
chdir($rootdir);
&io_confirmdir("analyses/stat/block/$input->{Modelroot}");
open (STAT, ">$input->{Modelroot}.txt") || die "couldn't open nexus file output too in out_stat - \n";
print STAT "stat regex: $char\n\n";
foreach my $tax ($slice->loop("Taxa")) {
print STAT "$ters{$tax}\t";
foreach my $blk ($slice->loop("Blocks")) {
$c = ( $mx[$tax][$blk][0] =~ s/$char//gi);
$c = 0 if not $c;
print STAT $c, "\t";
push @{$count{$blk}} , $c;
}
print STAT "\n";
}
my $sta_out;
foreach my $blk ($slice->loop("Blocks")) {
my $sta = Statistics::Descriptive::Sparse->new();
$sta -> add_data(@{$count{$blk}});
$sta_out->{"min"}->{$blk} = $sta->min();
$sta_out->{"max"}->{$blk} = $sta->max();
$sta_out->{"sum"}->{$blk} = $sta->sum();
$sta_out->{"mean"}->{$blk} = $sta->mean();
$sta_out->{"std_dev"}->{$blk} = $sta->standard_deviation();
$sta_out->{"var"}->{$blk} = $sta->variance();
}
my @report_order = qw(min max sum mean std_dev var); # so the stats reports in a standard order
print STAT "\n";
foreach my $key (@report_order) {
print STAT "$key\t";
foreach my $blk ($slice->loop("Blocks")) {
print STAT $sta_out->{$key}->{$blk};
print STAT "\t";
}
print STAT "\n";
}
close STAT;
print "\nout_stat results saved at analyses/stat/block/",$input->{Modelroot},".txt \n";
}
sub out_slice_stats () { # acts the same as blockstats, but assumes the slice represents one block
my (
$input,
$slice,
$char, # a regex expression, the count/stats of which will be returned
$mode # 0 stats only; 1- include merged slice column
) = @_;
die "must provide a regex for out_block_stats\n" if $char eq "";
my @count;
my $c;
my %merged;
foreach my $tax ($slice->loop("Taxa")) {
foreach my $blk ($slice->loop("Blocks")) {
$merged{$tax} .= $mx[$tax][$blk][0];
}
}
chdir($rootdir);
&io_confirmdir("analyses/stat/slice/$input->{Modelroot}");
open (STAT, ">$input->{Modelroot}.txt") || die "couldn't open nexus file output too in out_stat - \n";
print STAT "stat regex: $char\n\n";
foreach my $tax ($slice->loop("Taxa")) {
print STAT "$ters{$tax}\t";
$c = ( $merged{$tax} =~ s/$char//gi);
$c = 0 if not $c;
print STAT $c, "\t";
print STAT "\t$merged{$tax}" if $mode == 1;
push @count , $c;
print STAT "\n";
}
my $sta_out;
my $sta = Statistics::Descriptive::Sparse->new();
$sta -> add_data(@count);
$sta_out->{"min"} = $sta->min();
$sta_out->{"max"} = $sta->max();
$sta_out->{"sum"} = $sta->sum();
$sta_out->{"mean"} = $sta->mean();
$sta_out->{"std_dev"} = $sta->standard_deviation();
$sta_out->{"var"} = $sta->variance();
my @report_order = qw(min max sum mean std_dev var); # so the stats reports in a standard order
print STAT "\n";
foreach my $key (@report_order) {
print STAT "$key\t$sta_out->{$key}\n";
}
close STAT;
print "\nout_stat results saved at analyses/stat/slice/",$input->{Modelroot},".txt \n";
}
sub out_web () {
my $input = shift; # an input object
my (
$slice, # restrict output to this slice
$mode, # see intlv_working
$size, # see intlv_build, not required for mode == 1
$interleave, # model interleave, if not included is based on $orig_intlv ** will need to modify ultimately to explicitly pass
) = @_;
$slice->describe;
my $working_interleave;
if (defined $interleave) {
$working_interleave = $interleave->Jrna::intlv_working($slice, $mode, $size);
}
else {
$working_interleave = $orig_interleave->Jrna::intlv_working($slice, $mode, $size);
}
$working_interleave->describe;
my $colcounttmp = 1;
#directory functions - confirmat that they exist
chdir($rootdir);
&io_confirmdir("models/$input->{Modelroot}");
foreach my $path ( qw(blkd bpf mx sl stm) ) {
chdir($rootdir);
&io_confirmdir("models/$input->{Modelroot}/$path");
}
chdir($rootdir);
&calc_colstats($slice);
print "\ncolumn stats computed\n";
print "stem base-pair frequencies computed\n";
print "\ngenerating html\n";
#interleave overview
open (ILVOOUT, ">models/$input->{Modelroot}/interleave_index.html") || die " - can't open interleave overview file - \n";
&html_head (*ILVOOUT, "model - interleave overview", "../../style/model_index.css", "../../style/nav.css" );
print &descr_mx(*ILVOOUT, $input, $working_interleave,$slice, 1);
&html_foot (*ILVOOUT);
close ILVOOUT;
my $count =1;
foreach my $i ($working_interleave->interleaves) {
print "writing web_out - interleave $i, $count of ", $working_interleave->total_interleaves, "\n";
$count++;
# generate a slice object to pass to each descr_
my $curslice = slice->new;
$curslice->blocks($working_interleave->blocks($i));
$curslice->taxa(keys %{$slice->taxa});
## bpf
open (BPFOUT, ">models/$input->{Modelroot}/bpf/bpf$i.html") || die " - can't open stm file - \n";
&descr_blkheader(*BPFOUT, $i);
&html_head (*BPFOUT, "Stems - interleave $i, $deschead[$i][1]", "../../../style/bpf_style.css", "../../../style/nav.css");
&out_web_nav (*BPFOUT, $working_interleave, $i, 4);
&descr_stembpfreq (*BPFOUT, $curslice, 1, 1);
&out_web_nav (*BPFOUT, $working_interleave, $i, 4);
&html_foot (*BPFOUT);
close BPFOUT;
## stems
open (STEMOUT, ">models/$input->{Modelroot}/stm/stm$i.html") || die " - can't open stm file - \n";
&html_head (*STEMOUT, "Stems - interleave $i, $deschead[$i][1]", "../../../style/stm_style.css", "../../../style/base_style.css", "../../../style/nav.css");
&descr_blkheader(*STEMOUT, $i);
&out_web_nav (*STEMOUT, $working_interleave, $i, 3);
&descr_stems(*STEMOUT, 1, $curslice, $i, $working_interleave);
&out_web_nav (*STEMOUT, $working_interleave, $i, 3);
&html_foot (*STEMOUT);
close STEMOUT;
## matrix
open (MXBLKOUT, ">models/$input->{Modelroot}/mx/mx$i.html") || die " - can't open mx file - \n";
&html_head (*MXBLKOUT, "Matrix data - interleave $i, $deschead[$i][1]", "../../../style/jrnastyle.css","../../../style/nav.css");
&descr_blkheader(*MXBLKOUT, $i);
&out_web_nav (*MXBLKOUT, $working_interleave, $i, 1);
&out_mx (*MXBLKOUT, $curslice, 3, 0, 1, 1, 1, " ", 0, 2, 2, $defaultcolstart, 2, 0, 0);
&out_web_nav (*MXBLKOUT, $working_interleave, $i, 1);
&html_foot (*MXBLKOUT);
close MXBLKOUT;
## sequence lines
open (SLOUT, ">models/$input->{Modelroot}/sl/sl$i.html") || die " - can't open sl file - \n";
&html_head (*SLOUT, "Column composition - interleave $i, $deschead[$i][1]", "../../../style/sl_style.css", "../../../style/nav.css");
&descr_blkheader(*SLOUT, $i);
&out_web_nav (*SLOUT, $working_interleave, $i, 2);
$colcounttmp = &descr_seqline (*SLOUT, $curslice, 1, 40, 10, 1, $colcounttmp); # doesn't number on zero mode correctly $intlvdesc[$i][0],
#print "colcount: $colcounttmp\n";
&out_web_nav (*SLOUT, $working_interleave, $i, 2);
&html_foot (*SLOUT);
close SLOUT;
# block descriptions
open (MXBLKDESCOUT, ">models/$input->{Modelroot}/blkd/blkd$i.html") || die " - can't open blkd file - \n";
#multiple calls!
&html_head (*MXBLKDESCOUT, "Block description - interleave $i, $deschead[$i][1]", "../../../style/blkd_style.css","../../../style/nav.css");
&descr_blkheader(*MXBLKDESCOUT, $i);
&out_web_nav (*MXBLKDESCOUT, $working_interleave, $i, 0);
foreach my $blk ($curslice->loop("Blocks")) {
&descr_blkstats (*MXBLKDESCOUT, $blk, $curslice, $working_interleave, 1); #may need to doublecheck
}
&out_web_nav (*MXBLKDESCOUT, $working_interleave, $i, 0);
&html_foot (*MXBLKDESCOUT);
close MXBLKDESCOUT;
#print "done\n";
}
sub out_web_nav {
local (*FH) = shift;
my ($interleave, $intlv, $pos) = @_;
print FH '<div class="nav">';
print FH &html_blknav($interleave, $intlv, $pos, 0);
print FH &html_blknavlink( $intlv, $pos, 1, "", 0);
print FH '</div>';
print FH "\n";
}
}
sub str_nuc_at_stem_pos () {
# return the bp's at the nth stem positio in given the two helical pairs
my (
$pos,
$helix,
$mask
) = @_;
my @stempositions;
my $nuc;
if ($mask =~ /\)/g) {
$helix = scalar reverse $helix;
$mask = scalar reverse $mask;
#print $mask,$helix;
@stempositions = &str_pos_offsets('\)', $mask)
}
else {
@stempositions = &str_pos_offsets('\(', $mask)
}
return (substr($helix, $stempositions[$pos], 1), $stempositions[$pos])
}
# interleave package methods to be ported to interleave package
sub intlv_numseq () { # method for an interleave object; returns a string that column-numbers a given range of blocks or columns for a given interleave object
# requires:
my $self = shift;
my (
$interleave, # specific interleave to number
$mode, # numbering mode: 0 - number individual columns; 1- number blocks
$format, # string output format: 0 - normal; 1- ??; 3 - css; 4 - colored columns;
$consecutive, # 0 - renumber consecutively; 1 - number according to original format - NOT IMPLEMENTED
$gapped, # gapped (insert gaps between description blocks)
$gapchar, # gapchar (used to be gapsize)
$bracketed, # bracketed - inlcude or execlude bracketed blocks
$startcol
) = @_;
my ($numrows, $longest) = (0) x 2;
my ($t, $outstring);
if ($mode == 0) {
foreach my $blk ($self->blocks($interleave)) {
$longest = $nbd[$blk][2] if $nbd[$blk][3]==0;
}
}
elsif ($mode == 1) {
$longest = $self->last_block_in_interleave($interleave);
}
else { die "illegal mode [$mode] in intlv_numseq"}
$numrows = length("$longest");
if ($consecutive == 0) {
}
elsif ($consecutive == 1) {
for (my $i=0; $i < $numrows; $i++) { # print this many rows of data (for sequence numbering or color composition blocks
$outstring .= &css_tagswitch($format, 0, 0, "numrow");
$outstring .= &css_tagswitch($format, 1, 0, "rightholder");
#print &str_padright("["," ", $startcol); #put $i. to check number of rows
$outstring = $outstring.&str_padright("["," ", $startcol);
$outstring .= &css_tagswitch($format, 1, 1, "rightholder");
$outstring .= &css_tagswitch($format, 1, 0, "colnums");
foreach my $blk ($self->blocks($interleave)) {
if ($mode == 0) {
if ($nbd[$blk][3]==0) { #number or color code this block this block
for (my $k = $nbd[$blk][6]; $k < $nbd[$blk][7]+1 ; $k++) { #using unbracketed blocks only now, number individual positions
#if mode is standard (0,3)
$t = &str_padleft($k,"0",$numrows);
$t =~ /.{$i}(.)/; # there's likely a better way to choose either the ith character or print a zero.
#print $1;
$outstring = $outstring.$1;
# if mode is color columns (4)
# print a spanned space with color according to r
# if mode is links (5)
}
if ($gapped == 1) {
$outstring = $outstring.$gapchar
}
}
elsif ($nbd[$blk][3]==1 and $bracketed == 1) { #gap this block
$outstring = $outstring.&str_padright("[", "-", $nbd[$blk][0]+1); #+1 is correct!!!
#print ']';
$outstring = $outstring.']';
if ($gapped == 1) {
#print &str_padright(" "," ",$gapsize-1);
$outstring = $outstring.$gapchar #&str_padright(" "," ",$gapsize-1);
}
}
else {
} #do nothing
}
elsif ($mode ==1) { # number blocks by their original position
$t = &str_padleft($blk, "0" , $numrows);
$t =~ /.{$i}(.)/;
my $t1 = &str_padleft("$1"," ", $nbd[$blk][0]);
if ($nbd[$blk][3]==0) {
$outstring = $outstring.$t1;
if ($gapped == 1) {
$outstring = $outstring.$gapchar;
}
}
elsif ($nbd[$blk][3]==1 and $bracketed == 1) { # also bracket this block to indicate original bracketing
$outstring = $outstring."[$t1]";
if ($gapped == 1) {
$outstring = $outstring.$gapchar
}
}
else {
} #do nothing
}
}
#print "]\n";
$outstring = $outstring."]";
$outstring .= &css_tagswitch($format, 1, 1, "colnums");
$outstring .= &css_tagswitch($format, 0, 1, "numrow");
$outstring .= "\n";
}
}
return $outstring;
}
sub slice_stems { # a slice method that returns a list of blocks to exclude given a certain criterion
my $self = shift;
my $mode = shift;
my @excludedblks;
#pre-parse
if ($mode == 1) { # include only helices
foreach my $blk (keys %{$self->blocks}) {
push (@excludedblks, $blk) unless grep ($_ == $blk, %stems);
}
}
elsif ($mode == 2) { # exclude all non-helices
foreach my $blk (keys %{$self->blocks}) {
push (@excludedblks, $blk) unless not grep ($_ == $blk, %stems);
}
}
elsif ($mode == 3) { # exclude everyting but 5' strands
foreach my $blk (keys %{$self->blocks}) {
push (@excludedblks, $blk) unless grep ($_ == $blk, keys %stems);
}
}
elsif ($mode == 4) { # exclude everyting but 3' strands
foreach my $blk (keys %{$self->blocks}) {
push (@excludedblks, $blk) unless grep($_ == $blk, values %stems);
}
}
elsif ($mode == 5) { # exclude everything but a random 1/2 of the stems <--- NOT WORKING
}
return @excludedblks;
}
sub descr_taxa () {
my (
$slice
) = @_;
foreach my $t ($slice->loop("Taxa")) {