-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathputStatisticFilesTogether.py
62 lines (54 loc) · 1.83 KB
/
putStatisticFilesTogether.py
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
import sys
def putFilesTogether(dictionaries):
statistic = {}
for dic in dictionaries:
for spec in dic:
if spec in statistic:
for adj in dic[spec]:
if adj in statistic[spec]:
temp=statistic[spec][adj]
temp+=dic[spec][adj]
statistic[spec][adj]=temp
else:
#adj ins't in statistic[spec] by now
statistic[spec].update({adj: dic[spec][adj]})
else:
#species isn't in statistic by now
for adj in dic[spec]:
if spec in statistic:
statistic[spec].update({adj:dic[spec][adj]})
else:
statistic[spec]={}
statistic[spec].update({adj: dic[spec][adj]})
return statistic
def readFile(filePath):
dic={}
f=open(filePath,"r")
line=f.readline()
while line:
split=line.split("\t")
species=split[0]
if species != ">Header:":
gene=split[1]
number=int(split[2].strip())
if species in dic:
dic[species].update({gene:number})
else:
dic[species]={gene:number}
line=f.readline()
return dic
def writeOutput(statistic,output):
f = open(output, "w")
for species in statistic:
for gene in statistic[species]:
number=statistic[species][gene]
f.write(str(species)+"\t"+gene+"\t"+str(number)+"\n")
f.close()
if len(sys.argv) == 3:
file_list=sys.argv[1].split(',')
stat_list=[]
for file in file_list:
stat_list.append(readFile(file))
writeOutput(putFilesTogether(stat_list),sys.argv[2])
else:
print('Error: Wrong Parameter number!')