-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathstrip_blank_lines.py
executable file
·54 lines (42 loc) · 1.68 KB
/
strip_blank_lines.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
#!/usr/bin/env python
#==============================================================================
# author : Pavel Polishchuk
# date : 10-06-2020
# copyright : Pavel Polishchuk 2020
# license : GPL3
#==============================================================================
import re
import argparse
patt = re.compile('> {1,2}<(.*)>( +\([0-9]+\))?')
def strip_fields(molstr):
out = []
i = 0
while i < len(molstr):
out.append(molstr[i])
if patt.fullmatch(molstr[i]):
tmp = []
while not patt.fullmatch(molstr[i + 1]) and molstr[i + 1] != '$$$$':
tmp.append(molstr[i + 1])
i += 1
out.extend([line for i, line in enumerate(tmp) if line != '' or i == len(tmp) - 1])
i += 1
return out
def main_params(in_fname, out_fname):
with open(in_fname) as ifs, open(out_fname, 'wt') as ofs:
molstr = []
for line in ifs:
molstr.append(line.rstrip())
if line.strip() == '$$$$':
molstr = strip_fields(molstr)
ofs.write('\n'.join(molstr) + '\n')
molstr = []
def main():
parser = argparse.ArgumentParser(description='Remove empty lines in multi-line field values.')
parser.add_argument('-i', '--input', metavar='FILENAME', required=True,
help='input SDF file, molecules should have titles')
parser.add_argument('-o', '--output', metavar='FILENAME', required=True,
help='output text file with values of specified fields')
args = parser.parse_args()
main_params(args.input, args.output)
if __name__ == '__main__':
main()