-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_pdb_file.py
More file actions
41 lines (32 loc) · 1.39 KB
/
read_pdb_file.py
File metadata and controls
41 lines (32 loc) · 1.39 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
#!/usr/bin/env python3
# functions to read a PDB (Protein Data Bank) files
#-------------------------------------------------------
import os, gzip
#-------------------------------------------------------
def pdb_to_dtype(path_to_pdb_file, out_fmt = 'str', file_type = 'uncompressed'):
"""
parameters
----------
path_to_pdb_file: str
out_fmt: str
specifies whether to return file contents as a string or
a list (each element is a separate line)
returns
-------
str or list
"""
assert isinstance(path_to_pdb_file, str) , '<path_to_pdb_file> argument was not a str type. got {}'.format(type(path_to_pdb_file))
assert os.path.isfile(path_to_pdb_file) == True, '<path_to_pdb_file> argument was not a valid file path. got: {}'.format(path_to_pdb_file)
assert out_fmt in ['str', 'list'] , '<out_fmt> argument was not str or list'
if file_type == 'compressed':
with gzip.open(path_to_pdb_file, 'rt') as file_obj:
if out_fmt == 'str':
return file_obj.read()
elif out_fmt == 'list':
return file_obj.readlines()
elif file_type == 'uncompressed':
with open(path_to_pdb_file, 'r') as file_obj:
if out_fmt == 'str':
return file_obj.read()
elif out_fmt == 'list':
return file_obj.readlines()