cancer_predict/app/controllers/cancerpredicts_controller.rb

383 lines
21 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# encoding: utf-8
require 'rubyXL'
class CancerpredictsController < ApplicationController
def initialize
super
@app_title = "cancerpredict"
end
def calculate
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
create_first_field
#begin
if params['header'].to_i == 1
result = {}
@head_images = {}
@form_to_show.head_images_id.each do |image_id|
next if image_id.to_s == ""
@image = Headimages.find_by(:id=>image_id.to_s)
@url = @image.temp_file.to_s
@head_images[@image.sort_number.to_i] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s+'" src="'+@url+'\"/>')
end
result['head_images'] = Hash[@head_images.sort].values.join('')
@head_images = {}
@form_to_show.title_images_id.each do |image_id|
next if image_id.to_s == ""
@image = Headimages.find_by(:id=>image_id.to_s)
@url = @image.temp_file.to_s
@head_images[@image.sort_number.to_i] = ('<img class="head_logo" alt ="' + Pathname.new(@image.temp_file.file.file).basename.to_s+'" src="'+@url+'\"/>')
end
result['title'] = ((@form_to_show.title_texts == "") ? "" : '<a class="title_texts">'+@form_to_show.title_texts+'</a>')+Hash[@head_images.sort].values.join('')
else
@record = Cancerpredictrecord.new
@record.title = @app_title
@choice_keys = []
@choice_values = []
@choice_names = []
@form_to_show.form_show.values.each{|choice| @choice_keys.push choice[:variable]}
@form_to_show.form_show.values.each{|choice| @choice_values.push choice[:choice_fields]}
@form_to_show.form_show.values.each{|choice| @choice_names.push choice[:name]}
@choice_keys.each_with_index{|key,i| @record.names[key] = @choice_names[i]}
@choice_keys.each_with_index{|key,i| @record.values[key] = @choice_values[i]}
params['data'].each do |rec_key,rec_value|
@record.result[rec_key] = rec_value
end
@record.submit_time = Time.now.to_s
@record.save
locale = params['data']['locale'].to_s rescue 'zh_tw'
locale = 'zh_tw' if locale == 'zh_cn'
result = {}
result['age1'] = (params['data']['age'].to_f/100)**(0.5)
result['age2'] = ((params['data']['age'].to_f/100)**(0.5))*Math.log(params['data']['age'].to_f/100,Math.exp(1))
result['size1'] = Math.log(params['data']['size'].to_f/10,Math.exp(1))
result['ratio'] = params['data']['lymph_nodes_positive'].to_f/params['data']['lymph_nodes_examined'].to_f rescue 0
result['ratio'] = 1 if result['ratio'] > 1
result['nposit'] = ((result['ratio'].to_f+0.1)/0.1)**0.5
@T = ''
if params['data']['tumor_direct_extension'].to_i != 1
if params['data']['size'].to_i <= 20
@T = 'T1'
elsif params['data']['size'].to_i > 20 && params['data']['size'].to_i <= 50
@T = 'T2'
elsif params['data']['size'].to_i > 50
@T = 'T3'
end
else
@T = 'T4'
end
@N = ''
if params['data']['lymph_nodes_positive'].to_i == 0
@N = 'N0'
elsif params['data']['lymph_nodes_positive'].to_i >= 1 && params['data']['lymph_nodes_positive'].to_i <= 3 && params['data']['micrometastasis'].to_i != 1
@N = 'N1'
elsif params['data']['lymph_nodes_positive'].to_i >= 1 && params['data']['lymph_nodes_positive'].to_i <= 3 && params['data']['micrometastasis'].to_i == 1
@N = 'N1mi'
elsif params['data']['lymph_nodes_positive'].to_i >= 4 && params['data']['lymph_nodes_positive'].to_i <= 9
@N = 'N2'
elsif params['data']['lymph_nodes_positive'].to_i >= 10
@N = 'N3'
end
@M = ''
if params['data']['Distant_Metastasis'].to_i != 1
@M = 'M0'
else
@M = 'M1'
end
if @M == 'M0'
if @T == 'T1' && (@N == 'N0' || @N == 'N1mi')
result['pstage'] = 1
elsif ((@T == 'T2' || @T == 'T3') && (@N == 'N0')) || ((@T == 'T1' || @T == 'T2') && (@N == 'N1'))
result['pstage'] = 2
elsif (@T == 'T4' && @N == 'N0') || ((@T == 'T3' || @T == 'T4') && @N == 'N1') || (@N == 'N2' || @N == 'N3')
result['pstage'] = 3
end
else
result['pstage'] = 4
end
if (params['data']['ER_status'].to_i != 2 || params['data']['PR_status'].to_i != 2) && (params['data']['HER2_status'].to_i != 1)
result['subtype'] = 1
elsif params['data']['ER_status'].to_i == 2 && params['data']['PR_status'].to_i == 2 && (params['data']['HER2_status'].to_i == 1)
result['subtype'] = 2
elsif params['data']['ER_status'].to_i == 2 && params['data']['PR_status'].to_i == 2 && (params['data']['HER2_status'].to_i != 1)
result['subtype'] = 3
end
if params['data']['grade'].to_i == 2 || params['data']['grade'].to_i == 4
result['grade_2'] = 1
else
result['grade_2'] = 0
end
if params['data']['grade'].to_i == 3
result['grade_3'] = 1
else
result['grade_3'] = 0
end
if result['subtype'].to_i == 2
result['subtype_HER2'] = 1
else
result['subtype_HER2'] = 0
end
if result['subtype'].to_i == 3
result['subtype_triple'] = 1
else
result['subtype_triple'] = 0
end
if result['pstage'].to_i == 2
result['pstage_2'] = 1
else
result['pstage_2'] = 0
end
if result['pstage'].to_i == 3
result['pstage_3'] = 1
else
result['pstage_3'] = 0
end
if result['pstage'].to_i == 4
result['pstage_4'] = 1
else
result['pstage_4'] = 0
end
if params['data']['lvi'].to_i == 1
result['lvi_yes'] = 1
else
result['lvi_yes'] = 0
end
result['lpv'] = ((result['age1']-0.7276655)*(-10.87)+(result['age2']+0.4540707)*8.968+(result['size1']-0.643632)*0.7678+(result['nposit']-1.346932)*0.5339+
result['grade_2']*0.4795+result['grade_3']*0.818+result['subtype_HER2']*0.1806+result['subtype_triple']*0.6457+result['pstage_2']*0.5311+
result['pstage_3']*1.134+result['pstage_4']*2.172+result['lvi_yes']*0.3321-0.04 rescue 'error')
@years = ['1','3','5']
result['table'] = '<input id="current_year" type="hidden" value="'+@years[-1]+'" index="0"/><p id="cancer_table_texts">此分析微針對已接受手術後病人下表為此病人分別在第1年、3及5年下根據選定的術後治療所估計的存活率</p>'
result['table'] += '<a>第</a>'
result['years'] = @years
@years.each{|year| result['table'] += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm">'+year+'</button>')}
result['table'] += '<a>年</a>'
@table_head = (locale == "zh_tw") ? ['治療','額外治療效益','總生存率(%)'] :['Treatment','Additional Benefit','Overall Survival(%)']
@head_name = ['Treatment','Additional_Benefit','Overall_Survival']
@therapy_choices = (locale == "zh_tw") ? ['純手術'] :['Surgery only']
@form_to_show.form_show_in_result.values.each{|choice| @therapy_choices.push choice["name"][locale]}
@therapy_names = ['Surgery_only']
@form_to_show.form_show_in_result.values.each{|choice| @therapy_names.push choice["variable"]}
result['treatmeny_method'] = @therapy_names
result['table'] += '<table><thead><tr>'
@table_head.each_with_index{|head,index| result['table'] += ('<th class="cancer_th '+@head_name[index]+'">' + head + '</th>')}
result['table'] += '</tr></thead><tbody>'
@therapy_choices.each_with_index do |choice,i|
result['table'] += '<tr class="'+@therapy_names[i].to_s+'">'
@table_head.each_with_index do |head,index|
result['table'] += ('<td class="cancer_td '+ @head_name[index]+'">' + ((index == 0) ? (((i==0)? '' : '+') + choice) : '-') + '</td>')
end
result['table'] += '</tr>'
end
result['texts'] = '<a>此研究分析來自已接受根除性手術後之婦女所得之結果,根據您所輸入的資訊以及治療方式,</a>'+'<div style="clear:both;"></div>'+'<a>在術後第</a>'
@years.each{|year| result['texts'] += ('<button class="cancer_years cancer_table_btn btn btn-default btn-sm">'+year+'</button>')}
result['texts'] += '<a>年</a>'
@lpv_calc = [-0.001476145,-0.01261639,-0.02519608]
@servive_ratio = ((Math.exp(@lpv_calc[-1])**(Math.exp(result['lpv'])))*100).round
result['texts'] += ('<p class="show"><a>100 位只接受根除性手術的婦女中,有 '+ '<a class="'+@therapy_names[0]+' Overall_Survival">'+@servive_ratio.round.to_s+'</a>'+'<a> 位婦女,術後 </a>'+'<a class="surgery_year">'+@years[-1].to_s+'</a>'+'<a>年仍為存活</a></p>')
result['texts'] += '<a class="addition">'+',此外</a><div class="extra-text" style="display:none;"><div class="texts_show" style="clear:both;"></div>'
result['texts'] += '</div>'
result['servive_ratio'] = @servive_ratio
end
result = result.merge(params)
render :json=> result
# rescue
# render :json=> params
#end
end
def index
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
create_first_field
@table_str = '<div id="cancer_table"><div id="show_help_modal" class="modal fade"></div>'
@table_str += '<div id="cancer_table_top"><div id="text_descibe">'+@form_to_show.text_descibe[I18n.locale.to_s] +'</div>'
@size = {'small'=>@form_to_show.small,'medium'=>@form_to_show.medium,'large'=>@form_to_show.large}
@table_str += '<div id="font_size_choices">'+'<label id="font_texts">'+t('cancerpredict.font_size')+':</label>'
@size.each{|size_key,size_value| @table_str += ('<button class="cancer_table_btn btn btn-default btn-sm" onclick="document.getElementById(\'cancer_table\').style[\'font-size\']=\''+size_value+'\';document.getElementById(\'cancer_predict_result_block\').style[\'font-size\']=\''+size_value+'\';">'+t('cancerpredict.'+size_key)+'</button>' )}
@table_str += '</div></div><div style="clear:both;"></div>'
@table_str_left = '<div id="cancer_table_left">'
@table_str_right = '<div id="cancer_table_right">'
@form_to_show.form_show.each do |num,property|
@field_property = {}
property.each do |key,value|
@value= value
if @value.class == BSON::Document || @value.class == Hash
@disp_value = @value[I18n.locale.to_s] rescue ""
else
@disp_value = @value
end
@field_property[key] = @disp_value
end
if @field_property['right'] == 0
@table_str_left += '<div data-key='+num.to_s+'>'
break if @field_property["name"] == ""
@table_str_left += '<label for="'+@field_property["variable"]+'" style="float:left;'+(@field_property["comment_text"] == "" ? "margin-right: 2.125em;" : "")+'" class="cencer_table_name">'
@table_str_left += @field_property["name"]
@table_str_left += '</label>'
if @field_property["comment_text"] != ""
@table_str_left += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"></button>'
@table_str_left += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'/>'
else
@table_str_left += '<div style="margin-right: 2.125em;"></div>'
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_str_left += '<input class="num_only float_num cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'"/>'
else
@table_str_left += '<form class="for_num">'
@table_str_left += '<input class="num_only cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))"/>'
@please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_str_left += '<select class="select_num"><option value="">'+@please_choice+'</option>'
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_str_left += ('<option value="' + @num.to_s + '">' + @num.to_s + '</option>')
end
@table_str_left += '</select></form>'
end
else
@table_str_left += '<div class= "cancer-btn-group cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;">'
@field_property["choice_fields"].each do |create_choice|
@table_str_left += '<button class="cancer_table_btn btn btn-default btn-sm">'+create_choice+'</button>'
@table_str_left += '<input type="hidden" value="0" name="'+create_choice+'"/>'
end
@table_str_left += '</div>'
end
if @field_property["hint"].to_s != ""
@table_str_left +='<div style="color: rgb(104, 104, 104);font-size:0.75em;clear:both;">'+@field_property["hint"]+'</div>'
end
@table_str_left += '</div><div style="clear:both;"></div>'
else
@table_str_right += '<div data-key='+num.to_s+'>'
break if @field_property["name"] == ""
@table_str_right += '<label for="'+@field_property["variable"]+'" style="float:left;'+(@field_property["comment_text"] == "" ? "margin-right: 2.125em;" : "")+'" class="cencer_table_name">'
@table_str_right += @field_property["name"]
@table_str_right += '</label>'
if @field_property["comment_text"] != ""
@table_str_right += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"></button>'
@table_str_right += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'/>'
else
@table_str_right += '<div style="margin-right: 2.125em;"></div>'
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_str_right += '<input class="num_only float_num cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'"/><button class="btn-sub"></button><button class="btn-add"></button>'
else
@table_str_right += '<form class="for_num">'
@table_str_right += '<input class="num_only cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))"/>'
@please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_str_right += '<select class="select_num"><option value="">'+@please_choice+'</option>'
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_str_right += ('<option value="' + @num.to_s + '">' + @num.to_s + '</option>')
end
@table_str_right += '</select></form>'
end
else
@table_str_right += '<div class= "cancer-btn-group cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;">'
@field_property["choice_fields"].each do |create_choice|
@table_str_right += '<button class="cancer_table_btn btn btn-default btn-sm">'+create_choice+'</button>'
@table_str_right += '<input type="hidden" value="0" name="'+create_choice+'"/>'
end
@table_str_right += '</div>'
end
if @field_property["hint"].to_s != ""
@table_str_right +='<div class="hint-texts">'+@field_property["hint"]+'</div>'
end
@table_str_right += '</div><div style="clear:both;"></div>'
end
end
@table_str_left += '</div>'
@table_str_right += '</div>'
@table_str += (@table_str_left+@table_str_right)
@table_str +='<div style="clear:both;"></div>'
@table_result_str = '<div id="cancer_predict_result" style="'+((@form_to_show.form_result_is_right == 0) ? 'float:right;' : '')+'"><span class="result_title">'+t("cancerpredict.table.result").to_s+'</span><div style="clear:both;"></div>'
@tab_name = ['table','text']
@table_result_str += '<div id="result_contents"><div class="result_tab_group">'
@tab_name.each_with_index{|name,index| @table_result_str += ('<div id="result_'+name+'_tab" class="result_tab'+((index != 0) ? "" : " active")+'"><a>'+t("cancerpredict.table."+name).to_s+'</a></div><div style="clear:both;"></div>')}
@table_result_str += '</div><div class="result_content_group">'
@tab_name.each{|name| @table_result_str += '<div id="result_'+name+'_content" class="result_content"><a>'+'no content'+'</a></div>'}
@table_result_str += '</div></div></div>'
@table_button = '<div id="cancer_table_button_group">'
@submit_btn_str='<button id="cancer_table_submit"><a class="white_text" href="#cancer_predict_result_block">'+t('cancerpredict.table.Submit').to_s+'</a></button>'
@reset_btn_str='<button id="cancer_table_reset">'+t('cancerpredict.table.Reset').to_s+'</button>'
@table_button += (@submit_btn_str+@reset_btn_str+'<div style="clear:both;"></div></div></div>')
@table_result_choice_fileds = '<div id="cancer_table_right_result">' if @form_to_show.form_result_is_right == 1
@table_result_choice_fileds = '<div id="cancer_table_left_result">' if @form_to_show.form_result_is_right == 0
@table_result_choice_fileds += '<span class="result_title">'+t('cancerpredict.table.Therapy_choice').to_s+'</span><div style="clear:both;"></div>'
@table_result_choice_fileds += '<div id="choice_fields">'
@form_to_show.form_show_in_result.each do |num,property|
@field_property = {}
property.each do |key,value|
@value= value
if @value.class == BSON::Document || @value.class == Hash
@disp_value = @value[I18n.locale.to_s] rescue ""
else
@disp_value = @value
end
@field_property[key] = @disp_value
end
@table_result_choice_fileds += '<div data-key='+num.to_s+'>'
break if @field_property["name"] == ""
@table_result_choice_fileds += '<label for="'+@field_property["variable"]+'" style="float:left;'+(@field_property["comment_text"] == "" ? "margin-right: 2.125em;" : "")+'" class="cencer_table_name">'
@table_result_choice_fileds += @field_property["name"]
@table_result_choice_fileds += '</label>'
if @field_property["comment_text"] != ""
@table_result_choice_fileds += '<button tabindex="0" class="cancer_help_btn" data-target="#show_help_modal" style="float:left;cursor: pointer;padding: 0em 0.475em;font-size: 1.25em;border-radius: 15px;background-color: rgb(210, 106, 2);border-color: rgb(210, 106, 2);color: white;"></button>'
@table_result_choice_fileds += '<input class="help_texts" type="hidden" value="'+@field_property["comment_text"]+'" name='+@field_property["variable"]+'></input>'
else
@table_result_choice_fileds += '<div style="margin-right: 2.125em;"></div>'
end
if @field_property["is_num"] == 1
if @field_property["is_float"] == 1
@table_result_choice_fileds += '<input class="num_only float_num cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'"/><button class="btn-sub"></button><button class="btn-add"></button>'
else
@table_result_choice_fileds += '<form class="for_num">'
@table_result_choice_fileds += '<input class="num_only cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;" data-range="'+@field_property["range"].to_s+'" onkeyup="value=value.replace(/[^\d]/g,\'\') "onbeforepaste="clipboardData.setData(\'text\',clipboardData.getData(\'text\').replace(/[^\d]/g,\'\'))"/><button class="btn-sub"></button><button class="btn-add"></button>'
@please_choice = (I18n.locale.to_s == "zh_tw") ? "請選擇" : "Please choice"
@table_result_choice_fileds += '<select class="select_num"><option value="">'+@please_choice+'</option>'
for @num in @field_property["range"][0] .. @field_property["range"][1]
@table_result_choice_fileds += ('<option value="' + @num.to_s + '">' + @num.to_s + '</option>')
end
@table_result_choice_fileds += '</select></form>'
end
else
@table_result_choice_fileds += '<div class= "cancer-btn-group cancer_form_field" id="'+@field_property["variable"]+'" style="float:left;clear:right;">'
@field_property["choice_fields"].each do |create_choice|
@table_result_choice_fileds += '<button class="cancer_table_btn btn btn-default btn-sm">'+create_choice+'</button>'
@table_result_choice_fileds += '<input type="hidden" value="0" name="'+create_choice+'"></input>'
end
@table_result_choice_fileds += '</div>'
end
if @field_property["hint"].to_s != ""
@table_result_choice_fileds +='<div class="hint-texts">'+@field_property["hint"]+'</div>'
end
@table_result_choice_fileds += '</div><div style="clear:both;"></div>'
end
@table_result_choice_fileds += '</div></div>'
@table_str += (@table_button+'<div id="cancer_predict_result_block">'+@table_result_str+@table_result_choice_fileds+'</div>'+'<div style="clear:both;"></div>')
{
"cancerpredict" => [],
"extras"=>{"table"=> @table_str}
}
end
def widget
uid = OrbitHelper.params[:uid] rescue ""
tags = OrbitHelper.widget_tags
categories = OrbitHelper.widget_categories || []
end
def create_first_field
if Cancerpredictfields.where("title"=>(@app_title + "_back")).take_while{true}.count == 0
@form_to_show = Cancerpredictfields.new()
@form_to_show.title = @app_title +"_back"
@form_to_show.save
end
@form_to_show
if Cancerpredictfields.where("title"=>@app_title).take_while{true}.count == 0
@form_to_show = Cancerpredictfields.new()
@form_to_show.title=@app_title
@form_to_show.save
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
else
@form_to_show = Cancerpredictfields.where("title"=>@app_title).first
end
end
end