File: yaml2gff.1.4.pl

package info (click to toggle)
augustus 3.3.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 486,188 kB
  • sloc: cpp: 51,969; perl: 20,926; ansic: 1,251; makefile: 935; python: 120; sh: 118
file content (248 lines) | stat: -rwxr-xr-x 9,700 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl -w
################################################################################
#
#   Project: Gene Prediction with Protein Family Patterns 
#   Author:  Oliver Keller
#   Date:    2010-07-07
#   Version: accompanying Scipio 1.4
#
#
#   yaml2gff.pl
#
#
#   This script transforms the yaml output of scipio into easily readable GFF 
#   
#   The output produces is conforming to the GFF3 standard; however the term "Target" used
#   in GFF3 is replaced by "Query".
#   In the context of Scipio, the "target" sequence is the referenced genomic sequence while the
#   "query" is the aligned protein sequence. In other contexts (as in the GFF standard too), the 
#   "target" sequence is what Scipio is referring to as the "query". To avoid confusion, in the
#   GFF produces by yaml2gff the term "target" is omitted completely. 
#
#   the protein sequence shown contains only matched amino acids, together with the following 
#   extra symbols:
#   "." for an unmatched amino acid in the query (gaps, case 5)
#   "x" for a mismatch / undetermined amino acid (cases 1-3, 10-13, 15, 16)
#   "-" for an extra codon (or pair or single nucleotide) in the target (cases 4, 6-9, 17)
#   "*" for a matched stop codon (case 14)
#
#   Counting of bps/aas: is transforming coordinates from YAML file (see usage.html)
#                        into low-high coordinates according to GFF standard
#



use strict;
use List::Util('sum', 'max');
use YAML;
use Getopt::Long;


my $PROT_WIDTH=98;
my $DNA_ORDER=0;

my $QPFX="prot";
my $TPFX="dna";
my $TRPFX="trans";
my $QNPFX="nucl";

my $filterStatus; # filter this status out
my $help=0;       # whether to print usage information
my %PARAMETER = ("filterstatus:s" => \$filterStatus,
		 "help!" => \$help);

sub usage
{   
    print STDERR "                                                                                                                                                                                                                           
    usage:                                                                                                                                                                                                                                   
    yaml2gff.pl [<options>] < scipio.yaml > scipio.gff                                                                                                                                                                                       
                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                             
    Options:    --help                     print this help message                                                                                                                                                                           
                --filterstatus=<value>     filter out alignments with given status, e.g. 'incomplete'                                                                                                                                        
";  
    exit 1;
}

sub nu_to_aa {
    my $arg = shift;
    my $residue = ($arg+1) % 3 -1;   # -1, 0, 1
    return ($arg-$residue)/3;
}

sub escape {
    foreach(@_) {
	s/([^a-zA-Z0-9.:^*$@!+_?-|])/sprintf("%%%02X",ord($1))/ge;
    }
}

sub with_number {
    my ($n, $s, $alt) = @_;

    $alt = "${s}s" unless (defined $alt);
    return ($n==1) ? "$n$s" : "$n$alt";
}

sub get_gapstr {
    my ($frameshifts, $qfrom, $qto) = @_;
    return "" unless(defined $frameshifts && @$frameshifts);

    my $result="";
    my @matches = map { &nu_to_aa($_->{"${QNPFX}_start"}) } @$frameshifts;
    $matches[0] -= $qfrom;
    push @matches, $qto;
    for my $i (1..@$frameshifts) {
	$matches[$i] -= &nu_to_aa($frameshifts->[$i-1]{"${QNPFX}_end"});
    }
    foreach (@$frameshifts) {
	my $base_count = ($_->{"${TPFX}_end"}) - ($_->{"${TPFX}_start"});
	my $aa_count = (($_->{"${QNPFX}_end"}) - ($_->{"${QNPFX}_start"})) / 3;
	$result.="M".(shift @matches)." ";
	if ($aa_count == 0) {
	    my $fs = $base_count % 3;
	    $base_count -= $fs;
	    my $deletes = $base_count / 3;
	    $result.="D$deletes " if ($deletes);
	    $result.="F$fs " if ($fs);
	} else {
	    $result.="I$aa_count ";
	    $result.="F$base_count " if ($base_count);
	}
    }	
    return ";Gap=${result}M$matches[0]";
}

sub output {
    my ($queryname, $hitlist) = @_;
    next if ($filterStatus && $hitlist->[0]{"status"} eq $filterStatus);
    escape($queryname);
    print "##query\t$queryname 1 ".$hitlist->[0]{"${QPFX}_len"};
    my ($pstart, $pend, $plen) = ($hitlist->[0]{"${QPFX}_start"}, @{$hitlist->[-1]}{"${QPFX}_end", "${QPFX}_len"});
    my $prev_end = 0;
    my $matched_prot = "." x $plen;
    my @minuses = ();
    foreach my $hitref (@$hitlist) {
	my ($hit_id, $matchings, $strand, $targetname, $score, 
	    $gapcount,  $stopcodon, $status, $protseq, 
	    $querystart, $queryend, $total_length) = 
	    @$hitref{"ID", "matchings", "strand", "target", "score", 
		     "gaps", "stopcodon", "status", "${QPFX}_seq",
		     "${QPFX}_start", "${QPFX}_end", "${QPFX}_len"};
	$targetname =~ s/\s.*//;
	escape($targetname);
	$gapcount = 0 unless (defined $gapcount);
	$stopcodon = "" unless (defined $stopcodon);
	
	if ($strand eq "-" || $strand =~ /minus/ || $strand =~ /back/) {
	    $strand = "-";
	} elsif ($strand eq "+" || $strand =~ /plus/ || $strand =~ /forward/) {
	    $strand = "+";
	} else {
	    my $dnapos = $hitref->{"${TPFX}_start"};
	    $dnapos = $hitref->{matchings}[0]{"${TPFX}_start"} unless defined $dnapos;
	    if (defined $dnapos) {
		$strand = $dnapos < 0 ? "-" : "+";
	    } else {
		$strand = ".";
	    }
	}
	my $complement = $strand eq "-";

	my @exons = grep { $_->{type} eq "exon" } @$matchings;
	my @mismatchcounts = map  { scalar @{$_->{mismatchlist}} } @exons;
	my @undeterminedcounts = map  { scalar @{$_->{undeterminedlist}} } @exons;
	my $total_mismatches = sum(@mismatchcounts,@undeterminedcounts,0);
	
	$querystart=0 unless (defined $querystart);
	$queryend=$total_length unless (defined $queryend);
	if (defined $protseq) {
	    substr($matched_prot, $querystart, $queryend-$querystart) = $protseq;
	}
	if ($prev_end < $querystart) {
	    print "\n# gap ".($prev_end+1)." $querystart";
	}

	my $matchsize = $queryend-$querystart;
	my $matchcount = $matchsize - $gapcount - $total_mismatches;

	my $has_bad_introns = 
	    grep ({ /intron\?/ } map { $_->{type} } @$matchings);

	my @gff_lines = ();
	foreach (@$matchings) {
	    my ($type, $qnfrom,$qnto) = @$_{"type", "${QNPFX}_start","${QNPFX}_end"};
	    if ($type eq "exon") {
		my ($qfrom, $qto) = map { &nu_to_aa($_); } ($qnfrom, $qnto);
		my ($frameshifts,$mismatches,$undetermined,$addprotseq, $trans,
		    $tfrom, $tto) 
		    = @$_{"seqshifts","mismatchlist","undeterminedlist","${QPFX}_seq","translation",
			  "${TPFX}_start", "${TPFX}_end"};
		if (defined $addprotseq) {
		    substr($matched_prot, $qfrom, $qto-$qfrom)=$addprotseq;
		}
		my ($first, $last) = $complement? (-$tto+1, -$tfrom+0) : ($tfrom+1, $tto+0);
		my @xpos = sort { $a <=> $b } (@$mismatches,@$undetermined);
		my $misstr = (@xpos)? ";Mismatches=".join(" ", @xpos) : "";
		map { substr($matched_prot,$_-1,1)="x"; } @xpos;
		my $gapstr = &get_gapstr($frameshifts, $qfrom, $qto);
		
		foreach (@$frameshifts) {
		    my ($fsstart, $fsend) = map { &nu_to_aa($_) } @{$_}{"${QNPFX}_start", "${QNPFX}_end"};
		    my $excount = int(($_->{"${TPFX}_end"} - $_->{"${TPFX}_start"} + 2) / 3);
		    if ($fsend > $fsstart) {
			substr($matched_prot, $fsstart, $fsend-$fsstart) = ("x" x $excount).("." x ($fsend-$fsstart-$excount));
		    } else {
			push @minuses, ($fsstart) x $excount;
		    }
		}

		push (@gff_lines, 
		      "$targetname\tScipio\tprotein_match\t$first\t$last\t$score\t$strand\t".
		      (-$qnfrom % 3)."\tID=$hit_id;Query=$queryname ".($qfrom+1).
		      " $qto$misstr$gapstr");
	    } elsif ($type eq "gap") {
		my ($qfrom, $qto) = map { &nu_to_aa($_) } ($qnfrom, $qnto);
		substr($matched_prot, $qfrom, $qto-$qfrom) = "." x ($qto-$qfrom);
		push @gff_lines, "# gap ".($qfrom+1)." $qto";
	    }
	}
	@gff_lines = reverse @gff_lines if ($complement && $DNA_ORDER);
	print "\n".join("\n", @gff_lines);
	$prev_end = $queryend;
    }   
    if (defined $pend && $pend < $plen) {
	print "\n# gap ".($pend+1)." $plen";
    }
    foreach (reverse @minuses) {
	substr($matched_prot, $_, 0) = "-";
    }
    $matched_prot = "protein sequence = [$matched_prot";
    do {
	print "\n# ".substr($matched_prot,0,$PROT_WIDTH,"");
    } while ($matched_prot);
    print "]\n#\n";
} 

### Command Line                                                                                                                                                                                                                             
&GetOptions (%PARAMETER) or &usage;
&usage if ($help);

my @input  = split("\n---",join("",<>));
shift @input if (@input && $input[0]!~/^---/);

print "##gff-version\t3\n";
print STDERR "Found ".&with_number(scalar @input, " YAML entry", " YAML entries").".\n";
foreach (@input) {
    s/^(---)?/---/;
    s/$/\n/;
    my $hits = YAML::Load($_);
    print STDERR "[";
    foreach (sort { $a cmp $b } keys %$hits) {
	&output($_, $hits->{$_});
	print STDERR " $_ ";
    }
    print STDERR "]\n";
}