-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeled-trainer-95.pl
executable file
·140 lines (130 loc) · 3.33 KB
/
labeled-trainer-95.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
#!/usr/bin/perl
use strict;
use NgramIterator;
$|=1;
$0=~/([^\/]+)$/;
my $usage="$1 <labeled-sequence> <highest-state-num> <order> <out.hmm>";
die "$usage\n" unless @ARGV==4;
my ($seqFile,$highestState,$order,$outfile)=@ARGV;
my $MIN_SAMPLE_SIZE=200; #175;
# Initialization
my (%symbolMap,$label,$symbolId,@baseAlphabet);
@baseAlphabet=("A","C","G","N","T");
$symbolId=0;
for(my $i=1 ; $i<=$order+1 ; ++$i)
{
my $iterator=new NgramIterator("ACGNT",$i);
while($label=$iterator->nextString())
{
$symbolMap{$label}=$symbolId;
++$symbolId;
}
}
my $NUM_SYMBOLS=$symbolId;
# Read input and gather statistics
my (@transitions,@emissions,$prevState);
open(IN,$seqFile) || die "Can't open file: $seqFile\n";
while(<IN>)
{
if(/^([ACGTN]+)\s+(\d+)/)
{
my ($ngram,$state)=($1,$2);
my $len=length $ngram;
for(my $i=0 ; $i<$len ; ++$i)
{++$emissions[$state][$symbolMap{substr($ngram,$i,$len)}]}
if(defined $prevState) {++$transitions[$prevState][$state]}
else {++$transitions[0][$state]}
$prevState=$state;
}
elsif(/^\s*$/)
{
++$transitions[$prevState][0];
undef $prevState;
}
}
close(IN);
# Pool counts from intron windows
for(my $state=48 ; $state<=57 ; ++$state)
{ for(my $s=0 ; $s<$NUM_SYMBOLS ; ++$s)
{
my $sum=
$emissions[$state][$s]+
$emissions[$state+10][$s]+
$emissions[$state+20][$s];
$emissions[$state][$s]=$sum;
$emissions[$state+10][$s]=$sum;
$emissions[$state+20][$s]=$sum;
}
}
#for(my $state=78 ; $state<=83 ; ++$state)
# { for(my $s=0 ; $s<$NUM_SYMBOLS ; ++$s)
# {
# my $sum=
# $emissions[$state][$s]+
# $emissions[$state+6][$s]+
# $emissions[$state+12][$s];
# $emissions[$state][$s]=$sum;
# $emissions[$state+6][$s]=$sum;
# $emissions[$state+12][$s]=$sum;
# }
# }
# Convert integral counts into probabilities
for(my $state=0 ; $state<=$highestState ; ++$state)
{
my $totalTrans=0;
for(my $nextState=0 ; $nextState<=$highestState ; ++$nextState)
{ $totalTrans+=$transitions[$state][$nextState] }
if($totalTrans)
{ for(my $nextState=0 ; $nextState<=$highestState ; ++$nextState)
{ $transitions[$state][$nextState]/=$totalTrans }
}
next unless $state;
for(my $i=0 ; $i<=$order ; ++$i)
{
my $iterator=new NgramIterator("ACGNT",$i);
while(1)
{
my $history=$iterator->nextString();
last unless defined $history;
my $sum=0;
foreach my $base (@baseAlphabet)
{
my $label="$history$base";
$sum+=$emissions[$state][$symbolMap{$label}];
}
foreach my $base (@baseAlphabet)
{
my $label="$history$base";
my $symbolId=$symbolMap{$label};
if($sum==0 && $i==0) {die "0 sum for state $state"}
if($sum>=$MIN_SAMPLE_SIZE || $i==0)
{$emissions[$state][$symbolId]/=$sum}
else
{undef $emissions[$state][$symbolId]}
}
}
}
}
# Output HMM
open(OUT,">$outfile") || die "Can't write to file: $outfile\n";
my $numStates=$highestState+1;
print OUT "$numStates $order\n";
for(my $i=0 ; $i<$numStates ; ++$i)
{
for(my $j=0 ; $j<$numStates ; ++$j)
{
my $p=int(10000*$transitions[$j][$i]+0.5)/10000;
print OUT "$p\t";
}
print OUT "\n";
}
for(my $i=0 ; $i<$NUM_SYMBOLS ; ++$i)
{
for(my $j=0 ; $j<$numStates ; ++$j)
{
my $p=int(10000*$emissions[$j][$i]+0.5)/10000;
print OUT "$p\t";
}
print OUT "\n";
}
close(OUT);