R 套件相依性

程式碼

1    # coding=utf8 
2     
3    import os 
4    import codecs 
5    import networkx as nx 
6    from ete3 import Tree, TreeStyle 
7     
8     
9    class R_PKG: 
10       infoPackages = {} 
11    
12       def __init__(self, **kwargs): 
13           return super().__init__(**kwargs) 
14    
15       def loadCSV(self, csvFile): 
16           with codecs.open(csvFile, mode='r', encoding='utf8') as csvF: 
17               for line in csvF: 
18                   fields = line.strip().split(',') 
19    
20                   if len(fields) != 4: 
21                       continue 
22    
23                   namePackage = fields[0] 
24                   descPackage = fields[1] 
25                   urlPackage = fields[2] 
26                    
27                   dependentPackages = '' 
28                   if fields[3] is not None: 
29                      dependentPackages = fields[3].strip() 
30    
31                   package = { 
32                       'name': namePackage, 
33                       'url': urlPackage, 
34   #                   'desc': descPackage, 
35                       'child': dependentPackages 
36                   } 
37    
38                   self.infoPackages[namePackage] = package 
39    
40       def loadTree(self, nwFile): 
41           G = Tree(nwFile, format=1) 
42           return G 
43    
44       def makeTree(self, parent, nameNode=None): 
45           if nameNode is not None: 
46               if nameNode in self.infoPackages: 
47                   info = self.infoPackages[nameNode] 
48                   if info['child'] != '': 
49                       dependentPackages = info['child'].split() 
50                       for package in dependentPackages: 
51                           childNode = parent.add_child(name=package) 
52                           self.makeTree(childNode, nameNode=package) 
53           else: 
54               for namePackage in self.infoPackages: 
55                   info = self.infoPackages[namePackage] 
56    
57                   if info['child'] != '': 
58                       node = parent.add_child(name=namePackage) 
59    
60                       dependentPackages = info['child'].split() 
61                       for package in dependentPackages: 
62                           childNode = node.add_child(name=package) 
63                           self.makeTree(childNode, nameNode=package) 
64    
65       def saveTree(self, G, nwFile='R-PKG.nw'): 
66           G.write(format=1, outfile=nwFile) 
67    
68       def plotTree(self, G, txtFile='R-PKG.txt'): 
69           with codecs.open(txtFile, mode='w', encoding='utf8') as txtF: 
70               txtF.write(G.get_ascii(show_internal=True)) 
71    
72       def queryPackageInfo(self, parent, nameNode): 
73           result = {} 
74    
75           for node in parent.traverse("postorder"): 
76               if nameNode.lower() in node.name.lower(): 
77                   if node.up is not None: 
78                       result[node.up.name] = node.name 
79    
80           for package in result: 
81               print('%s' % (package)) 
82               print('\t%s' % (result[package])) 
83    
84    
85   if __name__ == '__main__': 
86       taskControl = [True, False] 
87    
88       worker = R_PKG() 
89    
90       csvFile = 'R-PKG.csv' 
91       nwFile  = 'R-PKG.nw' 
92    
93       G = None 
94    
95       if os.path.exists(nwFile): 
96           G = worker.loadTree(nwFile) 
97       elif os.path.exists(csvFile): 
98           worker.loadCSV(csvFile) 
99    
100          G = Tree() 
101          worker.makeTree(G) 
102          worker.saveTree(G) 
103   
104      if G is None: 
105          exit(0) 
106   
107      if taskControl[0]: 
108          worker.queryPackageInfo(G, 'stats') 
109   
110      if taskControl[1]: 
111          worker.plotTree(G) 


輸出結果

ċ
R-PKG.csv
(1083k)
李智,
2016年7月7日 下午6:35
ċ
R-PKG.txt
(688k)
李智,
2016年7月7日 下午6:36