-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsensusStems.pl
1506 lines (1442 loc) · 47 KB
/
ConsensusStems.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
#script that implements naive version of DBSCAN to explore clustering of stems
#some functions borrowed from detect_regions_stem
#currently not forming stems til after clustering
#input: Rfam family name
#output: consensus stems
#run from Cluster-graph/consensus-scripts
use strict;
use Common;
use POSIX;
if (scalar(@ARGV) < 1) {
manpage();
} else {
my $args = process_args(@ARGV);
my $verbose = $args->{"verbose"};
my $type = "Sfold";
my $names = run_sfold_profiling($args);
#if you've already run Sfold and don't want to rerun every time, comment above and uncomment next two lines
# my @names = map {get_name($_)} @{$args->{"seqs"}};
# my $names = \@names;
my $newclus = 0;
my $feats;
my $repeat = 1;
my $found = {};
my $extend = {};
while ($repeat) {
my $data = process_profiling($args->{"output"},$names,$extend);
$feats = $data->[0];
my $lengths = $data->[1];
my $cells = process_trip_feat($feats,$lengths,$found,$verbose);
my $eps = find_eps($lengths,$verbose);
my $clusters = cluster($cells,$eps,$verbose);
my $clusstems = print_clusters($clusters->[0],$cells,$feats,$verbose);
$newclus = find_centroid($clusstems->[0],$lengths,$data->[2],$verbose);
# print_clus_stats($newclus);
improve_cluster($newclus,$data,$args,$clusstems->[1],$clusters->[1]);
if (found_newclus($found,$newclus,$feats,$verbose)) {
$found = get_truepos($newclus,$feats);
# print_clus_stats($newclus);
$extend = resample($found,$args,$feats);
print "Resampling:\n" if ($verbose);
} else {
$repeat = 0;
}
}
print "FINAL CLUSTERS:\n";
# print_clus_stats($newclus);
print_clus($newclus);
print_clus_to_file($newclus,$args);
}
#usage instructions
sub manpage {
print "ConsensusStems code for Academic Users, Version 1.0 (April 2018)\n\n";
print "Usage: perl ConsensusStems.pl [options]... directory_of_sequences\n\n";
print "Required: perl, Boltzmann sampling with constraints, RNA profiling, a MFE prediction program\n";
print "\tBoltzmann sampling with constraints is implemented by Sfold, which can be requested from http://sfold.wadsworth.org/cgi-bin/index.pl\n";
print "\tRNA profiling is available at https://github.com/gtfold/RNAStructProfiling\n";
print "\tA recommended MFE prediction program is GTfold's gtmfe option, available at https://github.com/gtfold/gtfold\n\n";
print "Input a directory containing all sequences (in FASTA format) to be processed.\n\tRecommended: at least 8 related sequences.\n\tConsensusStems is not designed for nor tested with less than 8 sequences\n\n";
print "Final output with clusters written to [output directory]/ConsensusStems_output.txt\n";
print "Options:\n";
print "\t-s <path>\t Path to invoke Sfold, eg -s /usr/bin/sfold (default)\n";
print "\t-p <path>\t Path to invoke RNAprofiling, eg -p ../RNAprofiling (default is ./RNAprofile)\n";
print "\t-m <path>\t Path to invoke MFE program, eg -m /usr/bin/gtmfe (default)\n";
print "\t-d <name>\t Path to MFE NNTM parameters eg -d Desktop/data/Turner99 (default is ./data)\n";
print "\t-o <path>\t Path to store output files, eg -o ../output (default is ./output)\n";
print "\t-f <name>\t Family name, eg -f tRNA (default is last directory name if exists or RNA)\n";
print "\t-v \t Flag to enable verbose output concerning details of the method\n";
}
sub process_args {
my @args = @_;
my $verbose = 0;
my $arghash = initialize();
my $dir = pop(@args);
$arghash->{"seqs"} = process_seqs($dir);
while (scalar(@args) > 0) {
process_options($arghash,\@args);
}
if (!exists $arghash->{"family"}) {
my @dirs = split(/\//,$dir);
if (scalar(@dirs)>1) {
$arghash->{"family"} = pop(@dirs);
} else {
$arghash->{"family"} = "RNA";
}
}
if ($verbose) {
print "Sfold path is ",$arghash->{"sfold"},"\n";
print "Prof path is ",$arghash->{"prof"},"\n";
print "MFE path is ",$arghash->{"mfe"},"\n";
print "output path is ",$arghash->{"output"},"\n";
print "family is ",$arghash->{"family"},"\n";
}
return $arghash;
}
sub initialize {
my %args;
$args{"sfold"} = "/usr/bin/sfold";
$args{"mfe"} = "/usr/bin/gtmfe";
$args{"prof"} = "./RNAprofile";
# $args{"family"} = "RNA";
$args{"output"} = "./output/";
$args{"verbose"} = 0;
return \%args;
}
sub process_seqs {
my $dir = shift;
my @dir = split(//,$dir);
# print "Found dir ",@dir," with last char $dir[-1]\n";
$dir = "$dir"."/" if ($dir[-1] ne '/');
my $files = `ls $dir | wc -l`;
print "Warning! Found less than eight files!\n" if ($files < 8);
my @files = `ls $dir`;
my @newfiles;
foreach my $file (@files) {#one file per line assumed
chomp($file);
$file = "$dir" . "$file";
open SEQ, "$file" or die "cannot open $file\n";
my $first = 1;
foreach (<SEQ>) {
die "File $file is not in FASTA format\n" if ($first && !/^\>/);
last;
}
push(@newfiles,$file);
}
return \@newfiles;
}
sub process_options {
my ($arghash,$args) = @_;
while ($args->[0] ne "-s" && $args->[0] ne "-p" && $args->[0] ne "-f" && $args->[0] ne "-o" && $args->[0] ne "-m" && $args->[0] ne "-d" && $args->[0] ne "-v") {
shift(@$args);
return if (scalar(@$args)<1);
}
if ($args->[0] eq "-s") {
$arghash->{"sfold"} = $args->[1];
splice(@$args,0,2);
} elsif ($args->[0] eq "-p") {
$arghash->{"prof"} = $args->[1];
splice(@$args,0,2);
} elsif ($args->[0] eq "-m") {
$arghash->{"mfe"} = $args->[1];
splice(@$args,0,2);
} elsif ($args->[0] eq "-d") {
$arghash->{"paramdir"} = $args->[1];
splice(@$args,0,2);
} elsif ($args->[0] eq "-f") {
$arghash->{"family"} = $args->[1];
splice(@$args,0,2);
} elsif ($args->[0] eq "-v") {
$arghash->{"verbose"} = 1;
shift(@$args);
} elsif ($args->[0] eq "-o") {
my @out = split(//,$args->[1]);
$args->[1] = "$args->[1]" . "/" if ($out[-1] ne '/');
$arghash->{"output"} = $args->[1];
splice(@$args,0,2);
}
}
#runs profiling on sfold outputs of fam
sub run_sfold_profiling {
my ($args) = @_;
my $fam = $args->{"family"};
my $seqs = $args->{"seqs"};
my $spath = $args->{"sfold"};
my $ppath = $args->{"prof"};
my $output = $args->{"output"};
`mkdir $output`;
my @names=();
foreach my $seqfile (@$seqs) {
my $name = get_name($seqfile);
push(@names,$name);
my $profile = "$output"."$name.out";
my $outdir = "$output"."Sfold"."_$name";
print "Running Sfold on $seqfile\n";
`$spath -o $outdir $seqfile`;
my $outfile = $outdir."/sample_1000.out";
`$ppath -sfold $outfile -g -v $seqfile > $profile`;
# last;
}
# $args->{"seqs"} = \@names;
return \@names;
}
sub get_name {
my $seq = shift;
my @dirs = split(/\//,$seq);
$seq = $dirs[-1];
$seq = $1 if ($seq =~ /(\S+)\.txt/);
$seq = $1 if ($seq =~ /(\S+)\.fa/);
$seq = $1 if ($seq =~ /(\S+)\.fasta/);
$seq = $1 if ($seq =~ /(\S+)\.seq/);
return $seq;
}
#grabs feature triplet info and puts into hash
#processes input profiling file based on type:
# 0 = gtboltzmann input, 1 = sfold input, 2 = resample input
sub process_profiling {
my ($outdir,$names,$extension) = @_;
my %feats;
my %lengths;
my %nucl;
my %files;
foreach my $seq (@$names) {
my $extend = ".out";
if (exists $extension->{$seq}) {$extend = "_resample.out";}
my $file = "$outdir"."$seq"."$extend";
my @feat = ();
open OUT, "<$file" or die "cannot open $file\n";
foreach (<OUT>) {
if (/Featured helix (\d+)\: (\d+ \d+ \d+) with freq (\d+)/) {
# $feats{$1} = $2;
$feat[$1] = $2;
} elsif (/seq in .+ is ([UCAGTucagt]+) with length (\d+)/) {
$nucl{$seq} = [0,split(//,$1)];
$lengths{$seq} = $2;
}
}
close(OUT);
$feats{$seq} = \@feat;
$files{$seq} = $file;
}
return [\%feats,\%lengths,\%nucl,\%files];
}
#compare against already found features in %found; add if not present
sub process_trip_feat {
my ($feats,$lengths,$found,$verbose) = @_;
my $median = median([values %$lengths]);
my %featcells;
# my @heatmap = (0) x $median;
foreach my $seq (keys %$feats) {
print "processing $seq\n" if ($verbose);
my $myfeats = $feats->{$seq};
my $length = $lengths->{$seq};
my @coords = ();
if (exists $found->{$seq}) {
@coords = @{$found->{$seq}};
}
for (my $feat = 1; $feat<scalar(@$myfeats);$feat++) {
my $featlabel = "$seq $feat";
my $coord = $myfeats->[$feat];
remove_char(\@coords,$coord);
add_feature($coord,$median,$length,\%featcells,$featlabel);
}
#add those features from before that might not have made it through resampling
if (scalar(@coords)) {
my $k = scalar(@$myfeats);
foreach my $coord (@coords) {
add_feature($coord,$median,$length,\%featcells, "$seq $k");
push(@$myfeats,$coord);
print "Adding $seq ($k) $coord\n" if ($verbose);
$k++;
}
}
}
# print "Found seqs for cells ",keys %cells,"\n";
return \%featcells;
}
#takes coordinates and adds to featcells
sub add_feature {
my ($coord,$median,$length,$featcells,$featlabel) = @_;
my @trip = split(/\s/,$coord);
my $i = ceil($trip[0]*$median/$length);
my $j = ceil($trip[1]*$median/$length);
# print "processing $label $feat @trip -> ($i,$j)\n";
for (my $k = 0; $k < $trip[2]; $k++) {
add($featcells,"$i $j",$featlabel);
$i++;
$j--;
}
}
#finds an appropriate radius eps to use in dbscan
#based on density of lengths; need to adjust for normalization
#add median to lengths
sub find_eps {
my ($lengths,$verbose) = @_;
my $median = median([values %$lengths]);
my $n = scalar(keys %$lengths);
my $eps = 0;
my $minpts = scalar(keys %$lengths)/4;
my %vals = map {$_ => ceil(abs($median-$lengths->{$_})*$median/$lengths->{$_})} keys %$lengths;
my %ltoseq;
foreach (keys %$lengths) {
my $coord = ceil(abs($median-$lengths->{$_})*$median/$lengths->{$_});
add(\%ltoseq,$coord,$_);
}
my $noise = $n;
my $label; #label{length} = cluster
while ($noise > $n/2) {
$label = DBscan(\%ltoseq,$eps,$minpts,"length");
my @noise = map {$label->{$_} == 0 ? ($_):() } keys %$label;
my @n = map {scalar(@{$ltoseq{$_}}) } @noise;
$noise = sum(\@n);
print "found eps $eps with $noise seq as noise\n" if ($verbose);
$eps++;
}
print_eps($label,\%ltoseq);
$lengths->{"median"} = $median;
$eps = 2*($eps-1); #preparing to be used for 2D, so double
return [$eps,$minpts];
}
sub print_eps {
my ($label,$ltoseq) = @_;
my %clus;
foreach my $l (keys %$label) {
add(\%clus,$label->{$l},$l);
}
foreach my $k (keys %clus) {
my $lengths = $clus{$k};
# print "For cluster $k: ";
foreach my $len (@$lengths) {
my $seqs = $ltoseq->{$len};
# print "@$seqs ($len), ";
}
}
}
#clusters the cells, given the eps found from length densities
#if it produces over half of the points as noise, lower minpts
sub cluster {
my ($cells,$params,$verbose) = @_;
my $eps = $params->[0];
my $minpts = $params->[1];
my $n = scalar(keys %$cells);
my $noise = $n;
my $label;
while ($noise > $n/2) {
if ($minpts < 3) {
print "minpts below critical level of 3; raising eps to ",$eps+1,"\n" if ($verbose);
$minpts = $params->[1];
$eps = 3;
}
$label = DBscan($cells,$eps,$minpts,"coord");
my @noise = map {$label->{$_} == 0 ? ($_):() } keys %$label;
my @n = map {scalar(@{$cells->{$_}}) } @noise;
$noise = sum(\@n);
print "found minpts $minpts with $noise points as noise out of $n\n" if ($verbose);
$minpts--;
}
$minpts++;
return [$label,$minpts];
}
#$ltoseq{pt} = [members], $label{pt} = cluster
sub DBscan {
my ($ltoseq,$eps,$minpts,$type,$maxradius) = @_;
my $C = 0;
my %label;
my @sorted = sort {scalar(@{$ltoseq->{$b}}) <=> scalar(@{$ltoseq->{$a}}) } keys %$ltoseq;
foreach my $p (@sorted) {
#print "considering seq $_\n";
next if (exists $label{$p});
my $neighbors;
$neighbors = rangeQuery($p,$eps,$ltoseq) if ($type eq "length");
$neighbors = rangeQuery2($p,$eps,$ltoseq) if ($type eq "coord");
if ($neighbors->[1] < $minpts) {
$label{$p} = 0;
next;
}
$C++;
$label{$p} = $C;
my @set = @{$neighbors->[0]};
next if (!scalar(@set)); #if all neighbors are same point, no extra neighbors to process
foreach my $pt (@set) {
# print "checking point $pt\n";
if (exists $label{$pt}) {
$label{$pt} = $C if (!$label{$pt});
next;
}
$label{$pt} = $C;
$neighbors = rangeQuery($pt,$eps,$ltoseq) if ($type eq "length");
$neighbors = rangeQuery2($pt,$eps,$ltoseq) if ($type eq "coord");
push(@set,@{$neighbors->[0]}) if ($neighbors->[1] >= $minpts);
}
}
# print "Total num clusters: $C\n";
return \%label;
}
#finds all lengths within eps of val, adds to neighbors
#one dimensional
sub rangeQuery {
my ($val,$eps,$ltoseq) = @_;
my $numneigh = scalar(@{$ltoseq->{$val}});
my @neighbors = ();
for (my $i = 1; $i <= $eps; $i++) {
my $v = $val+$i;
if (exists $ltoseq->{$v}) {
$numneigh += scalar(@{$ltoseq->{$v}});
push(@neighbors,$v);
}
$v = $val - $i;
if (exists $ltoseq->{$v}) {
$numneigh += scalar(@{$ltoseq->{$v}});
push(@neighbors,$v);
}
}
# print "found for $val $numneigh neighbors: @neighbors\n";
return [\@neighbors,$numneigh];
}
#version of rangeQuery that's two dimensional
#also, we count the number of seqs represented not just points
sub rangeQuery2 {
my ($val,$eps,$ltoseq) = @_;
my %found = map {(split(/\s/,$_))[0] => 1} @{$ltoseq->{$val}};
# my $numneigh = scalar(@{$ltoseq->{$val}});
my @neighbors = ();
my @val = split(/\s/,$val);
for (my $x = $eps; $x >= $eps*-1; $x--) {
my $i = $val[0]+$x;
for (my $y = $eps-$x; $y >= $x-$eps; $y--) {
next if (!$x && !$y); #already processed original cell
my $j = $val[1]+$y;
my $v = "$i $j";
if (exists $ltoseq->{$v}) {
# $numneigh += scalar(@{$ltoseq->{$v}});
map {$found{(split(/\s/,$_))[0]}++} @{$ltoseq->{$v}};
push(@neighbors,$v);
}
}
}
my $numneigh = scalar(keys %found);
# print "found for $val $numneigh neighbors: @neighbors\n";
return [\@neighbors,$numneigh];
}
#takes the labels and prints them for matlab scatter plot
sub scatterplot_cluster {
my ($label,$fam) = @_;
my $file = "../../Matlab/data/dbscan_plot_$fam".".txt";
open OUT,">$file" or die "cannot open $file\n";
foreach (keys %$label) {
print OUT "$_ $label->{$_}\n";
}
close(OUT);
return;
}
sub scatterplot_cells {
my ($cells,$fam) = @_;
my $file = "../../Matlab/data/dbscan_cells_$fam".".txt";
open OUT,">$file" or die "cannot open $file\n";
foreach (keys %$cells) {
print OUT "$_ ",scalar(@{$cells->{$_}}),"\n";
}
close(OUT);
return;
}
#prints the stems making up each cluster
#returns an array indexed by cluster num, containing the stems in each cluster
sub print_clusters {
my ($label,$cells,$stems,$verbose) = @_;
my %clus;
my %found;
foreach my $cell (keys %$label) { #gives info by every cluster
add(\%clus,$label->{$cell},$cell);
}
my @clusters;
my @cluscells;
foreach my $clust (sort {$a <=> $b} keys %clus) {
print "For cluster $clust: \n" if ($verbose);
my %allstems;
foreach my $cell (@{$clus{$clust}}) {
my $stms = $cells->{$cell};
map {$allstems{$_}++} @$stms;
}
my @sorted = sort {$a cmp $b} keys %allstems;
my %myclus;
foreach my $stem (@sorted) { #for every stem in the cluster
my @stem = split(/\s/,$stem);
my $steminfo;
my $steminfoprint;
my $coords = $stems->{$stem[0]}[$stem[1]];
$steminfoprint = "($stem[1]) [$coords]";
$steminfo = "$stem[1] | $coords";
print "\t$stem[0] $steminfoprint\n" if ($verbose);
add(\%myclus,$stem[0],$steminfo);
$found{$stem} = $clust if ($clust);
}
# map {$found{$_} = $clust} @sorted if ($clust);
$clusters[$clust] = \%myclus;
}
return [\@clusters,\%found];
}
#combines all stems of a seq into a superstem
#finds the median coordinates of all seq stems
#variation: use normalized coords?
sub find_centroid {
my ($clusters,$lengths,$nucls,$verbose) = @_;
my @newclus;
my %hctoclus;
my $median = $lengths->{"median"};
for (my $clus = 0; $clus<scalar(@$clusters); $clus++) {
#skip if its the the noise cluster = 0
if (!$clus) {
$newclus[$clus] = $clusters->[$clus];
next;
}
print "For cluster $clus:\n" if ($verbose);
my $myclus = $clusters->[$clus];
my @sorted = sort {$a cmp $b} keys %$myclus;
my %newclus;
my %seqs;
my %constrain;
my @I;
my @J;
my @K;
my @L;
my @lengths;
my @longest;
my @mfe;
my @bpnums;
foreach my $seq (@sorted) {
my $stems = $myclus->{$seq};
my $nucl = $nucls->{$seq};
my @is = ();
my @js = ();
my $longest = [0,0,0];
my @feats = ();
my $bpnum = 0;
my $minHC = 0;
my $mincoords;
foreach my $stem (@$stems) {
my @stem = split(/ \| /,$stem);
my @coords = split(/\s/,$stem[1]);
if ($stem[0] < $minHC || !$minHC) {
$minHC = $stem[0];
$mincoords = \@coords;
}
my $k = scalar(@coords)-1; #allowing for both feat (i,j,k) and stem (i,j,k,l) coords
push(@is,($coords[0],$coords[0]+$coords[2]-1)); #push the boundaries, to calc k and l
push(@js,($coords[1],$coords[1]-$coords[$k]+1));
$bpnum += $coords[2]; #assuming using feats (i,j,k)
if ($longest->[2] < $coords[2]) {$longest = [@coords];}
elsif ($longest->[2] == $coords[2]) { push(@$longest,@coords);}
push(@feats,$stem[0]);
}
my ($i,$j,$k,$l) = @{find_stem_coords(\@is,\@js)};
push(@I,$i);
push(@J,$j);
push(@K,$k);
push(@L,$l);
push(@longest,$longest);
push(@lengths,$lengths->{$seq});
my $bounds = [$i,$i+$k-1,$j-$l+1,$j];
push(@bpnums,$bpnum);
$seqs{$seq} = [$i,$j,$k,$l];
$constrain{$seq} = \@feats;
print "\t$seq (@feats) [$i,$j,$k,$l] {$bpnum bp}\n" if ($verbose); #can print (L: @$longest) or $mfe->[1]
}
$newclus{"seqs"} = \%seqs;
$newclus{"constraints"} = \%constrain;
my @ks = map {$_->[2]} @longest;
my $centroid = [median(\@I),median(\@J),median(\@K),median(\@L)];
$newclus{"centroid"} = $centroid;
$newclus{"offset"} = median(\@lengths);
#calculating coordinates for putative median
my $offset = $median-$newclus{"offset"};
my $medcoords = find_medcoords($centroid,$median,$newclus{"offset"});
$newclus{"medcoords"} = $medcoords;
$newclus{"bpnum"} = median(\@bpnums);
print " Median offset: $offset\n" if ($verbose);
print " Median longest HC: ",median(\@ks),"\n" if ($verbose);
print " Median centroid: @$centroid\n" if ($verbose);
print " Median bpnum: ",$newclus{"bpnum"},"\n" if ($verbose);
print " Med coords: @$medcoords\n" if ($verbose);
# print " Median MFE: ",median(\@mfe),"\n" if ($verbose);
#print out the missing seqs
my @seqs = keys %$lengths;
remove_char(\@seqs,"median");
my @foundseqs = keys %seqs;
my @missing = map {exists_char(\@foundseqs,$_) ? () : ($_)} @seqs;
print " Missing seqs:\n\t@missing\n" if ($verbose);
$newclus{"missing"} = \@missing;
$newclus[$clus] = \%newclus;
# last if ($clus == 3);
}
return \@newclus;
}
sub find_stem_coords {
my ($is,$js) = @_;
my $i = min($is);
my $j = maxm($js);
my $k = maxm($is) - $i + 1;
my $l = $j - min($js) + 1;
return [$i,$j,$k,$l];
}
#finds what the putative centroid of median length would be
#shift to putative centroid by rate offset/clus_med
sub find_medcoords {
my ($centroid,$median,$clus_med) = @_;
my $offset = $median-$clus_med;
my $upperi = ceil($centroid->[0]+ $centroid->[0]*($offset/$clus_med));
if ($upperi < 1) { $upperi = 1;}
my $loweri = $upperi +$centroid->[2]-1;
my $lowerj = floor($centroid->[1]+$centroid->[1]*($offset/$clus_med));
if ($lowerj > $median) {$lowerj = $median; }
my $upperj = $lowerj - $centroid->[2]+1;
return [$upperi,$loweri,$upperj,$lowerj];
# my $med_coords = [$centroid->[0]+$offset,$centroid->[0]+$offset+$centroid->[2]-1,$centroid->[1]+$offset-$centroid->[3]+1,$centroid->[1]+$offset];
}
#based on centroid, go look for missing seq hc that might fit the bill
#look within range of 0 to offset from theoretical median
#offset composed of normalized length difference, plus length of hc to allow for split hc
sub improve_cluster {
my ($newclus,$data,$args,$found,$minpts) = @_;
my $feats = $data->[0];
my $lengths = $data->[1];
my $median = $lengths->{"median"};
my @seqs = keys %$lengths;
remove_char(\@seqs,"median");
my %combine;
my $verbose = $args->{"verbose"};
#make dir to store mfe seqs for missing seqs
my $outdir = $args->{"output"};
my $condir = $outdir . "constraints";
`mkdir $condir`;
$outdir = $outdir . "seqs";
`mkdir $outdir`;
#processing each cluster
for (my $i = 1; $i<scalar(@$newclus); $i++) {
my $clus = $newclus->[$i];
my $size = scalar(keys %{$clus->{"seqs"}});
if ($size < $minpts) {
# my $badclus = $newclus->[$i];
$newclus->[$i] = 0;
print "\tCluster $i with ",$size," below $minpts threshold: deleting\n" if ($verbose);
next;
}
#make dir for each cluster
my $clusdir = $outdir."/cluster_$i";
`mkdir $clusdir`;
my ($score,$newconstraints) = @{find_missing(@_,$i)};
my $total = $score + $size; #since each present member of clus gets score of 1, so sum = size
if ($total <= 0) {
$newclus->[$i] = 0;
print "\tCluster $i with score of $total: deleting\n" if ($verbose);
} else {
#add found seq HC to cluster: update constraints,
my $constraints = $clus->{"constraints"};
my $stemcoords = $clus->{"seqs"};
foreach my $seq (keys %$newconstraints) {
my $found = $newconstraints->{$seq};
$constraints->{$seq} = $found;
my $myfeats = $feats->{$seq};
my @is = ();
my @js = ();
foreach my $feat (@$found) {
my $max = $myfeats->[$feat];
my @coords = split(/\s+/,$max);
push(@is,($coords[0],$coords[0]+$coords[2]-1)); #push the boundaries, to calc k and l
push(@js,($coords[1],$coords[1]-$coords[2]+1));
}
$stemcoords->{$seq} = find_stem_coords(\@is,\@js);
print "\tAdding $seq (@{$constraints->{$seq}}) [@{$stemcoords->{$seq}}]\n" if ($verbose);
}
my $newcentroid = recalc_centroid($stemcoords);
$clus->{"centroid"} = $newcentroid;
print "\tNew centroid: @$newcentroid\n" if ($verbose);
print "\tTotal score of cluster $i: $total\n" if ($verbose);
}
# last if ($i == 3);
}
}
#recalculates the centroid after missing seqs added in
sub recalc_centroid {
my $stemcoords = shift;
my @is;
my @js;
my @ks;
my @ls;
foreach my $seq (keys %$stemcoords) {
my $coords = $stemcoords->{$seq};
push(@is,$coords->[0]);
push(@js,$coords->[1]);
push(@ks,$coords->[2]);
push(@ls,$coords->[3]);
}
return [median(\@is),median(\@js),median(\@ks),median(\@ls)];
}
sub find_mfe {
my ($med_coords,$lengths,$median,$nucls,$name,$seqs) = @_;
my %mfe;
foreach my $seq (@$seqs) {
my $length = $lengths->{$seq};
my $diff = $length-$median;
my $nucl = $nucls->{$seq};
my $bounds = find_boundaries($med_coords,$diff,$length);
my $mfe = run_mfe($name."_$seq",$seq,$nucl,$bounds);
$mfe{$seq} = $mfe;
my $bp = 0;
map {$_ eq '('? $bp++ : ()} @{$mfe->[0]};
print "\tFor $seq:\t",@{$mfe->[0]}," [$mfe->[1]] ($bp bp)\n";
}
return \%mfe;
}
# found{seq} = clus
# clus{seq} = [i,j,k,l]
#return in %constrain list of features found
sub find_missing {
my ($newclus,$data,$args,$found,$minpts,$i) = @_;
my $feats = $data->[0];
my $lengths = $data->[1];
my $clus = $newclus->[$i];
my $median = $lengths->{"median"};
my $missing = $clus->{"missing"};
my $centroid = $clus->{"centroid"};
my $bpnums = $clus->{"bpnum"};
my $med_coords = $clus->{"medcoords"};
my $verbose = $args->{"verbose"};
my $totalscore = 0;
my %constrain;
print "For cluster $i:\n" if ($verbose);
foreach my $seq (@$missing) {
print " For seq $seq, " if ($verbose);
my $feat = $feats->{$seq};
my $length = $lengths->{$seq};
my $diff = $length-$median;
my $nucl = $data->[2]{$seq};
my $bounds = find_boundaries($med_coords,$diff,$length);
my $scores = search_window($seq,$bounds,$nucl,$centroid,$found,$diff,$args,$newclus,$i,$lengths,$data->[3]);
my $mfe = run_mfe($args,$i,$seq,$nucl,$bounds);
my $bpnum = 0;
if ($mfe->[1] != 0) {
foreach my $bp (@{$mfe->[0]}) {
if ($bp eq '(') {$bpnum++;}
}
if ($bpnum >= $bpnums) {$totalscore++;}
# elsif ($bpnum >= ceil($bpnums/2)) {$scores{$seq} = 0;}
elsif ($bpnum < ceil($bpnums/2)) {$totalscore--;}
} else {
$totalscore -= 2;
}
#if the found is a feature, save it
my @found = map {$_->[0] < scalar(@$feat) ? ($_->[0]) : ()} @$scores;
if (scalar(@found)<1) {next;}
$constrain{$seq} = \@found;
print "found potential features @found\n" if ($verbose);
}
print "Cumulative score of missing seqs: $totalscore\n" if ($verbose);
return [$totalscore,\%constrain];
}
#right now finding only the innermost HC to constrain;
#can potentially constrain entire MFE
#returns ref to array of (i,j,k)
sub get_constraints {
my $adjusted = shift;
my $lastbp = 0;
my $k = 1;
foreach my $bp (@$adjusted) {
if ($lastbp) {
if ($bp->[0] == $lastbp->[0]-1 && $bp->[1] == $lastbp->[1]+1) {
$k++;
$lastbp = $bp;
} else {
push(@$lastbp,$k);
# print "found a break at @$bp so using @$lastbp\n";
return $lastbp;
}
} else {
$lastbp = $bp;
}
}
return 0 if (!$lastbp);
push(@$lastbp,$k);
return $lastbp;
}
#bounds = [i i' j' j]
sub run_mfe {
my ($args,$i,$seq,$nucl,$bounds) = @_;
my $outdir = $args->{"output"};
my $fam = $args->{"family"};
my $verbose = 0;
my $mkdirs = 1;
# my $name = "$outdir" . "$seq";
#make sequence file to run MFE
my $file = "$outdir"."seqs/cluster_$i"."/$seq".".txt";
open SEQ, ">$file" or die "cannot open $file\n";
print SEQ ">$fam, $seq for window @$bounds\n";
my @copy = @$nucl;
my $length;
my $diff = $bounds->[2]-$bounds->[1];
my $k = $bounds->[1]-$bounds->[0]+1;
if ($diff < 4) {
$length = $bounds->[3]-$bounds->[0]+1;
my @part = splice(@copy,$bounds->[0],$length);
print SEQ @part;
} else {
my @part = splice(@copy,$bounds->[0],$k);
print SEQ @part;
print SEQ "\nAAAA\n";
@copy = @$nucl;
my $l = $bounds->[3]-$bounds->[2]+1;
@part = splice(@copy,$bounds->[2],$l);
print SEQ @part;
$length = $k+$l+4;
}
close(SEQ);
#make constraints file
my $constraints = $outdir."constraints/$seq" . ".txt";
make_constraints($constraints,$diff,$k,$length);
my $gtmfe = $args->{"mfe"};
my $paramdir = $args->{"paramdir"};
my @mfe = `$gtmfe --paramdir $paramdir -c $constraints $file`;
print "@mfe\n" if $verbose;
my $mfe = process_mfe($diff,$k,@mfe);
return $mfe;
}
#write different code to process mfe output here
#if gtmfe format not used
sub process_mfe {
my ($diff,$k,@mfe) = @_;
my ($bp,$mfe);
foreach (@mfe) {
if (/^[\.\(\)]+$/) {
chomp;
my @bp = split(//);
if ($diff > 3) {
splice(@bp,$k,4,'x','x','x','x');
} elsif ($diff > 0) {
my @x = 'x' x ($diff-1);
splice(@bp,$k,$diff-1,@x);
}
$bp = \@bp;
} elsif (/Minimum Free Energy\:\s+(\-?[\d\.]+)/) {
chomp;
$mfe = $1;
}
}
return [$bp,$mfe];
}
#calculate bp, then translate back to original coords
sub translate_mfe {
my ($mfe,$bounds) = @_;
my $bp = $mfe->[0];
my @left;
my @bps;
for (my $i = 0; $i < scalar(@$bp); $i++) {
if ($bp->[$i] eq '(') {
push(@left,$i+1);
} elsif ($bp->[$i] eq ')') {
my $left = pop(@left);
push(@bps,[$left,$i+1]);
}
}
my $diff = $bounds->[2]-$bounds->[1];
my $i_offset = $bounds->[0]-1;
my $j_offset = $bounds->[0]-1;
if ($diff > 3) {
$j_offset = $bounds->[2]-($bounds->[1]-$bounds->[0]+1)-5;
}
my @adjusted = map {[$_->[0]+$i_offset,$_->[1]+$j_offset]} @bps;
return \@adjusted;
}
#matches the mfe bp to found HC
sub match_to_hc {
my ($adjusted,$scores) = @_;
my %mfebp;
map {$mfebp{"@$_"}++} @$adjusted;
my $found=0;
my @mincoords;
foreach my $hc (@$scores) {
for (my $k = 0; $k<$hc->[3]; $k++) {
my $i = $hc->[1] + $k;
my $j = $hc->[2] - $k;
if (exists $mfebp{"$i $j"}) {
@mincoords = @$hc if (!$found);
$found = $hc->[0];
print "\tFound most freq HC $found in mfe at @$hc\n";
last;
}
}
}
return 0 if (!$found);
return [splice(@mincoords,1,3)];
}
#forbid pairing within wings, and with dummy filler inbetween wings
sub make_constraints {
my ($constraints,$diff,$k,$length) = @_;
open CON,">$constraints" or die "cannot open $constraints\n";
my $overlap = 0;
#prohibit any filler
if ($diff > 3) {
print CON "P ",$k+1," 0 4\n";
} elsif ($diff > 0) {
print CON "P ",$k+1," 0 ",$diff-1,"\n" if ($diff != 1);
} else {
$overlap = abs($diff)+1;
}
#prohibit diagonals involving first nuc
for (my $j = $k-$overlap; ($j-3)/2 >= 1; $j--) {
my $n = floor(($j-3)/2);
print CON "P 1 $j $n\n";
}
#prohibit all other diagonals, starting with other nucleotides
for (my $i = 2;$i<$k-$overlap-3;$i++) {
my $n = floor(($k-$overlap-$i+1-3)/2);
print CON "P $i ",$k-$overlap," $n\n";
}
#determine where 3' wing begins
my $jp;
if ($diff > 3) {
$jp = $k + 5;
} elsif ($diff > 0) {
$jp = $k + $diff;
} else {
$jp = $k+1;
}
#do same thing for other wing
for (my $j = $length; ($j-$jp+1-3)/2 >= 1; $j--) {
my $n = floor(($j-$jp+1-3)/2);
print CON "P $jp $j $n\n";
}
for (my $i = $jp+1; $i<$length-3; $i++) {
my $n = floor(($length-$i+1-3)/2);
print CON "P $i $length $n\n";
}
close(CON);
}
#searchs the window within bounds by making dotplot
#make sure you get the profiling file right
sub search_window {
my ($seq,$bounds,$nucl,$centroid,$found,$diff,$fam,$allclus,$i,$lengths,$files) = @_;
my @scores;
my $verbose = 0;
my $move = 0; #to move from another cluster; disabled for now
my $newclus = $allclus->[$i];
my $med_coords = $newclus->{"medcoords"};
my $length = $lengths->{$seq};
my $median = $lengths->{"median"};
my $file = $files->{$seq};
# my $dotplot = make_dotplot($bounds,$nucl);
print "For seq $seq ($diff), looking within bounds [@$bounds]\n" if ($verbose);
open OUT,"<$file" or die "cannot open $file\n";
foreach (<OUT>) {
chomp;
if (/Helix (\d+) is (\d+) (\d+) (\d+) .+ with freq (\d+)/) {
my $i_end = $2+$4-1;
my $j_end = $3-$4+1;
if ($bounds->[0]<= $2 && $i_end <= $bounds->[1] && $bounds->[2]<= $j_end && $3 <= $bounds->[3]) {
my $coords = [$2,$3,$4,$4];
my $score;
my $ratios = calc_single_poisson($median,$med_coords,$coords,$length);
my $coverage = ($4/$centroid->[2] + $4/$centroid->[3])/2;
# my $avepoisson = ave($ratios);
printf "\tFound potential $_ with poisson %.2f (%.2f %.2f %.2f) coverage %.2f",
ave($ratios),$ratios->[0],$ratios->[1],$ratios->[2],$coverage if ($verbose);
# printf "\tFound potential $_ with coverage %.2f",$coverage if ($verbose);
if ($move) {
if (exists $found->{"$seq $1"}) { #if in another cluster, pick the larger one
my $old = $found->{"$seq $1"};
print " ++ ($old)" if ($verbose);
my $oldclus = $allclus->[$old];
if (scalar(keys %{$newclus->{"seqs"}}) > scalar(keys %{$oldclus->{"seqs"}})) {
$found->{"$seq $1"} = $i;
$newclus->{"seqs"}->{$seq} = $oldclus->{"seqs"}->{$seq};
delete $oldclus->{"seqs"}->{$seq};
print " (moving)" if ($verbose);
}
}
}
if (implausible_HC($ratios,$coords,$med_coords,$length,$median)) {
print " xxx\n" if ($verbose);
next;
}