0
|
1 <tool id="hmm_1" name="Fit HMM " version="1.0.0">
|
|
2 <description>on numeric data</description>
|
1
|
3 <requirements>
|
|
4 <requirement type="set_environment">R_SCRIPT_PATH</requirement>
|
|
5 <requirement type="package" version="2.15.0">R</requirement>
|
|
6 <requirement type="package" version="1.5.0">RHmm</requirement>
|
|
7 </requirements>
|
|
8 <command interpreter="bash">r_wrapper.sh $script_file</command>
|
0
|
9 <inputs>
|
|
10 <param name="input" type="data" format="tabular" label="Dataset"/>
|
|
11 <param name="var_cols" label="Select columns containing observations " type="data_column" data_ref="input" numerical="True" multiple="true" >
|
|
12 <validator type="no_options" message="Please select at least one column."/>
|
|
13 </param>
|
|
14 <param name="samp_col" label="Select column containing sample numbers " type="data_column" data_ref="input" numerical="True" multiple="false" >
|
|
15 <validator type="no_options" message="Please select a column."/>
|
|
16 </param>
|
|
17 <param name="header" type="select" label="Treat first line as header? ">
|
|
18 <option value="yes" selected="true">Yes</option>
|
|
19 <option value="no">No</option>
|
|
20 </param>
|
|
21 <param name="nStates" size="10" type="integer" value="2" label="Number of hidden states " />
|
|
22 <conditional name="disChoice">
|
|
23 <param name="dis" type="select" label="Distribution">
|
|
24 <option value="NORMAL" selected="true">Normal</option>
|
|
25 <option value="DISCRETE">Discrete</option>
|
|
26 <option value="MIXTURE">Mixture</option>
|
|
27 </param>
|
|
28 <when value="NORMAL" />
|
|
29 <when value="DISCRETE" />
|
|
30 <when value="MIXTURE">
|
|
31 <param name="nMixt" size="10" type="integer" value="2" label="Number of mixtures of normal distributions " />
|
|
32 </when>
|
|
33 </conditional>
|
|
34 <!--
|
|
35 <conditional name="asymptChoice">
|
|
36 <param name="asymptCov" type="select" label="Compute asymptotic covariance matrix? ">
|
|
37 <option value="FALSE" selected="true">No</option>
|
|
38 <option value="TRUE">Yes</option>
|
|
39 </param>
|
|
40 <when value="FALSE" />
|
|
41 <when value="TRUE">
|
|
42 <param name="asymptMethod" type="select" label="Method for computing asymptotic covariance matrix ">
|
|
43 <option value="nlme" selected="true">nlme</option>
|
|
44 <option value="optim">optim</option>
|
|
45 </param>
|
|
46 </when>
|
|
47 </conditional>
|
|
48 -->
|
|
49 </inputs>
|
|
50
|
|
51 <configfiles>
|
|
52 <configfile name="script_file">
|
|
53 ## Setup R error handling to go to stderr
|
|
54 options( show.error.messages=F,
|
|
55 error = function () { cat( geterrmessage(), file=stderr() ); q( "no", 1, F ) },
|
|
56 warn = -1 )
|
|
57 suppressPackageStartupMessages(library('RHmm'))
|
|
58
|
|
59 #if str($header) == "yes"
|
|
60 inp = read.table( "${input.file_name}", header=T )
|
|
61 #else
|
|
62 inp = read.table( "${input.file_name}", header=F )
|
|
63 #end if
|
|
64
|
|
65 samp_numbers = unique(inp[, ${samp_col}])
|
|
66
|
|
67 if (length(samp_numbers) == 1){
|
|
68 samp_list = inp[,c(${var_cols})]
|
|
69 } else {
|
|
70 samp_list=list()
|
|
71 for (i in 1:length(samp_numbers)) {
|
|
72 samp_list[[i]] = inp[(inp[,${samp_col}] == samp_numbers[i]),c(${var_cols})]
|
|
73 }
|
|
74 }
|
|
75
|
|
76 nStates = ${nStates}
|
|
77 dis = "$disChoice['dis']"
|
|
78 nMixt = 0
|
|
79
|
|
80 #if $disChoice['dis'] == "MIXTURE"
|
|
81 nMixt = ${disChoice.nMixt}
|
|
82 #end if
|
|
83
|
|
84 ##asymptCov = $asymptChoice['asymptCov']
|
|
85 asymptCov = "FALSE"
|
|
86 asymptMethod = "nlme"
|
|
87
|
|
88 ##if (asymptCov == "TRUE") {
|
|
89 ## asymptMethod = "${asymptChoice.asymptMethod}"
|
|
90 ##}
|
|
91
|
|
92 #if $disChoice['dis'] == "MIXTURE"
|
|
93 if (asymptCov == "TRUE") {
|
|
94 myfit = HMMFit(samp_list, nStates=nStates, dis=dis, nMixt=nMixt, asymptCov=asymptCov, asymptMethod=asymptMethod)
|
|
95 } else {
|
|
96 myfit = HMMFit(samp_list, nStates=nStates, dis=dis, nMixt=nMixt)
|
|
97 }
|
|
98 #else
|
|
99 if (asymptCov == "TRUE") {
|
|
100 myfit = HMMFit(samp_list, nStates=nStates, dis=dis, asymptCov=asymptCov, asymptMethod=asymptMethod)
|
|
101 } else {
|
|
102 myfit = HMMFit(samp_list, nStates=nStates, dis=dis)
|
|
103 }
|
|
104 #end if
|
|
105
|
|
106 myfittxt=capture.output(myfit)
|
|
107 cat(myfittxt,file="${out_file1}",sep="\n")
|
|
108
|
|
109
|
|
110 samp_list_stateSol = list()
|
|
111 if (length(samp_numbers) == 1){
|
|
112 samp_list_stateSol[[1]]=unlist(viterbi(myfit, samp_list)["states"])
|
|
113 } else {
|
|
114 for (i in 1:length(samp_numbers)) {
|
|
115 samp_list_stateSol[[i]]=unlist(viterbi(myfit, samp_list[[i]])["states"])
|
|
116 }
|
|
117 }
|
|
118 inp_stateSol=cbind(inp,unlist(samp_list_stateSol))
|
|
119 write.table(inp_stateSol,file="${out_file2}",sep="\t",row.names=F,col.names=F,quote=F)
|
|
120
|
|
121 </configfile>
|
|
122 </configfiles>
|
|
123
|
|
124 <outputs>
|
|
125 <data format="txt" name="out_file1" />
|
|
126 <data format="input" name="out_file2" />
|
|
127 </outputs>
|
|
128
|
|
129
|
|
130 <help>
|
|
131
|
|
132 .. class:: infomark
|
|
133
|
|
134 **What it does**
|
|
135
|
|
136 This tool uses the 'HMMFit' and 'viterbi' functions from 'RHmm' library from R statistical package to fit an Hidden Markov Model using Baum-Welch algorithm, and calculate the optimal hidden states sequence using Viterbi's algorithm.
|
|
137
|
|
138 It returns two outputs - one containing summary statistics for HMMFit, and the other containing state numbers appended as a new column to the input data.
|
|
139
|
|
140 *Ollivier TARAMASCO and Sebastian Bauer (2010). RHmm: Hidden Markov Models simulations and estimations. R package version 1.4.4. http://CRAN.R-project.org/package=RHmm.*
|
|
141
|
|
142 -----
|
|
143
|
|
144 .. class:: warningmark
|
|
145
|
|
146 **Note**
|
|
147
|
|
148 The tool fails if any of the observation columns contain non-numeric data.
|
|
149
|
|
150
|
|
151 </help>
|
|
152 </tool>
|