$LOAD_PATH << 'gems/1.8/gems/bio-1.2.1/lib'
$LOAD_PATH << 'site_ruby/1.8'
$LOAD_PATH << 'site_ruby'
$LOAD_PATH << '1.8'
$LOAD_PATH << '1.8/java'
$LOAD_PATH << 'jruby'

require 'set'
require 'java'
require 'bio'

include_class 'java.util.ArrayList'
include_class 'cytoscape.Cytoscape'
include_class 'cytoscape.data.CyAttributes'
include_class 'cytoscape.CyNode'
include_class 'cytoscape.CyEdge'
include_class 'cytoscape.CyNetwork'

# original node id type.  You can change this to uniprot, genbank, etc.
ID_TYPE = "ncbi-geneid"

# Nodes in current network
nodes =  Cytoscape.getCurrentNetwork.nodesList

# Node Attributes
nodeAttr = Cytoscape.getNodeAttributes

serv = Bio::KEGG::API.new

counter = 0
buffer = ""

# Conversion table as tab delimited text
keggid = ""

nodes.each do |node|
	if counter < 100 && counter != 99
		buffer = buffer + ID_TYPE + ":" + node.getIdentifier + " "
		counter = counter + 1
	elsif counter == 99
		puts buffer
		keggid = keggid + serv.bconv(buffer.strip)
		counter = 0
		buffer = ""
	end
end

keggid = keggid + serv.bconv(buffer.strip)

targetIDs = Set.new(enum = nil)
nameHash = Hash.new

keggid.each_line {|line| 
	id_array = line.split("\t")
	uniprotID = id_array[0].split(":")[1]
	targetIDs << id_array[1]
	nodeAttr.setAttribute(uniprotID, "KEGG ID", id_array[1])
	nameHash[id_array[1]] = uniprotID
}

pathwaySet = Set.new(enum = nil)

targetIDs.each do |kid|
	puts kid + " ====> " + nameHash[kid]
	if nameHash[kid] != nil
		genes = [kid]
		pathways = serv.get_pathways_by_genes(genes)
	
		pList = ArrayList.new()
		pathways.each do |pathway|
			if pathway.strip != ""
				pathwaySet << pathway.strip
				pList.add(pathway.strip)
			end
		end
	
		nodeAttr.setListAttribute(nameHash[kid], "KEGG Pathway", pList)
	end
end
