This repository was archived by the owner on Aug 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate_intermediate.rb
More file actions
291 lines (219 loc) · 11.5 KB
/
populate_intermediate.rb
File metadata and controls
291 lines (219 loc) · 11.5 KB
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
#!/usr/bin/ruby
#enables using path to classes without installed in path
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), '.' ) )
require 'set'
require 'psych'
# require 'gchart'
require 'uri'
require 'net/http'
require 'lib/homologenie/homolog'
require 'lib/homologenie/protein'
require 'lib/homologenie/taxonomy'
require 'lib/homologenie/results'
require 'lib/homologenie/species_set'
require 'lib/homologenie/cost/cost'
#require 'lib/homologenie/species_gene'
if __FILE__ == $0
file_prepend = "test_"
CHART_URI = "http://chart.googleapis.com/chart?"
post_uri = URI.parse("http://chart.googleapis.com/chart")
graph_options = {'cht' => 's', #chart type = scatter
'chs' => '650x450', #chart size
'chd' => '', #chart data will be filled later
'chds' => "0,1.0,0,80", #x,y data range
'chtt' => '', #chart title will be filled later
'chxt' => "x,x,y,y", #chart axes visible
'chxl' => '0:|0.0|0.2|0.4|0.6|0.8|1.0|1:|||Match|||2:|0|10|20|30|40|50|60|70|80|3:|||Cost||',
'chxs' => '1,333333,14|3,333333,14'
# 'chxr' => '0,0,1,.2|2,0,80,10'
}
tax_ids_to_use = [ 7955, #Danio rerio
# 9606, #Homo sapiens
# 9598, #Pan troglodytes
# 9544, #Macaca mulatta
# 9615, #Canis lupus familiaris
# 9913, #Bos taurus
# 10090, #Mus musculus
# 10116, #Rattus norvegicus
9031] #Gallus gallus
# tax_ids_to_use = [ 7955, #Danio rerio
# 9606, #Homo sapiens
# # 9598, #Pan troglodytes
# # 9544, #Macaca mulatta
# # 9615, #Canis lupus familiaris
# # 9913, #Bos taurus
# # 10090, #Mus musculus
# # 10116, #Rattus norvegicus
# # 9031 #Gallus gallus
# ]
name_lookup = { 7955 => "Danio rerio",
9606 => "Homo sapiens",
9598 => "Pan troglodytes",
9544 => "Macaca mulatta",
9615 => "Canis lupus familiaris",
9913 => "Bos taurus",
10090 => "Mus musculus",
10116 => "Rattus norvegicus",
9031 => "Gallus gallus"}
results = Hash.new()
# homologs_in = File.new( 'data/small_database.yaml', 'r')
homologs_in = File.new( 'data/homologene_database.data', 'r')
raw_homologs = Psych.load_file( homologs_in )
homolog_count = 0
#puts raw_homologs[9776].taxonomies[7955].proteins
# puts raw_homologs
puts ""
raw_homologs.each do |homologene_id, taxonomies|
inner_array = tax_ids_to_use.dup()
# itereates until last tax id in list since 2 are needed for a set
# TODO: replace with recursive function so >2 pair sets can be used
(0..( tax_ids_to_use.length() - 2 ) ).each do |i|
#grab remaining tax ids from list
inner_array = tax_ids_to_use.values_at((i + 1)..( tax_ids_to_use.length - 1))
outer_tax = tax_ids_to_use[i]
if raw_homologs[homologene_id].taxonomies.has_key?(outer_tax)
#avoids duplicating data reads for outer tax
outer_protein_obj = raw_homologs[homologene_id].taxonomies[outer_tax].proteins[raw_homologs[homologene_id].taxonomies[outer_tax].proteins.keys[0]]
#TODO add functionality to class so .calc_cost automatically performed
outer_protein_obj.set_cost( HomoloGenie::Cost.calc_cost(outer_protein_obj.sequence) )
if outer_protein_obj.cost == nil
next
else
# p outer_protein_obj
outer_taxonomy_obj = HomoloGenie::Taxonomy.new(raw_homologs[homologene_id].taxonomies[outer_tax].id,
raw_homologs[homologene_id].taxonomies[outer_tax].name)
outer_taxonomy_obj.proteins[outer_protein_obj.accession()] = outer_protein_obj
inner_array.each do |inner_tax|
if outer_tax != inner_tax #avoid duplicate taxes
if raw_homologs[homologene_id].taxonomies.has_key?(inner_tax)
#has both keys
tax_set = Set.new [outer_tax, inner_tax]
# inner_taxonomy_obj = prep_tax(homologene_id, inner_tax)
#p raw_homologs[homologene_id].taxonomies[inner_tax].proteins[raw_homologs[homologene_id].taxonomies[inner_tax].proteins.keys[0]]
inner_protein_obj = raw_homologs[homologene_id].taxonomies[inner_tax].proteins[raw_homologs[homologene_id].taxonomies[inner_tax].proteins.keys[0]]
#TODO add functionality to class so .calc_cost automatically performed
inner_protein_obj.set_cost( HomoloGenie::Cost.calc_cost(inner_protein_obj.sequence) )
if inner_protein_obj.cost == nil
next
else
# puts "Inner:" + inner_tax.to_s
# p inner_protein_obj
inner_taxonomy_obj = HomoloGenie::Taxonomy.new(raw_homologs[homologene_id].taxonomies[inner_tax].id,
raw_homologs[homologene_id].taxonomies[inner_tax].name)
inner_taxonomy_obj.proteins[inner_protein_obj.accession] = inner_protein_obj
#add species to homolog object
temp_homolog = HomoloGenie::Homolog.new(homologene_id)
temp_homolog.taxonomies[outer_taxonomy_obj.id] = outer_taxonomy_obj
temp_homolog.taxonomies[inner_taxonomy_obj.id] = inner_taxonomy_obj
temp_homolog.set_match
temp_species_set = HomoloGenie::SpeciesSet.new(tax_set)
temp_species_set.homologs[homologene_id] = temp_homolog
#will either create results[tax_set] or mod existing
if results.has_key?(tax_set)
results[tax_set].push(temp_species_set)
else
results[tax_set] = [temp_species_set]
end #if
end #if
end #if
else
puts "Duplicate keys at homologene id " + homologene_id.to_s()
end #if
end #do
end #if
end #if
end #do
homolog_count += 1
print "\rHomologs processed: " + ( homolog_count / raw_homologs.size ).round(0).to_s + "%"
end #do
# puts "Results:"
# p results
# p results.keys
if true
# if nil
results.each do |key, value|
set_graphs = Hash.new
species_names = String.new
# puts species_names.length.to_s
key.each do |taxii|
#set_graphs[taxii] = { "cost" => Array.new, "match" => Array.new }
#Binning
set_graphs[taxii] = Hash.new
# puts species_names.length.to_s
if species_names.length == 0
species_names = name_lookup[taxii]
else
species_names += " vs " + name_lookup[taxii]
end #if
end #do
# p key
# puts "Homologs:"
value.each do |homolog|
# p homolog.homologs.keys
# puts "\tSet"
homolog.homologs.each do |homologene_id, set_val|
set_val.taxonomies.each do |tax_key, tax_val|
#Binning - match score rounded to nearest .01
#Average cost rounded to nearest whole number,
# -length used does not include initial M
#ignore
set_graphs[tax_key][set_val.match.round(2)] = (tax_val.proteins[tax_val.proteins.keys[0]].cost / ( tax_val.proteins[tax_val.proteins.keys[0]].sequence.length - 1 ) ).round(0)
#set_graphs[tax_key]["match"].push(set_val.match.round(4))
# puts "\t\t" + tax_val.proteins[tax_val.proteins.keys[0]].accession.to_s
# puts "\t\t\tAvg cost:" + ( tax_val.proteins[tax_val.proteins.keys[0]].cost / tax_val.proteins[tax_val.proteins.keys[0]].sequence.length ).to_s
end #do
# puts "\t\tMatch: " + set_val.match.to_s
end #do
end #do
key.each do |taxii|
chd = 't:'
# set_graphs[taxii]["match"].each do |data|
# chd += data.to_s + ','
# end #do
# chd = chd.chop + '|'
# set_graphs[taxii]["cost"].each do |data|
# chd += data.to_s + ','
# end #do
# chd.chop!
#binning
chd_match = String.new
chd_cost = String.new
set_graphs[taxii].each do |match_binned,cost_binned|
chd_match += match_binned.to_s + ','
chd_cost += cost_binned.to_s + ','
end #do
chd += chd_match.chop! + '|' + chd_cost.chop!
graph_options["chd"] = chd
graph_options["chtt"] = name_lookup[taxii] + " for " + species_names
filename = ("temp " + graph_options["chtt"] + ".png").gsub(' ','_')
query = CHART_URI
graph_options.each do |feature, attribute|
query += feature + '=' + attribute + '&'
end
query = query.chop!.gsub(' ','%20')
#puts "length: " + query.length.to_s
#puts query
http = Net::HTTP.new(post_uri.host, post_uri.port)
# http.use_ssl = true
request = Net::HTTP::Post.new(post_uri.request_uri)
request.set_form_data(graph_options)
response = http.request(request)
out_file = File.new('./data/charts/' + filename, 'w')
out_file.write(response.body)
out_file.close
# puts set_graphs[taxii]["match"].to_s
#puts chd_cost
#puts chd_match
#p graph_options
end #do
end #do
end #if
homologs_in.close()
# def prep_tax(temp_homologene_id, temp_tax_id)
# protein_temp = raw_homologs[temp_homologene_id].taxonomies[temp_tax_id].proteins[0].clone()
# taxonomy_temp = Taxonomy.new(raw_homologs[temp_homologene_id].taxonomies[temp_tax_id].id.clone(),
# raw_homologs[temp_homologene_id].taxonomies[temp_tax_id].name.clone())
# taxonomy_temp.proteins[protein_temp.accession] = protein_temp
# return taxonomy_temp
# end #def
end #if