@@ -87,25 +87,40 @@ def get_sg_num_from_symbol(sg_symbol):
8787 try : return SpaceGroup .sg_encoding [sg_symbol ]['int_number' ]
8888 except KeyError : return None
8989
90- def _parse_cif_file (file_path ):
90+ def _parse_cif_file (file_path , parse_from_comment : bool = False ):
9191 """Parses a CIF file to get the space group number."""
9292 sg_num = None
9393 with open (file_path , 'r' , errors = 'ignore' ) as f :
94- for line in f :
95- if '_symmetry_Int_Tables_number' in line or '_space_group_IT_number' in line :
96- try :
97- sg_num = int (line .split ()[- 1 ])
94+ for raw_line in f :
95+ line = raw_line .strip ()
96+
97+ if parse_from_comment :
98+ # Prefer provenance comment inserted by downloader, e.g.:
99+ # # _original__symmetry_space_group_name_H-M 'I-43m'
100+ if line .startswith ("# _original_symmetry_space_group_name_H-M" ):
101+ if '"' in line :
102+ sg_sym = line .split ('"' )[1 ].replace (' ' , '' )
103+ elif "'" in line :
104+ sg_sym = line .split ("'" )[1 ].replace (' ' , '' )
105+ sg_num = get_sg_num_from_symbol (sg_sym )
98106 return sg_num , get_crystal_system (sg_num )
99- except (ValueError , IndexError ): continue
107+
108+ else :
109+ # Standard CIF tags (non-comment)
110+ if '_symmetry_Int_Tables_number' in line or '_space_group_IT_number' in line :
111+ try :
112+ sg_num = int (line .split ()[- 1 ])
113+ return sg_num , get_crystal_system (sg_num )
114+ except (ValueError , IndexError ): continue
100115
101- # H-M name tag with quoted values: handle original double quotes and the provided single-quote example
102- if '_symmetry_space_group_name_H-M' in line :
103- if '"' in line :
104- sg_sym = line .split ('"' )[1 ].replace (' ' , '' )
105- elif "'" in line :
106- sg_sym = line .split ("'" )[1 ].replace (' ' , '' )
107- sg_num = get_sg_num_from_symbol (sg_sym )
108- return sg_num , get_crystal_system (sg_num )
116+ # H-M name tag with quoted values (ignore commented lines)
117+ if '_symmetry_space_group_name_H-M' in line and not line . startswith ( "#" ) :
118+ if '"' in line :
119+ sg_sym = line .split ('"' )[1 ].replace (' ' , '' )
120+ elif "'" in line :
121+ sg_sym = line .split ("'" )[1 ].replace (' ' , '' )
122+ sg_num = get_sg_num_from_symbol (sg_sym )
123+ return sg_num , get_crystal_system (sg_num )
109124
110125 return None , None
111126
@@ -212,7 +227,7 @@ def run_single_simulation(params):
212227 try :
213228 if input_file .suffix .lower () == '.cif' :
214229 file_type = 'cif'
215- sg_num , cs = _parse_cif_file (input_file )
230+ sg_num , cs = _parse_cif_file (input_file , parse_from_comment = params [ 'parse_from_comment' ] )
216231 if sg_num is None : raise ValueError ("Could not parse space group number from CIF." )
217232 elif input_file .suffix .lower () in ['.dif' , '.txt' ]:
218233 file_type = 'dif'
0 commit comments