PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/util.rb

https://github.com/dtenenbaum/echidna
Ruby | 284 lines | 219 code | 50 blank | 15 comment | 15 complexity | 151957200762ff0c389dbc8bdd62cf40 MD5 | raw file
  1. module Util
  2. require 'pp'
  3. def valid_cookie?(cookie)
  4. email, hash = cookie.split(";")
  5. Password::check("#{email};#{SECRET_SALT}", hash)
  6. end
  7. def create_cookie(email)
  8. plaintext = "#{email};#{SECRET_SALT}"
  9. "#{email};#{Password::update(plaintext)}"
  10. end
  11. def find_related_groups(group_id)
  12. sql = "select r.*, t.name, t.inverse from relationships r, relationship_types t where t.id = r.relationship_type_id and (r.group1 = ? or r.group2 = ?)"
  13. related_groups = Relationship.find_by_sql([sql, group_id, group_id])
  14. id_map= {}
  15. related_groups.each do |g|
  16. id_map[g.group1] = 1
  17. id_map[g.group2] = 1
  18. end
  19. names = ConditionGroup.find_by_sql(["select id, name from condition_groups where id in (?)", id_map.keys])
  20. names.each do |n|
  21. id_map[n.id] = n.name
  22. end
  23. #pp id_map
  24. related_groups.each do |g|
  25. g.relationship_id = g.id
  26. if (g.group1 == group_id)
  27. g.id = g.group2
  28. g.relationship = g.inverse
  29. g.name = id_map[g.group2]
  30. else
  31. g.id = g.group1
  32. g.relationship = g.name
  33. g.name = id_map[g.group1]
  34. end
  35. end
  36. related_groups.sort! do |a,b|
  37. a.name <=> b.name
  38. end
  39. related_groups
  40. end
  41. def find_conditions_for_group(group_id)
  42. sql=<<"EOF"
  43. select c.id as id, c.name from conditions c, condition_groupings g
  44. where g.condition_id = c.id
  45. and g.condition_group_id = ?
  46. order by g.sequence
  47. EOF
  48. conds = Condition.find_by_sql([sql, group_id])
  49. end
  50. def sort_conditions_for_time_series(conds)
  51. regex = /_t([+-][0-9]*)/
  52. conds.sort() do |a,b|
  53. #puts "a: #{a.name}, b: #{b.name}"
  54. if (a.name =~ regex and b.name =~ regex)
  55. a.name =~ regex
  56. a_num = $1
  57. b.name =~ regex
  58. b_num = $1
  59. a_temp = a.name.gsub(regex, "__PLACEHOLDER__")
  60. b_temp = b.name.gsub(regex, "__PLACEHOLDER__")
  61. if (a_temp == b_temp) # same time series
  62. a_pos = b_pos = true
  63. #puts "a_num: #{a_num}, b_num: #{b_num}"
  64. a_pos = false if a_num =~ /^-/
  65. b_pos = false if b_num =~ /^-/
  66. [a_num,b_num].each {|i|i.gsub!(/^[+-]/,"")}
  67. [a_num,b_num].each {|i|i.gsub!(/^0/,"")}
  68. begin
  69. a = Integer(a_num)
  70. b = Integer(b_num)
  71. a = a - (a*2) unless a_pos
  72. b = b - (b*2) unless b_pos
  73. #puts "a = #{a}, b = #{b}"
  74. a <=> b
  75. rescue
  76. a.name <=> b.name
  77. end
  78. else
  79. #puts "not the same time series"
  80. a.name <=> b.name
  81. end
  82. else
  83. a.name <=> b.name
  84. end
  85. end
  86. end
  87. def update_search_terms
  88. begin
  89. Condition.transaction do
  90. Condition.connection.execute "truncate table search_terms"
  91. add_search_term("Condition", "name")
  92. add_group_names()
  93. add_controlled_vocab_items()
  94. add_environmental_perturbations()
  95. add_knockouts()
  96. add_search_term("Tag", "tag")
  97. # todo - knockouts
  98. end
  99. rescue Exception => ex
  100. puts ex.message
  101. puts ex.backtrace
  102. end
  103. "done"
  104. end
  105. #############################
  106. # private functions
  107. #############################
  108. private
  109. def add_knockouts
  110. puts "add_knockouts"
  111. sql = "select k.gene as name, ka.condition_id from knockouts k, knockout_associations ka " +
  112. "where ka.knockout_id = k.id order by name, condition_id"
  113. add_items sql
  114. end
  115. def add_environmental_perturbations()
  116. puts "add_environmental_perturbations"
  117. sql = "select e.perturbation as name, a.condition_id from environmental_perturbations e, " +
  118. "environmental_perturbation_associations a where a.environmental_perturbation_id = e.id " +
  119. "order by name, condition_id"
  120. add_items sql
  121. end
  122. def add_controlled_vocab_items()
  123. puts "add_controlled_vocab_items"
  124. sql = "select o.condition_id, n.name from observations o, controlled_vocab_items n " +
  125. "where o.name_id = n.id order by o.condition_id, n.name"
  126. add_items sql
  127. end
  128. def add_group_names()
  129. puts "add_group_names"
  130. sql = "select g.name, c.condition_id from condition_groups g, condition_groupings c " +
  131. "where c.condition_group_id = g.id order by name, condition_id"
  132. add_items sql
  133. end
  134. def add_items(sql)
  135. items = Condition.find_by_sql(sql)
  136. #puts "# of items: #{items.size}, uniq size = #{items.uniq.size}"
  137. for item in items
  138. s = SearchTerm.new(:condition_id => item.condition_id, :word => item.name, :creation_time => Time.now, :int_timestamp => Time.now.to_i)
  139. s.save
  140. end
  141. end
  142. def get_groups_for_conditions(conds)
  143. groupings = ConditionGrouping.find :all
  144. groupmap = {}
  145. for grouping in groupings
  146. groupmap[grouping.condition_id] = [] unless groupmap.has_key?(grouping.condition_id)
  147. groupmap[grouping.condition_id] << grouping.condition_group_id
  148. end
  149. # puts "groupmap:"
  150. # pp groupmap
  151. group_ids_to_get = {}
  152. ungrouped_ids = []
  153. for cond in conds
  154. if (groupmap.has_key?(cond.id))
  155. groupmap[cond.id].each do |item|
  156. group_ids_to_get[item] = 1
  157. end
  158. else
  159. ungrouped_ids << cond.id
  160. end
  161. end
  162. puts "group_ids_to_get:"
  163. pp group_ids_to_get
  164. puts
  165. puts "ungrouped_ids:"
  166. pp ungrouped_ids
  167. groups = ConditionGroup.find_by_sql(["select * from condition_groups where id in (?) order by name", group_ids_to_get.keys])
  168. ungroup = ConditionGroup.new(:name => 'Ungrouped Results')
  169. ungroup.ungrouped_ids = ungrouped_ids
  170. groups << ungroup unless ungrouped_ids.empty?
  171. #groups.sort! do |a,b|
  172. #end
  173. groups
  174. end
  175. def add_search_term(table, fields)
  176. items = []
  177. line = "items = #{table}.find :all"
  178. eval(line)
  179. id_column = items.first.attributes.has_key?('condition_id') ? "condition_id" : "id"
  180. f_arr = (fields.is_a?(Array)) ? fields : [fields]
  181. for item in items
  182. for thing in f_arr
  183. s = SearchTerm.new(:condition_id => item.send(id_column), :word => item.send(thing), :creation_time => Time.now, :int_timestamp => Time.now.to_i)
  184. #pp s
  185. s.save
  186. end
  187. end
  188. end
  189. def group_description(group_id)
  190. group = ConditionGroup.find(params[:group_id])
  191. conds = Condition.find_by_sql(["select * from conditions where id in (select condition_id from condition_groupings where condition_group_id = ?) order by sequence",params[:group_id]])
  192. tags = Tag.find_by_sql(["select * from tags where condition_id in (#{conds.map{|i|i.id}.join(",")}) order by tag"])
  193. auto_tags = tags.find_all{|i|i.auto}.map{|i|i.tag}.uniq
  194. manual_tags = tags.reject{|i|i.auto}.map{|i|i.tag}.uniq
  195. ret = "Group Description:\n"
  196. ret += "Name: #{group.name}\n"
  197. ret += "Manual Tags: #{manual_tags.join(", ")}\n"
  198. ret += "Auto Tags: #{auto_tags.join(", ")}\n"
  199. ret += (group.is_time_series) ? "Time Series\n" : "Not Time Series\n"
  200. owner = (group.owner_id.nil?) ? nil : User.find(group.owner_id)
  201. importer = (group.importer_id.nil?) ? nil : User.find(group.importer_id)
  202. ret += "Owner: #{(owner.nil?) ? "unknown" : owner.email}\n"
  203. ret += "Conditions: #{conds.size}\n\n"
  204. for cond in conds
  205. ret += condition_description(cond.id)
  206. end
  207. ret
  208. end
  209. def condition_description(condition_id)
  210. cond = Condition.find(condition_id)
  211. species = Species.find(cond.species_id)
  212. ref = ReferenceSample.find(cond.reference_sample_id)
  213. recipe = GrowthMediaRecipe.find(cond.growth_media_recipe_id)
  214. obs = cond.observations
  215. ret = "Condition name: #{cond.name}\n"
  216. ret += "SBEAMS ID: #{cond.sbeams_project_id}\n"
  217. ret += "SBEAMS Timestamp: #{cond.sbeams_timestamp}\n"
  218. ret += "Forward Slide #: #{cond.forward_slide_number}\n" if cond.forward_slide_number
  219. ret += "Reverse Slide #: #{cond.reverse_slide_number}\n" if cond.reverse_slide_number
  220. ret += "Species: #{species.name}\n"
  221. ret += "Reference Sample: #{ref.name}\n"
  222. owner = (cond.owner_id.nil?) ? nil : User.find(cond.owner_id)
  223. importer = (cond.importer_id.nil?) ? nil : User.find(cond.importer_id)
  224. ret += "Owner: #{(owner.nil?) ? "unknown" : owner.email}\n"
  225. ret += "Imported By: #{(importer.nil?) ? "unknown" : importer.email}\n"
  226. ret += "Recipe: #{recipe.name}\n"
  227. ret += "Observations:\n\n"
  228. for ob in obs
  229. ret += "#{ob.name} = #{ob.string_value}\n"
  230. end
  231. ret += "\n"
  232. ret
  233. end
  234. end