-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_html2.pl
2149 lines (1682 loc) · 67.9 KB
/
make_html2.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;
use Cwd;
use PDF::API2; ## needed to create a PDF file
use Math::Trig; ## needed for sinus function
use List::Util qw(min max); ## needed to provide min and max function for lists
use File::Basename;
Usage() if(not $ARGV[0] or $ARGV[0] =~ /-*-he*l*p*/);
my $n_count = 0;
my $k_count = 0;
my $e_count = 0;
my $sig = 0;
my $infile; ## miRDeep output file
my $pdfs = 0; ## force pdf creation
my $threshold= 0; ## hairpins have to have score above threshold in order to be reported
my $csv = 0;
my $xposshift = 40;
my $lstruct_multi;
my $sb_obs;
## read in available organisms at the end of the script
my %organisms;
while(<DATA>){
chomp;
my $tmp;
$tmp=$_;
$_ =~ s/\s+//g;
$organisms{$_}=$tmp;
}
my $known;
## read in miRDeep output file
my $id;
my %hash; ## takes up all entries from the miRDeep2 module
my %seen;
my $created = 1;
my $in;
my $counter=0;
my %struct; ## counts how often a nt is covered reads
my $i;
my $offset = 0;
my $me=0; ## mature end coordinate
my @desc;
my %mat_pre_arf =();
my $lflank1; ## length(string of left flank)
my $fl1; ##
my $lflank2; ## length string of right flank
my $fl2b=0; ## right flank begin
my $lloop; ## string of loop
my $lb=0; ## starting position of loop
my $lstar; ## string of star sequence
my $sb=0; ## starting
my $lmature; ## string of mature
my $mb=0; ## mature begin
my $struct; ## structure string
my $pri_seq;## pri-cursor sequence
my $lenstr=0;
my $pdf; ## pdf descriptor
my $page; ## page descriptor
my $gfx; ## graphic variable
my $trb; ## fontvariable
my $text;
my $text2;
my $aligned; ## reads reading
my %hash2; ## begin of read in precursor
my %hash2c; ## number of reads per read sequence
my %hash2key;
my %hash2mm; ## number of mismatches
my %hash2order; ## output order saved
my %hash2seq;
my %hash2sample;
my %star_exp_hit_pos;
my $blat;
my $spacer; ## length of longest entry
my $spaces; ## string of spaces to fill up spacer
my %order; ## stores begin coordinates of fl1,m,l,s,fl2 sequences
my $multiplier = 3.6;#4.825; ## minimal distance between two letters
## calculate predefined pdf loci for alignment characters
my %position_hash;
$counter = 0;
for(my $i=0;$i < 200; $i++){
$position_hash{$counter} = $xposshift+$multiplier+20+$i*$multiplier;
$counter++;
}
my $yorig = 500; ## 500
my $downy = 50;
my $dline; ## line graphic handler
my $first=1;
my $lastx;
my $lasty;
my $final; ## final output string of a read
my @pseq; ## precursor sequence
my @rseq; ## read sequence
my $totalreads = 0;
my %assign_str; ## color assigned to position where letter is drawn
my %assign_str_exp;
my $bpo1=-10; ## left nt pos in first bp
my $bpo2=-10; ## right nt pos in first bp
my $bpo1r=-10; ## left nt pos in second bp
my $bpo2r=-10; ## right nt pos in second bp
my $ffe=0; ## first flank end position
my $ff2b=0; ## second flank begin position
my @sorted; ## array that stores sorted order of fl1,m,l,s,fl2
my $y=$yorig; ## y coordinate
my ($minx,$miny,$maxx,$maxy); ## min and max x,y coordinates of rna sequence
my @rna; ## rna sequence
my @rna_d;
my %xc; ## holds x cooridnate of each nt
my %yc; ## holds y coordinate of each nt
my $sid="";
## pdf histogram colors
my $col_star_exp = 'lightskyblue';
my $col_star_obs = 'darkviolet';
my $col_mature = 'red';
my $col_loop = 'orange';
my %hm;
my %hs;
my %hp;
## options
my %options=();
getopts("ugv:f:ck:os:t:w:er:q:dx:y:ab:i:j:lm:M:PW:",\%options);
my %weighted=();
if(-s $options{'W'}){
open IN,$options{'W'} or die "Could not open file $options{'W'}\n";
while(<IN>){
if(/(\S+)\s+(\d+)/){
$weighted{$1}=$2;
}
}
close IN;
}
## everything else given to it corresponds to the samples
my @files_mirnaex=split(",",$options{'M'});
foreach(@files_mirnaex){
print STDERR "$_ file with miRNA expression values\n";
}
#die "here in makehtml2\n";
my $time = $options{'y'} or die "no timestamp given with parameter y\n";
if($options{'x'} and not $options{'q'}){
die "\nError:\n\toption -x can only be used together with option -q\n\n";
}
## determine pdf path when running on a cluster
my $pdf_path;
if($options{'w'}){
$pdf_path = "http://localhost:8001/links/miRDeep/$options{'w'}";
}
## obtain current working directory
my $cwd = cwd;
if(not $options{'w'}){
$pdf_path = "file://$cwd";
}
## order output by sample (give -o option) or just by beginning position (no -o option)
## organism parameter
my $org=$organisms{$options{'t'}};
## some quantifier variables
my $mirbase = 0;
my %mature2hairpin;
my %hairpin2mature; ## some hairpins have more than 1 mature assigned, circumvent this problem
my %hash_q; ## takes up all entries from the quantifier module
my $blast="http://blast.ncbi.nlm.nih.gov/Blast.cgi?QUERY=";
my $blast_query = "&db=nucleotide&QUERY_FROM=&QUERY_TO=&QUERYFILE=&GENETIC_CODE=1&SUBJECTS=&stype=nucleotide&SUBJECTS_FROM=&SUBJECTS_TO=&SUBJECTFILE=&DBTYPE=gc&DATABASE=nr&EQ_MENU=&NUM_ORG=1&EQ_TEXT=&BLAST_PROGRAMS=blastn&PHI_PATTERN=&MAX_NUM_SEQ=100&SHORT_QUERY_ADJUST=on&EXPECT=10&WORD_SIZE=7&MATRIX_NAME=PAM30&MATCH_SCORES=2,-3&GAPCOSTS=5+2&COMPOSITION_BASED_STATISTICS=0&FILTER=L&REPEATS=repeat_9606&FILTER=m&TEMPLATE_LENGTH=0&TEMPLATE_TYPE=0&PSSM=&I_THRESH=&SHOW_OVERVIEW=true&SHOW_LINKOUT=true&GET_SEQUENCE=auauauaauauauauauauuauaa&FORMAT_OBJECT=Alignment&FORMAT_TYPE=HTML&ALIGNMENT_VIEW=Pairwise&MASK_CHAR=2&MASK_COLOR=1&DESCRIPTIONS=100&ALIGNMENTS=100&NEW_VIEW=true&OLD_BLAST=false&NCBI_GI=false&SHOW_CDS_FEATURE=false&NUM_OVERVIEW=100&FORMAT_EQ_TEXT=&FORMAT_ORGANISM=&EXPECT_LOW=&EXPECT_HIGH=&QUERY_INDEX=&CLIENT=web&SERVICE=plain&CMD=request&PAGE=Nucleotides&PROGRAM=blastn&MEGABLAST=&RUN_PSIBLAST=&TWO_HITS=&DEFAULT_PROG=megaBlast&WWW_BLAST_TYPE=&DB_ABBR=&SAVED_PSSM=&SELECTED_PROG_TYPE=blastn&SAVED_SEARCH=true&BLAST_SPEC=&QUERY_BELIEVE_DEFLINE=&DB_DIR_PREFIX=&USER_DATABASE=&USER_WORD_SIZE=&USER_MATCH_SCORES=&USER_FORMAT_DEFAULTS=&NO_COMMON=&NUM_DIFFS=2&NUM_OPTS_DIFFS=1&UNIQ_DEFAULTS_NAME=A_SearchDefaults_1Mn7ZD_2Sq4_1Z58HQ5Jb_23tpbD_167y9p&PAGE_TYPE=BlastSearch&USER_DEFAULT_PROG_TYPE=blastn&USER_DEFAULT_MATCH_SCORES=3.";
## get mature positions if options a is set
my %mature_pos_hash;
if($options{'a'} and -f "mirdeep_runs/run_$time/tmp/signature.arf"){
get_mature_pos();
}
my %confident =();
if($options{b}){
open IN,"<$options{'b'}" or die "file not found\n";
while(<IN>){
next if(/\#/);
chomp;
my $r = $_;
$r =~ s/\|/_/g;
$confident{$r} = 1;
}
close IN;
}
PrintQuantifier();
CloseHTML();
system("cp expression_analyses/expression_analyses_${time}/expression_${time}.html expression_${time}.html");
if(not $options{'d'}){
$mirbase = 1;
CreateStructurePDFQuantifier(%hash_q);
}
exit;
sub CreateStructurePDFQuantifier{
my %hash = @_;
my $filename;
print STDERR "creating PDF files\n";
for(sort { $hash{$b}{"score"} <=> $hash{$a}{"score"} } keys %hash){
next if(not $hash{$_}{'pdf'});
next if(not $hash{$_}{'freq_total'});
$sid = $_;
$sid =~ tr/\|/_/;
%star_exp_hit_pos =();
$filename = $sid;
if($mirbase){
$filename = $sid; #$hairpin2mature{$sid};
}
next if ($seen{$filename});
next if(-f "$cwd/pdfs_$time/$filename.pdf");
# next if($hash{$sid}{"score"} < $threshold); ## skip if threshold is not reached not used anymore
## reinit variables;
$i=0;
$offset = 0;
$me=0; ## mature end coordinate
@desc;
$lflank1 = 0; ## length(string of left flank)
$fl1 = 0; ##
$lflank2 = 0; ## length string of right flank
$fl2b=-1; ## right flank begin
$lloop = 0; ## string of loop
$lb=-1; ## starting position of loop
$lstar = 0; ## string of star sequence
$sb=$mat_pre_arf{$sid}{'sb'}; ## starting
$lmature = 0; ## string of mature
$mb= $mat_pre_arf{$sid}{'mb'}; ## mature begin
$struct = 0; ## structure string
$pri_seq="";## pri-cursor sequence
$lenstr=0;
$pdf; ## pdf descriptor
$page; ## page descriptor
$gfx; ## graphic variable
$trb; ## fontvariable
%hash2 = ();
%hash2c = ();
%hash2mm = ();
%hash2order = ();
%order = ();
$yorig = 500;
$downy = 50;
$dline; ## line graphic handler
$first=1;
$lastx=0;
$lasty=0;
$final=""; ## final output string of a read
@pseq; ## precursor sequence
@rseq; ## read sequence
$totalreads = 0;
%assign_str = ();
%assign_str_exp = ();
%struct = ();
$bpo1=-10; ## left nt pos in first bp
$bpo2=-10; ## right nt pos in first bp
$bpo1r=-10; ## left nt pos in second bp
$bpo2r=-10; ## right nt pos in second bp
$ffe=0; ## first flank end position
$ff2b=0; ## second flank begin position
@sorted; ## array that stores sorted order of fl1,m,l,s,fl2
$y=$yorig; ## y coordinate
($minx,$miny,$maxx,$maxy); ## min and max x,y coordinates of rna sequence
@rna; ## rna sequence
%xc = ();
%yc = ();
#### main program;
$pri_seq = $hash{$sid}{"pri_seq"};
@rna = split(//,$pri_seq);
chomp $pri_seq;
my @desc2 =split(//,$hash{$sid}{'exp'});
for (my $i=0;$i < scalar @desc2; $i++){
if ($desc2[$i] eq "f"){ ## assign_str now starts at 0 not at one
$assign_str_exp{$i} = "black";
$assign_str{$i} = "black";
} elsif ($desc2[$i] eq "l"){
$assign_str_exp{$i} = $col_loop;
$assign_str{$i} = $col_loop;
} elsif ($desc2[$i] eq "S"){
$assign_str_exp{$i} = $col_star_obs;
$assign_str{$i} = $col_star_obs;
} else{
$assign_str_exp{$i} = $col_mature;
$assign_str{$i} = $col_mature;
}
}
if(not -d "$cwd/pdfs_$time"){
mkdir "$cwd/pdfs_$time";
}
open FOLD,">$cwd/pdfs_$time/$filename.tmp" or die "Error: cannot create tmp file$!\n";
$struct = $hash{$sid}{"pri_struct"};
$lstruct_multi = ((length($struct)+2)*$multiplier);
print FOLD "5${pri_seq}3\n";
close FOLD;
for ($i=0; $i < length($struct);$i++)
{
$struct{$i} = 0;
}
## new approach
for my $tag(keys %{$hash{$sid}{"reads"}}){ ## for each tag
for my $read(sort keys %{$hash{$sid}{"reads"}{$tag}}){
# print "$tag x \t $read r \t $sid s \n";
if ($hash{$sid}{"reads"}{$tag}{$read}{"seq"} =~ /^(\.*)(\w+)\.*$/){
my $v1=$1;
my $v2=$2;
$hash2{$read}=length($v1); ## begin of read in precursor
if($hash{$sid}{"reads"}{$tag}{$read}{"rid"} =~ /_x(\d+)/){
my $dc = $1;
if($options{'W'}){
$dc/=$weighted{$hash{$sid}{"reads"}{$tag}{$read}{"rid"}};
#die "here === $hash{$sid}{'reads'}{$tag}{$read}{'rid'} $dc\n";
}
$totalreads+= $dc;
#$hash2c{$tag}{$v2}+=$1; ## number of reads with same sequence
$hash2c{$tag}{$v2}+=$dc;
$hash2key{$read}=$v2;
$hash2order{$read} = $read;
$hash2mm{$read}=$hash{$sid}{"reads"}{$tag}{$read}{"mm"}; ## number of mismatches with precursor sequence
$hash2seq{$read} = $hash{$sid}{"reads"}{$tag}{$read}{"seq"};
$hash2sample{$read} = $tag;
for ($i=length($v1); $i < (length($v1)+length($v2)); $i++)
{
#$struct{$i+1}+= $dc; ## saves how often a nt in precursor is covered by a read
$struct{$i}+= $dc; ## saves how often a nt in precursor is covered by a read
}
}
}
}
} ## end of reading aligned sequences
$y = $yorig-$downy;
chdir "./pdfs_$time";
## by ref
CreatePDFQuantifier(\%hash);
##############################################################################################
##############################################################################################
## insert secondary structure now
DrawStructure($filename);
if($totalreads ne '0'){
CreateHistogramQuantifier();
#ClosePDF($filename);
#exit;
}
## by ref
CreateAlignmentQuantifier(\%hash);
$y -=20;
### here the Frequency histogram is drawn
ClosePDF($filename);
unlink("$cwd/pdfs_$time/${filename}_ss.ps");
unlink("$cwd/pdfs_$time/$filename.tmp");
unlink("$cwd/pdfs_$time/rna.ps");
chdir "..";
print STDERR "creating pdf for $filename finished\n";
}
unlink("$cwd/pdfs_$time/tmp");
}
sub CreateHistogramQuantifier{
# my ($hash) = @_;
$dline->linewidth(2);
$y = $yorig-$downy;
$dline = $page->gfx;
##draw axes
$dline->strokecolor('black');
$dline->move($xposshift+20,$y+160);
$dline->line($xposshift+20,$y+50-1);
$dline->stroke;
$dline->move($xposshift+20,$y+50);
$dline->strokecolor('grey');
$dline->line($xposshift+20+$lstruct_multi,$y+50);
$dline->stroke;
$dline->strokecolor('black');
$dline->move($xposshift+17+$lstruct_multi,$y+53);
$dline->line($xposshift+20+$lstruct_multi,$y+50);
$dline->line($xposshift+17+$lstruct_multi,$y+47);
$dline->move($xposshift+17,$y+157);
$dline->line($xposshift+20,$y+160);
$dline->line($xposshift+23,$y+157);
$dline->move($xposshift+17,$y+150);
$dline->line($xposshift+23,$y+150);
$gfx->textlabel($xposshift+12,$y+165,$trb,6,"freq." ,-color=>'black');
$gfx->textlabel($xposshift+$lstruct_multi,$y+40,$trb,8,"length" ,-color=>'black');
$gfx->textlabel($xposshift+10,$y+148,$trb,6,"1" ,-color=>'black');
$dline->move($xposshift+17,$y+125); ##.75
$dline->line($xposshift+23,$y+125);
$gfx->textlabel($xposshift+2,$y+122,$trb,6,"0.75" ,-color=>'black');
$dline->move($xposshift+17,$y+100); ## .5
$dline->line($xposshift+23,$y+100);
$gfx->textlabel($xposshift+6,$y+98,$trb,6,"0.5" ,-color=>'black');
$dline->move($xposshift+17,$y+75); ## .25
$dline->line($xposshift+23,$y+75); ## .25
$gfx->textlabel($xposshift+2,$y+73,$trb,6,"0.25",-color=>'black');
$gfx->textlabel($xposshift+12,$y+48,$trb,6,"0" ,-color=>'black');
$dline->stroke;
## draw flank1
$dline->strokecolor('black');
$dline->move($xposshift+20,$y+50);
$lastx = $position_hash{0};
$lasty = (($struct{0}/$totalreads)*100)+$y+50;
$dline->strokecolor($assign_str{0});
$dline->move($lastx,$lasty);
for($i = 0; $i < length($struct); $i++){
$dline->strokecolor($assign_str{$i});
$dline->move($lastx,$lasty);
$dline->line($position_hash{$i+1},(($struct{$i}/$totalreads)*100)+$y+50); ## .25
$dline->stroke;
$lastx = $position_hash{$i+1};
$lasty = (($struct{$i}/$totalreads)*100)+$y+50;
}
}
sub CreatePDFQuantifier{
my ($hash) = @_;
$pdf=PDF::API2->new;
$spacer = length($sid);
$pdf->mediabox('A4');
$page=$pdf->page;
$gfx=$page->gfx;
$text=$gfx;
$trb=$pdf->corefont('Times-Roman', -encode=>'latin1');
## move everything except the structure downwards if $mirbase is set
my $madd = 60;
$gfx->textlabel($xposshift+20,$y+300+$downy,$trb,8,"miRBase precursor",-color=>'black');
$gfx->textlabel($xposshift+110,$y+300+$downy,$trb,8,": $sid",-color=>'black');
$spaces = " " x ($spacer - length($$hash{$sid}{"freq_total"}));
$gfx->textlabel($xposshift+20,$y+230+$madd+$downy,$trb,8,"Total read count",-color=>'black');
$gfx->textlabel($xposshift+110,$y+230+$madd+$downy,$trb,8,": $$hash{$sid}{'freq_total'}",-color=>'black');
## here should be written how many annotated stuff is actually there and how many not
my $jk =10;
# old
#for(sort {$$hash{$sid}{'mapped'}{$b} <=> $$hash{$sid}{'mapped'}{$a}} keys %{$$hash{$sid}{'mapped'}}){
#new
my @h2m=split(",",$hairpin2mature{$sid});
foreach my $h(@h2m){
next if($h =~ /^\s*$/);
if($options{'t'}){
next if($_ !~ $options{'m'});
}
$spaces = " " x ($spacer - length($_));
$gfx->textlabel($xposshift+20,$y+230-$jk+$madd+$downy,$trb,8,"$h read count",-color=>'black');
$gfx->textlabel($xposshift+110,$y+230-$jk+$madd+$downy,$trb,8,": $$hash{$sid}{'mapped'}{$h}",-color=>'black');
$jk+=10;
}
$spaces = " " x ($spacer - length("remaining reads"));
$gfx->textlabel($xposshift+20,$y+230-$jk+$madd+$downy,$trb,8,"remaining reads",-color=>'black');
$gfx->textlabel($xposshift+110,$y+230-$jk+$madd+$downy,$trb,8,": $$hash{$sid}{'remaining_rc'}",-color=>'black');
$jk+=10;
$trb=$pdf->corefont('Courier', -encode=>'latin1');
}
sub ClosePDF{
my $file = shift;
$file = "output" if($file eq"");
$pdf->saveas("$cwd/pdfs_$time/$file.pdf");
}
## draw a line in PDF
sub Line{
my ($x1,$y1,$x2,$y2,$col,$width) = @_;
$dline->linewidth($width);
$dline->strokecolor($col);
$dline->move($x1,$y1);
$dline->line($x2,$y2);
$dline->stroke;
}
## draw a letter in PDF
sub Base{
my ($x1,$y1,$base,$col,$size) = @_;
$gfx->textlabel($x1,$y1,$trb,$size,$base,-color=>$col);
}
sub CreateAlignmentQuantifier{
my ($hash) = @_;
# $gfx->textlabel($xposshift+(18+(($mb+1)*$multiplier)),$y+40,$trb,6,$mb-$lflank1+1,-color=>'black');
$y+=20;
for my $k1(keys %{$mat_pre_arf{$sid}}){
# print STDERR "$sid\t$k1\n";
if($k1 =~ /\*/){
$gfx->textlabel($xposshift+(20+(($mat_pre_arf{$sid}{$k1}{'start'})*$multiplier)),$y,$trb,6,"$k1",-color=>$col_star_obs);
}else{
$gfx->textlabel($xposshift+(20+(($mat_pre_arf{$sid}{$k1}{'start'})*$multiplier)),$y,$trb,6,"$k1",-color=>$col_mature);
}
$y-=10;
}
for(my $i=0; $i < scalar @rna; $i++){
$gfx->textlabel($position_hash{$i},$y,$trb,6,$rna[$i],-color=>$assign_str{$i});
}
$gfx->textlabel($xposshift+25+ $lstruct_multi,$y,$trb,6,'-3\'' ,-color=>'black');
$gfx->textlabel($xposshift+10,$y,$trb,6,'5\'-' ,-color=>'black');
if($$hash{$sid}{'obs'}){
$gfx->textlabel($xposshift+50+ $lstruct_multi,$y,$trb,6,'obs' ,-color=>'black');
}else{
$gfx->textlabel($xposshift+50+ $lstruct_multi,$y,$trb,6,'exp' ,-color=>'black');
}
if($$hash{$sid}{'obs'}){
$y -= 10;
for(my $i=0; $i < scalar @rna; $i++){
$gfx->textlabel($position_hash{$i},$y,$trb,6,$rna[$i],-color=>$assign_str_exp{$i});
}
$gfx->textlabel($xposshift+50+ $lstruct_multi,$y,$trb,6,'exp' ,-color=>'black');
}
$y -= 10;
my @structx = split(//,$struct);
my $sadd = 0;
$gfx->textlabel($position_hash{0},$y,$trb,6,$struct,-color=>'black');
$gfx->textlabel($xposshift+30+ $lstruct_multi,$y,$trb,6,'reads' ,-color=>'black');
$gfx->textlabel($xposshift+70+ $lstruct_multi,$y,$trb,6,'mm' ,-color=>'black');
$gfx->textlabel($xposshift+110+ $lstruct_multi,$y,$trb,6,'sample' ,-color=>'black');
$y -= 10;
if($options{'o'}){
for my $tag(keys %{$$hash{$sid}{"reads"}}){
for my $k(sort { $hash2order{$a} <=> $hash2order{$b} } keys %hash2order){
next if($hash2sample{$k} ne $tag);
$gfx->textlabel($position_hash{0},$y,$trb,6,$hash2seq{$k},-color=>'black');
## matches and read numbers
$gfx->textlabel($xposshift+30+ $lstruct_multi,$y,$trb,6,int($hash2c{$tag}{$hash2key{$k}}) ,-color=>'black');
$gfx->textlabel($xposshift+70+ $lstruct_multi,$y,$trb,6,$hash2mm{$k} ,-color=>'black');
$gfx->textlabel($xposshift+110+ $lstruct_multi,$y,$trb,6,$hash2sample{$k} ,-color=>'black');
$y -= 10;
if($y < 100){
$page=$pdf->page();
$pdf->mediabox('A4');
$text2=$page->text();
$gfx = $text2;
$y=800;
for(my $i=0; $i < scalar @rna; $i++){
$gfx->textlabel($position_hash{$i},$y,$trb,6,$rna[$i],-color=>$assign_str{$i});
}
$y+=20;
for my $k1(keys %{$mat_pre_arf{$sid}}){
if($k1 =~ /\*/){
$gfx->textlabel($xposshift+(20+(($mat_pre_arf{$sid}{$k1}{'start'})*$multiplier)),$y,$trb,6,"$k1",-color=>$col_star_obs);
}else{
$gfx->textlabel($xposshift+(20+(($mat_pre_arf{$sid}{$k1}{'start'})*$multiplier)),$y,$trb,6,"$k1",-color=>$col_mature);
}
$y-=10;
}
$y-=20;
}
}
$y-=10;
}
}else{
for my $k(sort { $hash2order{$a} <=> $hash2order{$b} } keys %hash2order){
my $tag = $hash2sample{$k};
$gfx->textlabel($position_hash{0},$y,$trb,6,$hash2seq{$k},-color=>'black');
## matches and read numbers
$gfx->textlabel($xposshift+30+ $lstruct_multi,$y,$trb,6,int($hash2c{$tag}{$hash2key{$k}}) ,-color=>'black');
$gfx->textlabel($xposshift+70+ $lstruct_multi,$y,$trb,6,$hash2mm{$k} ,-color=>'black');
$gfx->textlabel($xposshift+110+ $lstruct_multi,$y,$trb,6,$hash2sample{$k} ,-color=>'black');
$y -= 10;
if($y < 100){
$page=$pdf->page();
$pdf->mediabox('A4');
$text2=$page->text();
$gfx = $text2;
$y=800;
$y+=20;
for my $k1(keys %{$mat_pre_arf{$sid}}){
if($k1 =~ /\*/){
$gfx->textlabel($xposshift+(20+(($mat_pre_arf{$sid}{$k1}{'start'})*$multiplier)),$y,$trb,6,"$k1",-color=>$col_star_obs);
}else{
$gfx->textlabel($xposshift+(20+(($mat_pre_arf{$sid}{$k1}{'start'})*$multiplier)),$y,$trb,6,"$k1",-color=>$col_mature);
}
$y-=10;
}
for(my $i=0; $i < scalar @rna; $i++){
$gfx->textlabel($position_hash{$i},$y,$trb,6,$rna[$i],-color=>$assign_str{$i});
}
$y-=10;
}
}
}
}
sub Shifting{
$minx = min(values %xc);
$maxx = max(values %xc);
$miny = min(values %yc);
$maxy = max(values %yc);
## now place sec-structure in upper right corner
my $shiftx = abs($minx)+10;
my $shifty = abs($miny)+10;
#shift everything to printable area;
if($minx < 0){
for(my $i=0; $i < scalar @rna_d-1; $i++){
$xc{$i}+=$shiftx;
}
}
if($miny < 0){
for(my $i=0; $i < scalar @rna_d-1; $i++){
$yc{$i}+=$shifty;
}
}
if($maxx > 600){
for(my $i=0; $i < scalar @rna_d-1; $i++){
$xc{$i}-=$minx+10;
}
}
if($maxy > 800){
for(my $i=0; $i < scalar @rna_d-1; $i++){
$yc{$i}-=$miny+10;
}
}
}
sub DrawStructure{
my $filename = shift;
#return
$dline = $page->gfx;
## run RNAplot to create secondary structure ps file
my $cw =cwd;
#print STDERR "$sid\tcwd\t$cw\n";
system("RNAfold -d 0 < $cwd/pdfs_$time/${filename}.tmp > $cwd/pdfs_$time/tmp");
#exit;
my $in_pos=0;
my $in_pairs=0;
my $count=0; ## counter
my %bp; ## which nt pair
my $line; ## input line of file
open PS,"<$cwd/pdfs_$time/rna.ps" or die "Error: cannot open $cwd/pdfs_$time/rna.ps\t$!\n";
my ($minx, $miny) = 10000;
my ($maxx,$maxy) = 0;
my $centering_x= 0; ## base in precursor sequences that is choosen as center point for rotation
my $twisted=0; ## if mature sequence comes after loop twisted is 1
my $sums=0; ## dif between 2bp nts
while (<PS>)
{
if (/\/sequence/) ## if sequence matched in rna.ps
{
$line = <PS>; ## read in rna sequence
chomp $line;
#$line =~ s/U/T/g;
@rna_d=split(//,$line); ## read in to @rna_d
next;
}
if (/\/coor\s*\[/) ## if nt coordinates section in ps comes now
{
$in_pos = 1;
next;
}
if ($in_pos and /\[(\S+)\s+(\S+)\]/)
{
$xc{$count} = $1; ## x cooridnate
$yc{$count} = $2; ## y coordinate
$count++;
next;
}
if (/\/pairs/) ## read in base pairs
{
$count=0;
$in_pos=0;
$in_pairs = 1;
next;
}
$twisted = 1 if($mb > $lb ); ## mature begin is after loop begin
if ($in_pairs and /\[(\S+)\s+(\S+)\]/)
{
if ($twisted)
{
$bpo2r=$1;
$bpo1r=$2;
} else {
$bpo2r=$2;
$bpo1r=$1;
}
## determine two subsequent bases in mature having subsequent paired bases
if ($bpo1r >= $mb-$offset and $bpo1r < $me-$offset and $centering_x==0)
{
if ($twisted)
{
$sums = -1;
} else {
$sums=1;
}
if (($bpo1r-$bpo1) == $sums and ($bpo2-$bpo2r ) == $sums)
{
if($twisted){
$centering_x=$bpo1r;
}else{
$centering_x=$bpo1r-2;
}
}
}
$bpo1= $bpo1r;
$bpo2 = $bpo2r;
$bp{$bpo1r-1}=$bpo2r-1; ## saving nt pairs in hash %bp
next;
}
if ($in_pairs and /\]\s*def/) ## end of nt pairs in ps file
{
$in_pairs = 0;
last;
}
}
close PS;
Shifting();
$minx = min(values %xc);
$maxx = max(values %xc);
$miny = min(values %yc);
$maxy = max(values %yc);
##determine if mirror or not
my $mir=0;
$mir = 1 if($twisted);
my $yshift=0;
########### mirror sequence so that loop is on the right hand side
my $cshift = 3; ## determines the nt in mature sequence which should be the rotating center
if ($mir)
{
for (my $i=0; $i < scalar @rna_d; $i++)
{
$xc{$i} = -$xc{$i};
}
}
$minx = min(values %xc);
$maxx = max(values %xc);
$miny = min(values %yc);
$maxy = max(values %yc);
#translate back to positive area
if ($mir)
{
for (my $i=0; $i < scalar @rna_d; $i++)
{
$xc{$i} += abs($minx)+10;
}
}
$minx = min(values %xc);
$maxx = max(values %xc);
$miny = min(values %yc);
$maxy = max(values %yc);
my $ax = $xc{$centering_x};
my $ay = $yc{$centering_x};
## point relativ to center
my $bx;
my $by;
#print "twisted $twisted\n";
if ($twisted)
{
$bx = $xc{$centering_x-1};
$by = $yc{$centering_x-1};
} else {
$bx = $xc{$centering_x+1};
$by = $yc{$centering_x+1};
}
my $gk = $by-$ay;
my $ak = $bx-$ax;
my $r = sqrt(($ak**2)+($gk**2));
my $phi = asin($gk/$r);
if ($bx < $ax and $by > $ay)
{
$phi = 3.141593-$phi;
}
if ($bx <= $ax and $by <= $ay)
{
$phi *= (-1);
$phi += 3.141593;
}
my $alpha;
my $do_rot = 1;
if ($do_rot)
{
my $last = $xc{0};
### now rotate every point in a designated angle of phi
for (my $i=0; $i < scalar @rna_d -1; $i++)
{
next if ($i == $centering_x);
$bx = $xc{$i};
$by = $yc{$i};
$gk = $by-$ay;
$ak = $bx-$ax;
$r = sqrt(($ak**2)+($gk**2));
$alpha = asin($gk/$r);
if ($bx < $ax and $by > $ay)
{
$alpha = 3.141593-$alpha;