Module object_to_circos
[hide private]
[frames] | no frames]

Source Code for Module object_to_circos

  1  #! /usr/local/bin/python 
  2  #-*- coding: utf-8 -*- 
  3   
  4  """object_to_circos 
  5   
  6  Loads a serialized graph object in memory and generates an input file 
  7  for Circos (matrix). 
  8  """ 
  9   
 10  __author__ = "Jerome Hussenet, Cedric Bonhomme" 
 11  __version__ = "$Revision: 0.4 $" 
 12  __date__ = "$Date: 2009/03/14 $" 
 13  __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme" 
 14  __license__ = "Python" 
 15   
 16  import os 
 17  import sys 
 18   
 19  import pickle 
 20   
 21   
22 -def object_to_circos(obj_file, circos_file):
23 """Create the input file for Circos. 24 """ 25 if options.verbose: 26 print "Loading objet..." 27 dic_obj = open(obj_file, "r") 28 dic_ip = pickle.load(dic_obj) 29 30 if options.verbose: 31 print "Searching IP that are source and destination..." 32 ip_state = {} 33 for ip_src in dic_ip: 34 if ip_src not in ip_state: 35 ip_state[ip_src] = 1 36 elif ip_state[ip_src] == 2: 37 ip_state[ip_src] += 1 38 39 for ip_dst in dic_ip[ip_src]: 40 if ip_dst not in ip_state: 41 ip_state[ip_dst] = 2 42 elif ip_state[ip_dst] == 1: 43 ip_state[ip_dst] += 2 44 45 liste_ip = [] 46 for ip in ip_state: 47 if ip_state[ip] == 3: 48 liste_ip.append(ip) 49 50 if options.verbose: 51 print "Circos matrix generation..." 52 # build the table s/d 53 # first dimension : source ip 54 # second dimension : destination ip 55 # data : relations number 56 tab = {} 57 for i in liste_ip: 58 tab[i] = {} 59 for j in dic_ip[i]: 60 if ip_state[j] == 3: 61 tab[i][j] = dic_ip[i][j] 62 tab[i][i] = "-" 63 64 # Freeing memory 65 del dic_ip 66 del liste_ip 67 del ip_state 68 69 if options.verbose: 70 print "Saving the matrix..." 71 tab_file = open(circos_file, "wb") 72 s= [] 73 s.append("ip") 74 for i in tab: 75 s.append(i) 76 tab_file.write("\t".join(s) + "\n") 77 78 for i in tab: 79 s = [] 80 s.append(i) 81 for j in tab: 82 try: 83 s.append(str(tab[i][j])) 84 except: 85 s.append("0") 86 tab_file.write("\t".join(s) + "\n") 87 88 tab_file.close()
89 90 91 if __name__ == "__main__": 92 # Point of entry in execution mode. 93 from optparse import OptionParser 94 parser = OptionParser() 95 parser.add_option("-i", "--input", dest="obj_file", 96 help="Python serialized object") 97 parser.add_option("-o", "--output", dest="circos_file", 98 help="Circos file") 99 parser.add_option("-q", "--quiet", 100 action="store_false", dest="verbose", 101 help="be vewwy quiet (I'm hunting wabbits)") 102 parser.set_defaults(obj_file = './data/dic.pyobj', 103 circos_file = './data/ip.circos', 104 verbose = True) 105 106 (options, args) = parser.parse_args() 107 108 object_to_circos(options.obj_file, options.circos_file) 109