-
Notifications
You must be signed in to change notification settings - Fork 52
/
agat_sp_add_introns.pl
executable file
·239 lines (182 loc) · 7 KB
/
agat_sp_add_introns.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env perl
use strict;
use warnings;
use POSIX qw(strftime);
use List::MoreUtils qw(natatime);;
use Carp;
use Getopt::Long;
use Pod::Usage;
use Clone 'clone';
use AGAT::AGAT;
my $header = get_agat_header();
my $config;
my $intronID = 1;
my $opt_file;
my $opt_output=undef;
my $opt_help = 0;
my @copyARGV=@ARGV;
if ( !GetOptions( 'f|gff|ref|reffile=s' => \$opt_file,
'o|out|output=s' => \$opt_output,
'c|config=s' => \$config,
'h|help!' => \$opt_help ) )
{
pod2usage( { -message => 'Failed to parse command line',
-verbose => 1,
-exitval => 1 } );
}
# Print Help and exit
if ($opt_help) {
pod2usage( { -verbose => 99,
-exitval => 0,
-message => "$header\n" } );
}
if ( ! defined( $opt_file) ) {
pod2usage( {
-message => "$header\nMust specify at least 1 parameters:\nReference data gff3 file (--gff)\n",
-verbose => 0,
-exitval => 1 } );
}
# --- Manage config ---
$config = get_agat_config({config_file_in => $config});
# #######################
# # START Manage Option #
# #######################
my $gffout = prepare_gffout($config, $opt_output);
# #####################################
# # END Manage OPTION
# #####################################
# #######################
# >>>>>>>>>>>>>>>>>>>>>>>># MAIN #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# #######################
#PART 1
###################################
# Read input gff3 files one by one and save value in hash of list
######################
### Parse GFF input #
my ($hash_omniscient, $hash_mRNAGeneLink) = slurp_gff3_file_JD({ input => $opt_file,
config => $config });
print("Parsing Finished\n\n");
### END Parse GFF input #
#########################
my $intron_added=0;
######################
### Parse GFF input #
# get nb of each feature in omniscient;
foreach my $tag_l2 (sort keys %{$hash_omniscient->{'level2'}}){
foreach my $id_l1 (sort keys %{$hash_omniscient->{'level2'}{$tag_l2}}){
my $one_f2 = $hash_omniscient->{'level2'}{$tag_l2}{$id_l1}[0];
#######################
#get feature1 and info
my $feature_l1=undef;
my $tag_l1;
foreach my $tag_level1 (sort keys %{$hash_omniscient->{'level1'}}){
if (exists ($hash_omniscient->{'level1'}{$tag_level1}{$id_l1})){
$feature_l1=$hash_omniscient->{'level1'}{$tag_level1}{$id_l1};
$tag_l1=$tag_level1;
last;
}
}
if(! $feature_l1){print "Problem ! We didnt retrieve the level1 feature with id $id_l1\n";exit;}
#####
# get all level2
my $All_l2_single=1;
foreach my $feature_l2 ( @{$hash_omniscient->{'level2'}{$tag_l2}{$id_l1}} ){
my @introns=();
my $feature_example;
######
#get all level3
my $id_l2=lc($feature_l2->_tag_value('ID'));
if(exists_keys($hash_omniscient, ('level3','exon',$id_l2) ) ){
my $counterL3=-1;
#Initialize intron to 0 to avoid error during printing results
my $indexLast = $#{$hash_omniscient->{'level3'}{'exon'}{$id_l2}};
my @sortedList = sort {$a->start <=> $b->start} @{$hash_omniscient->{'level3'}{'exon'}{$id_l2}};
foreach my $feature_l3 ( @sortedList ){
#count number feature of tag_l3 type
$counterL3++;
################
#Manage Introns#
# from the second intron to the last (from index 1 to last index of the table sortedList)
# We go inside this loop only if we have more than 1 feature.
if($counterL3 > 0 and $counterL3 <= $indexLast){
my $intronStart = $sortedList[$counterL3-1]->end+1;
my $intronEnd = $sortedList[$counterL3]->start-1;
push @introns, ($intronStart, $intronEnd);
$feature_example=clone($sortedList[$counterL3]);
}
}# END FOREACH L3
}
#Now add introns features
if(@introns){
my $it = natatime 2, @introns;
while (my @tuple = $it->()) {
$intron_added++;
my $intron_feature = clone($feature_example);
$intron_feature->primary_tag('intron');
my $ID='intron_added-'.$intronID;
$intronID++;
create_or_replace_tag($intron_feature,'ID', $ID); #modify ID to replace by parent value
$intron_feature->start($tuple[0]);
$intron_feature->end($tuple[1]);
push (@{$hash_omniscient->{"level3"}{'intron'}{lc($id_l2)}}, $intron_feature);
}
}
}
}
}
print_omniscient( {omniscient => $hash_omniscient, output => $gffout} );
print "$intron_added introns added\nBye Bye\n";
#########################
######### END ###########
#########################
#######################################################################################################################
####################
# methods #
################
##############
############
##########
########
######
####
##
__END__
=head1 NAME
agat_sp_add_introns.pl
=head1 DESCRIPTION
The script aims to add intron features to gtf/gff file without intron features.
=head1 SYNOPSIS
agat_sp_add_introns.pl --gff infile --out outFile
agat_sp_add_introns.pl --help
=head1 OPTIONS
=over 8
=item B<--gff>, B<-f>, B<--ref> or B<-reffile>
Input GTF/GFF file.
=item B<--out>, B<--output> or B<-o>
Output GFF3 file.
=item B<-c> or B<--config>
String - Input agat config file. By default AGAT takes as input agat_config.yaml file from the working directory if any,
otherwise it takes the orignal agat_config.yaml shipped with AGAT. To get the agat_config.yaml locally type: "agat config --expose".
The --config option gives you the possibility to use your own AGAT config file (located elsewhere or named differently).
=item B<--help> or B<-h>
Display this helpful text.
=back
=head1 FEEDBACK
=head2 Did you find a bug?
Do not hesitate to report bugs to help us keep track of the bugs and their
resolution. Please use the GitHub issue tracking system available at this
address:
https://github.com/NBISweden/AGAT/issues
Ensure that the bug was not already reported by searching under Issues.
If you're unable to find an (open) issue addressing the problem, open a new one.
Try as much as possible to include in the issue when relevant:
- a clear description,
- as much relevant information as possible,
- the command used,
- a data sample,
- an explanation of the expected behaviour that is not occurring.
=head2 Do you want to contribute?
You are very welcome, visit this address for the Contributing guidelines:
https://github.com/NBISweden/AGAT/blob/master/CONTRIBUTING.md
=cut
AUTHOR - Jacques Dainat