File: EM.pm

package info (click to toggle)
trinityrnaseq 2.15.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 468,004 kB
  • sloc: perl: 49,905; cpp: 17,993; java: 12,489; python: 3,282; sh: 1,989; ansic: 985; makefile: 717; xml: 62
file content (516 lines) | stat: -rwxr-xr-x 11,394 bytes parent folder | download | duplicates (5)
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
package EM;

use strict;
use warnings;
use SAM_entry;
use Carp;

use vars qw($TOKEN);
BEGIN {
	$TOKEN = "$;";
}

my $MIN_EFF_TRANS_LENGTH = 10; # // large enough to keep abundance estimates from going absurdly high for short transcripts.

####
sub  new {
	my $packagename = shift;
	my ($transcript_seqs_href, $fragment_length) = @_;
	
    unless ($fragment_length) {
        $fragment_length = 1; # negligible with respect to the length of the transcript
    }


	# transcript_seqs_href:  ( seq_acc => sequence, ...)
	# trans_reads_aref: ( [trans_accA, read_accA], [trans_accB, read_accB], ...)
	
	unless ($transcript_seqs_href) {
		confess "Error, need param: transcript_seqs_href";
	}
	
	

	my $self = {
		
		# mapping info
		_trans_to_multi_map_counts => {}, # trans => { transA\$;transB\$;transC... } => count_of_reads

		# model params
		_ENt => {}, # trans => expected read count
		_theta => {}, # trans => fraction of all reads
		
		# transcript info
		_trans_lengths => {}, # trans => length
        _frag_length => $fragment_length,
        
				
		# misc
		_total_reads => 0, # total read count

	};
	

	bless ($self, $packagename);

	$self->_init_trans_lengths($transcript_seqs_href);
	
	return($self);

}


####
sub _init_trans_lengths {
	my $self = shift;
	my ($transcript_seqs_href) = @_;
	
	foreach my $transcript (keys %$transcript_seqs_href) {
		
		my $sequence = $transcript_seqs_href->{$transcript} or confess "Error, no sequence for transcript: $transcript";
		
		$self->{_trans_lengths}->{$transcript} = length($sequence);
		
	}

	return;
}

sub run {
	my $self = shift;
	my (%settings) = @_;

	my $max_iterations = $settings{max_iterations} || 1000;
	my $min_delta_ML = $settings{min_delta_ML} || 0.01;
    my $verbose_flag = $settings{verbose} || 0;

	
	$self->_init_theta();
	
	#########################
	## Do the EM:
	
	my $prev_ML_val = undef;
	
	my $round = 0;
	my $delta = 1;
	
	print STDERR "\tEM started\n";
	while (1) {
	
		$round++;
		
		$self->_E_step();

		$self->_M_step();

		
		my $ml = $self->_compute_Max_Likelihood();
		
		if (defined $prev_ML_val) {
			$delta = $ml - $prev_ML_val;
		}
		
		print STDERR "\rML[$round]: $ml       $delta       ";
		
		$prev_ML_val = $ml;
		
        if ($verbose_flag) {
            print "\n\nEM_results, round: $round:\n";
            $self->report_results();
            print "\n";
        }
        
		if ($round >= $max_iterations || $delta  < $min_delta_ML) {
			
			last;
		}

	}
	print STDERR "\n\tEM finished.\n";
	
	return;
}


####
sub _init_theta {
	my $self = shift;

	###########################
	## init theta:
	##   assume mutliply mapped reads equally divided among their mapped locations for init
	
	my @transcripts = $self->_get_all_transcripts();

	my $total_reads = $self->{_total_reads};
	
	foreach my $transcript (@transcripts) {
		
		my $read_count_sum = 0;
		
		my @combos = $self->_get_multi_map_list_including_transcript($transcript);
		
		foreach my $combo (@combos) {
			
			my @other_trans = $self->_get_transcripts_in_combo($combo);
			
			my $num_other = scalar(@other_trans);
			my $count = $self->_get_multi_map_read_count_for_combo($transcript, $combo);
			
			$read_count_sum += ($count/$num_other);
		}
				
		$self->{_theta}->{$transcript} = $read_count_sum / $total_reads;

	}

	return;
}

####
sub _get_all_transcripts {
	my $self = shift;
	
	my @transcripts = keys %{$self->{_trans_to_multi_map_counts}};
	
	return(@transcripts);
}


####
sub _get_reads_mapped_to_transcript {
	my $self = shift;
	my ($transcript) = @_;

	if (exists $self->{_trans_to_reads_href}->{$transcript}) {
		my @reads = @{$self->{_trans_to_reads_href}->{$transcript}};
		return(@reads);
	}
	else {
		confess "Error, no reads mapped to transcript $transcript";
	}
}

####
sub _get_transcript_length {
	my $self = shift;
	my ($transcript) = @_;

	return($self->{_trans_lengths}->{$transcript});
}




####
sub _E_step {
	my $self = shift;

	my @transcripts = $self->_get_all_transcripts();
	
	## do the E
	foreach my $transcript (@transcripts) {
		
		my $expected_read_count = 0;
		
		
		
		my $trans_length = $self->_get_transcript_length($transcript) or die "Error, no length for transcript: $transcript";
        my $eff_trans_length = $trans_length - $self->{_frag_length} + 1;
        if ($eff_trans_length < $MIN_EFF_TRANS_LENGTH) {
            $eff_trans_length = $MIN_EFF_TRANS_LENGTH;
        }
        

		my $theta_t = $self->{_theta}->{$transcript};
		if (! defined $theta_t) {
			die "Error, no theta($transcript), $theta_t";
		}
		
		
		my @combos = $self->_get_multi_map_list_including_transcript($transcript);
		
		#print "Combos: @combos\n";
		

	    foreach my $combo (@combos) {
						
			my $combo_count = $self->_get_multi_map_read_count_for_combo($transcript, $combo);
			
			#print "Got combo: $combo, count: $combo_count\n";
						
			my @other_transcripts = split(/$TOKEN/, $combo);
			if (scalar @other_transcripts == 1) {
				# only current transcript:
				unless ($other_transcripts[0] eq $transcript) {
					confess "Error, mapped single transcript should be the current transcript";
				}
				$expected_read_count += $combo_count;
			}
			else {
				## compute partial mapping.
				
							
				my $numerator = (1 / $eff_trans_length) * $theta_t;
                #my $numerator = $theta_t;
                
			
				# demonator: sum for all t: P(r|t) * theta(t)
				my $denominator = 0;
			
				
				#print STDERR "Other transcripts: @other_transcripts\n";
				
				foreach my $other_transcript (@other_transcripts) {  # other transcripts includes the current transcript as well.
				
					my $other_trans_len = $self->_get_transcript_length($other_transcript);
					unless ($other_trans_len) {
						die "Error, no trans length for $other_transcript";
					}
					
                    my $eff_other_trans_length = $other_trans_len - $self->{_frag_length} + 1;
                    if ($eff_other_trans_length < $MIN_EFF_TRANS_LENGTH) {
                        $eff_other_trans_length = $MIN_EFF_TRANS_LENGTH;
                    }
                    
					my $other_theta = $self->{_theta}->{$other_transcript};
					
					if (! defined $other_theta) {
						die "no theta for $other_transcript, $other_theta";
					}
					
					my $val = (1 / $eff_other_trans_length) * $other_theta;
                    #my $val = $other_theta;
                    
					$denominator += $val;
				}
			
				#print "Fractional read count for $transcript = $numerator / $denominator\n";
			
				my $fractional_read_count = $combo_count * ($numerator/$denominator);
				
				
				$expected_read_count += $fractional_read_count;
			}
		}
		
		$self->{_ENt}->{$transcript} = $expected_read_count;
	}
	
	return;
}
						
			
####
sub _M_step {
	my $self = shift;
	
	my $sum_NT = 0;

	foreach my $E (values %{$self->{_ENt}}) {
		$sum_NT += $E;
	}
	
	## theta = ENt(t) / sum( for all t: ENt(t) )

	
	foreach my $transcript ($self->_get_all_transcripts()) {
		my $new_theta = $self->{_ENt}->{$transcript} / $sum_NT;
		
		$self->{_theta}->{$transcript} = $new_theta;
	}

	return;
}


####
sub _compute_Max_Likelihood {
	my $self = shift;
	
	# ML = sum:t (ENt * log(theta(t)))

	my $ML = 0;

	foreach my $transcript ($self->_get_all_transcripts()) {
		
		my $theta = $self->{_theta}->{$transcript};
		
		if ($theta > 0) {
			
			my $ENt = $self->{_ENt}->{$transcript};

			$ML += $ENt * log($theta);
			
		}
	}

	return($ML);
}


####
sub get_results {
	my $self = shift;
	
	my $total_reads = $self->{_total_reads};

    my @results;
	
	foreach my $transcript (sort $self->_get_all_transcripts()) {
		
		my $num_frags = $self->{_ENt}->{$transcript};
		my $len = $self->_get_transcript_length($transcript);
		
		$num_frags = sprintf("%.2f", $num_frags);
		
		my $fpkm = $num_frags / ($len/1e3) / ($total_reads/1e6);
		$fpkm = sprintf("%.2f", $fpkm);

		my ($num_unique_reads, $num_multi_map_reads) = $self->count_reads_mapped_to_transcript($transcript);
		
        my $struct = { trans_id => $transcript,
                       length => $len,
                       unique_map => $num_unique_reads,
                       multi_map => $num_multi_map_reads,
                       expected_map => $num_frags,
                       FPKM => $fpkm,
        };

        push (@results, $struct);


		
	}
	

	return (@results);
}


sub print_results {
    my $self = shift;
    
    print $self->report_results();
    return;
}



sub report_results {
    my ($self) = shift;

    my @results = $self->get_results();
        
    my $out_text = join("\t", "#transcript", "trans_length", "unique_map", "multi_map", "EM_frag_count", "FPKM") . "\n";
    
    foreach my $result (@results) {
            
        $out_text .= join("\t", 
                          $result->{trans_id},
                          $result->{length},
                          $result->{unique_map},
                          $result->{multi_map},
                          $result->{expected_map},
                          $result->{FPKM}) . "\n";
    }
    
    $out_text .= "\n"; # last spacer
    
    return ($out_text);

}






####
sub _get_multi_map_list_including_transcript {
	my $self = shift;
	my ($transcript) = @_;

	my @combos = keys %{$self->{_trans_to_multi_map_counts}->{$transcript}};
	return(@combos);
}

####
sub _get_multi_map_read_count_for_combo {
	my $self = shift;
	my ($transcript, $combo) = @_;
	
	my $val = $self->{_trans_to_multi_map_counts}->{$transcript}->{$combo};
	
	return($val);
}

####
sub _get_transcripts_in_combo {
	my $self = shift;
	my ($combo) = @_;

	my @transcripts = split(/$TOKEN/, $combo);
	return(@transcripts);
}


####
sub _create_combo {
	my $self = shift;
	my @transcripts = @_;

	my $combo = join($TOKEN, sort @transcripts);
	
	return($combo);
}

####
sub add_read { # really add_fragment: count pairs only once.
	my $self = shift;
	my @transcripts = @_;

	my $combo = $self->_create_combo(@transcripts);

	foreach my $transcript (@transcripts) {
		$self->{_trans_to_multi_map_counts}->{$transcript}->{$combo}++;
	}

	$self->{_total_reads}++;

	return;
}

####
sub count_reads_mapped_to_transcript {
	my $self = shift;
	my ($transcript) = @_;
	
	my $unique_read_count = 0;
	my $multi_map_count = 0;


	my @combos = $self->_get_multi_map_list_including_transcript($transcript);                                                       
	
	foreach my $combo (@combos) {                                                                                                    
                                                                                                                                         
		my $combo_count = $self->_get_multi_map_read_count_for_combo($transcript, $combo);                                           
		
		
		my @other_transcripts = $self->_get_transcripts_in_combo($combo);
		
		if (scalar @other_transcripts == 1) {                                                                                        
			# only current transcript:                                                                                               
			$unique_read_count += $combo_count;
		}                                                                                                                            
		else {                                   
			$multi_map_count += $combo_count;
		}
	}

	return($unique_read_count, $multi_map_count);
}

1; #EOM