unpack_proteinnet.py 1.15 KB
Newer Older
Gustaf's avatar
Gustaf committed
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
import argparse
import os
from pathlib import Path


def _write_file(args, file_in_progress):
    file_id = file_in_progress[1]
    fname = file_id.upper() + ".core"
    fpath = os.path.join(args.output_dir, fname)
    with open(fpath, "w") as fp:
        fp.write('\n'.join(file_in_progress))


def main(args):
    Path(args.output_dir).mkdir(parents=True, exist_ok=True)    

    with open(args.proteinnet_file, "r") as fp:
        proteinnet_string = fp.readlines()

    file_in_progress = []
    for line in proteinnet_string:
        if(line == "[ID]\n"):
            if(len(file_in_progress) > 0):
                _write_file(args, file_in_progress)
                file_in_progress = []

        file_in_progress.append(line.strip())

    if(len(file_in_progress) > 0):
        _write_file(args, file_in_progress)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "proteinnet_file", type=str,
        help="Path to ProteinNet file to unpack"
    )
    parser.add_argument(
        "output_dir", type=str,
        help="Path to directory in which to output .core files"
    )

    args = parser.parse_args()

    main(args)