Class: Network

Inherits:
Object
  • Object
show all
Defined in:
lib/NetAnalyzer/network.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layers) ⇒ Network

BASIC METHODS



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/NetAnalyzer/network.rb', line 31

def initialize(layers)
	@threads = 0
	@nodes = {}
	@edges = {}
	@reference_nodes = []
	@group_nodes = {}
	@adjacency_matrices = {}
	@kernels = {}
	@layers = layers
	@association_values = {}
	@control_connections = {}
	@compute_pairs = :conn
	@compute_autorelations = true
	@loaded_obos = []
	@ontologies = []
	@layer_ontologies = {}
end

Instance Attribute Details

#association_valuesObject

Returns the value of attribute association_values.



27
28
29
# File 'lib/NetAnalyzer/network.rb', line 27

def association_values
  @association_values
end

#control_connectionsObject

Returns the value of attribute control_connections.



27
28
29
# File 'lib/NetAnalyzer/network.rb', line 27

def control_connections
  @control_connections
end

#group_nodesObject

Returns the value of attribute group_nodes.



27
28
29
# File 'lib/NetAnalyzer/network.rb', line 27

def group_nodes
  @group_nodes
end

#kernelsObject

Returns the value of attribute kernels.



27
28
29
# File 'lib/NetAnalyzer/network.rb', line 27

def kernels
  @kernels
end

#reference_nodesObject

Returns the value of attribute reference_nodes.



27
28
29
# File 'lib/NetAnalyzer/network.rb', line 27

def reference_nodes
  @reference_nodes
end

#threadsObject

Returns the value of attribute threads.



27
28
29
# File 'lib/NetAnalyzer/network.rb', line 27

def threads
  @threads
end

Instance Method Details

#add_edge(nodeID1, nodeID2) ⇒ Object



58
59
60
61
# File 'lib/NetAnalyzer/network.rb', line 58

def add_edge(nodeID1, nodeID2)
	query_edge(nodeID1, nodeID2)
	query_edge(nodeID2, nodeID1)
end

#add_nested_record(hash, node1, node2, val) ⇒ Object



753
754
755
756
757
758
759
760
# File 'lib/NetAnalyzer/network.rb', line 753

def add_nested_record(hash, node1, node2, val)
	query_node1 = hash[node1]
	if query_node1.nil?
		hash[node1] = {node2 => val}
	else
		query_node1[node2] = val
	end
end

#add_node(nodeID, nodeType = 0) ⇒ Object



54
55
56
# File 'lib/NetAnalyzer/network.rb', line 54

def add_node(nodeID, nodeType = 0)
	@nodes[nodeID] = Node.new(nodeID, nodeType)
end

#add_record(hash, node1, node2) ⇒ Object



744
745
746
747
748
749
750
751
# File 'lib/NetAnalyzer/network.rb', line 744

def add_record(hash, node1, node2)
	query = hash[node1]
	if query.nil?
		hash[node1] = [node2]
	else
		query << node2
	end
end

#bfs_shortest_path(start, goal, paths = false) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/NetAnalyzer/network.rb', line 318

def bfs_shortest_path(start, goal, paths=false)
	dist = nil
    explored = {} # keep track of explored nodes
    previous = {}
    queue = [[start, 0]] # keep track of all the paths to be checked
    is_goal = false
    while !queue.empty? && !is_goal # keeps looping until all possible paths have been checked
        node, dist = queue.pop # pop the first path from the queue
        if !explored.include?(node) # get the last node from the path
            neighbours = @edges[node] 
            explored[node] = true # mark node as explored
            next if neighbours.nil?
            dist += 1 
            neighbours.each do |neighbour| # go through all neighbour nodes, construct a new path
            	next if explored.include?(neighbour)
                queue.unshift([neighbour, dist]) # push it into the queue
                previous[neighbour] = node if paths
                if neighbour == goal # return path if neighbour is goal
                	is_goal = true
                	break
                end
            end
        end
    end
	if is_goal
		path = build_path(previous, start, goal) if paths
	else
		dist = nil 
		path = []
	end
    return dist, path
end

#build_path(previous, startNode, stopNode) ⇒ Object



351
352
353
354
355
356
357
358
359
360
# File 'lib/NetAnalyzer/network.rb', line 351

def build_path(previous, startNode, stopNode)
	path = []
	currentNode = stopNode
	path << currentNode
	while currentNode != startNode
	    currentNode = previous[currentNode]
		path << currentNode
	end
	return path
end

#clean_autorelations_on_association_valuesObject



528
529
530
531
532
# File 'lib/NetAnalyzer/network.rb', line 528

def clean_autorelations_on_association_values
	@association_values.each do |meth, values|
		values.select!{|relation| @nodes[relation[0]].type != @nodes[relation[1]].type}
	end
end

#collect_nodes(args) ⇒ Object



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/NetAnalyzer/network.rb', line 468

def collect_nodes(args)
	nodeIDsA = nil
	nodeIDsB = nil
	if @compute_autorelations
		if args[:layers] == :all
			nodeIDsA = @nodes.keys
		else
			nodeIDsA = []
			args[:layers].each do |layer|
				nodeIDsA.concat(@nodes.select{|id, node| node.type == layer}.keys)
			end
		end
	else
		if args[:layers] != :all
			nodeIDsA = @nodes.select{|id, node| node.type == args[:layers][0]}.keys
			nodeIDsB = @nodes.select{|id, node| node.type == args[:layers][1]}.keys
		end
	end
	return nodeIDsA, nodeIDsB
end

#communities_avg_sht_path(coms) ⇒ Object



260
261
262
263
264
265
266
267
# File 'lib/NetAnalyzer/network.rb', line 260

def communities_avg_sht_path(coms) 
	avg_sht_path = []
	coms.each do |com_id, com|
		dist, paths = compute_avg_sht_path(com)
		avg_sht_path << dist
	end
	return avg_sht_path
end

#communities_comparative_degree(coms) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/NetAnalyzer/network.rb', line 252

def communities_comparative_degree(coms) 
	comparative_degrees = []
	coms.each do |com_id, com|
		comparative_degrees << compute_comparative_degree(com)
	end
	return comparative_degrees
end

#compute_adjusted_pvalue(relations, log_val = true) ⇒ Object



714
715
716
717
718
719
720
# File 'lib/NetAnalyzer/network.rb', line 714

def compute_adjusted_pvalue(relations, log_val=true)
	relations.each_with_index do |data, i| #p1, p2, pval
		pval_adj = yield(data.last, i)		
		pval_adj = -Math.log10(pval_adj) if log_val && pval_adj > 0
		data[2] = pval_adj 
	end
end

#compute_adjusted_pvalue_benjaminiHochberg(relations) ⇒ Object



737
738
739
740
741
742
# File 'lib/NetAnalyzer/network.rb', line 737

def compute_adjusted_pvalue_benjaminiHochberg(relations)
	adj_pvalues = get_benjaminiHochberg_pvalues(relations.map{|rel| rel.last})
	compute_adjusted_pvalue(relations) do |pval, index|
		adj_pvalues[index]
	end
end

#compute_adjusted_pvalue_bonferroni(relations) ⇒ Object



728
729
730
731
732
733
734
735
# File 'lib/NetAnalyzer/network.rb', line 728

def compute_adjusted_pvalue_bonferroni(relations)
	n_comparations = relations.length
	compute_adjusted_pvalue(relations) do |pval, index|
		adj = pval * n_comparations
		adj = 1 if adj > 1
		adj
	end
end

#compute_avg_sht_path(com, paths = false) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/NetAnalyzer/network.rb', line 290

def compute_avg_sht_path(com, paths=false)
	path_lengths = []
	all_paths = []
	group = com.dup
	while !group.empty?
		node_start = group.shift
		sht_paths = Parallel.map(group, in_processes: @threads) do |node_stop|
		#group.each do |node_stop|
			dist, path = shortest_path(node_start, node_stop, paths)
			[dist, path]
			#path_lengths << dist if !dist.nil?
			#all_paths << path if !path.empty?
		end
		sht_paths.each do |dist, path|
			path_lengths << dist
			all_paths << path				
		end
	end
	if path_lengths.include?(nil)
		avg_sht_path = nil
	else
		avg_sht_path = path_lengths.inject(0){|sum,l| sum + l}.fdiv(path_lengths.length)
	end
	return avg_sht_path, all_paths
end

#compute_comparative_degree(com) ⇒ Object

see Girvan-Newman Benchmark control parameter in networksciencebook.com/chapter/9#testing (communities chapter)



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/NetAnalyzer/network.rb', line 277

def compute_comparative_degree(com) # see Girvan-Newman Benchmark control parameter in http://networksciencebook.com/chapter/9#testing (communities chapter)
	internal_degree = 0
	external_degree = 0
	com.each do |nodeID|
		nodeIDneigh = @edges[nodeID]
		next if nodeIDneigh.nil?
		internal_degree += (nodeIDneigh & com).length
		external_degree += (nodeIDneigh - com).length
	end
	comparative_degree = external_degree.fdiv(external_degree + internal_degree)
	return comparative_degree
end

#compute_group_metrics(output_filename) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/NetAnalyzer/network.rb', line 223

def compute_group_metrics(output_filename)
	metrics = []
	header = ['group']
	@group_nodes.keys.each do |k|
		metrics << [k]
	end
	header << 'comparative_degree'
	comparative_degree = communities_comparative_degree(@group_nodes)
	comparative_degree.each_with_index{|val,i| metrics[i] << replace_nil_vals(val)}
	header << 'avg_sht_path'
	avg_sht_path = communities_avg_sht_path(@group_nodes)
	avg_sht_path.each_with_index{|val,i| metrics[i] << replace_nil_vals(val)}
	if !@reference_nodes.empty?
		header.concat(%w[node_com_assoc_by_edge node_com_assoc_by_node])
		node_com_assoc = compute_node_com_assoc_in_precomputed_communities(@group_nodes, @reference_nodes.first)
		node_com_assoc.each_with_index{|val,i| metrics[i].concat(val)}
	end
	File.open(output_filename, 'w') do |f|
		f.puts header.join("\t")
		metrics.each do |gr|
			f. puts gr.join("\t")
		end
	end
end

#compute_log_transformation(relations) ⇒ Object

Only perform log transform whitout adjust pvalue. Called when adjusted method is not defined



722
723
724
725
726
# File 'lib/NetAnalyzer/network.rb', line 722

def compute_log_transformation(relations) #Only perform log transform whitout adjust pvalue. Called when adjusted method is not defined 
	compute_adjusted_pvalue(relations) do |pval, index| 
		pval
	end
end

#compute_node_com_assoc(com, ref_node) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/NetAnalyzer/network.rb', line 382

def compute_node_com_assoc(com, ref_node)
	ref_cons = 0
	ref_secondary_cons = 0
	secondary_nodes = {}
	other_cons = 0
	other_nodes = {}

	refNneigh = @edges[ref_node]
	com.each do |nodeID|
		nodeIDneigh = @edges[nodeID]
		next if nodeIDneigh.nil?
		ref_cons += 1 if nodeIDneigh.include?(ref_node)
		if !refNneigh.nil?
			common_nodes = nodeIDneigh & refNneigh
			common_nodes.each {|id| secondary_nodes[id] = true}
			ref_secondary_cons += common_nodes.length 
		end
		specific_nodes = nodeIDneigh - refNneigh - [ref_node]
		specific_nodes.each {|id| other_nodes[id] = true}
		other_cons += specific_nodes.length
	end
	by_edge = (ref_cons + ref_secondary_cons).fdiv(other_cons)
	by_node = (ref_cons + secondary_nodes.length).fdiv(other_nodes.length)
	return by_edge, by_node
end

#compute_node_com_assoc_in_precomputed_communities(coms, ref_node) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/NetAnalyzer/network.rb', line 269

def compute_node_com_assoc_in_precomputed_communities(coms, ref_node)
	node_com_assoc = []
	coms.each do |com_id, com|
		node_com_assoc << [compute_node_com_assoc(com, ref_node)]
	end
	return node_com_assoc
end

#delete_nodes(node_list, mode = 'd') ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/NetAnalyzer/network.rb', line 72

def delete_nodes(node_list, mode='d')
	if mode == 'd'
		@nodes.reject!{|n| node_list.include?(n)}
		@edges.reject!{|n, connections| node_list.include?(n)}
		@edges.each do |n, connections|
			connections.reject!{|c| node_list.include?(c)}
		end
	elsif mode == 'r'
		@nodes.select!{|n| node_list.include?(n)}
		@edges.select!{|n, connections| node_list.include?(n)}
		@edges.each do |n, connections|
			connections.select!{|c| node_list.include?(c)}
		end
	end
	@edges.reject!{|n, connections| connections.empty?}
end

#expand_clusters(expand_method) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
# File 'lib/NetAnalyzer/network.rb', line 370

def expand_clusters(expand_method)
	clusters = {}
	@group_nodes.each do |id, nodes|
		if expand_method == 'sht_path'
			dist, paths = compute_avg_sht_path(nodes, paths=true) # this uses bfs, maybe Dijkstra is the best one
			new_nodes = paths.flatten.uniq
			clusters[id] = nodes | new_nodes # If some node pair are not connected, recover them
		end
	end
	return clusters
end

#generate_adjacency_matrix(layerA, layerB) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/NetAnalyzer/network.rb', line 510

def generate_adjacency_matrix(layerA, layerB)
	layerAidNodes = @nodes.select{|id, node| node.type == layerA}.keys
	layerBidNodes = @nodes.select{|id, node| node.type == layerB}.keys
	matrix = Numo::DFloat.zeros(layerAidNodes.length, layerBidNodes.length)
	layerAidNodes.each_with_index do |nodeA, i|
		layerBidNodes.each_with_index do |nodeB, j|
			if @edges[nodeB].include?(nodeA)
				matrix[i, j] = 1
			else
				matrix[i, j] = 0
			end
		end
	end
	all_info_matrix = [matrix, layerAidNodes, layerBidNodes]
	@adjacency_matrices[[layerA, layerB]] = all_info_matrix
	return all_info_matrix
end

#get_all_intersectionsObject



408
409
410
411
412
413
# File 'lib/NetAnalyzer/network.rb', line 408

def get_all_intersections
	intersection_lengths = get_all_pairs do |node1, node2|
		intersection(node1, node2).length
	end
	return intersection_lengths
end

#get_all_pairs(args = {}) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/NetAnalyzer/network.rb', line 415

def get_all_pairs(args = {})
	all_pairs = []
	default = {:layers => :all}
	args = default.merge(args)
	nodeIDsA, nodeIDsB = collect_nodes(args)
	if @compute_autorelations
		if @compute_pairs == :all
			while !nodeIDsA.empty?
				node1 = nodeIDsA.shift
				pairs = Parallel.map(nodeIDsA, in_processes: @threads) do |node2|
					yield(node1, node2)
				end
				all_pairs.concat(pairs)
			end
		elsif @compute_pairs == :conn # TODO: Review this case to avoid return nil values
			while !nodeIDsA.empty?
				node1 = nodeIDsA.shift
				ids_connected_to_n1 = @edges[node1]
				pairs = Parallel.map(nodeIDsA, in_processes: @threads) do |node2|
					result = nil
					ids_connected_to_n2 = @edges[node2]
					if exist_connections?(ids_connected_to_n1, ids_connected_to_n2)
						result = yield(node1, node2)
					end
					result
				end
				pairs.compact!
				all_pairs.concat(pairs)
			end
		end
	else
		#MAIN METHOD
		if @compute_pairs == :conn
			all_pairs = Parallel.map(nodeIDsA, in_processes: @threads) do |node1|
				ids_connected_to_n1 = @edges[node1]
				node1_pairs = []
				nodeIDsB.each do |node2|
					ids_connected_to_n2 = @edges[node2]
					if exist_connections?(ids_connected_to_n1, ids_connected_to_n2)
						node1_pairs << yield(node1, node2)
					end
				end
				node1_pairs
			end
			all_pairs.flatten!(1)
		elsif @compute_pairs == :all
			raise 'Not implemented'
		end
	end

	return all_pairs
end

#get_association_by_transference_resources(firstPairLayers, secondPairLayers, lambda_value1 = 0.5, lambda_value2 = 0.5) ⇒ Object

association methods adjacency matrix based


Alaimo 2014, doi: 10.3389/fbioe.2014.00071



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/NetAnalyzer/network.rb', line 569

def get_association_by_transference_resources(firstPairLayers, secondPairLayers, lambda_value1 = 0.5, lambda_value2 = 0.5)
	relations = []
	matrix1 = @adjacency_matrices[firstPairLayers].first
	rowIds = @adjacency_matrices[firstPairLayers][1]
	matrix2 = @adjacency_matrices[secondPairLayers].first
	colIds =  @adjacency_matrices[secondPairLayers][2]
	m1rowNumber, m1colNumber = matrix1.shape
	m2rowNumber, m2colNumber = matrix2.shape
	#puts m1rowNumber, m1colNumber, m2rowNumber, m2colNumber
	matrix1Weight = graphWeights(m1colNumber, m1rowNumber, matrix1.transpose, lambda_value1)
	matrix2Weight = graphWeights(m2colNumber, m2rowNumber, matrix2.transpose, lambda_value2)
	matrixWeightProduct = Numo::Linalg.dot(matrix1Weight, Numo::Linalg.dot(matrix2, matrix2Weight))
	finalMatrix = Numo::Linalg.dot(matrix1, matrixWeightProduct)
	relations = matrix2relations(finalMatrix, rowIds, colIds)
	@association_values[:transference] = relations
	return relations
end

#get_association_values(layers, base_layer, meth) ⇒ Object

ASSOCIATION METHODS



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/NetAnalyzer/network.rb', line 536

def get_association_values(layers, base_layer, meth)
	relations = [] #node A, node B, val
	if meth == :jaccard #all networks
		relations = get_jaccard_association(layers, base_layer)
	elsif meth == :simpson #all networks
		relations = get_simpson_association(layers, base_layer)
	elsif meth == :geometric #all networks
		relations = get_geometric_associations(layers, base_layer)
	elsif meth == :cosine #all networks
		relations = get_cosine_associations(layers, base_layer)
	elsif meth == :pcc #all networks
		relations = get_pcc_associations(layers, base_layer)
	elsif meth == :hypergeometric #all networks
		relations = get_hypergeometric_associations(layers, base_layer)
	elsif meth == :hypergeometric_bf #all networks
		relations = get_hypergeometric_associations(layers, base_layer, :bonferroni)
	elsif meth == :hypergeometric_bh #all networks
		relations = get_hypergeometric_associations(layers, base_layer, :benjamini_hochberg)
	elsif meth == :hypergeometric_elim #tripartite networks?
		relations = get_hypergeometric_associations_with_topology(layers, base_layer, :elim)
	elsif meth == :hypergeometric_weight #tripartite networks?
		relations = get_hypergeometric_associations_with_topology(layers, base_layer, :weight)			
	elsif meth == :csi #all networks
		relations = get_csi_associations(layers, base_layer)
	elsif meth == :transference #tripartite networks
		relations = get_association_by_transference_resources(layers, base_layer)
	end
	return relations
end

#get_associations(layers, base_layer) ⇒ Object

association methods node pairs based


Bass 2013, doi:10.1038/nmeth.2728



590
591
592
593
594
595
596
597
598
599
# File 'lib/NetAnalyzer/network.rb', line 590

def get_associations(layers, base_layer) # BASE METHOD
	associations = get_all_pairs(layers: layers) do |node1, node2|
		associatedIDs_node1 = @edges[node1].map{|id| @nodes[id]}.select{|node| node.type == base_layer}.map{|node| node.id}
		associatedIDs_node2 = @edges[node2].map{|id| @nodes[id]}.select{|node| node.type == base_layer}.map{|node| node.id}
		intersectedIDs = associatedIDs_node1 & associatedIDs_node2
		associationValue = yield(associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2)
		[node1, node2, associationValue]  
	end
	return associations
end

#get_bipartite_subgraph(from_layer_node_ids, from_layer, to_layer) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/NetAnalyzer/network.rb', line 97

def get_bipartite_subgraph(from_layer_node_ids, from_layer, to_layer)
	bipartite_subgraph = {}
	from_layer_node_ids.each do |from_layer_node_id| 
		connected_nodes = @edges[from_layer_node_id]
		connected_nodes.each do |connected_node| 
			if @nodes[connected_node].type == to_layer
				query = bipartite_subgraph[connected_node]
				if query.nil?
					bipartite_subgraph[connected_node] = get_connected_nodes(connected_node, from_layer)
				end
			end
		end
	end
	return bipartite_subgraph
end

#get_connected_nodes(node_id, from_layer) ⇒ Object



89
90
91
# File 'lib/NetAnalyzer/network.rb', line 89

def get_connected_nodes(node_id, from_layer)
	return @edges[node_id].map{|id| @nodes[id]}.select{|node| node.type == from_layer}.map{|node| node.id}
end

#get_cosine_associations(layers, base_layer) ⇒ Object



630
631
632
633
634
635
636
637
# File 'lib/NetAnalyzer/network.rb', line 630

def get_cosine_associations(layers, base_layer)
	relations = get_associations(layers, base_layer) do |associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2|
		productLength = Math.sqrt(associatedIDs_node1.length * associatedIDs_node2.length)
		cosineValue = intersectedIDs.length/productLength
	end
	@association_values[:cosine] = relations
	return relations
end

#get_csi_associations(layers, base_layer) ⇒ Object



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/NetAnalyzer/network.rb', line 763

def get_csi_associations(layers, base_layer)
	pcc_relations = get_pcc_associations(layers, base_layer)
	clean_autorelations_on_association_values if layers.length > 1
	nx = get_nodes_layer(layers).length
	pcc_vals = {}
	node_rels = {}
	pcc_relations.each do |node1, node2, assoc_index|
		add_nested_record(pcc_vals, node1, node2, assoc_index.abs)
		add_nested_record(pcc_vals, node2, node1, assoc_index.abs)
		add_record(node_rels, node1, node2)
		add_record(node_rels, node2, node1)
	end
	relations = []
	pcc_relations.each do |node1, node2 ,assoc_index|
		pccAB = assoc_index - 0.05
		valid_nodes = 0
		node_rels[node1].each do |node|
			valid_nodes += 1 if pcc_vals[node1][node] >= pccAB
		end
		node_rels[node2].each do |node|
			valid_nodes += 1 if pcc_vals[node2][node] >= pccAB
		end
		csiValue = 1 - (valid_nodes-1).fdiv(nx) 
		# valid_nodes-1 is done due to the connection node1-node2 is counted twice (one for each loop)
		relations << [node1, node2, csiValue]
	end
	@association_values[:csi] = relations
	return relations
end

#get_degree(zscore = false) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/NetAnalyzer/network.rb', line 140

def get_degree(zscore=false)
	degree = {}
	@edges.each do |id, nodes|
		degree[id] = nodes.length
	end
	if !zscore
		degree_values = degree.values
		mean_degree = degree_values.mean
		std_degree = degree_values.standard_deviation
		degree.transform_values!{|v| (v - mean_degree).fdiv(std_degree)}
	end
	return degree
end

#get_edge_numberObject



135
136
137
138
# File 'lib/NetAnalyzer/network.rb', line 135

def get_edge_number
	node_connections = get_degree.values.inject(0){|sum, n| sum + n}
	return node_connections/2
end

#get_geometric_associations(layers, base_layer) ⇒ Object



619
620
621
622
623
624
625
626
627
628
# File 'lib/NetAnalyzer/network.rb', line 619

def get_geometric_associations(layers, base_layer)
	#wang 2016 method
	relations = get_associations(layers, base_layer) do |associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2|	
		intersectedIDs = intersectedIDs.length**2
		productLength = Math.sqrt(associatedIDs_node1.length * associatedIDs_node2.length)
		geometricValue = intersectedIDs.to_f/productLength
	end
	@association_values[:geometric] = relations
	return relations
end

#get_hypergeometric_associations(layers, base_layer, pvalue_adj_method = nil) ⇒ Object



655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/NetAnalyzer/network.rb', line 655

def get_hypergeometric_associations(layers, base_layer, pvalue_adj_method= nil)
	ny = get_nodes_layer([base_layer]).length
	fet = Rubystats::FishersExactTest.new
	relations = get_associations(layers, base_layer) do |associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2|
		fisher = 0
		intersection_lengths = intersectedIDs.length
		if intersection_lengths > 0
			n1_items = associatedIDs_node1.length
			n2_items = associatedIDs_node2.length
			fisher = fet.calculate(
					intersection_lengths,
					n1_items - intersection_lengths,
					n2_items - intersection_lengths, 
					ny - (n1_items + n2_items - intersection_lengths)
			)
			fisher = fisher[:right]
		end
		fisher
	end
	if pvalue_adj_method == :bonferroni
		meth = :hypergeometric_bf
		compute_adjusted_pvalue_bonferroni(relations)
	elsif pvalue_adj_method == :benjamini_hochberg
		meth = :hypergeometric_bh
		compute_adjusted_pvalue_benjaminiHochberg(relations)
	else
		meth = :hypergeometric
		compute_log_transformation(relations)
	end
	@association_values[meth] = relations
	return relations
end

#get_hypergeometric_associations_with_topology(layers, base_layer, mode, thresold = 0.01) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/NetAnalyzer/network.rb', line 688

def get_hypergeometric_associations_with_topology(layers, base_layer, mode, thresold = 0.01)
	relations = []
	reference_layer = (layers - @layer_ontologies.keys).first
	ontology_layer = (layers - [reference_layer]).first
	ref_nodes = get_nodes_from_layer(reference_layer) # get nodes from NOT ontology layer
	ontology = @layer_ontologies[ontology_layer]
	base_layer_length = @nodes.values.count{|n| n.type == base_layer}
	ref_nodes.each do |ref_node|
		base_nodes = get_connected_nodes(ref_node, base_layer)
		ontology_base_subgraph = get_bipartite_subgraph(base_nodes, base_layer, ontology_layer) # get shared nodes between nodes from NOT ontology layer and ONTOLOGY layer. Also get the conections between shared nodes and ontology nodes.
		next if ontology_base_subgraph.empty?
		ontology_base_subgraph.transform_keys!{|k| k.to_sym}
		ontology.load_item_relations_to_terms(ontology_base_subgraph, remove_old_relations = true)
		term_pvals = ontology.compute_relations_to_items(base_nodes, base_layer_length, mode, thresold)
		relations.concat(term_pvals.map{|term| [ref_node, term[0], term[1]]})
	end
	compute_log_transformation(relations)
	if mode == :elim
		meth = :hypergeometric_elim
	elsif mode == :weight
		meth = :hypergeometric_weight
	end
	@association_values[meth] = relations
	return relations
end

#get_jaccard_association(layers, base_layer) ⇒ Object



601
602
603
604
605
606
607
608
# File 'lib/NetAnalyzer/network.rb', line 601

def get_jaccard_association(layers, base_layer)
	relations = get_associations(layers, base_layer) do |associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2|
		unionIDS = associatedIDs_node1 | associatedIDs_node2
		jaccValue = intersectedIDs.length.to_f/unionIDS.length		
	end
	@association_values[:jaccard] = relations
	return relations
end

#get_kernel(layer2kernel, kernel, normalization = false) ⇒ Object

KERNEL METHODS



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
# File 'lib/NetAnalyzer/network.rb', line 871

def get_kernel(layer2kernel, kernel, normalization=false)
	matrix, node_names = @adjacency_matrices[layer2kernel]
	#I = identity matrix
	#D = Diagonal matrix
	#A = adjacency matrix
	#L = laplacian matrix = D − A
	matrix_result = nil
	dimension_elements = matrix.shape.last
	# In scuba code, the diagonal values of A is set to 0. In weighted matrix the kernel result is the same with or without this operation. Maybe increases the computing performance?
	# In the md kernel this operation affects the values of the final kernel
	#dimension_elements.times do |n|
	#	matrix[n,n] = 0.0
	#end
	if kernel == 'el' || kernel == 'ct' || kernel == 'rf' || 
		kernel.include?('vn') || kernel.include?('rl') || kernel == 'me'
		diagonal_matrix = matrix.sum(1).diag 	# get the total sum for each row, for this reason the sum method takes the 1 value. If sum colums is desired, use 0
												# Make a matrix whose diagonal is row_sum
		matrix_L = diagonal_matrix - matrix
		if kernel == 'el' #Exponential Laplacian diffusion kernel(active). F Fouss 2012 | doi: 10.1016/j.neunet.2012.03.001
		    beta = 0.02
		    beta_product = matrix_L * -beta
		    #matrix_result = beta_product.expm
		    matrix_result = Numo::Linalg.expm(beta_product, 14)
		elsif kernel == 'ct' # Commute time kernel (active). J.-K. Heriche 2014 | doi: 10.1091/mbc.E13-04-0221
		    matrix_result = Numo::Linalg.pinv(matrix_L) # Anibal saids that this kernel was normalized. Why?. Paper do not seem to describe this operation for ct, it describes for Kvn or for all kernels, it is not clear.
		elsif kernel == 'rf' # Random forest kernel. J.-K. Heriche 2014 | doi: 10.1091/mbc.E13-04-0221
		    matrix_result = Numo::Linalg.inv(Numo::DFloat.eye(dimension_elements) + matrix_L) #Krf = (I +L ) ^ −1
		elsif kernel.include?('vn') # von Neumann diffusion kernel. J.-K. Heriche 2014 | doi: 10.1091/mbc.E13-04-0221
		    alpha = kernel.gsub('vn', '').to_f * matrix.max_eigenvalue ** -1  # alpha = impact_of_penalization (1, 0.5 or 0.1) * spectral radius of A. spectral radius of A = absolute value of max eigenvalue of A 
		    matrix_result = Numo::Linalg.inv(Numo::DFloat.eye(dimension_elements) - matrix * alpha ) #  (I -alphaA ) ^ −1
		elsif kernel.include?('rl') # Regularized Laplacian kernel matrix (active)
		    alpha = kernel.gsub('rl', '').to_f * matrix.max_eigenvalue ** -1  # alpha = impact_of_penalization (1, 0.5 or 0.1) * spectral radius of A. spectral radius of A = absolute value of max eigenvalue of A
		    matrix_result = Numo::Linalg.inv(Numo::DFloat.eye(dimension_elements) + matrix_L * alpha ) #  (I + alphaL ) ^ −1
		elsif kernel == 'me' # Markov exponential diffusion kernel (active). G Zampieri 2018 | doi.org/10.1186/s12859-018-2025-5 . Taken from compute_kernel script
			beta=0.04
			#(beta/N)*(N*I - D + A)
			id_mat = Numo::DFloat.eye(dimension_elements)
			m_matrix = (id_mat * dimension_elements - diagonal_matrix + matrix ) * (beta/dimension_elements)
			#matrix_result = m_matrix.expm
		    matrix_result = Numo::Linalg.expm(m_matrix, 16)
		end
	elsif kernel == 'ka' # Kernelized adjacency matrix (active). J.-K. Heriche 2014 | doi: 10.1091/mbc.E13-04-0221
		lambda_value = matrix.min_eigenvalue
		matrix_result = matrix + Numo::DFloat.eye(dimension_elements) * lambda_value.abs # Ka = A + lambda*I # lambda = the absolute value of the smallest eigenvalue of A
	elsif kernel.include?('md') # Markov diffusion kernel matrix. G Zampieri 2018 | doi.org/10.1186/s12859-018-2025-5 . Taken from compute_kernel script
		t = kernel.gsub('md', '').to_i
		#TODO: check implementation with Numo::array
		col_sum = matrix.sum(1)
		p_mat = matrix.div_by_vector(col_sum)
		p_temp_mat = p_mat.clone
		zt_mat = p_mat.clone
		(t-1).times do
			p_temp_mat = p_temp_mat.dot(p_mat)
			zt_mat = zt_mat + p_temp_mat
		end
		zt_mat = zt_mat * (1.0/t)
		matrix_result = zt_mat.dot(zt_mat.transpose)
	else
		matrix_result = matrix
		warn('Warning: The kernel method was not specified or not exists. The adjacency matrix will be given as result')
		# This allows process a previous kernel and perform the normalization in a separated step.
	end
	matrix_result = matrix_result.cosine_normalization if normalization #TODO: check implementation with Numo::array
	@kernels[layer2kernel] = matrix_result
end

#get_node_attributes(attr_names) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/NetAnalyzer/network.rb', line 154

def get_node_attributes(attr_names)
	attrs = []
	attr_names.each do |attr_name|
		if attr_name == 'get_degree'
			attrs << get_degree
		elsif attr_name == 'get_degreeZ'
			attrs << get_degree(zscore=true)
		end
	end
	node_ids = attrs.first.keys
	node_attrs = []
	node_ids.each do |n|
		node_attrs << [n].concat(attrs.map{|at| at[n]})
	end
	return node_attrs
end

#get_nodes_from_layer(from_layer) ⇒ Object



93
94
95
# File 'lib/NetAnalyzer/network.rb', line 93

def get_nodes_from_layer(from_layer)
	return @nodes.values.select{|node| node.type == from_layer}.map{|node| node.id}
end

#get_nodes_layer(layers) ⇒ Object



490
491
492
493
494
495
496
497
# File 'lib/NetAnalyzer/network.rb', line 490

def get_nodes_layer(layers)
	#for creating ny value in hypergeometric and pcc index
	nodes = []
	layers.each do |layer|
		nodes.concat(@nodes.select{|nodeId, node| node.type == layer}.values)
	end
	return nodes
end

#get_pcc_associations(layers, base_layer) ⇒ Object



639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/NetAnalyzer/network.rb', line 639

def get_pcc_associations(layers, base_layer)
	#for Ny calcule use get_nodes_layer
	base_layer_nodes = get_nodes_layer([base_layer])
	ny = base_layer_nodes.length
	relations = get_associations(layers, base_layer) do |associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2|
		intersProd = intersectedIDs.length * ny
		nodesProd = associatedIDs_node1.length * associatedIDs_node2.length
		nodesSubs = intersProd - nodesProd
		nodesAInNetwork = ny - associatedIDs_node1.length
		nodesBInNetwork = ny - associatedIDs_node2.length
		pccValue = nodesSubs.to_f / Math.sqrt(nodesProd * nodesAInNetwork * nodesBInNetwork)
	end
	@association_values[:pcc] = relations
	return relations
end

#get_pred_rec(meth, cut_number = 100, top_number = 10000) ⇒ Object

Pandey 2007, Association Analysis-based Transformations for Protein Interaction Networks: A Function Prediction Case Study



836
837
838
839
840
841
842
843
844
845
# File 'lib/NetAnalyzer/network.rb', line 836

def get_pred_rec(meth, cut_number = 100, top_number = 10000)
	performance = [] #cut, pred, rec
	preds, limits = load_prediction(@association_values[meth])
	cuts = get_cuts(limits, cut_number)
	cuts.each do |cut|
		prec, rec = pred_rec(preds, cut, top_number)
		performance << [cut, prec, rec]
	end
	return performance
end

#get_simpson_association(layers, base_layer) ⇒ Object



610
611
612
613
614
615
616
617
# File 'lib/NetAnalyzer/network.rb', line 610

def get_simpson_association(layers, base_layer)
	relations = get_associations(layers, base_layer) do |associatedIDs_node1, associatedIDs_node2, intersectedIDs, node1, node2|
		minLength = [associatedIDs_node1.length, associatedIDs_node2.length].min
		simpsonValue = intersectedIDs.length.to_f/minLength
	end
	@association_values[:simpson] = relations
	return relations
end

#intersection(node1, node2) ⇒ Object



499
500
501
502
503
504
505
506
507
508
# File 'lib/NetAnalyzer/network.rb', line 499

def intersection(node1, node2)
	shared_nodes = []
	associatedIDs_node1 = @edges[node1]
	associatedIDs_node2 = @edges[node2]
	intersectedIDs = associatedIDs_node1 & associatedIDs_node2
	intersectedIDs.each do |id|
		shared_nodes << @nodes[id]
	end
	return shared_nodes
end


941
942
943
944
945
946
947
948
949
950
# File 'lib/NetAnalyzer/network.rb', line 941

def link_ontology(ontology_file_path, layer_name)
	if !@loaded_obos.include?(ontology_file_path) #Load new ontology
		ontology = Ontology.new(file: ontology_file_path, load_file: true)
		@loaded_obos << ontology_file_path
		@ontologies << ontology
	else #Link loaded ontology to current layer
		ontology = @ontologies[@loaded_obos.index(ontology_file_path)]
	end
	@layer_ontologies[layer_name] = ontology
end

#load_control(ref_array) ⇒ Object

PERFORMANCE METHODS



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# File 'lib/NetAnalyzer/network.rb', line 796

def load_control(ref_array)
	control = {}
	ref_array.each do |node1, node2|
		if node2 != '-'
			query = control[node1]
			if query.nil?
				control[node1] = [node2]
			else
				query << node2
			end
		end
	end
	@control_connections = control
	return control
end

#load_network_by_bin_matrix(input_file, node_file, layers) ⇒ Object



125
126
127
128
# File 'lib/NetAnalyzer/network.rb', line 125

def load_network_by_bin_matrix(input_file, node_file, layers)
	node_names = load_input_list(node_file)
	@adjacency_matrices[layers.map{|l| l.first}] = [Numo::NArray.load(input_file, type='npy'), node_names, node_names]
end

#load_network_by_pairs(file, layers, split_character = "\t") ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/NetAnalyzer/network.rb', line 113

def load_network_by_pairs(file, layers, split_character="\t")
	File.open(file).each do |line|
		line.chomp!
		pair = line.split(split_character)
		node1 = pair[0]
		node2 = pair[1]
		add_node(node1, set_layer(layers, node1))
		add_node(node2, set_layer(layers, node2))
		add_edge(node1, node2)	
	end
end

#load_network_by_plain_matrix(input_file, node_file, layers, splitChar) ⇒ Object



130
131
132
133
# File 'lib/NetAnalyzer/network.rb', line 130

def load_network_by_plain_matrix(input_file, node_file, layers, splitChar)
	node_names = load_input_list(node_file)
	@adjacency_matrices[layers.map{|l| l.first}] = [Numo::NArray.load(input_file, type='txt', splitChar=splitChar), node_names, node_names]
end

#load_prediction(pairs_array) ⇒ Object



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
# File 'lib/NetAnalyzer/network.rb', line 812

def load_prediction(pairs_array)
	pred = {}
	min = nil
	max = nil
	pairs_array.each do |key, label, score|
		query = pred[key]
		if !min.nil? && !max.nil?
			min = score if score < min
			max = score if score > max
		else
			min = score; max = score
		end
		if query.nil?
			pred[key] = [[label], [score]]
		else
			query.first << label
			query.last << score
		end
	end
	return pred, [min, max]
end

#plot_dot(user_options = {}) ⇒ Object

input keys: layout



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
# File 'lib/NetAnalyzer/network.rb', line 187

def plot_dot(user_options = {}) # input keys: layout
	options = {layout: "sfdp"}
	options = options.merge(user_options)
	graphviz_colors = %w[lightsteelblue1 lightyellow1 lightgray orchid2]
	palette = {}
	@layers.each do |layer|
		palette[layer] = graphviz_colors.shift
	end
	graph = GV::Graph.open('g', type = :undirected)
	plotted_edges = {}
	@edges.each do |nodeID, associatedIDs|
		associatedIDs.each do |associatedID|
			pair = [nodeID, associatedID].sort.join('_').to_sym
			if !plotted_edges[pair]
				graph.edge 'e', 
					graph.node(nodeID, label: '', style: 'filled', fillcolor: palette[@nodes[nodeID].type]), 
					graph.node(associatedID, label: '', style: 'filled' , fillcolor: palette[@nodes[associatedID].type])
				plotted_edges[pair] = true
			end
		end
	end
	@reference_nodes.each do |nodeID|
		graph.node(nodeID, style: 'filled', fillcolor: 'firebrick1', label: '')
	end
	graphviz_border_colors = %w[blue darkorange red olivedrab4]
	@group_nodes.each do |groupID, gNodes|
		border_color = graphviz_border_colors.shift
		gNodes.each do |nodeID|
				graph.node(nodeID, color: border_color, penwidth: '10', label: '')
		end
	end
	graph[:overlap] = false
	STDERR.puts 'Save graph'
	graph.save(options[:output_file] + '.png', format='png', layout=options[:layout])
end

#plot_network(options = {}) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/NetAnalyzer/network.rb', line 171

def plot_network(options = {})
	if options[:method] == 'graphviz'
		plot_dot(options)
	else
		if options[:method] == 'elgrapho'
			template = 'el_grapho'
		elsif options[:method] == 'cytoscape'
			template = 'cytoscape'
		elsif options[:method] == 'sigma'
			template = 'sigma'
		end
		renderered_template = ERB.new(File.open(File.join(TEMPLATES, template + '.erb')).read).result(binding)
		File.open(options[:output_file] + '.html', 'w'){|f| f.puts renderered_template}
	end	
end

#pred_rec(preds, cut, top) ⇒ Object



847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/NetAnalyzer/network.rb', line 847

def pred_rec(preds, cut, top)
	predicted_labels = 0 #m
	true_labels = 0 #n
	common_labels = 0 # k
	@control_connections.each do |key, c_labels|
		true_labels += c_labels.length #n
		pred_info = preds[key]
		if !pred_info.nil?
			labels, scores = pred_info
			reliable_labels = get_reliable_labels(labels, scores, cut, top)
			predicted_labels += reliable_labels.length #m
			common_labels += (c_labels & reliable_labels).length #k
		end
	end
	#puts "cut: #{cut} trueL: #{true_labels} predL: #{predicted_labels} commL: #{common_labels}"
	prec = common_labels.to_f/predicted_labels
	rec = common_labels.to_f/true_labels
	prec = 0.0 if prec.nan?
	rec = 0.0 if rec.nan?
	return prec, rec
end

#query_edge(nodeA, nodeB) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/NetAnalyzer/network.rb', line 63

def query_edge(nodeA, nodeB)
	query = @edges[nodeA]
	if query.nil?
		@edges[nodeA] = [nodeB]
	else
		query << nodeB
	end
end

#replace_nil_vals(val) ⇒ Object



248
249
250
# File 'lib/NetAnalyzer/network.rb', line 248

def replace_nil_vals(val)
	return val.nil? ? 'NULL' : val
end

#set_compute_pairs(use_pairs, get_autorelations) ⇒ Object



49
50
51
52
# File 'lib/NetAnalyzer/network.rb', line 49

def set_compute_pairs(use_pairs, get_autorelations)
	@compute_pairs = use_pairs
	@compute_autorelations = get_autorelations
end

#shortest_path(node_start, node_stop, paths = false) ⇒ Object



362
363
364
365
366
367
368
# File 'lib/NetAnalyzer/network.rb', line 362

def shortest_path(node_start, node_stop, paths=false)
	#https://betterprogramming.pub/5-ways-to-find-the-shortest-path-in-a-graph-88cfefd0030f
	#return bidirectionalSearch(node_start, node_stop)
	#https://efficientcodeblog.wordpress.com/2017/12/13/bidirectional-search-two-end-bfs/
	dist, all_paths = bfs_shortest_path(node_start, node_stop, paths)
	return  dist, all_paths
end

#write_kernel(layer2kernel, output_file) ⇒ Object



937
938
939
# File 'lib/NetAnalyzer/network.rb', line 937

def write_kernel(layer2kernel, output_file)
	@kernels[layer2kernel].save(output_file)
end