-
Notifications
You must be signed in to change notification settings - Fork 4
/
ratt_correction.pm
2304 lines (1877 loc) · 55.5 KB
/
ratt_correction.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
package ratt_correction;
# tdo 01.11.09: include that when new contig, the contig with max
# overhang left, is take
#To DO lkist
# inlcude %lefthandPosContig
use 5.008008;
use strict;
use warnings;
use Data::Dumper;
no warnings "recursion";
require Exporter;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use Mapping::MappingTools ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = (
'all' => [
qw(
correctEMBL
)
]
);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( startAnnotationCorrection
startAnnotationCheck
correctEMBL
);
our $VERSION = '0.01';
my $DEBUG = 0;
my %STOP_CODON = (
'TGA' => 1,
'TAG' => 1,
'TAA' => 1
);
my $CORRECT_SPLICESITE=1;
my $CORRECT_PSEUDO=1;
my %START_CODON = ( 'ATG' => 1 );
my %SPLICE_DONOR = ( 'GT' => 1 );
my %SPLICE_ACCEPTOR = ( 'AG' => 1 );
# Below is stub documentation for your module. You'd better edit it!
=head1 NAME
Protocols::Assembly::TillingRaph - Perl extension for performing
assemblies guided with a reference.
=head1 SYNOPSIS
use ratt_Correction;
=head1 DESCRIPTION
=head1 EXPORT
startTiling
=head1 SEE ALSO
http://scratchy.internal.sanger.ac.uk/wiki/index.php/Team_133
# Preloaded methods go here.
=head1 FUNCTIONS
=head2 startTilling - This function will call all programs
Usage :
Arg [1] : LSF queue for jobs
Arg [2] : file of mapped lanes to be summarised
Arg [3] : faidx index file for reference
Example :
Description : Summarises the coverage of the mapped lanes specified in the lanes.fofn file
Returntype : none
Author : Jacqueline McQuillan E<lt>tdo@sanger.ac.uk<gt>
=cut
sub startAnnotationCheck {
my $embl = shift;
my $sequenceFile = shift;
my $ResultName = shift;
doit( $embl, $sequenceFile, $ResultName, "Check" );
}
sub startAnnotationCorrection {
my $embl = shift;
my $sequenceFile = shift;
my $ResultName = shift;
doit( $embl, $sequenceFile, $ResultName, "Correct" );
}
sub doit {
my $embl = shift;
my $sequenceFile = shift;
my $ResultName = shift;
my $method = shift;
loadConfig();
my $sequence = loadSequence($sequenceFile);
### will have the new annotation
my $newAnnotation;
### will have the stats per genes, as hash, see description
my $ref_stats;
### gff file including position of errors and done changes
my $GFFfile="";
my $revGFFfile='';
my ( $revAnnotation, $ref_revStats, , $newrevEMBL, $newEMBL );
if ( $method eq "Check" ) {
my $revSequence = reverseEMBL( $embl, $sequence, "reverse.embl" );
( $newAnnotation, $ref_stats, $GFFfile ) =
checkEMBL( $embl, $sequence );
( $revAnnotation, $ref_revStats, $revGFFfile ) =
checkEMBL( $embl . ".reverse.embl", $revSequence );
$revGFFfile = reverseGFF( $revGFFfile, $sequence );
}
elsif ( $method eq "Correct" ) {
( $newAnnotation, $ref_stats, $GFFfile, $newEMBL ) =
correctModel( $embl, $sequence );
$revGFFfile = "";
$ref_revStats = "";
}
# foreach my $gene (keys %$ref_stats) {
# print Dumper $$ref_stats{$gene}
# }
# foreach my $gene (keys %$ref_revStats) {
# print Dumper $$ref_revStats{$gene}
# }
if ( $method eq "Correct" ) {
open( F, "> $ResultName.tmp2.embl" )
or die "Couldn't write the gff file...\n";
print F $newEMBL;
close(F);
}
# write results:
open( F, "> $ResultName.Report.gff" )
or die "Couldn't write the gff file...\n";
if (defined($GFFfile)){
print F $GFFfile;
}
if (defined($revGFFfile)) {
print F $revGFFfile;
}
close(F);
my $res .= printErrorStats($ref_stats);
if ( $method eq "Check" ) {
$res .= printErrorStats($ref_revStats);
}
open( F, "> $ResultName.Report.txt" )
or die "Couldn't write the gff file...\n";
print F $res;
close(F);
print
"done.\nPlease see the file $ResultName.Report.txt for reporting the errors and the file $ResultName.Report.gff for reporting the error in Artemis or other sequence viewer.\n";
}
####################
### correctModel
####################
sub correctModel {
my $emblFile = shift;
my $originalSequence = shift;
my $revSequence = revcomp($originalSequence);
my $sequence = $originalSequence;
my $isComplement = 0;
my $ref_annotation;
my $ref_stats;
my $GFFfile;
my $ref_structure;
open( F, $emblFile )
or die "Sorry, couldn't open annotation file $emblFile: $_\n";
my @EMBL = <F>;
my $pos = 0;
my $res;
foreach (@EMBL) {
chomp;
if (/^FT CDS\s{2,}(\S+)$/) {
my $isPseudo=checkPseudo(\@EMBL,$pos);
if (!($isPseudo) ||
($CORRECT_PSEUDO)) {
my $line = $1;
if (/complement/) {
$isComplement = 1;
$sequence = $revSequence;
$line = reverseCDS( $line, length($sequence) );
}
my $ref_structure = getStructure($line);
my $cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my ( $id, $product ) = getID( \@EMBL, $pos );
if ( !defined($id) ) {
$id = $$ref_structure{start};
}
$$ref_stats{$id}{product} = $product;
debug(1,"\nWorking on $id ".$$ref_structure{pos}[0]);
if ($DEBUG > 500) {
print Dumper \@{ $$ref_structure{pos} };
}
### check for ok start codon
if ( !isStartOK($cds) ) {
debug(50,"Start is wrong");
# $GFFfile.=doGFF($$ref_structure{start},"BadStart","Start wrong",$isComplement,length($sequence));
$$ref_stats{$id}{StartBad} = 1;
$$ref_stats{$id}{error}++;
$ref_structure = correctStart(
$ref_structure, $sequence,
\%{ $$ref_stats{$id} }, \$GFFfile
);
my $ok = 0;
if ($DEBUG>50) {
print Dumper @{$$ref_structure{pos}}
}
( $ref_structure, $ok ) = extentModelUpstreamStart(
$ref_structure, $sequence,
\%{ $$ref_stats{$id} }, \$GFFfile
);
if ($DEBUG>50) {
print Dumper @{$$ref_structure{pos}}
}
if ( !$ok ) {
( $ref_structure, $ok ) = extentModelDownstreamStart(
$ref_structure, $sequence,
\%{ $$ref_stats{$id} }, \$GFFfile
);
}
if ( !$ok ) {
$GFFfile .=
doGFF( $$ref_structure{start}, "BadStart", "Start wrong",
$isComplement, length($sequence) );
$$ref_stats{$id}{StartStillBad} = 1;
$$ref_stats{$id}{errorStill}++;
}
if ($ok) {
$$ref_stats{$id}{CorrectionLog} .= " // Corrected Start";
$GFFfile .= doGFF(
$$ref_structure{start}, "CorrectStart",
"Corrected Start", $isComplement,
length($sequence)
);
}
if ($DEBUG>50) {
print Dumper @{$$ref_structure{pos}}
}
}
### check to undo old frameshifts
$ref_structure =
checkIntrons( $ref_structure, $sequence, \%{ $$ref_stats{$id} },
\$GFFfile, $isComplement );
my ( $amount, $lastPos ) =
amountWrongspliceDonors( $ref_structure, $sequence );
if ($amount && $CORRECT_SPLICESITE) {
debug( 1, "Splice donor wrong" );
$GFFfile .=
doGFF( $lastPos, "Wrong_Splice",
"Model has $amount wrong splice sites.",
$isComplement, length($sequence) );
$$ref_stats{$id}{splicesites} = $amount;
$$ref_stats{$id}{error}++;
}
### try to correct one splice site.
if ($amount ==1 && $CORRECT_SPLICESITE){
my $result = correctSplicesite(
$ref_structure, $sequence,
\%{ $$ref_stats{$id} }, \$GFFfile, $isComplement
);
}
##update the cds
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
debug(10,"Check Frame shifts");
if ($DEBUG > 500) {
print Dumper \@{ $$ref_structure{pos} };
}
##update the cds
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
debug(10,"Check Frame shifts");
if ($DEBUG > 500) {
print Dumper \@{ $$ref_structure{pos} };
}
#### Check for frameshifts;
if ( ( my $amount = getAmountFrameshifts($cds) ) ) {
$$ref_stats{$id}{frameshifts} = $amount;
$$ref_stats{$id}{error}++;
my ($ok) = 0;
$ref_structure =
checkFrameShits( $ref_structure, \%{ $$ref_stats{$id} },
$sequence );
### check if model still have framesfhits
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $amount2 = getAmountFrameshifts($cds);
if ( $amount2 == 0 ) {
$GFFfile .= doGFF(
$$ref_structure{start},
"FrameshiftCorrected",
( $amount - $amount2 ) . " frameshifts corrected.",
$isComplement,
length($sequence)
);
}
}
if ($DEBUG > 500) {
print Dumper \@{ $$ref_structure{pos} };
}
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
debug(10,"Check Length");
debug(10,length($cds)." length");
if ( !isMod3Length($cds) ) {
$GFFfile .= doGFF(
$$ref_structure{start}, "Error",
"Model length is wrong - shiftet it to correct length", $isComplement,
length($sequence)
);
$$ref_stats{$id}{length} = 1;
$$ref_stats{$id}{error}++;
my $count_=0;
debug(10,"Wrong length");
while (! isMod3Length($cds) && $count_ < 6 ){
$count_++;
debug(10,length($cds)." length");
$ref_structure=shiftStructureLast($ref_structure,1);
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
}
debug(10,length($cds)." length");
}
debug(10,"Check Stop");
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $AmountExons = scalar( @{ $$ref_structure{pos} } );
my ($Last_start,$last_end) = $$ref_structure{pos}[ ( $AmountExons - 1 ) ] =~ /(\d+)\.\.(\d+)$/;
my $lengthLastExon = ($last_end-$Last_start+1);
my $diff = ($lengthLastExon%3);
$lengthLastExon -= $diff;
my $peace;
if ($lengthLastExon<150) {
$peace=substr( $cds, (length($cds)-$lengthLastExon));
}
else {
$peace=substr( $cds, (length($cds)-150));
}
my $amountFrame = getAmountFrameshifts($peace);
if ( !isStopOK($cds) || ($amountFrame > 0 && $amountFrame < 5)) {
# print "$lengthLastExon\n";
### two case, no stop codon, so look at the end.
### more than one, find an earlier stop...
debug( 10, "Stop is wrong" );
$$ref_stats{$id}{StopBad} = 1;
$$ref_stats{$id}{error}++;
if (isStopOK($cds)) {
### sequence has Two stop codons, get of the last rid.
$ref_structure=shiftStructureLast($ref_structure,-3);
}
my $ok = 0;
debug(10,"OK $ok amountFrameshift $amountFrame");
if ( $amountFrame == 0 ) {
( $ref_structure, $ok ) = extentModelDownstreamStop(
$ref_structure, $sequence,
\%{ $$ref_stats{$id} }, \$GFFfile
);
}
else {
( $ref_structure, $ok ) = extentModelUpstreamStop(
$ref_structure, $sequence,
\%{ $$ref_stats{$id} }, \$GFFfile
);
}
### check if still stop
debug(99,"OK $ok amount $amount");
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
if ( !isStopOK($cds) ) {
$$ref_stats{$id}{StopStillBad} = 1;
$$ref_stats{$id}{errorStill}++;
$GFFfile .= doGFF(
$$ref_structure{end}, "BadStop",
"Stop wrong", $isComplement,
length($sequence)
);
}
else {
$$ref_stats{$id}{CorrectionLog} .= " // Corrected Stop";
$GFFfile .= doGFF(
$$ref_structure{end}, "CorrectStop",
"Corrected Stop", $isComplement,
length($sequence)
);
}
### check if model still have framesfhits
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $amount2 = getAmountFrameshifts($cds);
if ( $amount2 > 0 ) {
$GFFfile .= doGFF(
$$ref_structure{start}, "Frameshift",
"The model has $amount frameshifts", $isComplement,
length($sequence)
);
$$ref_stats{$id}{frameshiftsStill} = $amount2;
}
}
if ($isComplement) {
my $tmp = printStructurePos($ref_structure);
$tmp =~ /^(FT \S+\s+)(\S+)$/;
my $CDS = reverseCDS( $2, length($sequence) );
$res .= $1 . $CDS . "\n";
$sequence = $originalSequence;
$isComplement = 0;
}
else {
$res .= printStructurePos($ref_structure) . "\n";
}
} ### is pseudo
else {
$res .= $_ . "\n";
}
} # enf if
else {
$res .= $_ . "\n";
} # else if CDS
$pos++;
}
return ( $ref_annotation, $ref_stats, $GFFfile, $res );
}
####################
### checkIntrons
####################
sub checkFrameShits {
my ( $ref_structure, $ref_statsGene, $sequence ) = @_;
# 1. find the exon that has the frameshift
# 2. get the real sequence position of it.
# 3. find the needed amount of frames, as for the start
# 4. rebuild the model and call checkFrameshifts
### go through the exons
my $cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $pos=0;
while (! isStopOK(uc(substr($cds,$pos,3))) && $pos <= length($cds) ){
$pos+=3;
}
# as the function is just called, if there are frameshifts, $pos holds the start of the stop
### never the less check, if a stop was found
if ($pos >= length($cds)){
warn "checkFrameShifts was called, without a cds in the coding sequence\n";
return $ref_structure
}
else {
my $ref_structPos = getStructure2ar( $ref_structure );
### exon must stop $posFirstStop-1-1
my ($posStop,$numExon) = $$ref_structPos[$pos] =~ /^(\d+)\s(\d+)$/;
### the next "exon" will start +3 bases. All three frames should be tested.
my $foundNewExon=0;
# holds the shift position 1-3. If it is 0, means that two or more are equal
my ($start,$stop) = $$ref_structure{pos}[($numExon-1)] =~ /^(\d+)\.\.(\d+)$/;
# ( cannot be one base exon
my $midStop = ($posStop - 3); # position (absolute in str) of last ok codon
my $midStart = ($posStop + 3) ; # first possible start of new exon
my $minAllpos = 0;
my $minAll = 9999999;
my $amountF = 0;
my $newExon;
if (($stop-$midStart+1) > 150){
for my $shift ( 0 .. 2 ) {
$newExon = substr($sequence,($midStart-1+$shift),($stop-$midStart+1));
$amountF = getAmountFrameshifts($newExon);
if ($amountF < $minAll) {
( $minAll, $minAllpos ) = min( $amountF, $shift, $minAll, $minAllpos );
}
}
### change the $ref_structure, include this new exon
$ref_structure = updateStructure($ref_structure,$numExon,($midStop+2),($midStart+$minAllpos));
### check if the model has still frameshift.
### as it can be a dogy end, we have the variable $foundNewExon
### Todo This is a possible problem,
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $amountFrameShift = getAmountFrameshifts($cds);
if ($foundNewExon && $amountFrameShift > 0){
$ref_structure = checkFrameShits($ref_structure, $ref_statsGene, $sequence);
}
}
}
return $ref_structure;
}
####################
### getStructure2ar
####################
sub getStructure2ar{
my $ref_structure = shift;
my $amountExons = scalar(@{$$ref_structure{pos} });
my @ar;
my $pos;
foreach my $exons (1..$amountExons){
if ($$ref_structure{pos}[($exons-1)] =~ /^(\d+)\.\.(\d+)$/) {
### if a gene model got shiftet, the last exon might be gone, so it should be rebuild
for ($1..$2){
push @ar, "$_ $exons"
}
}
elsif ($$ref_structure{pos}[($exons-1)] =~ /^(\d+)$/) {
push @ar, "$_ $exons"
}
else {
die "Problem with getStructure2ar\n";
}
}
return \@ar
}
####################
### updateStructure
####################
sub updateStructure{
my $ref_structure = shift;
my $exon = shift;
my $midStop = shift;
my $midStart = shift;
my @new;
my $amountExons = scalar(@{$$ref_structure{pos}});
foreach my $num (1..$amountExons){
if ($num == $exon){
my ($s,$e) = $$ref_structure{pos}[($num-1)] =~ /^(\d+)\.\.(\d+)$/;
push @new, "$s..$midStop";
push @new, "$midStart..$e";
$$ref_structure{exons}++
}
else {
push @new, $$ref_structure{pos}[($num-1)];
}
}
### put the new coordantes onto the structure
$$ref_structure{pos}=\@new;
return $ref_structure
}
####################
### checkIntrons
####################
sub checkIntrons {
my ( $ref_structure, $sequence, $ref_statsGene, $ref_GFFfile,
$isComplement ) = @_;
### if an intron in %mod3=0 long, and has no stop at all, it can be taken away.
my $exons = ( scalar( @{ $$ref_structure{pos} } ) );
my @ar;
my $found = 0;
for my $number ( 0 .. ( $exons - 2 ) ) {
my ($start) = $$ref_structure{pos}[$number] =~ /(\d+)$/;
my ($end) = $$ref_structure{pos}[ ( $number + 1 ) ] =~ /^(\d+)/;
my $intron = substr( $sequence, ($start), ( $end - $start - 1 ) );
if (
isMod3Length($intron)
&& !isStopOK($intron)
&& getAmountFrameshifts($intron) == 0
&& !defined( $SPLICE_DONOR{ uc( substr( $intron, 0, 2 ) ) } )
&& !defined(
$SPLICE_ACCEPTOR{ uc( substr( $sequence, ( $end - 3 ), 2 ) ) }
)
)
{
my ($oldstart) = $$ref_structure{pos}[$number] =~ /^(\d+)/;
my ($oldend) = $$ref_structure{pos}[ ( $number + 1 ) ] =~ /(\d+)$/;
$$ref_structure{pos}[ ( $number + 1 ) ] =
$oldstart . ".." . $oldend;
$$ref_structure{pos}[ ($number) ] = '';
$found++;
}
}
if ($found) {
$$ref_statsGene{JoinExons} = $found;
$$ref_statsGene{error}--;
$$ref_GFFfile .=
doGFF( $$ref_structure{start}, "JoinedExons",
"$found introns/frameshitf were eliminated",
$isComplement, length($sequence) );
$$ref_statsGene{CorrectionLog} .= " // Joined $found introns";
my @new;
foreach ( @{ $$ref_structure{pos} } ) {
if ($_) {
push @new, $_;
}
}
$$ref_structure{pos} = \@new;
}
return $ref_structure;
}
####################
### correctStart
####################
sub printStructurePos {
my $ref_structure = shift;
my $Tag = "CDS";
if ( !defined($Tag) ) {
$Tag = "CDS";
}
my $pos = join( ',', @{ $$ref_structure{pos} } );
if ( scalar( @{ $$ref_structure{pos} } ) > 1 ) {
$pos = "join($pos)";
}
return "FT $Tag " . $pos;
}
####################
### correctStart
####################
sub correctStart {
my $ref_structure = shift;
my $sequence = shift;
my $ref_statsGene = shift;
my $ref_GFF = shift;
### we want to have less possible frameshift in the first 50aa and in total.
my $minAll = 99999;
# holds the shift position 1-3. If it is 0, means that two or more are equal
my $minAllpos = 0;
my $min150 = 99999;
my $min150pos = 0;
my $amountF = 0;
my $ref_structureN = copyStructure($ref_structure);
for ( 1 .. 3 ) {
$ref_structureN = shiftStructure( $ref_structureN, 1 );
my $cds = buildGene( \@{ $$ref_structureN{pos} }, $sequence );
$amountF = getAmountFrameshifts($cds);
( $minAll, $minAllpos ) = min( $amountF, $_, $minAll, $minAllpos );
$amountF = getAmountFrameshifts( substr( $cds, 0, 150 ) );
( $min150, $min150pos ) = min( $amountF, $_, $min150, $min150pos );
}
if ( $min150pos == "0" ) {
if ( $minAllpos != "0" ) {
$ref_structure =
correctModelShift( $ref_structure, $minAllpos, $ref_statsGene,
$ref_GFF );
}
else {
$$ref_statsGene{'CorrectionLog'} .=
" // No unique shift for model found... MANUAL check";
}
}
### one shift has a minimum
else {
$ref_structure =
correctModelShift( $ref_structure, $min150pos, $ref_statsGene,
$ref_GFF );
}
return $ref_structure;
}
####################
### copyStructure
####################
sub copyStructure {
my $ref_structure = shift;
my %new;
foreach my $key ( keys %$ref_structure ) {
if (ref($$ref_structure{$key}) eq 'ARRAY') {
foreach my $val ( @{ $$ref_structure{$key} } ) {
push @{ $new{$key} }, $val;
}
}
else {
$new{$key} = $$ref_structure{$key};
}
}
return \%new;
}
####################
### findFirstStop
####################
sub findFirstStop {
my ( $ref_structure, $sequence, $ref_statsGene, $refGFF ) = @_;
die "not implemented\n";
my $cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $pos=0;
while ( !isMod3Length($cds) && $pos <=5 ) {
debug( 5, "shift in extentModelDownstreamStop" );
$ref_structure = shiftStructureLast( $ref_structure, -1 );
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
$pos++
}
if ($pos > 3){
debug(1,"Couldn't find a good shift in extentModelDownstreamStop")
}
my $AmountExons = scalar( @{ $$ref_structure{pos} } );
my ($stopPos) = $$ref_structure{pos}[ ( $AmountExons - 1 ) ] =~ /(\d+)$/;
my $ok = walkFirstStop( ( $stopPos + 1 ), \$sequence, 1, 9991, 3 );
if ( $ok != 9991 ) {
### the +2 is necessary as it is the first position of the stopcodon...
$ref_structure = shiftStructureLast( $ref_structure, ( $ok + 2 ) );
$ok = 1;
}
else { $ok = 0 }
return ( $ref_structure, $ok );
}
####################
### extentModelDownstreamStop
####################
sub extentModelDownstreamStop {
my ( $ref_structure, $sequence, $ref_statsGene, $refGFF ) = @_;
my $cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my $pos=0;
while ( !isMod3Length($cds) && $pos <=5 ) {
debug( 5, "shift in extentModelDownstreamStop" );
$ref_structure = shiftStructureLast( $ref_structure, -1 );
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
$pos++
}
if ($pos > 3){
debug(1, "Couldn't find a good shift in extentModelDownstreamStop")
}
my $AmountExons = scalar( @{ $$ref_structure{pos} } );
my ($stopPos) = $$ref_structure{pos}[ ( $AmountExons - 1 ) ] =~ /(\d+)$/;
my $ok = walkFirstStop( ( $stopPos + 1 ), \$sequence, 1, 9991, 3 );
if ( $ok != 9991 ) {
### the +2 is necessary as it is the first position of the stopcodon...
$ref_structure = shiftStructureLast( $ref_structure, ( $ok + 2 ) );
$ok = 1;
}
else { $ok = 0 }
return ( $ref_structure, $ok );
}
####################
### extentModelUpstreamStop
####################
sub extentModelUpstreamStop {
my ( $ref_structure, $sequence, $ref_statsGene, $refGFF ) = @_;
my $cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
debug(99,"In extentModelUpstreamStop...");
debug(10,length($cds)." length"); while ( !isMod3Length($cds) ) {
$ref_structure = shiftStructureLast( $ref_structure, -1 );
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
return ( $ref_structure, 9991 );
}
debug(99,"In extentModelUpstreamStop... 2 ");
my $AmountExons = scalar( @{ $$ref_structure{pos} } );
my ($stopPos) = $$ref_structure{pos}[ ( $AmountExons - 1 ) ] =~ /(\d+)$/;
my $ok = walkFirstStop( ( $stopPos - 2 ), \$sequence, -2, 9991, -3 );
if ( $ok != 9991 ) {
### the +2 is necessary as it is the first position of the stopcodon...
$ref_structure = shiftStructureLast( $ref_structure, ( $ok + 2 ) );
$ok = 1;
}
else {
$ok = 0
}
debug(99,"In extentModelUpstreamStop... 3");
$cds = buildGene( \@{ $$ref_structure{pos} }, $sequence );
my ($Last_start,$last_end) = $$ref_structure{pos}[ ( $AmountExons - 1 ) ] =~ /(\d+)\.\.(\d+)$/;
my $lengthLastExon = ($last_end-$Last_start+1);
my $peace;
my $diff = ($lengthLastExon%3);
$lengthLastExon -= $diff;
if ($lengthLastExon<150) {
$peace=substr( $cds, (length($cds)-$lengthLastExon));
}
else {
$peace=substr( $cds, (length($cds)-150));
}
my $amount = getAmountFrameshifts( $peace );
debug(99,"In extentModelUpstreamStop ".$$ref_structure{pos}[0]);
# check this
debug(99,"In extentModelUpstreamStop: Amount of frame shifts $amount ".length($cds));
if ($amount > 0 && $amount < 5 && length($cds) > 50 ) {
$ref_structure = shiftStructureLast( $ref_structure, -3 );
( $ref_structure, $ok ) =
extentModelUpstreamStop( $ref_structure, $sequence, $ref_statsGene,
$refGFF );
}
debug(99,"In extentModelUpstreamStop: done");
return ( $ref_structure, $ok );
}
####################
### extentModelUpstreamStart
####################
sub extentModelUpstreamStart {
my ( $ref_structure, $sequence, $ref_statsGene, $refGFF ) = @_;
my ($startPos) = $$ref_structure{pos}[0] =~ /^(\d+)/;
my $ok = walkLastStart( $startPos, \$sequence, 0, 9991, -3 );
if ( $ok != 9991 ) {
$ref_structure = shiftStructure( $ref_structure, $ok );
$ok = 1;
}
else { $ok = 0 }
return ( $ref_structure, $ok );
}
####################
### extentModelDownstreamStart
####################
sub extentModelDownstreamStart {
my ( $ref_structure, $sequence, $ref_statsGene, $refGFF ) = @_;
###
my $ok=0;
my ($startPos,$endPos) = $$ref_structure{pos}[0] =~ /^(\d+)\.\.(\d+)/;
if (defined($startPos)) { # one exon genes
# if maximal distance a start codon can be looked for - half the
# gene model
my $maxSearch=int(($endPos-$startPos)/2);
$ok = walkFirstStart( $startPos, \$sequence, 0, 9991, 3, $maxSearch );
if ( $ok != 9991 ) {
$ref_structure = shiftStructure( $ref_structure, $ok );
$ok = 1;
}
else { $ok = 0 }
}
return ( $ref_structure, $ok );
}
####################
### walkStart
####################
sub walkLastStart {
my ( $check, $ref_sequence, $dist, $ok, $step ) = @_;
## recursive procedure, stops, if
### has no sequence start end sequence
### finds a stop in frame
### return the difference to the possible start to the first starting point
###
if ( $check > 3 && $check < ( length($$ref_sequence) - 2 ) ) {
my $codon = uc( substr( $$ref_sequence, ( $check - 1 ), 3 ) );
if ( isStopOK($codon) ) {
return $ok;
}
else {
if ( defined( $START_CODON{$codon} ) ) {
$ok = $dist;
}
$dist += $step;
$ok = walkLastStart( ( $check + $step ),
$ref_sequence, $dist, $ok, $step );
}
}
return $ok;
}
####################
### walkStart
####################
sub walkFirstStart {
my ( $check, $ref_sequence, $dist, $ok, $step, $maxRun ) = @_;
## recursive procedure, stops, if
### has no sequence start end sequence
### finds a stop in frame
### return the difference to the possible start to the first starting point
###
if ( $maxRun > 1 && $check > 3 && $check < ( length($$ref_sequence) - 2 ) ) {
my $codon = uc( substr( $$ref_sequence, ( $check - 1 ), 3 ) );
$maxRun-=$step;