-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenumberCaraSystems.py
More file actions
executable file
·64 lines (52 loc) · 2.24 KB
/
RenumberCaraSystems.py
File metadata and controls
executable file
·64 lines (52 loc) · 2.24 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
#!/nmr/programs/python/bin/python2.5
"""
RenumberCaraSystems.py takes a cara repository file and renumbers the spin systems so they
run in order with no gaps. By default, the spin system numbers start with 1.
Future plans may include an option to start at another number.
Note that RenumberCaraSystems.py only alters the first project in a repository, and does not alter tags in peaklists.
"""
import sys
sys.path.append('/nmr/programs/python/')
import xml.etree.ElementTree as ET
def main():
if len(sys.argv) != 3:
print '=============================================================================='
print 'RenumberCaraSystems.py renumbers the spin systems in a cara repository so that they begin at 1 and there are no gaps.'
print 'Note that RenumberCaraSystems.py only alters the first project in a repository, and does not alter tags in peaklists.'
print ''
print 'Usage: RenumberCaraSystems.py infile.cara outfile.cara'
print 'Number of arguments given: %d'%(len(sys.argv)-1)
print '=============================================================================='
return
infile = sys.argv[1]
outfile = sys.argv[2]
syscounter = 0
sysdict = {}
tree = ET.parse(infile)
root = tree.getroot()
firstproject = root.find('project')
spinbase = firstproject.find('spinbase')
spinsystems = spinbase.findall('spinsys')
for spinsystem in spinsystems:
syscounter = syscounter + 1
oldid = spinsystem.get('id')
sysdict[oldid] = '%d'%syscounter
spinsystem.set('id',sysdict[oldid])
print '%d systems found.'%syscounter
links = spinbase.findall('link')
for link in links:
oldpred = link.get('pred')
oldsucc = link.get('succ')
link.set('pred',sysdict[oldpred])
link.set('succ',sysdict[oldsucc])
spins = spinbase.findall('spin')
for spin in spins:
oldsys = spin.get('sys')
if oldsys:
spin.set('sys',sysdict[oldsys])
else:
print 'Warning: spin %s has no parent system. The parent system was probably deleted.'%spin.get('id')
print spin.items()
# note that the offending spin could be removed.
tree.write(outfile)
main()