-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmfannot
More file actions
executable file
·9016 lines (7767 loc) · 379 KB
/
mfannot
File metadata and controls
executable file
·9016 lines (7767 loc) · 379 KB
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 -- -*-Perl-*-
##############################################################################
#
# mfannot
#
# DESCRIPTION:
# Attempts to do a quick first pass at annotating a masterfile
# with meaningfull start/stop lines by calling blast on collections
# of known genes. Manual intervention is still required to check/adjust
# the new annotations.
#
# Known issues: - Annotations for START/STOP introns are interleaved (they
# are at the correct position, though).
#
##############################################################################
#############################################################################
# MFANNOT #
# #
# Copyright (C) 2008 #
# Departement de Biochimie, #
# Universite de Montreal, #
# C.P. 6128, succursale Centre-ville, #
# Montreal, Quebec, Canada, H3C 2J7 #
# #
# Programming: Natacha Beck, Pierre Rioux. #
# Old version programming: David To, Thomas Hoellinger. #
# Project management: Franz Lang (OGMP) #
# E-Mail information: Franz.Lang@Umontreal.ca #
# #
# This software is distributed under the GNU GENERAL PUBLIC LICENSE, as #
# published by the Free Software Foundation. A copy of version 2 of this #
# license should be included in a file called COPYING. If not, write to the #
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #
#############################################################################
##########################
# Initialization section #
##########################
require 5.00;
use strict;
use IO::File;
use IO::Dir;
use Cwd;
use File::Basename;
use File::Path;
use File::Spec;
use PirObject; # Pir module treatment
use Bio::AlignIO; # Bioperl (used for parsing alignement)
use Bio::SeqUtils; # Some biological tools
use Bio::Tools::CodonTable; # To create a codon table, to be used with genewise
use Bio::Matrix::IO; # Used for read matrix like PAM
use List::Util qw(min max);
use Sys::Hostname;
use FindBin;
use List::MoreUtils 'first_index';
use Data::Dumper;
sub info;
BEGIN {
# Load PirObject
PirObject->LoadDataModel("Masterfile"); # Masterfile object
PirObject->LoadDataModel("MfAnnotExternalProgs"); # Support for execution of external programs
PirObject->LoadDataModel("AnnotPairCollection"); # Also for external programs.
PirObject->LoadDataModel("ExonerateOutput"); # Parser for Exonerate output
PirObject->LoadDataModel("FlipBlastProt"); # Objects for storing ORFs predicted by Flip and kept with blast
PirObject->LoadDataModel("HypProt"); # Hypothetical protein : We believe its a protein after flip.
PirObject->LoadDataModel("HypFusion"); # Contain information about gene fusion
PirObject->LoadDataModel("Option"); # A module for options gestion
PirObject->LoadDataModel("EmptyOrf"); # Object containing Orfs having no corresponding genes
PirObject->LoadDataModel("BlastOutput"); # Model of blast results in XML (blastall -m 7)
PirObject->LoadDataModel("AlignedSeq");
PirObject->LoadDataModel("MultAlign");
PirObject->LoadDataModel("HMMsearchOutput");
} # End Begin
# Default umask
umask 027;
# Program's name and version number.
our $MFANNOT_COMMIT_DATE = "Unknown";
my $resultat = system("git -C /mfannot/ show --summary | grep Date > /dev/null 2>&1");
my $hascoredump = ($resultat & 128) >> 7; # 0 if no core dump, 1 if core dump
my $signal = $resultat & 127; # SIGNAL received by subprocess, from 0 to 127;
my $returncode = $resultat >> 8; # exit status of subprogram
if (!($returncode > 1 || $signal > 0 || $hascoredump == 1)) {
$MFANNOT_COMMIT_DATE = `git -C /mfannot/ show --summary | grep Date | head -1`;
chomp($MFANNOT_COMMIT_DATE);
}
our $VERSION = "Unknown";
my $MFANNOT_PATH = $FindBin::Bin;
$resultat = system("git -C $MFANNOT_PATH tag > /dev/null 2>&1");
$hascoredump = ($resultat & 128) >> 7; # 0 if no core dump, 1 if core dump
$signal = $resultat & 127; # SIGNAL received by subprocess, from 0 to 127;
$returncode = $resultat >> 8; # exit status of subprogram
if (!($returncode > 1 || $signal > 0 || $hascoredump == 1)) {
$VERSION = `git -C $MFANNOT_PATH tag | tail -n 1`;
chomp($VERSION);
}
our $COMMIT = "commit Unknown";
$resultat = system("git -C $MFANNOT_PATH log > /dev/null 2>&1");
$hascoredump = ($resultat & 128) >> 7; # 0 if no core dump, 1 if core dump
$signal = $resultat & 127; # SIGNAL received by subprocess, from 0 to 127;
$returncode = $resultat >> 8; # exit status of subprogram
if (!($returncode > 1 || $signal > 0 || $hascoredump == 1)) {
$COMMIT = `git -C $MFANNOT_PATH log | head -n 1`;
chomp($COMMIT);
}
our $PIR_COMMIT = "Unknown";
$resultat = system("git -C $ENV{'PIR_DATAMODEL_PATH'} log > /dev/null 2>&1");
$hascoredump = ($resultat & 128) >> 7; # 0 if no core dump, 1 if core dump
$signal = $resultat & 127; # SIGNAL received by subprocess, from 0 to 127;
$returncode = $resultat >> 8; # exit status of subprogram
if (!($returncode > 1 || $signal > 0 || $hascoredump == 1)) {
$PIR_COMMIT = `git -C $ENV{'PIR_DATAMODEL_PATH'} log | head -1`;
chomp($PIR_COMMIT);
}
our ($BASENAME) = ($0 =~ /([^\/]+)$/);
our $MAINPROGRAM_PID = $$;
# Get login name
my $USER = getpwuid($<) or getlogin or die "Can't find USER from environment!\n";
##################################
# Global variables and constants #
##################################
# Command-line args (first, original program)
my $ANNOT_STATS = {'Added' => {} }; # Use for create the header.
my $FLIPBLASTPROTS = []; # Array containing the Proteins predicted by Blast and Flip
my $HYPPROTS = []; # Array containing the Proteins predicted by Blast and Flip and reprocessed after
my $ENDO_BY_CG = {}; # Hash with endonuclease by contig
my $EMPTYORFS = []; # Array containing ORFs having no corresponding genes
my $TRI_NT_START_COUNT = {}; # Hash with count of tri_nt start.
my $FAMILY_LIST = {}; # Hash with family list.
my $TMPDIR = ""; # Temporary directory
# General options
my $DEBUG; # Debug mode. If true, display message on the screen
my $GENCODE = undef; # Genetique code
my $LOG_FILE; # Log file : store all the information about run
my $ISLOGFILE = 0; # Just to know if the $LOG_FILE is not empty (0 or 1)
my $MASTERFILE = undef; # Path for Masterfile
my $CACHE_PEPLIBRARY = {}; # Cache of all proteins in library
my @PROT_FOR_EXONERATE; # Array with homolog protein for Exonerate
my $OUTPUTFILE; # Path for the new
my $ORFPROCESS; # Allows ORF appearing in the masterfile as annotations
my $PEPLIBRARIES; # The libraries, in input, by default datagenescollection and dataorfcollection
my $EXTCONFIGFILE; # The path for configuration file, it's use for annotation of rnpB, rnl and rns;
my $MOTFILE; # The path of pattren file.
my $LVL_MOT; # Indicate level of motif identification.
my $EXTSELECTPROG; # List of name for external programs.
my $LVL_INTRON; # Indicate level of intron identification (1 or 2).
my $BLASTOUTPUT = ""; # The path for blast file results
my $BLASTEVALUECUTOFF; # Cutoff value for the blast
my $HMMVALUECUTOFF = "10e-3"; # Cutoff value for HMM
my $MINLENGTHORF; # This is the minimum length for creating an ORF with flip
my $MAXLENGTHFORGROUPINGORF; # Maximum size of a gap between 2 ORf. If 2 same ORF
my $MAXLENINTRONS; # Max intron size for exonerate.
my $MINLENEMPTYORF; # Minimum length for ORF empty orfs (non corresponding orfs)
my $OVERLAPPINGCUTOFF; # Overlapping cutoff for ORFs
my $OVERLORFOVORF; # Overlapping cutoff for ORFs
my $OVERLAPORFOVGENE; # Overlapping cutoff for ORFs
my $MININTRONSIZE; # Opposite of MAXLENGTHFORGROUPINGORF. Minimum size for intron
my $MINEXONSIZE; # Minimum exon size for intron.
my $MATRIX; # PAM or BLOSUM matrix
my $PARTIAL; # This will cause mfannot to only run a subset of all its built-in analysis
my $INSERTION; # Minimum length for report insertion.
my $LIGHT; # Don't run endonuclease search and don't search for every gene by HMM
my $USEPRM; # Set to 1 if user want to use .prm file
my $SQN_FORMAT; # Boolean if set does the converstion mf -> sqn
my $TBL_FORMAT; # Boolean if set save tbl output
my $ENDO_HASH = {}; # Hash with endonuclease list.
$ENDO_HASH->{"HNH"}++; $ENDO_HASH->{"GIY-YIG"}++ ;$ENDO_HASH->{"LAGLIDADG"}++;
$ENDO_HASH->{"ReverseTranscriptase"}++; $ENDO_HASH->{"matR"}++; $ENDO_HASH->{"matK"}++;
$ENDO_HASH->{"rpo"} = "PSEUDO"; $ENDO_HASH->{"dpo"} = "PSEUDO";
# Command line Program's paths
my $PATH = $ENV{"PATH"} || "";
my @PATH = split(/:/, $PATH);
my $HOME = $ENV{"HOME"};
my $FLIPPATH = &GetPath("flip");
my $MAKEBLASTDBPATH = &GetPath("makeblastdb");
my $BLASTPATH = &GetPath("blastp");
my $BLASTNPATH = &GetPath("blastn");
my $MUSCLEPATH = &GetPath("muscle");
my $UMACPATH = &GetPath("umac");
my $HMMBUILDPATH = &GetPath("hmmbuild");
my $HMMALIGNPATH = &GetPath("hmmalign");
my $HMMSEARCHPATH = &GetPath("hmmsearch");
my $EXONERATEPATH = &GetPath("exonerate");
my $GETORFPATH = &GetPath("getorf");
my $MF2SQNPATH = &GetPath("mf2sqn");
# First get option in order to check some variables.
&GetOptions ; # Get the different options and put them into global variables
###################################################################
# Check file existance: models, external config file, motif file. #
###################################################################
# 1. Check for models path
my @MOD_PATH = (($ENV{"MFANNOT_MOD_PATH"} || $HOME || ".") . "/MFannot_data/models");
push(@MOD_PATH,split(/:/,$ENV{"MFANNOT_MOD_PATH"})) if $ENV{"MFANNOT_MOD_PATH"};
my $MODEL_PATH = "";
foreach my $path (@MOD_PATH) {
next if !(-d $path);
$MODEL_PATH = $path;
}
#HMMmodel path
my $HMM_model_path = "$MODEL_PATH/HMM_models/id_by_gene";
die "Directory for gene identified by HMM model not found\n"
if (! -e "$HMM_model_path");
$MODEL_PATH =~ s/\/$//;
die "No path for ErpinModels and HMMweaselModels were found\n"
if !$MODEL_PATH;
# 2. Check for external config file
die "File $EXTCONFIGFILE give with option 'ext_select' doesn't exist.\n"
if (! -e "$EXTCONFIGFILE");
# 3. Check for motif file
die "File $MOTFILE give with option 'motfile' doesn't exist.\n"
if ( ! -e "$MOTFILE");
my $LISTPAT = &read_pat_file($MOTFILE) if ($LVL_MOT != 0);
# 4. Check for library path
my @LIB_PATH = (($ENV{"MFANNOT_LIB_PATH"} || $HOME || ".")); # You can add other search directories here
push(@LIB_PATH,split(/:/,$ENV{"MFANNOT_LIB_PATH"})) if $ENV{"MFANNOT_LIB_PATH"};
#Check for directory
#Check for intronic lib
my $intronic_lib = "";
foreach my $dir (@LIB_PATH) {
next if !( -e "$dir/intronic/intronic_orfs.pep" );
$intronic_lib = "$dir/intronic/intronic_orfs.pep";
last;
}
die "File not found in intronic orf library\n"
if !$intronic_lib;
#Check for family lib
my $family_lib = "";
foreach my $dir (@LIB_PATH) {
next if !( -e "$dir/family.lib" );
$family_lib = "$dir/family.lib";
last;
}
die "File not found in family library\n"
if !$family_lib;
die "Please make sure the BLASTMAT environment variable is set\n".
"to point to a directory where the blast matrices are stored.\n"
unless defined($ENV{"BLASTMAT"}) and (-d $ENV{"BLASTMAT"}) and (-f ($ENV{"BLASTMAT"} . "/PAM70"));
my $matrix_path = $ENV{"BLASTMAT"}."/PAM70";
my $parser = new Bio::Matrix::IO(-format => 'scoring',
-file => $matrix_path);
my $matrix = $parser->next_matrix;
# TODO
# Parameters that don't have (yet) options on the command-line
# E-value cutoff for HSPS
my $SHORT_HSPS_MIN_EVALUE = "1e-11";
################
# Trap Signals #
################
$SIG{'INT'} = \&SigCleanup;
$SIG{'TERM'} = \&SigCleanup;
$SIG{'HUP'} = \&SigCleanup;
$SIG{'QUIT'} = \&SigCleanup;
$SIG{'PIPE'} = \&SigCleanup;
$SIG{'ALRM'} = \&SigCleanup;
###############################
# M A I N P R O G R A M #
###############################
my $LOG_F = new IO::File ">$LOG_FILE" if $ISLOGFILE;
#----------HEADER PRINTED OUT----------------------------------------------------------
my $header = "\n".
"######################################################################\n".
"MFANNOT, ORGANELLAR GENOME ANNOTATION PROGRAM \n".
"\n".
"MFANNOT $MFANNOT_COMMIT_DATE \n".
"MFANNOT: version $VERSION \n".
"MFANNOT: $COMMIT \n".
"PirModel: $PIR_COMMIT \n".
"\n".
"Programmed by N. Beck and P. Rioux \n".
"######################################################################\n\n";
print $header;
print $LOG_F "$header" if $ISLOGFILE;
my $add_text_in_header = "";
# OPTIONS OF THE PROGRAM #
my $options ="----------------------------------\n".
" General Options \n".
"----------------------------------\n";
$options .= "Masterfile used : $MASTERFILE\n";
$options .= "New Masterfile created : $OUTPUTFILE\n";
$options .= "Genetic code : $GENCODE\n";
$options .= "Logfile : $LOG_FILE\n" if $ISLOGFILE;
$options .= "Usage of RNAweasel\n";
$options .= "The Path of configfile is : $EXTCONFIGFILE\n";
$options .= "The Path of pattern file is : $MOTFILE\n";
$options .= "Look in the peptide library $PEPLIBRARIES\n";
print $LOG_F "$options" if $ISLOGFILE; # Print options in the logfile
print $options if $DEBUG; # Print the options on the screen
# CREATE THE TMPDIR #
my $EGC_FILE = "$ENV{'EGC'}/EGC.$GENCODE";
die "No EGC file (with ncbi genetic code) was found for your genetic code '$GENCODE'\n"
if (!(-f $EGC_FILE));
$TMPDIR = "/tmp/mfannot.$$" if !$TMPDIR;
if (! -d $TMPDIR) {
mkdir($TMPDIR,0700) or die "Error: can't create working directory '$TMPDIR': $!\n";
}
# Initialization of $CODON_TABLE (Hashtable with genetic code)
my $BIOCODONTABLE = Bio::Tools::CodonTable->new( -id => $GENCODE);
die "Genetic id does not exist\n" if (not $BIOCODONTABLE->id($GENCODE));
my ($CODON_TABLE,$DEVIATION,$START_CODONS) = &CacheCodonTableWithGeneticId ($BIOCODONTABLE); # Hashtable with genetic code
my $ALTERNATIVE_ACCEPTED_START = {};
# Foreach key in $START_CODON fill $ALTERNATIVE_ACCEPTED_START
# with the alternative accepted start codon and the associated aa
foreach my $start_codon (keys %$START_CODONS) {
my $aa = $CODON_TABLE->{$start_codon};
$ALTERNATIVE_ACCEPTED_START->{$start_codon} = [$aa];
}
# Define all aa accepted as start codon based on list of alternative accepted
# and the standard genetic code
my $AA_ACCEPTED_START = {};
$AA_ACCEPTED_START = &GetAaAcceptedStartCodon($ALTERNATIVE_ACCEPTED_START, $CODON_TABLE);
# LIB CREATION #
die "'$PEPLIBRARIES' isn't a directory\n"
if (not -d $PEPLIBRARIES);
my $LIB_FILE = "$TMPDIR/library.pep";
my ($MITO_GENE_LIST,$CHLORO_GENE_LIST) = ({},{});
($CACHE_PEPLIBRARY,$MITO_GENE_LIST,$CHLORO_GENE_LIST) = &CreatePepfileWithLibrary;
die "Unable to read '$LIB_FILE'\n"
if (not -r $LIB_FILE);
# List of typiccal gene in order to determine if the genome is a mitochondiral genome or a chloroplastic genome.
my $MITO_GENE_TYP = ["cox1","cox2","cox3","cob","atp6"];
my $CHLORO_GENE_TYP = ["apcA","apcB","apcD","apcE","apcF","cpcG","petA","petB"];
# ANNOTATION #
# Print option of the annotation
print "----------------------------------\n",
" Gene Annotation \n",
"----------------------------------\n";
print $LOG_F "----------------------------------\n",
" Gene Annotation \n",
"----------------------------------\n" if $ISLOGFILE;
print "File used for motif finding is '$MOTFILE'\n" if $LVL_MOT != 0;
$options = "Options : \n"; # Options & parameters used for the blast
$options .= "Minimum size ORF, for flip running $MINLENGTHORF\n";
$options .= "Blast e-value cutoff : $BLASTEVALUECUTOFF\n";
$options .= "Minimum exon size : $MINEXONSIZE\n";
$options .= "Maximum intron size : $MAXLENGTHFORGROUPINGORF\n";
$options .= "Minimum intron size : $MININTRONSIZE\n";
$options .= "Matrix : $MATRIX\n";
$options .= "Minimum length of non corresponding ORFs : $MINLENEMPTYORF\n";
$options .= "Overlapping cutoff for non-corresponding ORFs: $OVERLAPPINGCUTOFF\n";
print $options if $DEBUG;
print $LOG_F $options if $ISLOGFILE;
# - Create a masterfile object
# - Load the annotations
# - Load the sequence of each contig
print "Parsing masterfile $MASTERFILE...\n\n";
my $pirmaster = PirObject::Masterfile->ObjectFromMasterfile($MASTERFILE,1);
my $contigs = $pirmaster->get_contigs();
my $numbercontigs = scalar (@$contigs); # Number of contigs in the masterfile
die "No contig header found in the masterfile '$MASTERFILE'\n" if $numbercontigs == 0;
my $numberannot = 0; # Number of annotations detected in the program
my $CONTIG = {};
&CleanPirmaster() if ($LVL_MOT != 1);
my $NB_PROC = &DefineNumberOfProcessor();
print "Number of contigs in Masterfile : $numbercontigs\n",
"Number of annotations (or comments) detected : $numberannot\n" if $DEBUG;
print $LOG_F "Number of contigs in Masterfile : $numbercontigs\n",
"Number of annotations (or comments) detected : $numberannot\n" if $ISLOGFILE;
# START THE PROCESS #
my $step = 1;
# Core #
if ($LVL_MOT != 1) {
# Run the programm step by, to know the right step
# Flip running, generate ORFS
print "$step) Translate (flip)...\n";
$step += 1;
&RunFlip;
# Blast the flip result with the blast
print "$step) Blast...\n";
$step += 1;
&BlastFlipVSGene;
# Parse the blast result, create a flipblastprot object
print "$step) Parse Blast Results...\n";
$step += 1;
&ParseInformationFromBlastResult;
# Selection of protein
print "$step) Select best proteins for Exonerate...\n";
$step += 1;
&SelectProteinForExo;
# Create hypothetical proteins
print "$step) Annotate genes w/o introns...\n";
$step += 1;
&FillHYPPROTSArrayWithFLIPBLASTPROTSArray;
# Annotation of all introns
if ($LVL_INTRON == 2 ) {
print "$step) Intron identification...\n";
&Annotate_Using_external_programs("IntronII,IntronI");
$step += 1;
}
# Make the alignement and give the exons
print "$step) Annotate genes with introns...\n";
$step += 1;
&FindExonsInHypProtArray;
# Organise the hypothetical proteins (find the real start)
print "$step) Identify gene fusions...\n";
$step += 1;
&TreatGeneFusion;
# Search mini exons
if (!$PARTIAL) {
print "$step) Annotate mini exons...\n";
$step += 1;
&AnnotateMiniExonsByHMM;
}
# Annotate rRNA -> Use RNAweasel for 5SrRNA and rnpB, Use HMMweasel for rns and rnl
print "$step) Annotate RNA genes...\n";
$step += 1;
&Annotate_Using_external_programs;
&AnnotateMfFromHYPPROTSArray;
# Adjust intronic boundaries with rules for type I and type II
print "$step) Adjust intron boundaries...\n";
$step += 1;
&Adjust_all_intronic_junctions;
print "$step) Identify start codons; identify gene fusions...\n";
$step += 1;
&CommentFusion;
print "$step) Find extra genes with HMM\n";
$step +=1;
$ENDO_BY_CG = &FindLowConservedGenesAndEndoByHMM();
# Process empty orfs, -> annotate empty ORFs in the masterfile (whose who correspond to something good)
if ($ORFPROCESS) {
print "$step) ORF annotation...\n";
$step += 1;
&AnnotateEmptyOrfs;
}
# Process intronical ORF -> annotate intronicale ORF not detected by Blast
print "$step) Intron ORF annotation...\n";
$step += 1;
&AnnotateIntronicOrfs($ENDO_BY_CG);
&MulticommentConfidence;
&LcIntrons;
}
if ($LVL_MOT != 0) {
print "$step) Process motifs...\n";
$step += 1;
&SearchMotAndMakeAP;
}
&ReplaceCgName if $LVL_MOT != 1;
# PROGRAMME ENDING #
print "\n----------------------------------\n",
" This is it, Folks ! \n",
"----------------------------------\n";
# Unreferencing
undef $FLIPBLASTPROTS;
undef $HYPPROTS;
undef $EMPTYORFS;
# Renumber genes with _1, _2 etc correctly this time.
&RenumberFeatures;
# Add informations in the masterfile
&LogInfo;
# Dump it
print "Writing new masterfile in $OUTPUTFILE\n";
$pirmaster->ObjectToMasterfile("$OUTPUTFILE");
&CreateTBLoutput($OUTPUTFILE) if $SQN_FORMAT || $TBL_FORMAT;
if (0) {
print "Writing PirMaster object to $OUTPUTFILE.xml\n";
$pirmaster->ObjectToFile("$OUTPUTFILE.xml");
}
if ($ISLOGFILE == 1) {
print "close Logfile : $LOG_FILE\n" if $DEBUG;
$LOG_F->close() or print "Logfile not closed\n";
}
my $filename = basename($OUTPUTFILE);
system("cp $OUTPUTFILE $TMPDIR/$filename");
exit 0;
END {
# With exit, programme will go here
# Cleanup temp directory when program exits.
return unless $$ == $MAINPROGRAM_PID; # We don't want to run this in a CHILD!
return unless defined($TMPDIR) and $TMPDIR =~ m#^/tmp/#;
print "Temporary work directory $TMPDIR NOT cleaned up ...\n" if $DEBUG;
rmtree($TMPDIR) unless $DEBUG;
}
#############################
# S U B R O U T I N E S #
#############################
#-------------------------------------------------------------#
# Subs calling at beginning before to run the core of Mfannot #
#-------------------------------------------------------------#
sub GetPath {
my $name_prog = shift;
foreach my $dir (@PATH) {
if (-f "$dir/$name_prog") {
if (-r _ && -x _) {
return "$dir/$name_prog";
}
else {
die " -> ERROR: $name_prog is not readable and executable! Please run:\n",
" chmod 755 \"$dir/ $name_prog\"\n";
}
last;
}
}
die "-> ERROR: Could not find '$name_prog' in your search path. Please install\n",
" $name_prog from the source (see INSTALL.txt).\n";
}
sub SigCleanup {
die "\nExiting: received signal \"" . $_[0] . "\".\n";
# Note that some cleanup will be performed in the END block at this point.
}
sub CreatePepfileWithLibrary {
my ($library,$pepfile) = ($PEPLIBRARIES,$LIB_FILE);
opendir (DIR, $library) or die "Cannot open directory '$library': $!\n"; # Open the directory
my @files = grep((/\.faa$/ and -f "$library/$_"), readdir(DIR)); # Keep the existing *.pep files
close (DIR); # Close the directory
my $outfh = new IO::File ">$pepfile"
or die "Can't write to library file '$pepfile': $!\n";
my $desc_to_prot = {}; # Save all proteins
my ($mito_gene,$chloro_gene) = ({},{});
# Concatenate all the file from the library
my $desc = "";
foreach my $file (@files) {
my $loc_file = "$library/$file";
my $name = $file =~ s/\.faa$//r;
my $PEP_F = new IO::File "<$loc_file" or die "Cannot open the pepfile '$loc_file': $!\n";
my $to_annot = 0;
while (my $line = <$PEP_F>) {
next if $line =~ /^;|^\s*$/;
if ($line =~ /^>/) {
die "The library file \"$loc_file\" has incorrect FASTA syntax.\nThe line faulty line is: \n$line"
if (!($line =~ m/(pt|pt_cyano|mt|mt_alpha)\s+[\S]+\s*;/i));
my $origin = lc($1);
$chloro_gene->{$name}++ if $origin =~ m/pt/ || !$to_annot;
$mito_gene->{$name}++ if $origin =~ m/mt/ || !$to_annot;
$desc = $line;
$desc =~ s/^>?\s*//;
$desc =~ s/\s*$//;
print $outfh ">$desc\n";
die "Error in library $library: duplicated FASTA header '$desc'.\n"
if exists $desc_to_prot->{$desc};
next;
}
print $outfh $line; # seq line
$line =~ s/^\s*//; # cleanup seq line
$line =~ s/\s*$//;
$desc_to_prot->{$desc} .= $line;
}
$PEP_F->close();
}
$outfh->close();
return ($desc_to_prot,$mito_gene,$chloro_gene); # cache of all proteins in library
} # End sub
sub GetOptions {
# This function is here to manipulate options
my $option = new PirObject::Option (); # Buil a new option model
$option->FillOption (); # This one build with the default option and look for a rc file
# Now The object contains all the options
# General options
$DEBUG = $option->debug; # Debug mode. If true, display message on the screen
$GENCODE = $option->genetic; # Genetique code
$MASTERFILE = $option->masterfile; # Path for Masterfile
$OUTPUTFILE = $option->outputfile; # Path for the new
$ORFPROCESS = $option->orf; # For Orf process, allowing presence or not in the masterfile
$PEPLIBRARIES = $option->pepdirectory; # List of path for peplibrary directory
$EXTCONFIGFILE = $option->ext_config; # Path for configfile
$MOTFILE = $option->motfile; # Path for pattern file.
$LVL_MOT = $option->lvlmot; # Indicate level of motif identification.
die "Option --lvlmot must be 0,1 or 2\n" if $LVL_MOT < 0 || $LVL_MOT > 2;
$EXTSELECTPROG = $option->ext_select; # List of names for external prog
$LVL_INTRON = $option->lvlintron; # Indicate level of intron identification (0,1 or 2).
die "Option --lvlintron must be 1 or 2\n" if $LVL_INTRON < 1 || $LVL_INTRON > 2;
$BLASTEVALUECUTOFF = $option->blast2; # Cutoff value for the blast
$MINLENGTHORF = $option->flip2; # This is the minimum length for creating an ORF with flip
$MAXLENGTHFORGROUPINGORF = $option->maxintronsize; # Minimum size of a gap between 2 ORf. If 2 same ORF
$MAXLENINTRONS = $option->maxintronsize; # Max intron size for exonerate.
$MINLENEMPTYORF = $option->minlenemptyorf; # Minimum length for ORF empty orfs (non corresponding orfs)
$OVERLAPPINGCUTOFF = $option->overlappingcutoff; # Overlapping cutoff for ORFs
$OVERLORFOVORF = $option->orfOVorf; # Overlapping cutoff for ORFs
$OVERLAPORFOVGENE = $option->orfOVgene; # Overlapping cutoff for ORFs
$MININTRONSIZE = $option->minintronsize; # Opposite of $MAXLENGTHFORGROUPINGORF. Minimum size for intron
$MINEXONSIZE = $option->minexonsize; # Minimum exon size for intron. In genewise.
$MATRIX = $option->matrix; # The matrix used in genewise alignement
$PARTIAL = $option->partial; # This will cause mfannot to only run a subset of all its built-in analysis
$INSERTION = $option->insertion; # Minimum length for report insertion
$LIGHT = $option->light; # Don't search for endonuclease and don't search all gene by HMM
$SQN_FORMAT = $option->sqnformat; # If true do mf -> sqn conversion
$TBL_FORMAT = $option->tblformat; # If true save tbl file
$TMPDIR = $option->tmpdir; # Temporary work directory
$USEPRM = $option->prm; # 1 if user want use .prm
if($option->islogfile and defined($option->logfile)) {
if(-w (dirname($option->logfile))) {
$ISLOGFILE = $option->islogfile; # Just to know if the $LOG_FILE is not empty (0 or 1)
$LOG_FILE = $option->logfile; # log file : store all the information about run
}
else {
print "\nThe path to your logfile \"" . ($option->logfile). "\" is not writable by you.\n";
print "No file logging will be performed\n";
$ISLOGFILE = 0;
$LOG_FILE = undef;
}
}
my $count = "";
my $name = $OUTPUTFILE;
# Checking to see if the path to the output file is writable
die "\nThe path for your outputfile is not writable by you ($name).\nPlease resolve this problem before running again\n"
if (!-w (dirname($name)));
# Determing new name of output file if file already exists
chomp ($name);
$name =~ s/\d*$//;
while (-r ("$name"."$count")) {
if ($count eq "") {$count = 1;}
else {$count++;}
}
$OUTPUTFILE = "$name"."$count";
} # End sub
sub CacheCodonTableWithGeneticId {
# This function create a file
# Having a codon table.
# ATT F....
# ATG M...
# Comment will begin with "!"
my $codontable = shift; # The bio::tools::codontable
# Parse "prot.prm" present in the current directory to take into account the deviation of the genetic code
my $initial_dir = getcwd;
my ($tri_nt_to_change,$dev_line,$start_codons) = ({},"",{});
my $protprmFile = "$initial_dir/prot.prm";
# Read `prm` file to set $tri_nt_to_change
if (-f $protprmFile && $USEPRM == 1) {
system("cp $protprmFile $TMPDIR");
my $infh = new IO::File "<$protprmFile"
or die "Can't read from file '$protprmFile': $!\n";
# Read prm file to define deviation codon
while( <$infh> ){
my $line = $_;
next if $line !~ m/^Deviation\s*=/;
$dev_line = $line;
$dev_line =~ s/^Deviation\s*=\s*//;
$dev_line =~ s/\s+$//;
my @correspondence = split(/,/,$dev_line);
foreach my $cor (@correspondence) {
my @tab = split(/=/,$cor);
$tri_nt_to_change->{uc($tab[0])} = uc($tab[1]);
}
}
$infh->close();
}
$start_codons = &read_and_change_EGC_file($tri_nt_to_change);
# Cache codon table
my $ct_cache = {};
my @letter = qw (A T C G N);
my $i = 0;
my $j = 0;
my $z = 0;
while ($i < scalar(@letter)) {
$j = 0;
while ($j < scalar(@letter)) {
$z = 0;
while ($z < scalar(@letter)) {
my $codon = $letter[$i].$letter[$j].$letter[$z];
my $aa = $codontable->translate($codon);
$ct_cache->{$codon} = $aa;
$ct_cache->{$codon} = $tri_nt_to_change->{$codon} if $tri_nt_to_change->{$codon};
$z++;
}
$j++;
}
$i++;
}
return ($ct_cache,$dev_line,$start_codons);
} # End sub CacheCodonTableWithGeneticId
sub read_and_change_EGC_file {
my $tri_nt_to_change = shift;
# Read EGC File
my $infh = new IO::File "<$EGC_FILE"
or die "Can't read from file '$EGC_FILE': $!\n";
my $outfh = new IO::File ">$TMPDIR/EGC.$GENCODE"
or die "Can't write to $TMPDIR file '$TMPDIR/EGC.$GENCODE': $!\n";
my $isHeader = 1;
my ($AAs,$Starts,$Base1,$Base2,$Base3) = ([],[].[],[],[]);
my ($Starts_l,$Base1_l,$Base2_l,$Base3_l) = ("","","","");
while ( my $line = <$infh> ) {
$isHeader = 0 if ($line =~ m/^AAs =/);
# If line is before AAs line
if ($isHeader) {
print $outfh $line;
# Otherwise
} else {
$line =~ s/\n$//;
my @this_line = split(/\s*=\s*/,$line);
my $info_for_array = $this_line[-1];
next if !$info_for_array;
my @this_array = split(//,$info_for_array);
if ($line =~ m/^AAs/) {
$AAs = \@this_array;
}
elsif ($line =~ m/^Starts/) {
$Starts = \@this_array;
$Starts_l = $line;
}
elsif ($line =~ m/^Base1/) {
$Base1 = \@this_array;
$Base1_l = $line;
}
elsif ($line =~ m/^Base2/) {
$Base2 = \@this_array;
$Base2_l = $line;
}
elsif ($line =~ m/^Base3/) {
$Base3 = \@this_array;
$Base3_l = $line;
}
}
}
$infh->close();
my $tri_nt_list = [];
for (my $i = 0; $i < @$AAs; $i++) {
my $AA = $AAs->[$i];
my $start = $Starts->[$i];
my $tri_nt = $Base1->[$i].$Base2->[$i].$Base3->[$i];
push(@$tri_nt_list,[$AA, $start, $tri_nt]);
}
my $new_AAs = "";
my $start_codons = {};
foreach my $info (@$tri_nt_list) {
my ($AA,$start,$tri_nt) = ($info->[0],$info->[1],$info->[2]);
$start_codons->{$tri_nt} = 1 if $start eq "M";
$new_AAs .= $tri_nt_to_change->{$tri_nt} ? $tri_nt_to_change->{$tri_nt} : $AA;
}
print $outfh "\nAAs = $new_AAs\n";
print $outfh "$Starts_l\n";
print $outfh "$Base1_l\n";
print $outfh "$Base2_l\n";
print $outfh "$Base3_l\n";
$outfh->close();
return $start_codons;
}
sub CleanPirmaster {
my $AP_to_rm = [];
# Check each annot push all annot to remove on $AP_to_rm, changed the other one
my $isUnique = {};
my $count = 0;
foreach my $contig (@$contigs) {
my $annotations = $contig->get_annotations();
my $contigname = $contig->get_name();
my $comments = $contig->get_namecomments() || "";
my $header = $contigname.$comments;
$isUnique->{$header}++;
die "Two contigs have same header '$header'\n" if $isUnique->{$header} > 1;
$count++;
$CONTIG->{"contig$count"} = "$header";
$contig->set_namecomments("");
$contig->set_name("contig$count");
my $seq = $contig->get_sequence();
$seq =~ s/\!//g;
$seq = $seq;
$contig->set_sequence($seq);
foreach my $AP (@$annotations) {
my $type = $AP->get_type();
my $startline = $AP->get_startline() || "";
my $endline = $AP->get_endline() || "";
my ($new_startline,$new_endline) = ("","");
my $id_AP = $1 if scalar($AP) =~ m/0x(.+)\)/;
# push (@$AP_to_rm, $id_AP) if $startline =~ m/;;\s+mfannot:/;
next if ($type eq "C");
$AP->set_type("C");
if ($endline ne "" && $startline ne "") {
$new_startline = $2 if $startline =~ m/(.+)\s*(;;.+)$/ && $2 !~ m/mfannot:/;
$AP->set_startline($new_startline);
$AP->set_startpos() if $new_startline eq "";
$new_endline = $2 if $endline =~ m/(.+)\s*(;;.+)$/ && $2 !~ m/mfannot:/;
$AP->set_endline($new_endline) ;
$AP->set_endpos() if $new_endline eq "";
push (@$AP_to_rm, $id_AP) if $new_endline eq "" && $new_startline eq "";
next;
}
elsif ($endline eq "") {
$new_startline = ";$startline ;; mfannot: no end found";
$AP->set_startline($new_startline);
next;
}
else {
$new_endline = ";$endline ;; mfannot: no start found";
$AP->set_endline($new_endline);
next;
}
}
&Remove_AP($AP_to_rm,$contig);
}
$pirmaster->ObjectToMasterfile("$TMPDIR/Masterfile_copy");
}
sub Remove_AP {
my ($AP_to_rm,$contig) = @_;
my $all_annots = $contig->get_annotations();
for (my $i = @$all_annots - 1; $i >= 0 ; $i--) {
my $contig_AP = @$all_annots[$i];
my $id_contig_AP = $1 if scalar($contig_AP) =~ m/0x(.+)\)/;
foreach my $id_rm_AP (@$AP_to_rm) {
splice(@$all_annots, $i, 1) if $id_rm_AP eq $id_contig_AP;
}
}
}
sub CreateCgFile {
my $cg = shift;
my $dir = "$TMPDIR/CgFiles";
mkdir($dir) if !(-d $dir);
my $cg_name = $cg->get_name();
my $cg_seq = $cg->get_sequence();
my $cg_file = "$dir/$cg_name";
my $cg_Fh = new IO::File ">$cg_file"
or die "Can't write to file '$cg_file': $!\n";
$cg_seq =~ s/\s+$/\n/;
print $cg_Fh ">$cg_name\n$cg_seq\n";
$cg_Fh->close();
}
sub DefineNumberOfProcessor {
my $nb_proc = "$TMPDIR/nb_proc";
system("grep processor /proc/cpuinfo > $nb_proc");
my $count_proc = 0;
my $infh = new IO::File "<$nb_proc"
or die "Can't read from file '$nb_proc': $!\n";
while (my $line = <$infh>) {
next if $line !~ m/^processor\s*:\s*\d+$/;
$count_proc++;
}
return $count_proc;
}
#-----------------------------------#
# Subs forming the core of Mfannot #
#-----------------------------------#
#------------------------#
# Subs for running flip #
#------------------------#
sub RunFlip {
# This function run flip with the masterfile
# Flip is a program generating ORF
my $cwd = cwd(); # Get current working directory name
chdir ("$TMPDIR"); # Need to change the temporary directory to run flip
# Just check if Flip outfiles exist here already, if so get rid of them
unlink("prot.lst") if (-e "prot.lst");
unlink("prot.src") if (-e "prot.src");
unlink("compl") if (-e "compl");
unlink("uncompl") if (-e "uncompl");
# Checking the see if the masterfile path is aboslute or relative
my $tmpMasterfile = "$TMPDIR/Masterfile_copy";
# Need to remove CR from masterfile, so let's make a local copy
print "Making local copy of masterfile with no CRs.\n" if $DEBUG;
my $ifh = new IO::File "<$tmpMasterfile"
or die "Cannot read from masterfile '$tmpMasterfile': $!\n";
my $ofh = new IO::File ">mf_noCr.all"
or die "Cannot write to temp file 'mf_noCr.all': $!\n"; # in TMPDIR
while (my $line = <$ifh>) {
$line =~ s/\s+$/\n/;
print $ofh $line;
}
$ofh->close();
$ifh->close();
my $to_add = $DEVIATION ne "" ? "-d '$DEVIATION'" : "";
my $cmdflip = "$FLIPPATH $to_add -m -l $MINLENGTHORF -g $GENCODE mf_noCr.all > $TMPDIR/flip.output";
# -m With this switch, flip will translate the first codon of a protein by 'M' if the codon is a start codon
system("$cmdflip >/dev/null 2>/dev/null");
print "$cmdflip\n" if $DEBUG;
chdir($cwd); # Changing back to the original directory
} # End sub
#--------------------------#
# Subs for running Blast #