forked from fy2/rapid_annotation_transfer_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ratt.pl
1752 lines (1382 loc) · 42.9 KB
/
main.ratt.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 -w
#
# File: annotation.correctString.pl
# Time-stamp: <14-Dec-2011 15:11:58 tdo>
# $Id: $
#
# Copyright (C) 2010 by Pathogene Group, Sanger Center
#
# Author: Thomas Dan Otto [email protected] and Gary Dilon
#
# Description: Please see http://ratt.sourceforge.net for information
#
use strict;
use Data::Dumper;
use lib $ENV{RATT_HOME};
use ratt_correction;
my $debug=1;
my $SET=1;
my $COLOR_BAD=4;
if (!defined($ENV{RATT_HOME})) {
print "Please set global variable RATT_HOME in your shell!\n";
exit 1
}
if (!defined($ARGV[0])) {
print "Sorry, wrong option.\n";
print "Tranfer / Correct / Check / EMBLFormatCheck / Mutate / Split / Difference / Embl2Fasta / doEMBL \ncan be used.\n\n";
exit 1
}
elsif ($ARGV[0] eq "doEMBL") {
if (! defined($ARGV[1])) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl <ResultName> <embl-Annotation> <fasta-file>\n\n".
"This part will generate a vaild embl file, with an ID line and the sequence. The resultfile will be called <ResultName>.embl\n\n";
exit;
}
doEMBL($ARGV[1],$ARGV[2],$ARGV[3]);
exit;
}
elsif ($ARGV[0] eq "Mutate") {
if (! defined($ARGV[1])) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Mutate <(multi-)fasta-file>\n\n".
"Every 250 base pairs a base is changed (mutated). The result is saved as <fastafile>.mutated. This is necessary to recalibrate RATT for similar genomes.\n\n";
exit;
}
putMutation($ARGV[1]);
exit;
}
elsif ($ARGV[0] eq "MutateEMBL") {
if (! defined($ARGV[1])) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Mutate <embl dir> <Result Name>\n\n".
"Each feature will be muated 5 bases into it, from both sites. This is necessary to recalibrate RATT for similar genomes. We hope that it goes better results than normal mutate.\n\n";
exit;
}
putMutationEmbl($ARGV[1],$ARGV[2]);
exit;
}
elsif ($ARGV[0] eq "Split") {
if (! defined($ARGV[1])) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Split <(multifasta-file>\n\n".
"Splits a given multifasta file into individual files containing one sequence. This is necessary as visualization tools (e.g. Artemis) prefer single fasta files.\n\n";
exit (1);
}
Split($ARGV[1]);
exit;
}
elsif ($ARGV[0] eq "Difference") {
my $mummerSNP=$ARGV[1];
my $mummerCoords=$ARGV[2];
my $resultName=$ARGV[3];
if (!defined($resultName)) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Difference <mummer SNP file> <mummer coord file> <ResultName>\n\n".
"Generates files that report the SNP, indels and regions not shared by the reference and query. It also prints a statistic reporting coverage for each replicon.\n\n";
exit (1);
}
my $stats=getMutations($mummerSNP,$mummerCoords,$resultName);
print $stats;
exit;
}
elsif($ARGV[0] eq "addTranslation"){
if (scalar(@ARGV) < 2) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl addTranslation <EMBL file> optional <Translationtable>\n\n".
"If you want to add the translation of the protein relative to the new sequence, run this. The Bioperl module Bioperl::seqio must be installed. Also, the CDS must have a gene, locustag or systematic_id tag.\nThe result will be the embl with a translationtag.\n";
exit (1);
}
addTranslation($ARGV[1],$ARGV[2]);
}
elsif ($ARGV[0] eq "EMBLFormatCheck") {
if (scalar(@ARGV) < 3) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl EMBLFormatCheck <EMBL file> <ResultName postfix>\n\n".
"Some EMBL files have feature positions spanning several lines, this function consolidates these features so they appear on one line. The result name is <EMBL File>.<ResultName postfix>.\n\n";
exit (1);
}
my $what =shift;
my $embl=shift;
my $fasta = shift;
my $resultName = shift;
correctEMBL($embl,"tmp.BBA.embl",$fasta);
}
elsif ($ARGV[0] eq "Correct") {
if (scalar(@ARGV) < 4) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Correct <EMBL file> <fasta file> <ResultName>\n\n".
"Corrects a given annotation, as described previously. The corrections are reported and the new file is saved as <ResultName>.embl.\n\n";
exit(1);
}
my $what =shift;
my $embl=shift;
my $fasta = shift;
my $resultName = shift;
correctEMBL($embl,"tmp.BBA.embl",$fasta);
startAnnotationCorrection( "$embl.tmp.BBA.embl",$fasta,$resultName);
print $resultName."\n";
correctEMBL("$resultName.tmp2.embl","$resultName.tmp3.embl",$fasta);
exit;
}
elsif ($ARGV[0] eq "Check") {
if (scalar(@ARGV) < 4) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Check <EMBL file> <fasta file> <ResultName>\n\n".
"Similar to the correct option, but it will only report errors in an EMBL file.\n\n";
exit ;
}
my $what =shift;
my $embl=shift;
my $fasta = shift;
my $resultName = shift;
correctEMBL($embl,"tmp.BBA.embl",$fasta);
startAnnotationCheck( "$embl.tmp.BBA.embl",$fasta,$resultName);
exit;
}
elsif ($ARGV[0] eq "Embl2Fasta") {
if (scalar(@ARGV) < 3) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Embl2Fasta <EMBL dir> <fasta file>\n\n".
"Extracts the sequence from embl files in the <EMBL directory> and saves it as a <fasta file>.\n\n";
exit 1;
}
my $what =shift;
my $embl=shift;
my $fasta = shift;
Embl2Fasta($embl,$fasta);
exit;
}
elsif ($ARGV[0] eq "Transfer") {
if (@ARGV< 5) {
print "\n\nusage: \$RATT_HOME/main.ratt.pl Transfer <embl Directory> <mummer SNP file> <mummer coord file> <ResultName>\n\n".
"This functionality uses the mummer output to map the annotation from embl files, which are in the <embl Directory>, to the query. It generates all the new annotation files (ResultName.replicon.embl), as well as files describing which annotations remain untransferred (Replicon_reference.NOTtransfered.embl).\n\n";
exit(1);
}
my $what = shift;
my $emblDir = shift;
my $mummerSNP = shift;
my $mummerCoords= shift;
my $resultName = shift;
my $dbg=49;
## main hash: %ref_shift{Ref_contig}[pos] [0] query_contig
# [1] position
# [2] strand
my $ref_shift;
#load the position of the
my $ref_cdsPosition=loadEmbl($emblDir);
my $ref_snp = getFile($mummerSNP);
my $ref_coords = getFile($mummerCoords);
# clean the space of the annotation position
# transfer the annotation the annotation
opendir (DIR, $emblDir) or die "Problem to open opendir $emblDir: $!\n";
### will hold the new annotation: $h{queryname}.=annotation as embl
my $ref_results;
my $ref_Counting= {'Partial' => 0,
'ExonNotTransfered' => 0,
'Split' => 0,
'NotTransfered' => 0,
'Transfered' => 0,
'CDS' => 0,
'CDSTransfered' => 0,
'CDSNotExons' => 0,
'CDSPartial' => 0
};
map {
if (/(\S+)\.embl$/){
my $refName=$1;
my $ref_shift;
print "working on $refName\n";
# fill the shift hash with the coords
$ref_shift = loadCoords($ref_coords,$ref_shift,$refName);
#print Dumper $ref_shift;
# tune the shift hash with the snp file
my $ref_shift2 = loadSNP($ref_snp,$ref_shift,$ref_cdsPosition,$refName);
($ref_results,$ref_Counting)=adaptAnnotationEMBL($emblDir,$_,$ref_shift2,$ref_results,$ref_Counting);
### cleaning step
if (defined(@{$$ref_shift{$refName}})) {
foreach (0..(scalar(@{$$ref_shift{$refName}}))-1) {
undef(@{ $$ref_shift{$refName}[$_]});
}
}
undef %$ref_shift;
}
} readdir(DIR);
### output results
print "Overview of transfere of annotation elements:\n$$ref_Counting{Elements}\telements found.\n";
print "$$ref_Counting{Transfered}\tElements were transfered.\n";
print "$$ref_Counting{Partial}\tElements could be transfered partially.\n";
print "$$ref_Counting{Split}\tElements split.\n";
print "$$ref_Counting{ExonNotTransfered}\tParts of elements (i.e.exons tRNA) not transferred.\n";
print "$$ref_Counting{NotTransfered}\tElements couldn't be transferred.\n";
print "\nCDS:\n$$ref_Counting{CDS}\tGene models to transfer.\n$$ref_Counting{CDSTransfered}\tGene models transferred correctly.\n";
print "$$ref_Counting{CDSPartial}\tGene models partially transferred.\n";
print "$$ref_Counting{CDSNotExons}\tExons not transferred from partial CDS matches.\n";
print ($$ref_Counting{CDS}-$$ref_Counting{CDSTransfered}-$$ref_Counting{CDSPartial});
print "\tGene models not transferred.\n\n";
### then just save it
saveAnnotation($resultName,$ref_results);
}
else {
print "Sorry, wrong option.\n";
print "Tranfer / Correct / Check / EMBLFormatCheck / Mutate / Split / Difference / Embl2Fasta / doEMBL\ncan be used.\n\n";
}
###########################################
### subs
sub getFile{
my $file = shift;
open F, $file or die "Problem to open $file: $!\n";
my @ar=<F>;
close(F);
return \@ar;
}
############################################
### doEMBL
############################################
sub doEMBL{
my $resultName = shift;
my $emblPart = shift;
my $fastapart = shift;
my ($seq,$length)=fasta2EMBLfasta($fastapart);
my $res="ID $resultName ; ; ; ; ; $length BP.\n".
"FH Key Location/Qualifiers\n".
"FH \n";
if (-f $emblPart) {
### if a file has no embl
open F, $emblPart or die "Couldn't open embl file ($emblPart) in doEMBL: $!\n";
#my @ar=<F>;
my @ar;my $ok=1;
while(<F>){
if (/^SQ/){
$ok=0
} elsif ($ok) {
$res.=$_
}
}
close(F);
# $res.=join ('',@ar);
}
### mal hack: if contig is node, it will have the contigs tag in
if ($fastapart =~ /NODE_/) {
$res.="FT contig 1..$length\n";
my $col=(3+int(rand(10)));
$res.="FT /note=\"Contig: $resultName.\"\n";
$res.="FT /colour=$col\n";
}
$res.=$seq."//\n";
open F, "> $resultName.embl" or die "Couldn't write $resultName.embl in doEMBL: $! \n";
print F $res;
close(F);
}
########################
### fasta2EMBLfasta
########################
sub fasta2EMBLfasta{
my $fastapart = shift;
open F, $fastapart or die "Couldn't open embl file ($fastapart) in doEMBL: $!\n";
### we assume single fasta. If it is multifasta, it is going to be flattend
$_=<F>;
my $seq;
while (<F>) {
chomp;
if (! (/^>/)) {
$seq.=lc($_);
}
}
my @ar=split(//,$seq);
my $length = length($seq);
### set parameter
my $line=60;
my $block=10;
my $res;
my %countB=('a' => 0,
't' => 0,
'g' => 0,
'c' => 0,
'o' => 0
);
my $count=0;
my $lastline='';
foreach (@ar) {
if (($count%$line)==0) {
$lastline.=" $_";
}
elsif (($count%$block)==0) {
$lastline.=" $_";
}
elsif ((($count+1)%$line)==0) {
$lastline.="$_";
my $l=(81-length($lastline));
$res.=sprintf("%-0s %*d\n",$lastline,$l,($count+1));
# $res.=$lastline;
$lastline=''
}
else {
$lastline.="$_"
}
### count bases
if ($_ eq 'a') {
$countB{a}++
}
elsif ($_ eq 't') {
$countB{t}++
}
elsif ($_ eq 'g') {
$countB{g}++
}
elsif ($_ eq 'c') {
$countB{c}++
}
else {
$countB{o}++
}
$count++;
}
if ($lastline ne '') {
my $l=(81-length($lastline));
$res.=sprintf("%-0s %*d\n",$lastline,$l,($count+1));
}
$res = "SQ Sequence $count BP; $countB{a} A; $countB{c} C; $countB{g} G; $countB{t} T; $countB{o} other;\n".$res;
return ($res,$length);
}
############################################
### loadEMBL
############################################
sub loadEmbl{
my ($emblDir) = @_;
opendir (DIR, $emblDir) or die "Problem to open opendir $emblDir: $!\n";
### will hold the new annotation: $h{queryname}.=annotation as embl
my $ref_annotation;
map {
if (/embl$/ or /embl.gz$/){
my $embl=$_;
my ($chr);
if (/embl.gz$/) {
open(F, " gunzip -c $emblDir."/".$embl | " ) or die "Problems open embl $embl: $!\n";
($chr) = $embl =~ /\/{0,1}(\S+)\.embl.gz$/;
}
else
{
open(F, $emblDir."/".$embl) or die "Problems open embl $embl: $!\n";
($chr) = $embl =~ /\/{0,1}(\S+)\.embl$/;
}
while (<F>){
if (/^FT.*CDS.*\d+/){
if ($_ =~ /^\W*\(*(\d+)\.\.(\d+)\)*$/) {
$ref_annotation=
Mask_CDS($chr,$ref_annotation,$1,$2);
}
elsif (/\.\./){
my @a=split(/,/);
foreach (@a) {
if (/(\d+)\.\.(\d+)/){
$ref_annotation=
Mask_CDS($chr,$ref_annotation,$1,$2);
}
}
}
}
}
}
} readdir(DIR);
return ($ref_annotation)
}
############################################
### Mask_CDS
############################################
sub Mask_CDS{
my ($chr,$ref,$f,$l,$out) = @_;
for ($f..$l){
$$ref{$chr}{$_}=1;
}
return $ref;
}
############################################
### adaptAnnotationEMBL
############################################
sub adaptAnnotationEMBL{
my ($DIR,$embl,$ref_shift,$ref_results,$ref_Counting) = @_;
open(F, $DIR."/".$embl) or die "Problems open embl $embl: $!\n";
my $res;
my $resDeleted='';
my @ar;
my $in=1;
my $maybeWrong=0;
### get the name of the contig/supercontig/chromosome
my ($chr) = $embl =~ /\/{0,1}(\S+)\.embl$/;
## tag to not transfer reference sequence specific tags.
my $inTranslation=0;
# print Dumper %core;
my $OKCore=1;
my $ref_queryTarget;
my $transfer=0;
while (<F>) {
# UTR must be saved
s/3\'UTR/3TUTR/g;
s/5\'UTR/5TUTR/g;
if (/FT \S+/) {
s/<//g;
s/>//g;
}
my $line=$_;
# check if entry is over more than one line
while ($line =~ /^FT \S+\s{2,}.*\d+,$/) {
$_=<F>;
chomp($line);
/^FT \s{2,}(.*)$/;
$line.=$1;
}
if ($line =~ /^>/) {
last;
}
elsif ($line =~ /^FT \S+\s{2,}\D+(\d+)\..*\.(\d+)/ ||
$line =~ /^FT \S+\s{2,}\D+\d+,(\d+)\..*\.(\d+)/ ||
$line =~ /^FT \S+\s{2,}\D+(\d+)/
) {
### This is necessary to not mapped things, which are not covered
my $posA=$1;
my $posE=$2;
if (!defined($posE)){
$posE=$posA
}
### check if CDS
if ($line =~ /FT CDS/) {
$$ref_Counting{CDS}++
}
chomp;
($ref_results,$ref_queryTarget,$ref_Counting,$transfer)=doTransfer($ref_shift,$ref_results,$chr,$posA,$line,$ref_Counting);
$$ref_Counting{Elements}++;
### case 1, all ok
if (defined($$ref_shift{$chr}[$posA][0]) &&
defined($$ref_shift{$chr}[$posE][0]) &&
($$ref_shift{$chr}[$posE][0] eq $$ref_shift{$chr}[$posA][0]) # transfer to same query
)
{
}
### case 2, defined, but gene model is split between two query
elsif (defined($$ref_shift{$chr}[$posA][0]) &&
defined($$ref_shift{$chr}[$posE][0])
)
{
$$ref_Counting{"Split"}++;
# print "$chr position $posA $posE \n $$ref_shift{$chr}[$posA][0] // ($$ref_shift{$chr}[$posE][0] \n";
}
elsif (defined($$ref_shift{$chr}[$posA][0]) ||
defined($$ref_shift{$chr}[$posE][0])
)
{
# $$ref_Counting{"Partial"}++
}
}
elsif (/^SQ/) {
last;
}
### transfer it to the new
elsif ($transfer==1){
my $count=0;
foreach my $queryTarget (keys %$ref_queryTarget) {
$count++;
### check for the translation tag:
if ($inTranslation && /\"/) {
### do not transfer , but it is the end
$inTranslation=0
}
elsif ($inTranslation) {
### do not transfer
}
elsif (/\/translation=/) {
$inTranslation=1;
### just to double check if not smalle
if (/\".*\"/) {
$inTranslation=0
}
}
else {
if ($count>1) {
if (/\/locus_tag/ || /_id=\"/) {
#s/\"$/.$count\"/g;
my @sp=split(/\"/);
my $line=$sp[0]."\"DUP-$count".$sp[1]."\"\n";
}
$$ref_results[0]{$queryTarget} .= $line;
}
else {
$$ref_results[0]{$queryTarget} .= $_;
}
}
# end else inTransation
}
}
elsif ($transfer==0){
$$ref_results[1]{$chr} .= $_;
}
# this is the case when the annotation could be just mapped partially.
elsif ($transfer==3){
$$ref_results[1]{$chr} .= $_;
my $count=0;
### April 2011
foreach my $queryTarget (keys %$ref_queryTarget) {
$count++;
if ($count>1) {
if (/\/locus_tag/ || /_id=\"/) {
#s/\"$/.$count\"/g;
my @sp=split(/\"/);
my $line=$sp[0]."\"DUP-$count".$sp[1]."\"\n";
}
$$ref_results[0]{$queryTarget} .= $line;
}
else {
$$ref_results[0]{$queryTarget} .= $_;
}
}
}
} # end while <F>
close(F);
return ($ref_results,$ref_Counting);
}
############################################
### doTransfer
############################################
sub doTransfer{
my $ref_shift=shift;
my $ref_resultsLocal =shift;
my $chr =shift;
my $pos =shift;
my $line=shift;
my $ref_Counting = shift;
my $RENAME=47;
my $RENAME2=249;
my $chrqry=0;
# the zero will hold the no puttable
my %ResultLine;
### put complement to it, or get rid of it,
### also will need to reorder the numbers
my $wasComplement=0;
if ($line =~ /complement/){
$wasComplement=1;
$line =~ s/complement\(//g;
$line =~ s/\)$//g;
}
$line =~ s/join\(//g;
$line =~ s/\)$//g;
### here to look for missed exons
# $line =~ s/(\d+)/($$ref_shift{$chr}[$1][1])/ge;
my (@parts) = split(/\s+/,$line);
my @ar=split(/,/,$parts[2]);
my $mappedOnce=0;
my $exonMissed=0;
my $oldQuery;
my $partialCount=0;
for (my $i=0;$i < scalar(@ar);$i++) {
my $new = $ar[$i];
#single base exon
if (! ($ar[$i] =~ /\.\./) ) {
$ar[$i] =~ /(\d+)/;
my $pos=$1;
# print " single exon $ar[$i] pos is $pos \n";
# print Dumper $$ref_shift{$chr};
if (defined($$ref_shift{$chr}[$pos][0])) {
$ar[$i] =~ s/(\d+)/($$ref_shift{$chr}[$1][1])/ge;
$mappedOnce++;
$oldQuery=$$ref_shift{$chr}[$pos][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $pos;
}
else {
$ResultLine{0}[0] .= "$ar[$i],";
$exonMissed++;
}
}
else {
$ar[$i] =~ /(\d+)\.\.(\d+)/;
my $posA=$1;
my $posE=$2;
my $geneLength=int ($posE-$posA);
my $half=int ($geneLength/2);
if (defined($$ref_shift{$chr}[$posA][0]) &&
defined($$ref_shift{$chr}[$posE][0]) &&
$$ref_shift{$chr}[$posE][0] eq $$ref_shift{$chr}[$posA][0] &&
abs($$ref_shift{$chr}[$posE][1] - $$ref_shift{$chr}[$posA][1])< (2*($posE-$posA))
) {
$ar[$i] =~ s/(\d+)/($$ref_shift{$chr}[$1][1])/ge;
$mappedOnce++;
$oldQuery=$$ref_shift{$chr}[$posA][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $pos;
}
elsif (defined($$ref_shift{$chr}[$posA][0]) &&
(defined($$ref_shift{$chr}[($posA+299)][0])) &&
$$ref_shift{$chr}[($posA+299)][0] eq $$ref_shift{$chr}[$posA][0]
&& abs($$ref_shift{$chr}[($posA+299)][1] - $$ref_shift{$chr}[$posA][1])< 20000
) {
$ar[$i] = $$ref_shift{$chr}[$posA][1]."..".($$ref_shift{$chr}[($posA)][1]+$geneLength);
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posA][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $posA;
}
### left part ok
elsif (defined($$ref_shift{$chr}[$posA][0]) &&
(defined($$ref_shift{$chr}[($posA+74)][0])) &&
$$ref_shift{$chr}[($posA+74)][0] eq $$ref_shift{$chr}[$posA][0]
&& abs($$ref_shift{$chr}[($posA+74)][1] - $$ref_shift{$chr}[$posA][1])< 20000
) {
$ar[$i] = $$ref_shift{$chr}[$posA][1]."..".($$ref_shift{$chr}[($posA)][1]+$geneLength);
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posA][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $posA;
}
elsif (defined($$ref_shift{$chr}[$posA][0]) &&
(defined($$ref_shift{$chr}[($posA+14)][0])) &&
$$ref_shift{$chr}[($posA+14)][0] eq $$ref_shift{$chr}[$posA][0]
&& abs($$ref_shift{$chr}[($posA+14)][1] - $$ref_shift{$chr}[$posA][1])< 20000
) {
$ar[$i] = $$ref_shift{$chr}[$posA][1]."..".($$ref_shift{$chr}[($posA)][1]+$geneLength);
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posA][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $posA;
}
### 3' ok
elsif (defined($$ref_shift{$chr}[$posE][0]) &&
defined($$ref_shift{$chr}[($posE-299)][0]) &&
$$ref_shift{$chr}[($posE-299)][0] eq $$ref_shift{$chr}[$posE][0] &&
abs($$ref_shift{$chr}[($posE-299)][1] - $$ref_shift{$chr}[$posE][1])< 20000
) {
$ar[$i] = ($$ref_shift{$chr}[($posE)][1]-$geneLength)."..".$$ref_shift{$chr}[($posE)][1];
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posE][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $posE;
}
elsif (defined($$ref_shift{$chr}[$posE][0]) &&
defined($$ref_shift{$chr}[($posE-74)][0]) &&
$$ref_shift{$chr}[($posE-74)][0] eq $$ref_shift{$chr}[$posE][0] &&
abs($$ref_shift{$chr}[($posE-74)][1] - $$ref_shift{$chr}[$posE][1])< 20000
) {
$ar[$i] = ($$ref_shift{$chr}[($posE)][1]-$geneLength)."..".$$ref_shift{$chr}[($posE)][1];
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posE][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $posE;
}
elsif (defined($$ref_shift{$chr}[$posE][0]) &&
defined($$ref_shift{$chr}[($posE-14)][0]) &&
$$ref_shift{$chr}[($posE-14)][0] eq $$ref_shift{$chr}[$posE][0] &&
abs($$ref_shift{$chr}[($posE-14)][1] - $$ref_shift{$chr}[$posE][1])< 20000
) {
$ar[$i] = ($$ref_shift{$chr}[($posE)][1]-$geneLength)."..".$$ref_shift{$chr}[($posE)][1];
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posE][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $posE;
}
elsif (defined($$ref_shift{$chr}[($posA+$half)][0])
) {
my $start=($$ref_shift{$chr}[($posA+$half)][1]-$half);
if ($start<1) {
$start=1;
}
my $end=($$ref_shift{$chr}[($posA+$half)][1]+$half);
$ar[$i] = $start."..".$end;
print
"$start $end $half ".$$ref_shift{$chr}[($posA+$half)][1]." \n";
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[($posA+$half)][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = $end;
}
elsif (defined($$ref_shift{$chr}[$posA+$RENAME][0]) &&
defined($$ref_shift{$chr}[($posE-$RENAME)][0]) &&
$$ref_shift{$chr}[$posE-$RENAME][0] eq $$ref_shift{$chr}[$posA+$RENAME][0]
# abs($$ref_shift{$chr}[$posE][1] - $$ref_shift{$chr}[$posA][1])< (2*($posE-$posA))
# $$ref_shift{$chr}[($posE-$RENAME)][0] eq $$ref_shift{$chr}[$posE][0] &&
# abs($$ref_shift{$chr}[($posE-$RENAME)][1] - $$ref_shift{$chr}[$posE][1])< 20000
) {
# print " TST:\n";
# print Dumper $$ref_shift{$chr}[($posA+$RENAME)];
# print Dumper $$ref_shift{$chr}[($posE-$RENAME)];
$ar[$i] = $$ref_shift{$chr}[($posA+$RENAME)][1]."..".$$ref_shift{$chr}[($posE-$RENAME)][1];
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posA+$RENAME][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = ($posE-$RENAME);
}
elsif (defined($$ref_shift{$chr}[$posA+$RENAME2][0]) &&
defined($$ref_shift{$chr}[($posE-$RENAME2)][0]) &&
$$ref_shift{$chr}[$posE-$RENAME2][0] eq $$ref_shift{$chr}[$posA+$RENAME2][0]
# abs($$ref_shift{$chr}[$posE][1] - $$ref_shift{$chr}[$posA][1])< (2*($posE-$posA))
# $$ref_shift{$chr}[($posE-$RENAME2)][0] eq $$ref_shift{$chr}[$posE][0] &&
# abs($$ref_shift{$chr}[($posE-$RENAME2)][1] - $$ref_shift{$chr}[$posE][1])< 20000
) {
# print " TST:\n";
# print Dumper $$ref_shift{$chr}[($posA+$RENAME2)];
# print Dumper $$ref_shift{$chr}[($posE-$RENAME2)];
$ar[$i] = $$ref_shift{$chr}[($posA+$RENAME2)][1]."..".$$ref_shift{$chr}[($posE-$RENAME2)][1];
$mappedOnce++;
$partialCount++;
$oldQuery=$$ref_shift{$chr}[$posA+$RENAME2][0];
$ResultLine{$oldQuery."::".$chr}[0] .= "$ar[$i],";
$ResultLine{$oldQuery."::".$chr}[1] = ($posE-$RENAME2);
}
else {
$ResultLine{0}[0] .= "$ar[$i],";
$exonMissed++;
}
}
}
## default do not transfer
my $transfer=0;
#### check the amount
if ($mappedOnce ==0) {
$$ref_Counting{NotTransfered}++
}
if (($mappedOnce > 0 && $exonMissed >0)
# ||
# $partialCount>0
){
$$ref_Counting{Partial}++;
### This means, put the annotation to the BB and LB, as it is partial
if ($line =~ /FT CDS/) {
$$ref_Counting{CDSPartial}++;
$$ref_Counting{CDSNotExons}+=$exonMissed;
}
$transfer=3;
}
if ($exonMissed==0) {
$$ref_Counting{Transfered}++;
if ($line =~ /FT CDS/) {
$$ref_Counting{CDSTransfered}++;
}
### Modell fully mapped, put it just to the LB
$transfer=1;
}
else {
$$ref_Counting{ExonNotTransfered}+=$exonMissed;
$ResultLine{0}[0]=~ s/,$//g;
if ($wasComplement==1){
$ResultLine{0}[0]="complement(join($ResultLine{0}[0]))";
}
else {
$ResultLine{0}[0]="join($ResultLine{0}[0])";
}
$$ref_resultsLocal[1]{$chr}.=sprintf("%-4s %-15s %s\n",$parts[0],$parts[1],$ResultLine{0}[0]);
$ResultLine{0}=undef;
undef $ResultLine{0};
}
my %targetChr;
foreach my $trans (keys %ResultLine) {
if ($trans ne '0'){
my ($chrqryLocal,$chrpart) = $trans =~ /^(\S+)::(\S+)$/;
$targetChr{$chrqryLocal}=1;
my $pos =$ResultLine{$trans}[1];
if (!defined($chrqry)){
print $trans."\n";
print Dumper %ResultLine;
exit;
}
$ResultLine{$trans}[0]=~ s/,$//g;
my @ar=split(/\,/,$ResultLine{$trans}[0]);
my $amountJoined=(scalar(@ar)-1);
if ($amountJoined > 1){
$ResultLine{$trans}[0]="join($ResultLine{$trans}[0])";
}
if ($wasComplement==1){
$ResultLine{$trans}[0]="complement($ResultLine{$trans}[0])";
}
### check if the entry must be inversed
$chrqry=$chrqryLocal;