From f915a7b0a8801605a89ffa9f9aa8bfc84c4b3bf6 Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 12:39:55 +0200 Subject: [PATCH 1/7] Add get_fname function get_fname handles fname from file handler as previously but also provides a name in case fhandler is an iterable and not a file handler per se. --- pdbtools/__init__.py | 18 ++++++++++++++++++ pdbtools/pdb_splitchain.py | 11 ++++++++--- pdbtools/pdb_splitmodel.py | 11 ++++++++--- pdbtools/pdb_splitseg.py | 11 ++++++++--- pdbtools/pdb_tocif.py | 6 +++++- 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/pdbtools/__init__.py b/pdbtools/__init__.py index c9f51aad..0b59f8a3 100644 --- a/pdbtools/__init__.py +++ b/pdbtools/__init__.py @@ -71,6 +71,8 @@ >>> help(MODULE) >>> help(MODULE.run) """ +import os + __all__ = [ 'pdb_b', @@ -121,3 +123,19 @@ 'pdb_validate', 'pdb_wc', ] + + +def get_fname(fhandle, output=None): + + if output: + return os.path.basename(output) + + else: + try: + fn = fhandle.name + fname_root = fn[:-4] if fn != '' else 'output' + except AttributeError: + print('got attribute error') + fname_root = 'output' + + return os.path.basename(fname_root) diff --git a/pdbtools/pdb_splitchain.py b/pdbtools/pdb_splitchain.py index 32c4be3d..730776f6 100644 --- a/pdbtools/pdb_splitchain.py +++ b/pdbtools/pdb_splitchain.py @@ -34,6 +34,9 @@ import os import sys +from pdbtools import get_fname + + __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -71,7 +74,7 @@ def check_input(args): return fh -def run(fhandle): +def run(fhandle, outname=None): """ Split the PDB into its different chains. @@ -81,9 +84,11 @@ def run(fhandle): Parameters ---------- fhandle : an iterable giving the PDB file line-by-line + + outname : str + The base name of the output files. """ - fname_root = fhandle.name[:-4] if fhandle.name != '' else 'output' - basename = os.path.basename(fname_root) + basename = get_fname(fhandle, outname) chain_data = {} # {chain_id: lines} diff --git a/pdbtools/pdb_splitmodel.py b/pdbtools/pdb_splitmodel.py index 5702fa5b..a4d03ffa 100644 --- a/pdbtools/pdb_splitmodel.py +++ b/pdbtools/pdb_splitmodel.py @@ -34,6 +34,9 @@ import os import sys +from pdbtools import get_fname + + __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -71,7 +74,7 @@ def check_input(args): return fh -def run(fhandle): +def run(fhandle, outname=None): """ Split PDB into MODELS. @@ -81,9 +84,11 @@ def run(fhandle): Parameters ---------- fhandle : a line-by-line iterator of the original PDB file. + + outname : str + The base name of the output files. """ - fname_root = fhandle.name[:-4] if fhandle.name != '' else 'pdbfile' - basename = os.path.basename(fname_root) + basename = get_fname(fhandle, outname) model_lines = [] records = ('ATOM', 'HETATM', 'ANISOU', 'TER') diff --git a/pdbtools/pdb_splitseg.py b/pdbtools/pdb_splitseg.py index edce0fd8..d8c3bd9a 100644 --- a/pdbtools/pdb_splitseg.py +++ b/pdbtools/pdb_splitseg.py @@ -34,6 +34,9 @@ import os import sys +from pdbtools import get_fname + + __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -71,7 +74,7 @@ def check_input(args): return fh -def run(fhandle): +def run(fhandle, outname=None): """ Split PDB into segments. @@ -81,9 +84,11 @@ def run(fhandle): Parameters ---------- fhandle : a line-by-line iterator of the original PDB file. + + outname : str + The base name of the output files. """ - fname_root = fhandle.name[:-4] if fhandle.name != '' else 'output' - basename = os.path.basename(fname_root) + basename = get_fname(fhandle, outname) segment_data = {} # {segment_id: lines} diff --git a/pdbtools/pdb_tocif.py b/pdbtools/pdb_tocif.py index 89b44de7..070f679a 100644 --- a/pdbtools/pdb_tocif.py +++ b/pdbtools/pdb_tocif.py @@ -36,6 +36,9 @@ import os import sys +from pdbtools import get_fname + + __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -106,7 +109,8 @@ def run(fhandle): yield '#\n' # Headers - fname, _ = os.path.splitext(os.path.basename(fhandle.name)) + fhandle_name = get_fname(fhandle) + fname, _ = os.path.splitext(get_fname(fhandle)) if fname == '': fname = 'cell' yield 'data_{}\n'.format(fname) From a41c3e8ef86a62a69dfc72d4eff7ac8433e111a4 Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 13:11:12 +0200 Subject: [PATCH 2/7] newtests on pdb_splitchain --- tests/test_pdb_splitchain.py | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/test_pdb_splitchain.py b/tests/test_pdb_splitchain.py index cefa313e..55b5062d 100644 --- a/tests/test_pdb_splitchain.py +++ b/tests/test_pdb_splitchain.py @@ -61,6 +61,7 @@ def exec_module(self): return + def test_default(self): """$ pdb_splitchain data/dummy.pdb""" @@ -96,6 +97,87 @@ def test_default(self): self.assertEqual(fname_chain, list(set(pdb_chains))[0]) + def test_run_fhandler(self): + """pdb_splitchain.run(fhandler)""" + from pdbtools import pdb_splitchain + + src = os.path.join(data_dir, 'dummy.pdb') + dst = os.path.join(self.tempdir, 'dummy.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + pdb_splitchain.run(fin) + + # Read files created by script and then delete + ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('dummy')] + self.assertEqual(len(ofiles), 4 + 1) # ori + 4 chains + + # Make sure each file has the chain it should have + records = (('ATOM', 'HETATM', 'TER', 'ANISOU')) + for fpath in ofiles: + if fpath == 'dummy.pdb': + continue + + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + fname_chain = fpath.split('_')[1][:-4] # xxx_(X).pdb + pdb_chains = [l[21] for l in handle if l.startswith(records)] + + self.assertEqual(fname_chain, list(set(pdb_chains))[0]) + + def test_run_iterable(self): + """pdb_splitchain.run(iterable)""" + from pdbtools import pdb_splitchain + + src = os.path.join(data_dir, 'dummy.pdb') + dst = os.path.join(self.tempdir, 'dummy.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + lines = fin.readlines() + + pdb_splitchain.run(lines) + + # Read files created by script and then delete + ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('output')] + self.assertEqual(len(ofiles), 4) # 4 chains + + # Make sure each file has the chain it should have + records = (('ATOM', 'HETATM', 'TER', 'ANISOU')) + for fpath in ofiles: + + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + fname_chain = fpath.split('_')[1][:-4] # xxx_(X).pdb + pdb_chains = [l[21] for l in handle if l.startswith(records)] + + self.assertEqual(fname_chain, list(set(pdb_chains))[0]) + + def test_run_iterable_with_name(self): + """pdb_splitchain.run(iterable)""" + from pdbtools import pdb_splitchain + + src = os.path.join(data_dir, 'dummy.pdb') + dst = os.path.join(self.tempdir, 'dummy.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + lines = fin.readlines() + + pdb_splitchain.run(lines, outname='newname') + + # Read files created by script and then delete + ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('newname')] + self.assertEqual(len(ofiles), 4) # 4 chains + + # Make sure each file has the chain it should have + records = (('ATOM', 'HETATM', 'TER', 'ANISOU')) + for fpath in ofiles: + + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + fname_chain = fpath.split('_')[1][:-4] # xxx_(X).pdb + pdb_chains = [l[21] for l in handle if l.startswith(records)] + + self.assertEqual(fname_chain, list(set(pdb_chains))[0]) + def test_file_not_found(self): """$ pdb_splitchain not_existing.pdb""" From ab7e1028090762b1aa42161c39038ea116185b4b Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 13:32:24 +0200 Subject: [PATCH 3/7] add tests for splitmodel and splitseg --- tests/test_pdb_splitmodel.py | 82 ++++++++++++++++++++++++++++++++ tests/test_pdb_splitseg.py | 91 ++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+) diff --git a/tests/test_pdb_splitmodel.py b/tests/test_pdb_splitmodel.py index ac163c72..33b1bd26 100644 --- a/tests/test_pdb_splitmodel.py +++ b/tests/test_pdb_splitmodel.py @@ -93,6 +93,88 @@ def test_default(self): n_lines = len(handle.readlines()) self.assertEqual(n_lines, 2) + def test_run_iterable(self): + """pdb_splitmodel.run(iterable)""" + from pdbtools import pdb_splitmodel + + # Copy input file to tempdir + + # Simulate input + src = os.path.join(data_dir, 'ensemble_OK.pdb') + dst = os.path.join(self.tempdir, 'ensemble_OK.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + lines = fin.readlines() + + pdb_splitmodel.run(lines) + + # Read files created by script + ofiles = [f for f in os.listdir(self.tempdir) + if f.startswith('output')] + self.assertEqual(len(ofiles), 2) + + for fpath in ofiles: + if fpath == 'ensemble_OK.pdb': + continue + + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + n_lines = len(handle.readlines()) + self.assertEqual(n_lines, 2) + + def test_run_iterable_with_name(self): + """pdb_splitmodel.run(iterable)""" + from pdbtools import pdb_splitmodel + + # Copy input file to tempdir + + # Simulate input + src = os.path.join(data_dir, 'ensemble_OK.pdb') + dst = os.path.join(self.tempdir, 'ensemble_OK.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + lines = fin.readlines() + + pdb_splitmodel.run(lines, outname='newname') + + # Read files created by script + ofiles = [f for f in os.listdir(self.tempdir) + if f.startswith('newname')] + self.assertEqual(len(ofiles), 2) + + for fpath in ofiles: + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + n_lines = len(handle.readlines()) + self.assertEqual(n_lines, 2) + + def test_run_fhandler(self): + """pdb_splitmodel.run(fhandler)""" + from pdbtools import pdb_splitmodel + + # Copy input file to tempdir + + # Simulate input + src = os.path.join(data_dir, 'ensemble_OK.pdb') + dst = os.path.join(self.tempdir, 'ensemble_OK.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + pdb_splitmodel.run(fin) + + # Read files created by script + ofiles = [f for f in os.listdir(self.tempdir) + if f.startswith('ensemble_OK')] + self.assertEqual(len(ofiles), 2 + 1) # ori + 2 models + + for fpath in ofiles: + if fpath == 'ensemble_OK.pdb': + continue + + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + n_lines = len(handle.readlines()) + self.assertEqual(n_lines, 2) + def test_file_not_found(self): """$ pdb_splitmodel not_existing.pdb""" diff --git a/tests/test_pdb_splitseg.py b/tests/test_pdb_splitseg.py index 11801397..b924971e 100644 --- a/tests/test_pdb_splitseg.py +++ b/tests/test_pdb_splitseg.py @@ -61,6 +61,97 @@ def exec_module(self): return + def test_run_fhandler(self): + """pdb_splitseg.run(iterable)""" + from pdbtools import pdb_splitseg + + # Copy input file to tempdir + + # Simulate input + src = os.path.join(data_dir, 'dummy.pdb') + dst = os.path.join(self.tempdir, 'dummy.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + pdb_splitseg.run(fin) + + # Read files created by script and then delete + ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('dummy')] + self.assertEqual(len(ofiles), 2 + 1) # ori + 2 segments + + # Make sure each file has the chain it should have + records = (('ATOM', 'HETATM', 'TER', 'ANISOU')) + for fpath in ofiles: + if fpath == 'dummy.pdb': + continue + + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + fname_seg = fpath.split('_')[1][:-4] # xxx_(X).pdb + pdb_segids = [l[72:76].strip() for l in handle + if l.startswith(records)] + + self.assertEqual(fname_seg, list(set(pdb_segids))[0]) + + def test_run_iterable_with_name(self): + """pdb_splitseg.run(iterable, outname='newname')""" + from pdbtools import pdb_splitseg + + # Copy input file to tempdir + + # Simulate input + src = os.path.join(data_dir, 'dummy.pdb') + dst = os.path.join(self.tempdir, 'dummy.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + lines = fin.readlines() + + pdb_splitseg.run(lines, outname='newname') + + # Read files created by script and then delete + ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('newname')] + self.assertEqual(len(ofiles), 2) + + # Make sure each file has the chain it should have + records = (('ATOM', 'HETATM', 'TER', 'ANISOU')) + for fpath in ofiles: + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + fname_seg = fpath.split('_')[1][:-4] # xxx_(X).pdb + pdb_segids = [l[72:76].strip() for l in handle + if l.startswith(records)] + + self.assertEqual(fname_seg, list(set(pdb_segids))[0]) + + def test_run_iterable(self): + """pdb_splitseg.run(fhandler)""" + from pdbtools import pdb_splitseg + + # Copy input file to tempdir + + # Simulate input + src = os.path.join(data_dir, 'dummy.pdb') + dst = os.path.join(self.tempdir, 'dummy.pdb') + shutil.copy(src, dst) + + with open(dst, 'r') as fin: + lines = fin.readlines() + + pdb_splitseg.run(lines) + + # Read files created by script and then delete + ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('output')] + self.assertEqual(len(ofiles), 2) + + # Make sure each file has the chain it should have + records = (('ATOM', 'HETATM', 'TER', 'ANISOU')) + for fpath in ofiles: + with open(os.path.join(self.tempdir, fpath), 'r') as handle: + fname_seg = fpath.split('_')[1][:-4] # xxx_(X).pdb + pdb_segids = [l[72:76].strip() for l in handle + if l.startswith(records)] + + self.assertEqual(fname_seg, list(set(pdb_segids))[0]) + def test_default(self): """$ pdb_splitseg data/dummy.pdb""" From d333cac50f59b83dc5a454501ded3fab6850c15d Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 15:28:04 +0200 Subject: [PATCH 4/7] add tests for pdb_tocif --- pdbtools/pdb_tocif.py | 6 ++-- tests/test_pdb_tocif.py | 80 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/pdbtools/pdb_tocif.py b/pdbtools/pdb_tocif.py index 070f679a..c7a3536e 100644 --- a/pdbtools/pdb_tocif.py +++ b/pdbtools/pdb_tocif.py @@ -83,7 +83,7 @@ def pad_line(line): return line[:81] # 80 + newline character -def run(fhandle): +def run(fhandle, outname=None): """ Convert a structure in PDB format to mmCIF format. @@ -109,8 +109,8 @@ def run(fhandle): yield '#\n' # Headers - fhandle_name = get_fname(fhandle) - fname, _ = os.path.splitext(get_fname(fhandle)) + fhandle_name = get_fname(fhandle, outname) + fname, _ = os.path.splitext(fhandle_name) if fname == '': fname = 'cell' yield 'data_{}\n'.format(fname) diff --git a/tests/test_pdb_tocif.py b/tests/test_pdb_tocif.py index cda5acc6..93918f64 100644 --- a/tests/test_pdb_tocif.py +++ b/tests/test_pdb_tocif.py @@ -53,6 +53,86 @@ def exec_module(self): return + def test_single_model_run_iterable(self): + """$ pdb_tocif.run(iterable)""" + from pdbtools import pdb_tocif + + fpath = os.path.join(data_dir, 'dummy.pdb') + with open(fpath, 'r') as fin: + lines = fin.readlines() + + newlines = list(pdb_tocif.run(lines)) + + # Check no of records + n_ATOM = sum(1 for l in newlines if l.startswith('ATOM')) + n_HETATM = sum(1 for l in newlines if l.startswith('HETATM')) + n_coord = n_ATOM + n_HETATM + self.assertEqual(n_ATOM, 176) + self.assertEqual(n_HETATM, 9) + self.assertEqual(n_coord, 185) + + # check name + self.assertEqual(newlines[2], 'data_output\n') + + def test_single_model_run_iterable_with_name(self): + """$ pdb_tocif.run(iterable)""" + from pdbtools import pdb_tocif + + fpath = os.path.join(data_dir, 'dummy.pdb') + with open(fpath, 'r') as fin: + lines = fin.readlines() + + newlines = list(pdb_tocif.run(lines, outname='newcif')) + + # Check no of records + n_ATOM = sum(1 for l in newlines if l.startswith('ATOM')) + n_HETATM = sum(1 for l in newlines if l.startswith('HETATM')) + n_coord = n_ATOM + n_HETATM + self.assertEqual(n_ATOM, 176) + self.assertEqual(n_HETATM, 9) + self.assertEqual(n_coord, 185) + + # check name + self.assertEqual(newlines[2], 'data_newcif\n') + + def test_single_model_run_fhandler(self): + """$ pdb_tocif.run(fhandler)""" + from pdbtools import pdb_tocif + + fpath = os.path.join(data_dir, 'dummy.pdb') + with open(fpath, 'r') as fin: + newlines = list(pdb_tocif.run(fin)) + + # Check no of records + n_ATOM = sum(1 for l in newlines if l.startswith('ATOM')) + n_HETATM = sum(1 for l in newlines if l.startswith('HETATM')) + n_coord = n_ATOM + n_HETATM + self.assertEqual(n_ATOM, 176) + self.assertEqual(n_HETATM, 9) + self.assertEqual(n_coord, 185) + + # check name + self.assertEqual(newlines[2], 'data_dummy\n') + + def test_single_model_run_fhandler_name(self): + """$ pdb_tocif.run(fhandler)""" + from pdbtools import pdb_tocif + + fpath = os.path.join(data_dir, 'dummy.pdb') + with open(fpath, 'r') as fin: + newlines = list(pdb_tocif.run(fin, outname='newname')) + + # Check no of records + n_ATOM = sum(1 for l in newlines if l.startswith('ATOM')) + n_HETATM = sum(1 for l in newlines if l.startswith('HETATM')) + n_coord = n_ATOM + n_HETATM + self.assertEqual(n_ATOM, 176) + self.assertEqual(n_HETATM, 9) + self.assertEqual(n_coord, 185) + + # check name + self.assertEqual(newlines[2], 'data_newname\n') + def test_single_model(self): """$ pdb_tocif data/dummy.pdb""" From 41d1a539e18be1d565b64a6fa93532f7addb6be7 Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 15:47:21 +0200 Subject: [PATCH 5/7] Reverted get_fname from function to builtin script I considered the fact that each pdb-tool should work also as a stand-alone script. Hence, I removed `get_fname` function and implemented that logic within each `run` function separately. --- pdbtools/__init__.py | 16 ---------------- pdbtools/pdb_splitchain.py | 12 +++++++++--- pdbtools/pdb_splitmodel.py | 12 +++++++++--- pdbtools/pdb_splitseg.py | 12 +++++++++--- pdbtools/pdb_tocif.py | 18 +++++++++++------- tests/test_pdb_splitchain.py | 6 +++++- tests/test_pdb_splitmodel.py | 2 +- tests/test_pdb_splitseg.py | 5 ++++- tests/test_pdb_tocif.py | 2 +- 9 files changed, 49 insertions(+), 36 deletions(-) diff --git a/pdbtools/__init__.py b/pdbtools/__init__.py index 0b59f8a3..2c27db4b 100644 --- a/pdbtools/__init__.py +++ b/pdbtools/__init__.py @@ -123,19 +123,3 @@ 'pdb_validate', 'pdb_wc', ] - - -def get_fname(fhandle, output=None): - - if output: - return os.path.basename(output) - - else: - try: - fn = fhandle.name - fname_root = fn[:-4] if fn != '' else 'output' - except AttributeError: - print('got attribute error') - fname_root = 'output' - - return os.path.basename(fname_root) diff --git a/pdbtools/pdb_splitchain.py b/pdbtools/pdb_splitchain.py index 730776f6..e7d08476 100644 --- a/pdbtools/pdb_splitchain.py +++ b/pdbtools/pdb_splitchain.py @@ -34,8 +34,6 @@ import os import sys -from pdbtools import get_fname - __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -88,7 +86,15 @@ def run(fhandle, outname=None): outname : str The base name of the output files. """ - basename = get_fname(fhandle, outname) + _defname = 'splitchains' + if outname is None: + try: + fn = fhandle.name + outname = fn[:-4] if fn != '' else _defname + except AttributeError: + outname = _defname + + basename = os.path.basename(outname) chain_data = {} # {chain_id: lines} diff --git a/pdbtools/pdb_splitmodel.py b/pdbtools/pdb_splitmodel.py index a4d03ffa..5f1a6b28 100644 --- a/pdbtools/pdb_splitmodel.py +++ b/pdbtools/pdb_splitmodel.py @@ -34,8 +34,6 @@ import os import sys -from pdbtools import get_fname - __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -88,7 +86,15 @@ def run(fhandle, outname=None): outname : str The base name of the output files. """ - basename = get_fname(fhandle, outname) + _defname = 'splitmodels' + if outname is None: + try: + fn = fhandle.name + outname = fn[:-4] if fn != '' else _defname + except AttributeError: + outname = _defname + + basename = os.path.basename(outname) model_lines = [] records = ('ATOM', 'HETATM', 'ANISOU', 'TER') diff --git a/pdbtools/pdb_splitseg.py b/pdbtools/pdb_splitseg.py index d8c3bd9a..4e03d9c0 100644 --- a/pdbtools/pdb_splitseg.py +++ b/pdbtools/pdb_splitseg.py @@ -34,8 +34,6 @@ import os import sys -from pdbtools import get_fname - __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -88,7 +86,15 @@ def run(fhandle, outname=None): outname : str The base name of the output files. """ - basename = get_fname(fhandle, outname) + _defname = 'splitsegs' + if outname is None: + try: + fn = fhandle.name + outname = fn[:-4] if fn != '' else _defname + except AttributeError: + outname = _defname + + basename = os.path.basename(outname) segment_data = {} # {segment_id: lines} diff --git a/pdbtools/pdb_tocif.py b/pdbtools/pdb_tocif.py index c7a3536e..2125f74f 100644 --- a/pdbtools/pdb_tocif.py +++ b/pdbtools/pdb_tocif.py @@ -36,8 +36,6 @@ import os import sys -from pdbtools import get_fname - __author__ = "Joao Rodrigues" __email__ = "j.p.g.l.m.rodrigues@gmail.com" @@ -109,11 +107,17 @@ def run(fhandle, outname=None): yield '#\n' # Headers - fhandle_name = get_fname(fhandle, outname) - fname, _ = os.path.splitext(fhandle_name) - if fname == '': - fname = 'cell' - yield 'data_{}\n'.format(fname) + _defname = 'cell' + if outname is None: + try: + fn = fhandle.name + outname = fn[:-4] if fn != '' else _defname + except AttributeError: + outname = _defname + + fname_root = os.path.basename(outname) + + yield 'data_{}\n'.format(fname_root) yield '#\n' yield 'loop_\n' diff --git a/tests/test_pdb_splitchain.py b/tests/test_pdb_splitchain.py index 55b5062d..93201514 100644 --- a/tests/test_pdb_splitchain.py +++ b/tests/test_pdb_splitchain.py @@ -138,7 +138,11 @@ def test_run_iterable(self): pdb_splitchain.run(lines) # Read files created by script and then delete - ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('output')] + ofiles = [ + f + for f in os.listdir(self.tempdir) + if f.startswith('splitchains') + ] self.assertEqual(len(ofiles), 4) # 4 chains # Make sure each file has the chain it should have diff --git a/tests/test_pdb_splitmodel.py b/tests/test_pdb_splitmodel.py index 33b1bd26..ac20c5e4 100644 --- a/tests/test_pdb_splitmodel.py +++ b/tests/test_pdb_splitmodel.py @@ -111,7 +111,7 @@ def test_run_iterable(self): # Read files created by script ofiles = [f for f in os.listdir(self.tempdir) - if f.startswith('output')] + if f.startswith('splitmodels')] self.assertEqual(len(ofiles), 2) for fpath in ofiles: diff --git a/tests/test_pdb_splitseg.py b/tests/test_pdb_splitseg.py index b924971e..f6d3b7f1 100644 --- a/tests/test_pdb_splitseg.py +++ b/tests/test_pdb_splitseg.py @@ -139,7 +139,10 @@ def test_run_iterable(self): pdb_splitseg.run(lines) # Read files created by script and then delete - ofiles = [f for f in os.listdir(self.tempdir) if f.startswith('output')] + ofiles = [ + f + for f in os.listdir(self.tempdir) + if f.startswith('splitsegs')] self.assertEqual(len(ofiles), 2) # Make sure each file has the chain it should have diff --git a/tests/test_pdb_tocif.py b/tests/test_pdb_tocif.py index 93918f64..1cfa12bf 100644 --- a/tests/test_pdb_tocif.py +++ b/tests/test_pdb_tocif.py @@ -72,7 +72,7 @@ def test_single_model_run_iterable(self): self.assertEqual(n_coord, 185) # check name - self.assertEqual(newlines[2], 'data_output\n') + self.assertEqual(newlines[2], 'data_cell\n') def test_single_model_run_iterable_with_name(self): """$ pdb_tocif.run(iterable)""" From a32924a7fc30f1e5b764d5f25c0454dbf13104bd Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 15:51:22 +0200 Subject: [PATCH 6/7] update docstring --- pdbtools/pdb_splitchain.py | 4 +++- pdbtools/pdb_splitmodel.py | 4 +++- pdbtools/pdb_splitseg.py | 4 +++- pdbtools/pdb_tocif.py | 5 +++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pdbtools/pdb_splitchain.py b/pdbtools/pdb_splitchain.py index e7d08476..5559d97e 100644 --- a/pdbtools/pdb_splitchain.py +++ b/pdbtools/pdb_splitchain.py @@ -84,7 +84,9 @@ def run(fhandle, outname=None): fhandle : an iterable giving the PDB file line-by-line outname : str - The base name of the output files. + The base name of the output files. If None is given, tries to + extract a name from the `.name` attribute of `fhandler`. If + `fhandler` has no attribute name, assigns `splitchains`. """ _defname = 'splitchains' if outname is None: diff --git a/pdbtools/pdb_splitmodel.py b/pdbtools/pdb_splitmodel.py index 5f1a6b28..052e3a17 100644 --- a/pdbtools/pdb_splitmodel.py +++ b/pdbtools/pdb_splitmodel.py @@ -84,7 +84,9 @@ def run(fhandle, outname=None): fhandle : a line-by-line iterator of the original PDB file. outname : str - The base name of the output files. + The base name of the output files. If None is given, tries to + extract a name from the `.name` attribute of `fhandler`. If + `fhandler` has no attribute name, assigns `splitmodels`. """ _defname = 'splitmodels' if outname is None: diff --git a/pdbtools/pdb_splitseg.py b/pdbtools/pdb_splitseg.py index 4e03d9c0..f5204ea7 100644 --- a/pdbtools/pdb_splitseg.py +++ b/pdbtools/pdb_splitseg.py @@ -84,7 +84,9 @@ def run(fhandle, outname=None): fhandle : a line-by-line iterator of the original PDB file. outname : str - The base name of the output files. + The base name of the output files. If None is given, tries to + extract a name from the `.name` attribute of `fhandler`. If + `fhandler` has no attribute name, assigns `splitsegs`. """ _defname = 'splitsegs' if outname is None: diff --git a/pdbtools/pdb_tocif.py b/pdbtools/pdb_tocif.py index 2125f74f..67668009 100644 --- a/pdbtools/pdb_tocif.py +++ b/pdbtools/pdb_tocif.py @@ -91,6 +91,11 @@ def run(fhandle, outname=None): ---------- fhandle : an iterable giving the PDB file line-by-line. + outname : str + The base name of the output files. If None is given, tries to + extract a name from the `.name` attribute of `fhandler`. If + `fhandler` has no attribute name, assigns `cell`. + Yields ------ str (line-by-line) From 852dae8ede4a34fbf04bde4380af7a9e58528d2c Mon Sep 17 00:00:00 2001 From: joaomcteixeira Date: Tue, 20 Jul 2021 15:55:06 +0200 Subject: [PATCH 7/7] removes unnecessary import os --- pdbtools/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pdbtools/__init__.py b/pdbtools/__init__.py index 2c27db4b..7e84f307 100644 --- a/pdbtools/__init__.py +++ b/pdbtools/__init__.py @@ -71,7 +71,6 @@ >>> help(MODULE) >>> help(MODULE.run) """ -import os __all__ = [