[Biopython-dev] [Bug 2751] PDBParser crashes on empty tempFactor fields

bugzilla-daemon at portal.open-bio.org bugzilla-daemon at portal.open-bio.org
Sat Feb 7 17:44:46 UTC 2009


http://bugzilla.open-bio.org/show_bug.cgi?id=2751





------- Comment #5 from eric.talevich at gmail.com  2009-02-07 12:44 EST -------
(From update of attachment 1216)
>=== modified file 'Bio/PDB/PDBParser.py'
>--- Bio/PDB/PDBParser.py	2009-02-06 20:42:42 +0000
>+++ Bio/PDB/PDBParser.py	2009-02-06 21:17:08 +0000
>@@ -111,6 +111,20 @@
>         current_segid=None
>         current_residue_id=None
>         current_resname=None
>+
>+        def try_float(field, default=None):
>+            """Try coercing a string into a float, safely.
>+
>+            If the string is not a valid float, then if default is given,
>+            default is returned; otherwise an exception is raised.
>+            """
>+            try:
>+                return float(field)
>+            except (ValueError, NameError):
>+                if (self.PERMISSIVE==0) or default is None:
>+                    raise PDBConstructionException("Detected an invalid value in a field")
>+                return default
>+
>         for i in range(0, len(coords_trailer)):
>             line=coords_trailer[i]
>             record_type=line[0:6]
>@@ -150,13 +164,13 @@
>                     hetero_flag=" "
>                 residue_id=(hetero_flag, resseq, icode)
>                 # atomic coordinates
>-                x=float(line[30:38]) 
>-                y=float(line[38:46]) 
>-                z=float(line[46:54])
>+                x=try_float(line[30:38]) 
>+                y=try_float(line[38:46]) 
>+                z=try_float(line[46:54])
>                 coord=numpy.array((x, y, z), 'f')
>                 # occupancy & B factor
>-                occupancy=float(line[54:60])
>-                bfactor=float(line[60:66])
>+                occupancy=try_float(line[54:60], default=0.0)
>+                bfactor=try_float(line[60:66], default=0.0)
>                 segid=line[72:76]
>                 if current_segid!=segid:
>                     current_segid=segid
>@@ -183,7 +197,7 @@
>                 except PDBConstructionException, message:
>                     self._handle_PDB_exception(message, global_line_counter)
>             elif(record_type=='ANISOU'):
>-                anisou=map(float, (line[28:35], line[35:42], line[43:49], line[49:56], line[56:63], line[63:70]))
>+                anisou=map(try_float, (line[28:35], line[35:42], line[43:49], line[49:56], line[56:63], line[63:70]))
>                 # U's are scaled by 10^4 
>                 anisou_array=(numpy.array(anisou, 'f')/10000.0).astype('f')
>                 structure_builder.set_anisou(anisou_array)
>@@ -203,13 +217,13 @@
>                 current_residue_id=None
>             elif(record_type=='SIGUIJ'):
>                 # standard deviation of anisotropic B factor
>-                siguij=map(float, (line[28:35], line[35:42], line[42:49], line[49:56], line[56:63], line[63:70]))
>+                siguij=map(try_float, (line[28:35], line[35:42], line[42:49], line[49:56], line[56:63], line[63:70]))
>                 # U sigma's are scaled by 10^4
>                 siguij_array=(numpy.array(siguij, 'f')/10000.0).astype('f')   
>                 structure_builder.set_siguij(siguij_array)
>             elif(record_type=='SIGATM'):
>                 # standard deviation of atomic positions
>-                sigatm=map(float, (line[30:38], line[38:45], line[46:54], line[54:60], line[60:66]))
>+                sigatm=map(try_float, (line[30:38], line[38:45], line[46:54], line[54:60], line[60:66]))
>                 sigatm_array=numpy.array(sigatm, 'f')
>                 structure_builder.set_sigatm(sigatm_array)
>             local_line_counter=local_line_counter+1
>


-- 
Configure bugmail: http://bugzilla.open-bio.org/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.



More information about the Biopython-dev mailing list