-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseconfig.py
More file actions
127 lines (112 loc) · 3.95 KB
/
parseconfig.py
File metadata and controls
127 lines (112 loc) · 3.95 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
import sys, getopt, warnings, os, re
def getConfigMasterKeyValue (sMasterKey, dictConfigKeyValues) :
dictKeyValueSet = {}
for currentKey, currentValue in dictConfigKeyValues.items() :
if( currentKey.startswith(sMasterKey+"{") and currentKey.endswith("}") and ((len(sMasterKey) + 2) < len(currentKey))) :
coreKey = currentKey[len(sMasterKey)+1: len(currentKey)-1]
dictKeyValueSet [coreKey] = currentValue
return dictKeyValueSet
def parseConfigLine (sLine, sSectionName) :
# sSectionName is a list, but only the first value is used
currentKey = ""
currentValue = ""
if ((sLine[0] == "[") and (sLine[len(sLine)-1] == "]" )) :
sSectionName[0] = sLine
elif (sSectionName[0] == "") :
print("no section name")
else :
twoParts = sLine.split("=")
if (len(twoParts) != 2) :
print("wrong line: "+sLine)
else :
currentKey = sSectionName[0] + twoParts[0].rstrip()
currentValue = twoParts[1].lstrip()
#print(currentKey+"=>"+currentValue)
return [currentKey, currentValue]
def parseConfigKeyValues (filepath) :
configFile = open(filepath)
sSectionName = [""]
dictConfigKeyValues = {}
for sLine in configFile.readlines() :
poundPos = sLine.find("#")
if (poundPos > -1) :
sLine = sLine[0:poundPos]
sLine = sLine.strip()
if (sLine == "") :
continue
else :
#print("!!!"+sLine+"!!!")
currentKey, currentValue = parseConfigLine (sLine, sSectionName)
if (currentKey != "") and (currentValue != "" ):
# print(currentKey+"=>"+currentValue)
if (dictConfigKeyValues.get(currentKey) == None ):
dictConfigKeyValues[currentKey] = currentValue
else :
print(currentKey + " has existed")
configFile.close()
# for currentKey, currentValue in dictConfigKeyValues.items() :
# print(currentKey, currentValue)
return dictConfigKeyValues
def getModificationDictionary(dictConfigKeyValues):
p = re.compile('{.+}')
pa = re.compile('[\d]+')
pat = re.compile('[a-zA-Z]+')
patt = re.compile('[-\.\d]+')
patte = re.compile('[A-Z]')
# get the element mass
element_mass_list_dict = {}
element_pert_list_dict = {}
element_mass_dict = {}
element_dict = {}
compound_list_dict = {}
element_modification_list_dict = {}
for e_key, e_value in dictConfigKeyValues.iteritems():
if e_key.startswith('[Peptide_Identification]Element_Masses'):
m = p.search(e_key)
e_str = m.group(0)[1:-1]
element_mass_list_dict[e_str] = e_value
if e_key.startswith('[Peptide_Identification]Element_Percent'):
m = p.search(e_key)
e_str = m.group(0)[1:-1]
element_pert_list_dict[e_str] = e_value
if e_key.startswith('[Peptide_Identification]Element_List'):
element_list = pat.findall(e_value)
idx = 0
for e in element_list:
element_dict[idx] = e
idx += 1
if e_key.startswith('[Peptide_Identification]Residue'):
m = p.search(e_key)
e_str = m.group(0)[1:-1]
compound_list_dict[e_str] = map(int, patt.findall(e_value))
if e_key.startswith('[Peptide_Identification]PTM'):
m = p.search(e_key)
e_str = m.group(0)[1:-1]
if len(e_str) > 1:
e_str = e_str[0]
element_list = patte.findall(e_value)
for e_element in element_list:
if e_element in element_modification_list_dict:
element_modification_list_dict[e_element].append(e_str)
else:
element_modification_list_dict[e_element] = [e_str]
for e_key, e_value in element_mass_list_dict.iteritems():
mass_list = map(float, patt.findall(e_value))
pert_list = map(float, patt.findall(element_pert_list_dict[e_key]))
max_value = max(pert_list)
max_index = pert_list.index(max_value)
element_mass_dict[e_key] = mass_list[max_index]
# get the mass for all compound
compound_mass_dict = {}
for e_key, e_value in compound_list_dict.iteritems():
'''
if e_key.isalpha():
continue
'''
mass_float = 0
for e_idx, e_num in enumerate(e_value):
e = element_dict[e_idx]
mass_float += element_mass_dict[e] * float(e_num)
compound_mass_dict[e_key] = mass_float
return (compound_mass_dict, element_modification_list_dict)