-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_cut_sequences.pl
executable file
·348 lines (274 loc) · 9.66 KB
/
_cut_sequences.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case);
use File::Copy qw(cp);
# Takes a GFF & FASTA pair of files and produces FASTA files with
# cDNA, CDS nucl & pep sequences. Optionally it can take also a
# GFF patch that modifies the input GFF.
# Note: also creates a FASTA index file (.fai)
#
# Uses external software: gffread [https://f1000research.com/articles/9-304/v2]
# Copyright [2021-23]
# EMBL-European Bioinformatics Institute & Estacion Experimental de Aula Dei-CSIC
# perl _cut_sequences.pl -sp oryza_sativa -fa Oryza_sativa.IRGSP-1.0.dna.toplevel.fa \
# -gf Oryza_sativa.IRGSP-1.0.51.gff3
my $GFFREADEXE = 'gffread'; # v0.12.7
my ( $help, $nored, $gffreadpath, $sp1, $fasta1) = (0, 0);
my ( $minlen, $gff1, $patchgff1, $tname, $outpath ) = (0, '', '');
my $patched_gff_filename = '';
GetOptions(
"help|?" => \$help,
"sp|species=s" => \$sp1,
"fa|fasta=s" => \$fasta1,
"gf|gff=s" => \$gff1,
"pt|patch=s" => \$patchgff1,
"l|minlen=i" => \$minlen,
"nr|n" => \$nored,
"p|path=s" => \$gffreadpath,
"o|outpath=s" => \$outpath
) || help_message();
sub help_message {
print "\nusage: $0 [options]\n\n"
. "-sp binomial/trinomial species name (required, example: -sp oryza_sativa, used to name outfiles)\n"
. "-fa genome FASTA filename (required, example: -fa oryza_sativa.fna)\n"
. "-gf GFF filename (required, example: -gf oryza_sativa.RAPDB.gff)\n"
. "-pt patch GFF filename (optional, example: -pt oryza_sativa.RAPDB.patch.gff,\n"
. " creates oryza_sativa.RAPDB.patched.gff)\n"
. "-l min length (bp) of features (optional, example: -l 100)\n"
. "-nr remove redundancy in seq names (optional, ie 'gene:ONIVA01G00100')\n"
. "-p path to gffread binary (optional, default: $GFFREADEXE)\n"
. "-o path to output folder (optional, default current folder)\n\n"
}
if($help || (!$sp1 || !$fasta1 || !$gff1)){
help_message();
exit(0);
}
if(!-s $fasta1 || !-s $gff1){
print "# ERROR: please make sure all input files exist and have content\n";
exit(-1);
}
if($patchgff1 && !-e $patchgff1){
print "# ERROR: please make sure patch GFF file exists\n";
exit(-2);
}
if(!$gffreadpath){
$gffreadpath = $GFFREADEXE;
}
if($minlen < 1){
$minlen = 0
}
print "\n# $0 -sp $sp1 -fa $fasta1 -gf $gff1 -pt $patchgff1 " .
"-l $minlen -nr $nored -path $gffreadpath\n\n";
# set output filenames
my $cdnafile = "$sp1.cdna.fna";
my $cdsfile = "$sp1.cds.fna";
my $pepfile = "$sp1.cds.faa";
if($patchgff1){
$cdnafile = "$sp1.patch.cdna.fna";
$cdsfile = "$sp1.patch.cds.fna";
$pepfile = "$sp1.patch.cds.faa";
$patched_gff_filename = $gff1;
$patched_gff_filename =~ s/\.gff$/.patched.gff/;
}
if($outpath) {
$cdnafile = "$outpath/$cdnafile";
$cdsfile = "$outpath/$cdsfile";
$pepfile = "$outpath/$pepfile";
}
# only patch if required
if(-s $patchgff1) {
my $num_patches = patch_gff($gff1, $patchgff1, $patched_gff_filename);
$gff1 = $patched_gff_filename;
} else {
# otherwise just make symb link
symlink($gff1, $patched_gff_filename);
}
my ($ref_names, $ref_coords) = parse_genes($gff1);
my $num_cdna = parse_gffread($gffreadpath,$fasta1,$gff1,$cdnafile,
'cdna',$minlen,$nored,$ref_names,$ref_coords);
my $num_cds = parse_gffread($gffreadpath,$fasta1,$gff1,$cdsfile,
'cds',$minlen,$nored,$ref_names,$ref_coords);
my $num_pep = parse_gffread($gffreadpath,$fasta1,$gff1,$pepfile,
'pep',$minlen,$nored,$ref_names,$ref_coords);
if(scalar(keys(%$ref_names)) == 0) {
die "# ERROR: cannot parse Parent IDs of mRNA/transcripts, please check GFF format ($gff1)\n";
}
if($num_cdna) {
print "# $cdnafile n=$num_cdna\n";
} else {
die "# ERROR: cannot extract cDNA sequences, please check GFF format and/or chr names ($gff1)\n";
}
if($num_cds) {
print "# $cdsfile n=$num_cds\n"
} else {
die "# ERROR: cannot extract CDS sequences, please check GFF format and/or chr names ($gff1)\n";
}
print "# $pepfile n=$num_pep\n";
###############################
# Runs gffread, parses its stdout and saves output in FASTA file.
# Returns number of sequences printed out.
sub parse_gffread {
my ($gffreadexe,$fasta_file,$gff_file,$outfile,
$seqtype,$minlen,$remove_red,$ref_tr2gene,$ref_tr2coords) = @_;
my ($params,$mrnaid,$geneid,$coords);
if($seqtype eq 'cds'){
$params = '-x - ';
} elsif($seqtype eq 'pep'){
$params = '-y - ';
} else {
$params = '-w - '; # cDNA, default
}
if($minlen > 0) {
$params .= " -l $minlen ";
}
my $num_seqs = 0;
open(OUT,">",$outfile) ||
die "# ERROR(parse_gffread): cannot create $outfile\n";
open(GFFREAD,"$gffreadexe $params -g $fasta_file $gff_file |") ||
die "# ERROR(parse_gffread): cannot run $gffreadexe\n";
while(<GFFREAD>){
if(/^>(\S+)/){
$mrnaid = $1;
$geneid = $ref_tr2gene->{$mrnaid} || '';
$coords = $ref_tr2coords->{$mrnaid} || '';
# remove redundant bits
if($remove_red){
$mrnaid =~ s/transcript://;
$geneid =~ s/gene://;
}
print OUT ">$mrnaid $geneid $coords [$sp1]\n";
$num_seqs++;
} else {
print OUT;
}
}
close(GFFREAD);
close(OUT);
# do not remove index (used later)
# unlink($fasta_file.'.fai');
return $num_seqs
}
# Reads in GFF file to parse gene names as parent IDs of transcripts.
# Returns:
# i) ref to hash mapping transcript ID -> gene ID
# ii) ref to hash mapping transcript ID -> gene coords
sub parse_genes {
my ($gff_file) = @_;
my ($mrnaid,$geneid,$coord,%names,%coords);
open(GFF,"<",$gff_file) || die "# ERROR(parse_genenames): cannot read $gff_file\n";
while(<GFF>){
my @F = split(/\t/,$_);
next if(scalar(@F)<9 || ($F[2] ne "mRNA" && $F[2] ne "transcript"));
# take only genes where ID can be parsed
if($F[8] =~ /ID=([^;]+).*?Parent=([^;]+)/){
$mrnaid = $1;
$geneid = $2;
chomp $geneid;
$coord = "$F[0]:$F[3]-$F[4]($F[6])";
$names{$mrnaid} = $geneid;
$coords{$mrnaid} = $coord;
}
}
close(GFF);
return (\%names,\%coords);
}
# Takes two GFF files (original and patch) and produces a new GFF file
# that includes patched gene models. Patched models match the original
# ones by means of 'old_locus_tag' tags in gene features.
# Patched GFF files might be concatenated; this means latter models
# can replaced models declared earlier in the same file. Two params:
# i) GFF filename
# ii) GFF filename with selected gene model patches
# iii) output patched GFF filename
# Returns integer with number of patched gene models
sub patch_gff {
my ($gff_file, $patchfile, $patched_gff_filename) = @_;
my ($patch,$gene_id,$old_gene_id,$comment);
my ($chr,$start,$line);
my (%depr_gene_id, %new2old, %patched_model, %coords);
# read in GFF patches, possible concatenated
open(PATCH,'<',$patchfile) ||
die "# ERROR(patch_gff): cannot read $patchfile\n";
while($line = <PATCH>) {
if($line =~ /^$/){
next
} elsif($line =~ /^#/) {
$comment = $line;
} else {
chomp($line);
my @gffdata = split(/\t/,$line);
#chr01 gmap gene 411 776 . - . ID=Os121164;..old_locus_tag=missing;
#chr01 gmap gene 411 776 . - . ID=gene:Os127564;..old_locus_tag=Os121164;
#chr01 gmap gene 411 800 . - . ID=Os22222;..old_locus_tag=Os127564;
if($gffdata[2] && $gffdata[2] eq 'gene') {
$chr = $gffdata[0];
$start = $gffdata[3];
if($gffdata[8] =~ m/ID=([^;]+)/) {
$gene_id = $1;
}
while($gffdata[8] =~ m/old_locus_tag=([^;]+)/g) {
$old_gene_id = $1;
# if gene replaces a previous patched model,
# correct old_locus_tag to point to original
if($new2old{ $old_gene_id }) {
# add original old locus tag
if($line !~ /$new2old{$old_gene_id}/) {
$line .= "old_locus_tag=$new2old{$old_gene_id};";
}
$depr_gene_id{ $old_gene_id } = $gene_id;
$old_gene_id = $new2old{ $old_gene_id };
}
$new2old{ $gene_id } = $old_gene_id;
$depr_gene_id{ $old_gene_id } = $gene_id;
}
$patched_model{ $gene_id } = "$line\n";
$coords{ $chr }{ $start } = $gene_id;
} else {
$patched_model{ $gene_id } .= "$line\n"
}
}
}
close(PATCH);
# find non-redundant patches, sort and concat them
my $total_patched = 0;
foreach $chr (sort {$a cmp $b} keys(%coords)) {
foreach $start (sort {$a <=> $b} keys(%{ $coords{$chr} })) {
$gene_id = $coords{ $chr }{ $start };
if($depr_gene_id{ $gene_id }) {
print "# skip old patched $gene_id\n";
next;
}
#if($gene_id eq 'gene:Os06g0705350.path1') { print "mira\n" }
$patch .= $patched_model{ $gene_id };
$total_patched++;
}
}
printf("# total patched: %d deprecated: %d\n\n",
$total_patched,scalar(keys(%depr_gene_id)));
# read in original GFF, apply patches and save output
open(PATCHED,">",$patched_gff_filename) ||
die "# ERROR(patch_gff): cannot create $patched_gff_filename\n";
my $geneOK = 1;
open(GFF,'<',$gff_file) ||
die "# ERROR(patch_gff): cannot read $gff_file\n";
while(<GFF>) {
my @gffdata = split(/\t/,$_);
if($gffdata[2] && $gffdata[2] eq 'gene') {
if($gffdata[8] =~ m/ID=([^;]+)/) {
$gene_id = $1;
if($depr_gene_id{ $gene_id }){
$geneOK = 0;
} else { $geneOK = 1 }
} else { $geneOK = 1 }
print PATCHED if($geneOK);
} else {
print PATCHED if($geneOK)
}
}
close(GFF);
# apply patches
print PATCHED $patch;
close(PATCHED);
return $total_patched
}