Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise exceptions instead of sys.exit #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 14 additions & 21 deletions barnaba/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ def rmsd_traj(reference,traj,out=None,heavy_atom=False):
assert(len(nn_traj.ok_residues)==len(nn_ref.ok_residues))
# check that sequence is identical when using heavy atoms
if(nn_traj.rna_seq_id!=nn_ref.rna_seq_id and heavy_atom==True):
sys.stderr.write("# Sequences are not identical, cannot superimpose using all heavy atoms. ")
sys.exit(1)
raise ValueError("Sequences are not identical, cannot superimpose using all heavy atoms.")

# loop over residues and find common heavy atoms
idx_ref = []
Expand Down Expand Up @@ -272,9 +271,7 @@ def rmsd_traj(reference,traj,out=None,heavy_atom=False):
print("# found ",len(idx_ref), "atoms in common")

if(len(idx_ref)<3):
warn = "# Only %d atoms in common. abort.\n" % len(idx_ref)
sys.stderr.write(warn)
sys.exit(1)
raise ValueError("Only %d atoms in common." % len(idx_ref))

traj.superpose(reference,atom_indices=idx_target, ref_atom_indices=idx_ref)
if(out!=None): traj.save(out)
Expand Down Expand Up @@ -334,11 +331,8 @@ def backbone_angles_traj(traj,residues=None,angles=None):
if(angles[i] in definitions.bb_angles):
idx_angles.append(definitions.bb_angles.index(angles[i]))
else:
msg = "# Fatal error. requested angle \"%s\" not available.\n" % angles[i]
msg += "# Choose from: %s \n" % definitions.bb_angles
sys.stderr.write(msg)
sys.exit(1)

raise ValueError("Requested angle '%s' not available. Choose from: %s." %
(angles[i], definitions.bb_angles))

idxs = (all_idx[:,idx_angles,:]).reshape(-1,4)
missing = np.where(np.sum(idxs,axis=1)==0)
Expand Down Expand Up @@ -403,10 +397,9 @@ def sugar_angles_traj(traj,residues=None,angles=None):
if(angles[i] in definitions.sugar_angles):
idx_angles.append(definitions.sugar_angles.index(angles[i]))
else:
msg = "# Fatal error. requested angle \"%s\" not available.\n" % angles[i]
msg += "# Choose from: %s \n" % (definitions.sugar_angles)
sys.stderr.write(msg)
sys.exit(1)
raise ValueError("Requested angle '%s' not available. Choose from: %s." %
(angles[i], definitions.sugar_angles))


idxs = (all_idx[:,idx_angles,:]).reshape(-1,4)
missing = np.where(np.sum(idxs,axis=1)==0)
Expand Down Expand Up @@ -543,10 +536,10 @@ def jcouplings_traj(traj,residues=None,couplings=None,raw=False):
idx_angles.append(definitions.couplings_idx[couplings[i]])
idx_angles1.append(i)
else:
msg = "# Fatal error. requested coupling \"%s\" not available.\n" % couplings[i]
msg += "# Choose from: %s \n" % definitions.couplings_idx
sys.stderr.write(msg)
sys.exit(1)
raise ValueError("Requested coupling '%s' not available. Choose from: %s." %
(couplings[i], definitions.couplings_idx))



idxs = (all_idx[:,idx_angles,:]).reshape(-1,4)
missing = np.where(np.sum(idxs,axis=1)==0)
Expand Down Expand Up @@ -1298,7 +1291,7 @@ def parse_dotbracket(threshold, file, weights):
if len(dotbr) != len(sequence):
print(mult_chain)
if not mult_chain:
sys.exit("Dot-bracket length does not match sequence length")
raise ValueError("Dot-bracket length does not match sequence length.")
else:
dotbr = dotbr[:len(sequence)]
base_pairs = parse_dotbr(dotbr)
Expand All @@ -1316,7 +1309,7 @@ def parse_dotbracket(threshold, file, weights):
dotbr = l[1]
if len(dotbr) != len(sequence):
if not mult_chain:
sys.exit("Dot-bracket length does not match sequence length")
raise ValueError("Dot-bracket length does not match sequence length.")
else:
dotbr = dotbr[:len(sequence)]
n_frames += 1
Expand All @@ -1337,7 +1330,7 @@ def parse_dotbracket(threshold, file, weights):
else:
ann_list[bp[0], bp[1], "WCc"] += weights[n_frames-1]
if len(list_base_pairs) == 0:
sys.exit("No basepairs found.")
raise ValueError("No basepairs found.")
if traj:
for ann, value in ann_list.items():
if len(weights) == 0:
Expand Down
2 changes: 1 addition & 1 deletion barnaba/kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def evaluate(self, points):
self.d)
raise ValueError(msg)

result = zeros((m,), dtype=np.float)
result = zeros((m,), dtype=float)

if m >= self.n:
# there are more points than data, so loop over data
Expand Down
3 changes: 1 addition & 2 deletions barnaba/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def __init__(self,data,set_lcs=False):

ll = len(data)
if(ll==0):
sys.stderr.write("# Fatal error. No valid residues in pdb file \n")
sys.exit(1)
raise ValueError("No valid residues in pdb file.")

# array with all coordinates
self.coords = np.array([data[i][5:8] for i in range(ll)])
Expand Down
23 changes: 8 additions & 15 deletions barnaba/nucleic.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def __init__(self,topology,modified=True):
self.indeces_lcs = np.asarray(indeces_lcs).T

if(len(self.ok_residues)<1):
warn = "# Only %d found in structure. Exiting \n" % len(self.ok_residues)
sys.stderr.write(warn)
sys.exit(1)
raise ValueError("Only %d found in structure." % len(self.ok_residues))
#else:
# warn = "# %d nucleotides found in structure \n" % len(ok_residues)
# sys.stderr.write(warn)
Expand All @@ -101,10 +99,8 @@ def get_bb_torsion_idx(self, residues=None):
if(residues[k] in self.rna_seq):
idx_residues.append((self.rna_seq).index(residues[k]))
else:
msg = "# Fatal error. Residue \"%s\" not found \n" % residues[k]
msg += "# The list of residues is: %s \n" % self.rna_seq
sys.stderr.write(msg)
sys.exit(1)
raise ValueError("Residue '%s' not found. The list of residues is: %s" %
(residues[k], self.rna_seq))
else:
idx_residues = np.arange(ll)

Expand Down Expand Up @@ -184,10 +180,9 @@ def get_sugar_torsion_idx(self,residues=None):
if(residues[k] in self.rna_seq):
idx_residues.append((self.rna_seq).index(residues[k]))
else:
msg = "# Fatal error. Residue \"%s\" not found \n" % residues[k]
msg += "# The list of residues is: %s \n" % self.rna_seq
sys.stderr.write(msg)
sys.exit(1)
raise ValueError("Residue '%s' not found. The list of residues is: %s" %
(residues[k], self.rna_seq))

else:
idx_residues = np.arange(ll)

Expand Down Expand Up @@ -230,10 +225,8 @@ def get_coupling_idx(self,residues=None):
if(residues[k] in self.rna_seq):
idx_residues.append((self.rna_seq).index(residues[k]))
else:
msg = "# Fatal error. Residue \"%s\" not found \n" % residues[k]
msg += "# The list of residues is: %s \n" % self.rna_seq
sys.stderr.write(msg)
sys.exit(1)
raise ValueError("Residue '%s' not found. The list of residues is: %s" %
(residues[k], self.rna_seq))
else:
idx_residues = np.arange(ll)

Expand Down
3 changes: 1 addition & 2 deletions barnaba/smm.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def __init__(self,gvecs,eps,weights=[]):
print("# Sequence lenght =",slen)
self.slen = slen
else:
print("# Error - check your gmat file")
sys.exit(1)
raise ValueError("Check your gmat file")

# calculate distance matrix
dmat_sq = cdist(gvecs,gvecs,'sqeuclidean')
Expand Down
Loading