-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmf2sqn
executable file
·1588 lines (1379 loc) · 60.9 KB
/
mf2sqn
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-*-
##############################################################################
#
# mf2sqn
#
# DESCRIPTION:
# mf2sqn - Converts an OGMP masterfile into a Sequin file in order to
# make submission
#
##############################################################################
#############################################################################
# mf2sqn #
# #
# Copyright (C) 2008 #
# Departement de Biochimie, #
# Universite de Montreal, #
# C.P. 6128, succursale Centre-ville, #
# Montreal, Quebec, Canada, H3C 2J7 #
# #
# Programming: Natacha Beck. #
# 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;
require "qualifs.pl";
use strict;
use vars qw( $VERSION $RCS_VERSION );
use PirObject; # Pir module treatment
use IO::File;
use File::Path;
use File::Copy;
use File::Basename;
use Cwd;
BEGIN {
# Load PirObject
PirObject->LoadDataModel("Masterfile"); # Masterfile object
} # End Begin
# Default umask
umask 027;
# Program's name and version number.
$RCS_VERSION='$Id: mf2sqn,v 1.21 2013/10/15 01:05:30 nbeck Exp $';
($VERSION) = ($RCS_VERSION =~ m#,v ([\w\.]+)#);
my ($BASENAME) = ($0 =~ /([^\/]+)$/);
# Get login name
my $USER = getpwuid($<) or getlogin or die "Can't find USER from environment!\n";
##################################
# Global variables and constants #
##################################
$|=1;
my $DEBUG=0;
my $TMPDIR="/tmp/$BASENAME.$$";
my $GENE_NAME={};
my $OGMP_QUALS_CLASSES= {};
my $OGMP_QUALS_TEXTS={};
my $OGMP_QUALS_USAGE={};
my $OGMP_QUALS_GENENAME={};
my $OGMP_QUALS_TO_NOTE={};
##############################
# Verification for required: #
# File and software #
##############################
# Files
my $HOME = $ENV{"HOME"};
my @LIB_PATH = (($HOME || "."));
push(@LIB_PATH,split(/:/,$ENV{"MF2SQN_LIB"})) if $ENV{"MF2SQN_LIB"};
my $GENE_NAME_FILE = "";
my $QUALIF_FILE = "";
my $SUB_INFO_MODEL = "";
my $SUBMITTERDIR = "";
foreach my $dir (@LIB_PATH) {
next if !( -e "$dir/gene_names.lst" || -e "$dir/ogmp_qualifiers.lst" ||
-e "$dir/mf2sqn_SubInfo" || -e "$dir/mf2sqn_Submitters");
$GENE_NAME_FILE = "$dir/gene_names.lst" if !$GENE_NAME_FILE && (-e "$dir/gene_names.lst");
$QUALIF_FILE = "$dir/ogmp_qualifiers.lst" if !$QUALIF_FILE && (-e "$dir/ogmp_qualifiers.lst");
$SUB_INFO_MODEL = "$dir/mf2sqn_SubInfo" if !$SUB_INFO_MODEL && (-e "$dir/mf2sqn_SubInfo");
$SUBMITTERDIR = "$dir/mf2sqn_Submitters" if !$SUBMITTERDIR && (-e "$dir/mf2sqn_Submitters");
last if $GENE_NAME_FILE && $QUALIF_FILE && $SUB_INFO_MODEL && $SUBMITTERDIR;
}
die "Doesn't find the 'gene name list' file 'gene_names.lst'. Check your installation for this one.\n"
if !$GENE_NAME_FILE;
die "Doesn't find the 'ogmp qualifiers' file 'ogmp_qualifiers.lst'. Check your installation for this one.\n"
if !$QUALIF_FILE;
die "Doesn't find the 'mf2sqn subinfo' file 'mf2sqn_SubInfo'. Check your installation for this one.\n"
if !$SUB_INFO_MODEL;
die "Doesn't find the 'mf2sqn submitters' directory 'mf2sqn_Submitters'. Check your installation for this one.\n"
if !$SUBMITTERDIR;
# Software
my $PATH = $ENV{"PATH"} || "";
my @PATH = split(/:/, $PATH);
my $TBL2ASN=&GetPath("tbl2asn");
# Basic command-line args
my $MASTERFILE=""; # The name of a file containing a genome in MF format
my $MF_NAME=""; # The name of masterfile
my $SUBINFOFILE=""; # The name of a file containing the submission information
my $AUTHOR=""; # Author name (Lang or Burger)
my $AUTHORFILE=""; # The name of a file containing autor information
my $CREATEGB=0; # Use in order to create Genbank file
my $CREATETB=0; # Use in order to create Table file
my $SUBINFO={};
my $ACCEPT_GENOME = { O => 'apicoplast',
C => 'chloroplast',
H => 'chromoplast',
Y => 'cyanelle',
E => 'extrachromosomal',
G => 'genomic',
K => 'kinetoplast',
L => 'leucoplast',
A => 'macronuclear',
M => 'mitochondrion',
N => 'nucleomorph',
D => 'plasmid',
P => 'plastid',
R => 'proplastid',
W => 'proviral',
U => 'unknown' };
my $ACCEPT_GCODE = { '1' => 'Standard',
'2' => 'Vertebrate Mitochondrial',
'3' => 'Yeast Mitochondrial',
'4' => 'Mold Mitochondrial',
'5' => 'Invertebrate Mitochondrial',
'6' => 'Ciliate Dasycladacean Hexamita Nuclear',
'9' => 'Echinoderm Flatworm Mitochondrial',
'10' => 'Euplotid Nuclear',
'11' => 'Bacterial and Plant Plastid',
'12' => 'Alternative Yeast Nuclear',
'13' => 'Ascidian Mitochondrial',
'14' => 'Alternative Flatworm Mitochondrial',
'15' => 'Blepharisma Macronuclear',
'16' => 'Chlorophycean Mitochondrial',
'21' => 'Trematode Mitochondrial',
'22' => 'Scenedesmus Obliquus Mitochondrial',
'23' => 'Thraustochytrium Mitochondrial'};
my $ACCEPT_MOLCLASS = { D => 'DNA',
R => 'RNA',
N => 'NUCLEOTIDE',
P => 'PROTEIN',
O => 'OTHER'};
my $ACCEPT_MOLTYPE = { G => 'genomic',
E => 'pre mrna',
M => 'mrna',
R => 'rrna',
T => 'trna',
S => 'snrna',
C => 'scrna',
P => 'peptide',
O => 'OTHER GENETIC',
X => 'GENOMIC MRNA'};
my $ACCEPT_TOPO = { C => 'CIRCULAR',
L => 'LINEAR',
C => 'CIRCULAR',
T => 'TANDEM',
O => 'OTHER'};
my $ACCEPT_COMPLETE = { C => 'Complete',
P => 'Partial',
NL => 'No Left',
NR => 'No Right',
NE => 'No Ends',
HL => 'Has Left',
HR => 'Has Right',
O => 'Other'};
$SUBINFO = { 'GENOME' => $ACCEPT_GENOME->{'M'},
'NUC_CODE' => '1',
'MIT_CODE' => '1',
'MOLCLASS' => $ACCEPT_MOLCLASS->{'D'},
'MOLTYPE' => $ACCEPT_MOLTYPE->{'G'},
'TOPOLOGY' => $ACCEPT_TOPO->{'C'},
'COMPLETEDNESS' => $ACCEPT_COMPLETE->{'C'}};
my $MODIFIERS = { 'TITLE' => '1',
'GENOME' => 'location',
'MIT_CODE' => 'mgcode',
'NUC_CODE' => 'gcode',
'SCIENTIFIC' => 'organism',
'STRAIN' => '1',
'COMMENT' => '1',
'MOLCLASS' => 'mol',
'MOLTYPE' => 'type',
'TOPOLOGY' => 'topology',
'COMPLETEDNESS' => 'completedness',
'CITATION' => '1'};
my $INFO = {};
# Advanced command-line args
#####################################
# Command-Line Arguments Processing #
#####################################
sub Usage {
my $message = shift || "";
print STDERR <<USAGE;
Basic usage: $BASENAME [-d] [-t] -a author -m masterfile
where: -a author : All submitter file need to be under `MF2SQN_LIB/mf2sqn_Submitters` (with MF2SQN_LIB your environment variable)
and have following format name "SubmitterName.sbt" (need to respect case).
A template can be found in "SubmitterGeneric.sbt".
Each file contain a text ASN.1 Submit-block object.
The Submit-block contains contact information (to whom questions
on the submission can be addressed) and a submission citation (which lists
the authors who get scientific credit for the sequencing).
-m masterfile : Is the masterfile for submission.
-d : Create GenBank file in output.
-t : Create Table file in output.
-g : gene_names_file.
USAGE
print STDERR "\n$message\n" if $message;
exit 20;
}
&Usage if scalar(@ARGV) == 0;
for (;@ARGV;) {
my ($opt,$arg) = ($ARGV[0] =~ /^-([\@madtg])(.*)$/o);
last if ! defined $opt;
if ($opt =~ /[\@mag]/ && $arg eq "") {
if (@ARGV < 2) {
print STDERR "Argument required for switch \"-$opt\".\n";
exit 1;
}
shift @ARGV;
$arg=$ARGV[0];
}
$DEBUG=(defined($arg) ? $arg : 1) if $opt eq '@';
$MASTERFILE=$arg if $opt eq 'm';
$AUTHOR=$arg if $opt eq 'a';
$CREATEGB=1 if $opt eq 'd';
$CREATETB=1 if $opt eq 't';
$GENE_NAME_FILE=$arg if $opt eq 'g';
shift @ARGV;
}
###########################################
# Validate remaining command-line options #
###########################################
&Usage( "Error: the masterfile '$MASTERFILE' supplied with -m doesn't exist\n" )
unless -f "$MASTERFILE";
$MF_NAME = basename($MASTERFILE);
my $MF_NAME_S = basename($MASTERFILE);
$MF_NAME_S =~ s/\.fsa/\.f_s_a_$$/g;
&Usage( "Accepted value for author is Lang or Burger")
if !(-e "$SUBMITTERDIR/Submitter$AUTHOR.sbt");
$AUTHORFILE= "$SUBMITTERDIR/Submitter$AUTHOR.sbt";
die "The author file '$AUTHORFILE' doesn't exist\n"
unless -f "$AUTHORFILE";
################
# Trap Signals #
################
sub SigCleanup { # private
die "\nExiting: received signal \"" . $_[0] . "\".\n";
# Note that some cleanup will be performed in the END block at this point.
}
$SIG{'INT'} = \&SigCleanup;
$SIG{'TERM'} = \&SigCleanup;
$SIG{'HUP'} = \&SigCleanup;
$SIG{'QUIT'} = \&SigCleanup;
$SIG{'PIPE'} = \&SigCleanup;
###############################
# M A I N P R O G R A M #
###############################
# CREATE THE TMPDIR #
mkdir($TMPDIR,0700) or die "Error: can't create work directory '$TMPDIR': $!\n";
# LOAD THE MF #
my $PIRMASTER = PirObject::Masterfile->ObjectFromMasterfile($MASTERFILE,1);
my $CONTIGS = $PIRMASTER->get_contigs();
my $NB_CONTIGS = scalar(@$CONTIGS);
my $STEP = 1;
my $to_print = ($NB_CONTIGS > 1 ? "Masterfile '$MASTERFILE' present $NB_CONTIGS contigs\n"
: "Masterfile '$MASTERFILE' present $NB_CONTIGS contig\n");
print $to_print;
print "$STEP) Look for SubInfo file\n";
&LookForSubInfo();
$STEP++;
print "$STEP) Parse information of SubInfo file\n";
&GetInfo();
$STEP++;
print "$STEP) Create fasta file for tbl2asn\n";
&MakeFastaFile();
$STEP++;
print "$STEP) Create table file for tbl2asn\n";
&ParseGeneList();
&ParseQualifList();
&CreateTableFile();
$STEP++;
print "$STEP) Run tbl2asn\n";
&RunTBL2ASN();
exit 0;
END {
# With exit, programme will go here
# Cleanup temp directory when program exits.
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 #
#############################
sub LookForSubInfo {
my $current_wd = getcwd();
my $subInfoName = "$MF_NAME.subInfo";
if (! (-r ($subInfoName))){
copy($SUB_INFO_MODEL, $subInfoName) or die "File '$SUB_INFO_MODEL' cannot be copied in '$subInfoName'.\n";
print "\nYou don't have local copy of subInfo file for '$MF_NAME'.\n"
."A local copy was made in '$subInfoName'.\n"
."Perhaps you need to make some modification.\n"
."After modification just re-run mf2sqn.\n\n";
exit 1;
}
$SUBINFOFILE = $subInfoName;
}
# Get input info
sub GetInfo {
my $INFO_F = new IO::File "<$SUBINFOFILE"
or die "Can not open the subinfo file '$SUBINFOFILE': $!\n";
my $Before_tag = 1;
my $concat = "";
my $info = {};
while (my $line = <$INFO_F>) {
next if $line =~ m/^#/;
next if $line =~ m/^\%SUBINFO\s*=\s*\(/;
chomp($line);
$concat .= $line unless $line =~ m/\);/;
}
$concat =~ s/\}/\}\n/g;
my @line_for_hash = split("\n",$concat);
foreach my $line_hash (@line_for_hash){
my $key = $1 if $line_hash =~ m/^\s*\'(.+)\'\s*=>\s*{/;
my $st_value = $1 if $line_hash =~ m/\{(.+)\}/;
$st_value =~ s/\'\s+\'/\'\n\'/g;
my @line_for_sub_hash = split(/\n/,$st_value);
my $sub_hash = {};
foreach my $line_sub (@line_for_sub_hash){
die "Can't parse line '$line_sub' in '$SUBINFOFILE'\n"
if !($line_sub =~ m/\s*\'.+\'\s*=>\s*\'.*\'/);
my ($sub_key,$sub_value) = ($1,$2) if $line_sub =~ m/\s*\'(.+)\'\s*=>\s*\'(.*)\'/;
$sub_hash->{$sub_key} = $sub_value;
}
$INFO->{$key} = $sub_hash;
}
&CheckInfoEntry();
$INFO_F->close();
}
sub CheckInfoEntry {
while (my ($tag, $hash) = each %$INFO ) {
while (my ($field, $value) = each %$hash ) {
&CheckFieldAndComp($field,"genome" ,$ACCEPT_GENOME,$value);
&CheckFieldAndComp($field,"nuc_code" ,$ACCEPT_GCODE,$value);
&CheckFieldAndComp($field,"mit_code" ,$ACCEPT_GCODE,$value);
&CheckFieldAndComp($field,"molclass" ,$ACCEPT_MOLCLASS,$value);
&CheckFieldAndComp($field,"moltype" ,$ACCEPT_MOLTYPE,$value);
&CheckFieldAndComp($field,"topology" ,$ACCEPT_TOPO,$value);
&CheckFieldAndComp($field,"completedness",$ACCEPT_COMPLETE,$value);
$SUBINFO->{'SCIENTIFIC'} = $value if lc($field) eq "scientific";
$SUBINFO->{'STRAIN'} = $value if lc($field) eq "strain";
$SUBINFO->{'TITLE'} = $value if lc($field) eq "title";
$SUBINFO->{'COMMENT'} = $value if lc($field) eq "comment";
if (lc($field) eq "citation") {
my @ids = split(",",$value);
foreach my $id (@ids) {
die "Accepted value for field 'CITATION' in $SUBINFOFILE is only number\n"
if $id =~ m/\D+/;
}
}
$SUBINFO->{'CITATION'} = $value if lc($field) eq "citation";
}
}
}
sub CheckFieldAndComp {
my ($field,$field_to_check,$accept_values,$value) = @_;
my $uc_field = uc($field);
if (lc($field) eq $field_to_check){
if (!($accept_values->{$value})){
my $list = "";
while (my ($ab,$complet) = each %$accept_values){
$list .= "$ab,";
}
$list =~ s/,$/\./;
die "Accepted value for field '$uc_field' in $SUBINFOFILE is : $list\n";
}
$SUBINFO->{'GENOME'} = $ACCEPT_GENOME->{$value} if lc($field) eq "genome";
$SUBINFO->{'NUC_CODE'} = $value if lc($field) eq "nuc_code";
$SUBINFO->{'MIT_CODE'} = $value if lc($field) eq "mit_code";
$SUBINFO->{'MOLCLASS'} = $ACCEPT_MOLCLASS->{$value} if lc($field) eq "molclass";
$SUBINFO->{'MOLTYPE'} = $ACCEPT_MOLTYPE->{$value} if lc($field) eq "moltype";
$SUBINFO->{'TOPOLOGY'} = $ACCEPT_TOPO->{$value} if lc($field) eq "topology";
$SUBINFO->{'COMPLETEDNESS'} = $ACCEPT_COMPLETE->{$value} if lc($field) eq "completedness";
}
}
# CreateFastaFile #
sub MakeFastaFile {
my $count = 0;
my $format_seq = "";
my $file_name = "$MF_NAME_S.fsa";
foreach my $contig (@$CONTIGS) {
my $seq = lc($contig->get_sequence());
$seq =~ s/!//g;
my $name = $contig->get_name();
die "Sequence of contig '$name' contain bad caracters"
if $seq =~ m/[^acgturykmswbdhvnx]/; #According with UPAC
$format_seq .= &CreateHeader($count);
for (my $i = 0 ; $i <= length($seq); $i += 80 ) {
my $rest_length = length($seq) - $i;
if ( $rest_length <= 80 ) {
$format_seq .= substr($seq, $i , $rest_length);
$format_seq .= "\n";
}
else {
$format_seq .= substr($seq, $i , 80);
$format_seq .= "\n";
}
}
$format_seq .= "\n";
$count++;
}
my $fasta_file = "$TMPDIR/$file_name";
my $FF = new IO::File ">$fasta_file" or die "Cannot open : $fasta_file\n";
print $FF "$format_seq";
$FF->close();
}
sub CreateHeader {
my $ct = shift;
my $format_header = ">C_$ct ";
my $modifiers_list = "";
while (my ($tag, $hash) = each %$INFO ) {
while (my ($field, $value) = each %$hash ) {
my $modifier = $MODIFIERS->{$field};
$value = $SUBINFO->{$field};
next if $modifier eq '1';
die "Modifier '$modifier' is not accepted in fasta header\n"
if !$modifier;
die "Field '$field' must have a value in subinfo file '$SUBINFOFILE'\n"
if !$value;
next if $modifier eq "strand";
$modifiers_list .= "[$modifier=$value]" if $modifier;
}
}
$format_header .= "$modifiers_list ";
my $title = $SUBINFO->{'TITLE'};
$format_header .= "$title\n";
return $format_header;
}
# ParseGeneList #
sub ParseGeneList {
open(GF,"<$GENE_NAME_FILE") || die "Can't read file \"$GENE_NAME_FILE\": $!\n";
my @G_FILE=<GF>;
close(GF);
@G_FILE=grep(!/^\s*#|^\s*$/,@G_FILE);
foreach my $line (@G_FILE) {
chomp($line);
my @fields=split(/\|/,$line);
if (@fields != 6) {
print "Error in $GENE_NAME_FILE: six fields needed at\n-> $line\n";
next;
}
grep(s/^\s+// && 0,@fields); # Remove leading spaces
grep(s/\s+$// && 0,@fields); # Remove trailing spaces
grep(s/^-$// && 0,@fields); # Replace dashes by empty strings.
my $names = $fields[0];
my $genetype = $fields[1] || "Unknown";
my $geneproduct = $fields[2] || "";
my $genefunction = $fields[3] || "";
my $genecomplex = $fields[4] || "";
my $geneecnumber = $fields[5] || "";
my @names=split(/=/,$names);
my $prefered=$names[0];
foreach my $name (@names) {
my $lname="\L$name";
$geneproduct = "Aspartate" if lc($geneproduct) =~ m/aspartic acid/;
$geneproduct = "Glutamate" if lc($geneproduct) =~ m/glutamic acid/;
$GENE_NAME->{$lname} = { 'NAMES' => $prefered,
'TYPES' => $genetype,
'PRODUCTS' => $geneproduct,
'FUNCTIONS' => $genefunction,
'COMPLEXES' => $genecomplex,
'ECNUMBERS' => $geneecnumber};
}
}
}
sub ParseQualifList {
open(FH,"<$QUALIF_FILE") ||
die "Package Gene_Name: Can't read file \"$QUALIF_FILE\": $!\n";
my @FILE=<FH>;
close(FH);
@FILE=grep(!/^\s*#|^\s*$/,@FILE);
foreach my $line (@FILE) {
my $n=$line;
my @fields = split(/\|\s+/,$line);
foreach my $field (@fields){
$field =~ s/^\s+//;
$field =~ s/\s+$//;
}
my ($qualifier,$classes,$text,$usage,$name) = ($fields[0],$fields[1],$fields[2],$fields[3],$fields[4]);
die "Package OGMP_Qualifs: can't parse line:\n$line" unless $qualifier;
$OGMP_QUALS_CLASSES->{$qualifier}=$classes;
$OGMP_QUALS_TEXTS->{$qualifier}=$text if $text;
$OGMP_QUALS_USAGE->{$qualifier}=$usage;
$OGMP_QUALS_GENENAME->{$qualifier}=$name;
}
$OGMP_QUALS_TO_NOTE->{"substitution"}++;$OGMP_QUALS_TO_NOTE->{"polymorph"}++;$OGMP_QUALS_TO_NOTE->{"non-silent"}++;
$OGMP_QUALS_TO_NOTE->{"deletion"}++;$OGMP_QUALS_TO_NOTE->{"repeat_element"}++;$OGMP_QUALS_TO_NOTE->{"codon_altern"}++;
$OGMP_QUALS_TO_NOTE->{"translate_altern"}++;$OGMP_QUALS_TO_NOTE->{"organization"}++;
}
# CreateTableFile #
sub CreateTableFile {
my $count = 0;
my $file_name = "$MF_NAME_S.tbl";
my $feature_tab = "";
foreach my $contig (@$CONTIGS) {
my $annotations = $contig->get_annotations();
my $seq = $contig->get_sequence();
my $seq_length = $contig->get_sequencelength();
my $PubMedId = $SUBINFO->{'CITATION'};
my @PubMedId = split(",",$PubMedId);
my $Already_annot = {}; # Hash use for trans-spliced gene.
my $annots_no_comment = [];
$feature_tab .= ">Feature C_$count Table1\n";
foreach my $id (@PubMedId) {
my $seq_len = $contig->get_sequencelength();
$feature_tab .= "1\t$seq_len\tREFERENCE\n";
$feature_tab .= "\t\t\tPubMed\t$id\n";
}
my ($cg_orfs,$ct_orfs) = ({},{});
foreach my $annot (@$annotations) {
my $type = $annot->get_type();
my $name = $annot->get_genename();
next if $type eq "C";
$cg_orfs->{$name}++ if defined($name) && $name =~ m/orf\d+/;
push(@$annots_no_comment,$annot);
}
my $AP_key = {};
foreach my $annot (@$annots_no_comment) {
next if ($annot->get_type() ne "G" && $annot->get_type() ne "S");
my $id_AP = $1 if scalar($annot) =~ m/0x(.+)\)/;
$AP_key->{$id_AP} += 1;
next if $AP_key->{$id_AP} != 1;
my $name = $annot->get_genename();
$name = $1 if $name =~ m#(.+)_[a-z]$#;
next if $name =~ m/byp\d+$/;
$feature_tab = &AddCitationInfo($annot,$feature_tab) if ($name =~ m/citation/i);
next if $name =~ m/citation/i;
next if $Already_annot->{$name};
my $start = $annot->get_startpos();
my $end = $annot->get_endpos();
# Annotation of gene
$ct_orfs->{$name}++ if $cg_orfs->{$name} && $cg_orfs->{$name} > 1;
my $annot_who_overlap = &WhatOverlapsThis($start,$end,$contig);
my $orfnum = $ct_orfs->{$name};
($feature_tab,$Already_annot) = &AddInfoForGene($annot_who_overlap,$feature_tab,$name,$orfnum,$contig,$Already_annot,$seq_length);
}
$feature_tab .= "\n";
$count++;
}
my $table_file = "$TMPDIR/$file_name";
my $TF = new IO::File ">$table_file" or die "Cannot open : $table_file\n";
print $TF "$feature_tab";
$TF->close();
}
sub AddInfoForGene {
my ($annot_who_overlap,$feature_tab,$name,$orfnum,$contig,$Already_annot,$seq_length) = @_;
# Separate X element
my ($annot_who_overlap_w_byp,$annot_byp) = ((),());
foreach my $annotation_who_overlap (@$annot_who_overlap){
if (scalar(@$annotation_who_overlap) != 1) {
push(@$annot_who_overlap_w_byp,$annotation_who_overlap);
next;
}
foreach my $feature_who_overlap (@$annotation_who_overlap) {
my $annot = $feature_who_overlap->[2];
my $a_name = $annot->get_genename();
($a_name =~ m/byp\d+$/) ? push(@$annot_byp,$annot) : push(@$annot_who_overlap_w_byp,$annotation_who_overlap);
}
}
my $AP_key = {};
my $X_key = {};
foreach my $annotation_who_overlap (@$annot_who_overlap_w_byp){
my ($CDS_info,$toAdd,$EI_info,$CIT_info,$id) = ("","","","","");
my ($exons,$introns,$gene,$citation) = ({},{},{},{});
my ($g_noStart,$g_noStop,$g_genequalifs,$g_CDSqualifs,$g_isTransSplice,$g_type) = ("","","","","","");
foreach my $feature_who_overlap (@$annotation_who_overlap) {
my $annot = $feature_who_overlap->[2];
my $a_suffix = "";
my $a_name = $annot->get_genename();
($a_name,$a_suffix) = ($1,"_$2") if $a_name =~ m#(.+)_([a-z])#;
my $id_AP = $1 if scalar($annot) =~ m/0x(.+)\)/;
$AP_key->{$id_AP} += 1;
my $already_treat = $AP_key->{$id_AP} != 1 ? 1 : 0;
last if $already_treat == 1 ;
next if $a_name =~ m/citation/i;
my $a_type = $annot->get_type();
my $start = $annot->get_startpos();
my $end = $annot->get_endpos();
my $startline = $annot->get_startline();
my $isMinus = ( $annot->get_direction eq "==>" ? 0 : 1);
if ($annot_byp) {
@$annot_byp = $isMinus ? sort { $b->get_startpos() <=> $a->get_startpos() } @$annot_byp
: sort { $a->get_startpos() <=> $b->get_startpos() } @$annot_byp;
}
$id =" G-$name" if $a_type eq "G";
my $add = $orfnum ? "_$orfnum" : "";
$add = !$add && $startline =~ m/G-\S+(_\d+)/ ? $1 : $add;
my $ac = $startline =~ m/G-trn.\((.+)\)/ ? lc($1) : "" ;
$ac =~ tr/u/t/;
$ac = "\($ac\)" if $ac;
my $lname = lc($name);
my $product = $GENE_NAME->{$lname}->{"PRODUCTS"} || "";
$product = "tRNA X" if $lname =~ m/trnx/;
my $function = $GENE_NAME->{$lname}->{"FUNCTIONS"} || "";
my $EC_number = $GENE_NAME->{$lname}->{"ECNUMBERS"} || "";
my $type = $GENE_NAME->{$lname}->{"TYPES"} || "";
$type = $type eq "protein" ? "CDS" : $type;
($product,$type) = ("hypothetical protein","CDS") if $name =~ m/orf/;
next if $name ne $a_name;
my ($gene_qualifs,$CDS_qualifs,$none_qualifs,
$noStart,$noStop,
$isTransSplice,$haveRef,$isPseudo,$FKey,
$CDS_notes,$gene_notes,$none_notes)
= &TreatQualifiers($annot);
$noStart = "" if !$noStart;
$noStop = "" if !$noStop;
my $all_notes = [@$CDS_notes,@$gene_notes,@$none_notes];
# Add information about Sig
if ($a_type eq "S") {
$feature_tab = &TreatMotAndSig($feature_tab,$noStart,$start,$g_noStop,$end,$a_name,$none_qualifs,$FKey,$add,$startline,$a_type,$all_notes);
}
elsif ($a_type eq "G") {
($g_noStart,$g_noStop,$g_genequalifs,$g_CDSqualifs,$g_isTransSplice,$g_type)
= ($noStart ,$noStop ,$gene_qualifs ,$CDS_qualifs ,$isTransSplice ,$type);
# Add information about Var
if ($a_name =~ m/^var/i) {
$feature_tab = &TreatVar($feature_tab,$noStart,$start,$g_noStop,$end,$a_name,$none_qualifs,$all_notes);
next;
}
# Add information about Mot
elsif ($a_name =~ m/^mot/i) {
$feature_tab = &TreatMotAndSig($feature_tab,$noStart,$start,$g_noStop,$end,$a_name,$none_qualifs,$FKey,$add,$startline,$a_type,$all_notes);
next;
}
# Add gene with introns
elsif ($isTransSplice) {
$Already_annot->{$name}++;
($exons,$introns,$gene,$citation,$CDS_qualifs) = &TreatTransSplice($contig,$name,$annotation_who_overlap);
$feature_tab = &AddGeneInfo($gene,$name,$add,$ac,$feature_tab,$g_noStart,$g_noStop,$gene_notes); # A verifier
}
# Add gene if without transplice just gene information
else {
my $type = $name =~ m/RNA/ ? "misc_RNA" : "gene";
$feature_tab .= $noStart.$start."\t".$g_noStop.$end."\t$type\n";
$feature_tab .= "\t\t\tgene\t$name$a_suffix$ac\n";
my $frag_name = $a_suffix;
$frag_name =~ s/_//;
$feature_tab .= $gene_qualifs if $gene_qualifs;
$feature_tab .= $CDS_qualifs if $CDS_qualifs && $isPseudo;
push(@$gene_notes,"fragment $frag_name") if $frag_name;
my $cp_num = $add;
$cp_num =~ s/_//;
push(@$gene_notes,"copy $cp_num") if $cp_num;
my $fusion_note = join("; ",@$gene_notes);
$feature_tab .= "\t\t\tnote\t$fusion_note\n" if $fusion_note;
$CIT_info .= "$start\t$end\tREFERENCE\n" if $haveRef;
$CIT_info .= "\t\t\tPubMed\t$haveRef\n" if $haveRef;
}
# CDS info
next if $isPseudo == 1;
my $anticodon = &LocateAc($annot,$contig) if $ac;
$toAdd .= "\t\t\tproduct\t$product\n\t\t\tprotein_id\tlcl|${id}$add\n";
$toAdd .= "\t\t\tEC_number\t$EC_number\n" if $EC_number;
$toAdd .= $anticodon if $anticodon;
next if (scalar(@$annotation_who_overlap) != 1);
# last argument is num_exon in case of gene without exon/intron value is set to 0.
$CDS_info = &InsertCoordinateOfBypElement($annot_byp,$annot->get_endpos(),$start,$noStart,$end,$g_noStop,$type,$isMinus,$CDS_info,0);
my $fusion_note = join("; ",@$CDS_notes,@$none_notes);
$CDS_info .= $toAdd;
$CDS_info .= "\t\t\tnote\t$fusion_note\n" if $fusion_note;
$CDS_info .= $CDS_qualifs if $CDS_qualifs;
}
# Add information about exon or intron
next if ($a_type ne "E" && $a_type ne "I");
next if $g_isTransSplice;
my $fusion_note = join("; ",@$CDS_notes) || undef;
$CDS_qualifs .= "\t\t\tnote\t$fusion_note\n" if $fusion_note;
# Add information about exon
if ($a_type eq "E") {
next unless $startline =~ m#G-(\S+)-E(\d+)#;
my $exnum = $2;
$exons->{$exnum} = { 'start' => $start , 'end' => $end,
'type' => $type, 'qualifs' => $CDS_qualifs, 'direction' => $annot->get_direction() };
next if !$haveRef;
$CIT_info .= "$start\t$end\tREFERENCE\n";
$CIT_info .= "\t\t\tPubMed\t$haveRef\n";
}
# Add information about intron
else {
next unless $startline =~ m#G-(\S+)-I(\d+)#;
my $innum = $2;
$introns->{$innum} = { 'start' => $start , 'end' => $end,
'qualifs' => $CDS_qualifs};
next if !$haveRef;
$CIT_info .= "$start\t$end\tREFERENCE\n";
$CIT_info .= "\t\t\tPubMed\t$haveRef\n";
}
}
my $X_info = "";
foreach my $byp_elem (@$annot_byp) {
my $id_X = $1 if scalar($byp_elem) =~ m/0x(.+)\)/;
$X_key->{$id_X} += 1;
my $already_treat = $X_key->{$id_X} != 1 ? 1 : 0;
last if $already_treat == 1 ;
my $start = $byp_elem->get_startpos();
my $end = $byp_elem->get_endpos();
my $name = $byp_elem->get_genename();
$name = $1 if $name =~ m#(\S+)-X(\d+)#;
$X_info .= $start."\t".$end."\tmisc_feature\n";
$X_info .= "\t\tgene\t$name\n";
$X_info .= "\t\tnote\tnon\-coding translational bypass region in mRNA due to secondary structure formation\n";
}
# Added information about CDS, exons/introns and Citation
($CDS_info,$EI_info) = &AddExonsInfo($exons,$CDS_info,$EI_info,$g_noStart,$g_noStop,$g_CDSqualifs,$toAdd,$g_type,$annot_byp,$seq_length);
($CDS_info,$EI_info) = &AddIntronsInfo($introns,$CDS_info,$EI_info) if !$g_isTransSplice;
($CDS_info,$EI_info) = &AddIntronsInfoForTranspliced($introns,$CDS_info,$EI_info) if $g_isTransSplice;
$CIT_info = &AddCitationsInfo($citation) if scalar(keys %$citation);
$feature_tab .= $CDS_info;
$feature_tab .= $EI_info;
$feature_tab .= $X_info;
$feature_tab .= $CIT_info;
}
return ($feature_tab,$Already_annot);
}
sub InsertCoordinateOfBypElement {
my ($annot_byp,$annot_endpos,$start,$noStart,$end,$g_noStop,$type,$isMinus,$CDS_info,$num_exon) = @_;
my $nb_elem = $annot_byp ? scalar(@$annot_byp) : 0 ;
if ($nb_elem != 0) {
my $cnt = 0;
foreach my $byp_elem (@$annot_byp) {
# First
if ($cnt == 0) {
$end = $isMinus ? $byp_elem->get_startpos() + 1 : $byp_elem->get_startpos() - 1;
}
# Other
elsif ( $cnt != 0 ) {
$start = $isMinus ? @$annot_byp[$cnt-1]->get_endpos() - 1 : @$annot_byp[$cnt-1]->get_endpos() + 1;
$end = $isMinus ? $byp_elem->get_startpos() + 1 : $byp_elem->get_startpos() - 1;
}
$type = "" if $cnt != 0 || $num_exon > 1;
$CDS_info .= $noStart.$start."\t".$g_noStop.$end."\t$type\n";
# Last
if ($cnt + 1 == $nb_elem) {
$start = $isMinus ? @$annot_byp[$cnt]->get_endpos() - 1 : @$annot_byp[$cnt]->get_endpos() + 1;
$end = $annot_endpos;
$CDS_info .= $noStart.$start."\t".$g_noStop.$end."\n";
}
$cnt++;
}
} else {
$CDS_info .= $noStart.$start."\t".$g_noStop.$end."\t$type\n";
}
return $CDS_info;
}
sub TreatQualifiers {
my ($annot,$isCitation) = @_;
my $startline = $annot->get_startline();
my $name = $annot->get_genename();
my $multicomment = $annot->get_startmulticomment();
my $isMinus = ($annot->get_direction eq "==>" ? 0 : 1);
my $start = $annot->get_startpos();
my $end = $annot->get_endpos();
my $type = $annot->get_type();
foreach my $comment (@$multicomment) {
$startline =~ s/\\\s*$//;
$comment =~ s/^;;\s*/ /;
$startline .= $comment;
}
my $string_for_qualif = $startline;
$string_for_qualif =~ s/;;.+$//;
$string_for_qualif = $string_for_qualif =~ m/(<==|==>)\s*(start|point)(.+)/ ? $3 : "";
# If qualifier invalid, it is ignored
my %qualifs = ();
my $err_mess = "";
if( $err_mess = &ParseQualifiers( $name, $string_for_qualif, \%qualifs ) ){
chomp($err_mess);
print "Warning: $err_mess This qualifier is ignored.\n";
}
return \%qualifs if $isCitation;
my ($gene_toAdd,$CDS_toAdd,$none_toAdd,$noStart,$noStop,$isTransSplice,$haveRef,$isPseudo,$FKey,$CDS_notes,$gene_notes,$none_notes)
= &TreatNote(\%qualifs,$isMinus,$start,$type,$name,$startline);
return ($gene_toAdd,$CDS_toAdd,$none_toAdd,$noStart,$noStop,$isTransSplice,$haveRef,$isPseudo || 0,$FKey || "",$CDS_notes,$gene_notes,$none_notes);
}
sub TreatNote {
my ($qualifs,$isMinus,$aa_start,$type,$name,$startline) = @_;
my ($CDS_toAdd,$gene_toAdd,$none_toAdd,$Fkey) = ("","","","","");
my ($CDS_notes,$gene_notes,$none_notes) = ([],[],[]);
my ($noStart,$noStop,$isTransSplice,$haveRef) = ("","","","","");
my $aa_end = $isMinus ? $aa_start - 2 : $aa_start + 2;
my $isIntronic = 1 if $startline =~ m/G-\w+-I\d+-\w+/;
my $isPseudo = 0;
while ( my ($qualif, $value) = each(%$qualifs) ) {
my $class = $OGMP_QUALS_CLASSES->{$qualif};
my $usage = $OGMP_QUALS_USAGE->{$qualif};
my $aut_name = lc($OGMP_QUALS_GENENAME->{$qualif});
my $forGene = 1 if $usage eq "gene";
my $forCDS = 1 if $usage eq "CDS" || $usage eq "all";
$forCDS = 1 if $usage eq "intron"; #Special case for intron
my $forNone = 1 if $usage eq "none";
# Validation of qualifiers
if ($usage ne "none") {
if ($type eq "I") {
print "Qualifiers : '$qualif' isn't accepted for Intron\n"
if $usage ne "intron" && $usage ne "all";
next if $usage ne "intron" && $usage ne "all";
}
else {
if ($aut_name =~ m/intronic ORF/) {
$aut_name = "ORF";
print "Qualifier : '$qualif' is only acepted for intronic gene\n"
if !$isIntronic;
next if !$isIntronic;
}
print "Qualifier : '$qualif' isn't accepted for '$name' is only accepted for $aut_name\n"
if $aut_name ne "all" && lc($name) !~ m/$aut_name/;
next if $aut_name ne "all" && lc($name) !~ m/$aut_name/;
}
}
# Treatment of qualifiers
$isPseudo = 1 if $qualif eq "pseudo";
if ($qualif eq "trans-spliced") {
$isTransSplice = 1;
}
elsif ($qualif eq "citation") {
$haveRef = $value;
}
elsif ($qualif eq "first_aa") {
my $first_aa_info = "(pos:$aa_start..$aa_end, aa : $value)";
$CDS_toAdd .= "\t\t\ttransl_except\t$first_aa_info\n";
}
elsif ($qualif eq "substitution") {
my $replacement = $qualifs->{"substitution"} =~ /==>\s*(.+)/ ? lc($1) : "";
$none_toAdd .= "\t\t\treplace\t$replacement\n";
}
elsif ($qualif eq "tandem") {
$none_toAdd .= "\t\t\trpt_type\ttandem\n";
}
elsif ($qualif eq "stem_loop") {
$Fkey = $qualif;
}
elsif ($qualif eq "repeat_element" || $qualif eq "repeat_region") {
$Fkey = "repeat_region";
}
elsif ($class ne "note") {
$noStart = "<" if $qualif eq "nostart";
$noStop = ">" if $qualif eq "nostop";
if ($qualif ne "nostart" && $qualif ne "nostop") {
$qualif = "gene_synonym" if $qualif eq "synonym";
$gene_toAdd .= "\t\t\t$qualif\t$value\n" if $forGene;
$CDS_toAdd .= "\t\t\t$qualif\t$value\n" if $forCDS;
}
}
else {
my $desc = $OGMP_QUALS_TEXTS->{$qualif} || "";
$desc =~ s/\%s/$value/;
push(@$CDS_notes,$desc) if $forCDS;
push(@$gene_notes,$desc) if $forGene;
push(@$none_notes,$desc) if $forNone;
}
}
$Fkey = "polyA_site" if ($type eq "S" && ($name =~ m/polyA/i || $name =~ m/polyadenylation/i));
# If annot is intron or exon
if ($type eq "I" || $type eq "E") {
push(@$CDS_notes,@$gene_notes);
$gene_notes = [];
}
return ($gene_toAdd,$CDS_toAdd,$none_toAdd,$noStart,$noStop,$isTransSplice,$haveRef,$isPseudo,$Fkey,$CDS_notes,$gene_notes,$none_notes);
}
sub TreatVar {
my ($feature_tab,$noStart,$start,$g_noStop,$end,$a_name,$none_qualifs,$notes) = @_;
$feature_tab .= $noStart.$start."\t".$g_noStop.$end."\tvariation\n";
$feature_tab .= $none_qualifs;