File: seq_n_baseprobs_to_logliklihood_vals.pl

package info (click to toggle)
transdecoder 3.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,356 kB
  • ctags: 274
  • sloc: perl: 5,454; sh: 81; makefile: 51
file content (172 lines) | stat: -rwxr-xr-x 3,910 bytes parent folder | download
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
#!/usr/bin/env perl

use strict;
use warnings;

use FindBin;
use lib ("$FindBin::Bin/../PerlLib");
use Fasta_reader;
use Nuc_translator;

## hexamer stats
my %framed_hexamers;
my %background_hexamers;


## pentamer stats
my %framed_pentamers;
my %background_base_probs;
my %framed_all_pentamer_counts;

my $usage = "usage: $0 targetCDSs base_probs.dat\n\n";

my $target_CDS = $ARGV[0] or die $usage;
my $base_probs_dat_file = $ARGV[1] or die $usage;
my $debug = $ARGV[2];

main: {

	&parse_targetCDSs($target_CDS);

	&parse_background($base_probs_dat_file);

	&add_pseudocounts();

	&report_logliklihood_ratios();
	
	exit(0);
}





####
sub report_logliklihood_ratios {
	

	## Markov-based probabilities (5th order markov chain):
	
	foreach my $framed_hexamer (sort keys %framed_hexamers) {
		my ($hexamer, $frame) = split (/-/, $framed_hexamer);
	
        if ($hexamer =~ /[^GATC]/) {
            ## ignoring hexamers containing non-GATC bases
            next;
        }
        
		my $pentamer = substr($hexamer, 0, 5);

		my $framed_hexamer_count = $framed_hexamers{$framed_hexamer};
		my $framed_pentamer_count = $framed_pentamers{"${pentamer}-${frame}"};

		my $markov_prob_framed = $framed_hexamer_count / $framed_pentamer_count;

		my $last_base = substr($hexamer, 5, 1);
		my $background_prob = $background_base_probs{$last_base} or die "Error, no background probability set for base: $last_base of hexamer $hexamer";;
        
        my $logliklihood = log($markov_prob_framed / $background_prob);
            
        print "$framed_hexamer\t$logliklihood\n";
        
    }
    


	## The Initialization Matrix based on framed pentamer frequencies.

	foreach my $framed_pentamer (sort keys %framed_pentamers) {
		
        if ($framed_pentamer =~ /[^GATC]/) { 
            next;
        }
        
		my ($pentamer, $frame) = split (/-/, $framed_pentamer);

		my $frame_counts = $framed_all_pentamer_counts{$frame};
		my $framed_pentamer_counts = $framed_pentamers{$framed_pentamer};

		my $prob_framed_pentamer = $framed_pentamer_counts / $frame_counts;

		## now background
		my @bases = split(//, $pentamer);
        my $prob_background_pentamer = 1;
        foreach my $base (@bases) {
            $prob_background_pentamer *= $background_base_probs{$base};
        }
        
		my $logliklihood = log($prob_framed_pentamer / $prob_background_pentamer);

		print "$framed_pentamer\t$logliklihood\n";

	}

	return;
}

####
sub add_pseudocounts {
	
	foreach my $framed_hexamer (keys %framed_hexamers) {
		my ($hexamer, $frame) = split (/-/, $framed_hexamer);
		
		my $pentamer = substr($hexamer, 0, 5);
		
		$framed_hexamers{$framed_hexamer}++;
		$framed_pentamers{"${pentamer}-${frame}"}++;
		$framed_all_pentamer_counts{$frame}++;

	}


	return;
}

	
####
sub parse_targetCDSs {
	my ($seqFile) = @_;

	my $fasta_reader = new Fasta_reader($seqFile);
	
	while (my $seq_obj = $fasta_reader->next()) {
		
		my $accession = $seq_obj->get_accession();
		print STDERR "\r     Target: processing $accession           " if $debug;
		
		my $sequence = uc $seq_obj->get_sequence();

		my $seq_len = length($sequence);

		for (my $i = 0; $i <= $seq_len - 5; $i++) {
			my $frame = $i % 3;
			my $pentamer = substr($sequence, $i, 5);
			$framed_pentamers{"${pentamer}-${frame}"}++;
			$framed_all_pentamer_counts{$frame}++;
			
			if ($i <= $seq_len - 6) { 
				# got a hexamer
				my $hexamer = substr($sequence, $i, 6);
				$framed_hexamers{"${hexamer}-${frame}"}++;
			}
		}
	}
	print "\r     CDS base frequency processing complete.           \n" if $debug;
	return;
}

#### 
sub parse_background {
	my ($base_probs_dat_file) = @_;

    open (my $fh, $base_probs_dat_file) or die "Error, cannot open file $base_probs_dat_file";
    while (<$fh>) {
        chomp;
        my ($base, $count, $ratio) = split(/\t/);
        $background_base_probs{$base} = $ratio;
    }
    close $fh;
        	
	return;
}