pdbe-mmcif-validator 0.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pdbe-mmcif-validator might be problematic. Click here for more details.

cif_parser.py ADDED
@@ -0,0 +1,408 @@
1
+ """
2
+ mmCIF file parser.
3
+ """
4
+
5
+ from pathlib import Path
6
+ from typing import Dict, List, Set, Tuple
7
+
8
+ from mmcif_types import ItemValue
9
+
10
+
11
+ class MmCIFParser:
12
+ """Parses mmCIF files."""
13
+
14
+ def __init__(self, cif_path: Path):
15
+ self.cif_path = cif_path
16
+ self.items: Dict[str, List[ItemValue]] = {} # item_name -> list of ItemValue(line_num, value, global_column_index, local_column_index)
17
+ self.categories: Set[str] = set() # Set of category names present in the file
18
+ self.lines: List[str] = []
19
+ # Each loop block: (loop_start_line, category_name, [(item_name, header_line_num), ...])
20
+ self.loop_blocks: List[Tuple[int, str, List[Tuple[str, int]]]] = []
21
+ # Frame blocks (non-loop item-name value pairs): (first_line, category_name, [(item_name, line_num), ...])
22
+ self.frame_blocks: List[Tuple[int, str, List[Tuple[str, int]]]] = []
23
+
24
+ def parse(self):
25
+ """Parse the mmCIF file. Only parses the first data block."""
26
+ with open(self.cif_path, 'r', encoding='utf-8') as f:
27
+ self.lines = f.readlines()
28
+
29
+ # Find the first data block and determine where to stop parsing
30
+ first_data_block_line = None
31
+ second_data_block_line = None
32
+
33
+ for line_num, line in enumerate(self.lines, 1):
34
+ stripped = line.strip()
35
+ if stripped.startswith('data_'):
36
+ if first_data_block_line is None:
37
+ first_data_block_line = line_num
38
+ else:
39
+ second_data_block_line = line_num
40
+ break
41
+
42
+ # Determine parsing range:
43
+ # - Start from first data block (or line 1 if no data block found, for backward compatibility)
44
+ # - Stop at second data block (or end of file if no second data block)
45
+ start_line = first_data_block_line if first_data_block_line is not None else 1
46
+ max_line = len(self.lines) if second_data_block_line is None else second_data_block_line - 1
47
+
48
+ current_loop_items = []
49
+ in_loop = False
50
+ is_real_loop = False # True if loop started with 'loop_' directive, False if pseudo-loop from single item
51
+ loop_start_line = 0
52
+ partial_row_values = [] # Accumulate values for multi-line rows: [(value, line_num), ...]
53
+ partial_row_line_nums = [] # Track line numbers for each value in partial_row_values
54
+ expected_columns = 0
55
+ self._in_multiline_string = False # Track if we're inside a multi-line string
56
+ self._multiline_content = "" # Accumulate multi-line string content
57
+ self._multiline_start_line = 0 # Track line where multi-line string started
58
+ # Frame block (non-loop): [start_line, category, [(item_name, line_num), ...]] or None
59
+ current_frame_block = None
60
+ # Categories that have been closed (we left them via loop_ or different category) - re-appearance is duplicate category
61
+ closed_frame_categories: Set[str] = set()
62
+
63
+ for line_num, line in enumerate(self.lines, 1):
64
+ # Skip lines before the first data block
65
+ if line_num < start_line:
66
+ continue
67
+ # Stop parsing if we've reached the second data block
68
+ if line_num > max_line:
69
+ break
70
+
71
+ stripped = line.strip()
72
+
73
+ # Skip comments and empty lines
74
+ if not stripped or stripped.startswith('#'):
75
+ # If we're in a loop and hit a comment, it might end the loop
76
+ # But also might be within a multi-line value, so we continue accumulating
77
+ if in_loop and current_loop_items and partial_row_values:
78
+ # Comment might indicate end of loop, but let's be safe and continue
79
+ continue
80
+ elif in_loop and current_loop_items:
81
+ # Empty line or comment - might be end of loop or continuation
82
+ # If we have partial values, continue accumulating
83
+ if partial_row_values:
84
+ continue
85
+ continue
86
+
87
+ # Check for loop_ directive
88
+ if stripped == 'loop_':
89
+ # Close current frame block (non-loop items) if any
90
+ if current_frame_block is not None:
91
+ closed_frame_categories.add(current_frame_block[1])
92
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
93
+ current_frame_block = None
94
+ # Record the previous loop block (for duplicate category/item detection)
95
+ if in_loop and is_real_loop and current_loop_items:
96
+ first_item_name = current_loop_items[0][0]
97
+ cat = first_item_name[1:].split('.')[0] if (first_item_name.startswith('_') and '.' in first_item_name) else ''
98
+ self.loop_blocks.append((loop_start_line, cat, list(current_loop_items)))
99
+ # Finish any partial row before starting new loop
100
+ if partial_row_values and current_loop_items:
101
+ self._assign_loop_row(current_loop_items, partial_row_values, partial_row_line_nums)
102
+ in_loop = True
103
+ is_real_loop = True # This is a real loop directive
104
+ current_loop_items = []
105
+ partial_row_values = []
106
+ partial_row_line_nums = []
107
+ expected_columns = 0
108
+ loop_start_line = line_num
109
+ self._in_multiline_string = False
110
+ self._multiline_content = ""
111
+ self._multiline_start_line = 0
112
+ continue
113
+
114
+ # Check for item names (start with _)
115
+ if stripped.startswith('_'):
116
+ if '.' in stripped:
117
+ parts = stripped.split(None, 1)
118
+ item_name = parts[0]
119
+ value = parts[1] if len(parts) > 1 else None
120
+
121
+ # Extract category name (part before the dot, without leading underscore)
122
+ if item_name.startswith('_') and '.' in item_name:
123
+ category = item_name[1:].split('.')[0]
124
+ self.categories.add(category)
125
+
126
+ if in_loop:
127
+ # In a loop: item with no value is a loop header; item with value ends the loop
128
+ if value is None:
129
+ current_loop_items.append((item_name, line_num))
130
+ expected_columns = len(current_loop_items)
131
+ else:
132
+ # Item name with value - this ends the loop, finish any partial row
133
+ if partial_row_values and current_loop_items:
134
+ self._assign_loop_row(current_loop_items, partial_row_values, partial_row_line_nums)
135
+ in_loop = False
136
+ is_real_loop = False
137
+ current_loop_items = []
138
+ partial_row_values = []
139
+ partial_row_line_nums = []
140
+ expected_columns = 0
141
+ # Process this as a regular item
142
+ if item_name not in self.items:
143
+ self.items[item_name] = []
144
+ # Strip quotes and whitespace from value
145
+ if value is not None:
146
+ value = value.strip("'\" ")
147
+ self.items[item_name].append(ItemValue(line_num, value, 0, 1)) # global_column_index = 0, local_column_index = 1 for non-loop items (item name is at 0, value is at 1)
148
+ # Record frame block (for duplicate category/item detection)
149
+ if category:
150
+ if category in closed_frame_categories:
151
+ # Re-appearing category (e.g. struct again after a loop) = new block, duplicate category
152
+ if current_frame_block is not None:
153
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
154
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
155
+ elif current_frame_block is not None and current_frame_block[1] == category:
156
+ # Same category: if this item already in block, we're re-starting category (duplicate)
157
+ if any(x[0] == item_name for x in current_frame_block[2]):
158
+ closed_frame_categories.add(category)
159
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
160
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
161
+ else:
162
+ current_frame_block[2].append((item_name, line_num))
163
+ elif current_frame_block is not None and current_frame_block[1] != category:
164
+ closed_frame_categories.add(current_frame_block[1])
165
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
166
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
167
+ elif current_frame_block is None:
168
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
169
+ else:
170
+ current_frame_block[2].append((item_name, line_num))
171
+ else:
172
+ # Not in a loop - regular item
173
+ if item_name not in self.items:
174
+ self.items[item_name] = []
175
+ if value is not None:
176
+ # Strip quotes and whitespace from value
177
+ value = value.strip("'\" ")
178
+ self.items[item_name].append(ItemValue(line_num, value, 0, 1)) # global_column_index = 0, local_column_index = 1 for non-loop items (item name is at 0, value is at 1)
179
+ # Record frame block (for duplicate category/item detection)
180
+ if category:
181
+ if category in closed_frame_categories:
182
+ if current_frame_block is not None:
183
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
184
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
185
+ elif current_frame_block is not None and current_frame_block[1] == category:
186
+ if any(x[0] == item_name for x in current_frame_block[2]):
187
+ closed_frame_categories.add(category)
188
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
189
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
190
+ else:
191
+ current_frame_block[2].append((item_name, line_num))
192
+ elif current_frame_block is not None and current_frame_block[1] != category:
193
+ closed_frame_categories.add(current_frame_block[1])
194
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
195
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
196
+ elif current_frame_block is None:
197
+ current_frame_block = [line_num, category, [(item_name, line_num)]]
198
+ else:
199
+ current_frame_block[2].append((item_name, line_num))
200
+ else:
201
+ # Item name without value - start a new pseudo-loop (for multi-line string handling)
202
+ current_loop_items = [(item_name, line_num)]
203
+ in_loop = True
204
+ is_real_loop = False # This is a pseudo-loop, not a real loop_ directive
205
+ loop_start_line = line_num
206
+ expected_columns = 1
207
+ partial_row_values = []
208
+ partial_row_line_nums = []
209
+
210
+ # Parse loop data
211
+ elif in_loop and current_loop_items:
212
+ # Check if this line starts a multi-line string (starts with ;)
213
+ if stripped.startswith(';'):
214
+ # Check if we're already in a multi-line string
215
+ if self._in_multiline_string:
216
+ # Check if this is the closing ; (must be exactly ;)
217
+ if stripped == ';':
218
+ # End of multi-line string - add accumulated content as a value
219
+ partial_row_values.append(self._multiline_content)
220
+ partial_row_line_nums.append(self._multiline_start_line)
221
+ self._multiline_content = ""
222
+ self._in_multiline_string = False
223
+ self._multiline_start_line = 0
224
+
225
+ # After closing multi-line string, check if row is complete
226
+ # Note: There might be more values on the next line(s)
227
+ if len(partial_row_values) >= expected_columns:
228
+ # Assign complete row
229
+ self._assign_loop_row(current_loop_items, partial_row_values[:expected_columns], partial_row_line_nums[:expected_columns])
230
+ # Keep any extra values for next row
231
+ partial_row_values = partial_row_values[expected_columns:]
232
+ partial_row_line_nums = partial_row_line_nums[expected_columns:]
233
+
234
+ # If this was a single-item pseudo-loop (not a real loop_) and row is complete,
235
+ # reset loop state to prevent next item from being incorrectly added to this loop
236
+ if not is_real_loop and expected_columns == 1 and len(current_loop_items) == 1 and len(partial_row_values) == 0:
237
+ in_loop = False
238
+ is_real_loop = False
239
+ current_loop_items = []
240
+ expected_columns = 0
241
+
242
+ # If this was a single-item pseudo-loop (not a real loop_),
243
+ # and we've completed the row, reset loop state
244
+ # This fixes the bug where consecutive single-value items with
245
+ # multi-line strings get incorrectly grouped together
246
+ if not is_real_loop and expected_columns == 1 and len(partial_row_values) == 0:
247
+ in_loop = False
248
+ is_real_loop = False
249
+ current_loop_items = []
250
+ expected_columns = 0
251
+ else:
252
+ # Continue accumulating multi-line content (line starts with ; but has more)
253
+ if self._multiline_content:
254
+ self._multiline_content += "\n"
255
+ # Don't include the leading ; in the content
256
+ self._multiline_content += stripped[1:] if len(stripped) > 1 else ""
257
+ else:
258
+ # Start of multi-line string
259
+ self._in_multiline_string = True
260
+ self._multiline_content = ""
261
+ self._multiline_start_line = line_num
262
+ # Check if there's content after the ; on the same line
263
+ if len(stripped) > 1:
264
+ # There's content on the same line after ;
265
+ remaining = stripped[1:].strip()
266
+ if remaining:
267
+ self._multiline_content = remaining
268
+ elif self._in_multiline_string:
269
+ # We're inside a multi-line string - accumulate content
270
+ if self._multiline_content:
271
+ self._multiline_content += "\n"
272
+ self._multiline_content += stripped
273
+ else:
274
+ # Regular data line in loop - parse values and accumulate
275
+ # This might be a continuation after a multi-line string closed
276
+ values = self._parse_loop_line(stripped)
277
+ partial_row_values.extend(values)
278
+ # All values from this line share the same line number
279
+ partial_row_line_nums.extend([line_num] * len(values))
280
+
281
+ # Check if we have a complete row (all columns filled)
282
+ if len(partial_row_values) >= expected_columns:
283
+ # Assign complete row
284
+ self._assign_loop_row(current_loop_items, partial_row_values[:expected_columns], partial_row_line_nums[:expected_columns])
285
+ # Keep any extra values for next row
286
+ partial_row_values = partial_row_values[expected_columns:]
287
+ partial_row_line_nums = partial_row_line_nums[expected_columns:]
288
+
289
+ # Finish any remaining partial row
290
+ if in_loop and current_loop_items and partial_row_values:
291
+ self._assign_loop_row(current_loop_items, partial_row_values, partial_row_line_nums)
292
+
293
+ # Record the last loop block (for duplicate category/item detection)
294
+ if in_loop and is_real_loop and current_loop_items:
295
+ first_item_name = current_loop_items[0][0]
296
+ cat = first_item_name[1:].split('.')[0] if (first_item_name.startswith('_') and '.' in first_item_name) else ''
297
+ self.loop_blocks.append((loop_start_line, cat, list(current_loop_items)))
298
+
299
+ # Close final frame block if any
300
+ if current_frame_block is not None:
301
+ self.frame_blocks.append((current_frame_block[0], current_frame_block[1], list(current_frame_block[2])))
302
+
303
+ # Clean up multi-line string state
304
+ if hasattr(self, '_in_multiline_string'):
305
+ delattr(self, '_in_multiline_string')
306
+ if hasattr(self, '_multiline_content'):
307
+ delattr(self, '_multiline_content')
308
+
309
+ return self
310
+
311
+ def _assign_loop_row(self, loop_items: List[Tuple[str, int]], values: List[str], line_nums: List[int]):
312
+ """Assign values from a complete loop row to their respective items.
313
+
314
+ Args:
315
+ loop_items: List of (item_name, header_line_num) tuples
316
+ values: List of values for this row
317
+ line_nums: List of line numbers, one per value (where each value appears)
318
+ """
319
+ # Calculate local column indices: count how many values appear on each line
320
+ line_value_counts = {} # Map line_num -> count of values on that line (so far)
321
+ local_column_indices = []
322
+
323
+ for i, line_num in enumerate(line_nums):
324
+ if line_num not in line_value_counts:
325
+ line_value_counts[line_num] = 0
326
+ local_column_indices.append(line_value_counts[line_num])
327
+ line_value_counts[line_num] += 1
328
+
329
+ for i, (item_name, _) in enumerate(loop_items):
330
+ if i < len(values):
331
+ # Extract category name
332
+ if item_name.startswith('_') and '.' in item_name:
333
+ category = item_name[1:].split('.')[0]
334
+ self.categories.add(category)
335
+ if item_name not in self.items:
336
+ self.items[item_name] = []
337
+ # Use the line number where this specific value appears
338
+ value_line_num = line_nums[i] if i < len(line_nums) else line_nums[-1] if line_nums else 1
339
+ # Global column index is the position in the row (0-based)
340
+ global_column_index = i
341
+ # Local column index is the position on this specific line (0-based)
342
+ local_column_index = local_column_indices[i] if i < len(local_column_indices) else 0
343
+ self.items[item_name].append(ItemValue(value_line_num, values[i], global_column_index, local_column_index))
344
+
345
+ def _parse_loop_line(self, line: str) -> List[str]:
346
+ """Parse a line of loop data, handling quoted strings."""
347
+ values = []
348
+ current = ""
349
+ in_quotes = False
350
+ quote_char = None
351
+
352
+ i = 0
353
+ while i < len(line):
354
+ char = line[i]
355
+
356
+ if not in_quotes:
357
+ if char in ["'", '"']:
358
+ in_quotes = True
359
+ quote_char = char
360
+ # Don't include the opening quote
361
+ elif char == ' ' or char == '\t':
362
+ if current.strip():
363
+ values.append(current.strip())
364
+ current = ""
365
+ else:
366
+ current += char
367
+ else:
368
+ if char == quote_char and (i == 0 or line[i-1] != '\\'):
369
+ # Closing quote - don't include it
370
+ in_quotes = False
371
+ quote_char = None
372
+ else:
373
+ current += char
374
+
375
+ i += 1
376
+
377
+ if current.strip():
378
+ values.append(current.strip())
379
+
380
+ return values
381
+
382
+ def get_category_rows(self, category: str) -> List[Dict[str, ItemValue]]:
383
+ """
384
+ Get all rows in a category as dictionaries.
385
+ Reconstructs rows by matching values at the same index across items.
386
+
387
+ Returns: List of dicts, each dict maps item_name -> ItemValue(line_num, value, global_col, local_col)
388
+ """
389
+ # Get all items in this category
390
+ category_items = {name: values for name, values in self.items.items()
391
+ if name.startswith('_') and name[1:].split('.')[0] == category}
392
+
393
+ if not category_items:
394
+ return []
395
+
396
+ # Find the maximum number of rows (max length of any item's value list)
397
+ max_rows = max(len(values) for values in category_items.values()) if category_items else 0
398
+
399
+ rows = []
400
+ for row_idx in range(max_rows):
401
+ row = {}
402
+ for item_name, values_list in category_items.items():
403
+ if row_idx < len(values_list):
404
+ row[item_name] = values_list[row_idx]
405
+ if row: # Only add non-empty rows
406
+ rows.append(row)
407
+
408
+ return rows