@@ -179,6 +179,31 @@ def get_anchor(mqc: object, module: str) -> str | None:
179
179
return m .anchor
180
180
return None
181
181
182
+ def order_columns_by_idgroup (df_wide : pd .DataFrame , original_columns : List [str ]) -> List [str ]:
183
+ """
184
+ Order columns by species-segment group (idgroup) while maintaining original column order within each group.
185
+
186
+ Args:
187
+ df_wide (pd.DataFrame): The wide-format DataFrame with columns formatted as "idgroup - column"
188
+ original_columns (List[str]): The original column order to maintain within each idgroup
189
+
190
+ Returns:
191
+ List[str]: Ordered list of column names
192
+ """
193
+ # Get all unique species-segment combinations (idgroups)
194
+ idgroups = sorted (set ([col .split (' - ' )[0 ] for col in df_wide .columns ]))
195
+
196
+ # Create an ordered column list that groups by species-segment while maintaining original column order for each group
197
+ ordered_columns = []
198
+ for idgroup in idgroups :
199
+ # For each species-segment group, add columns in the original order
200
+ for col in original_columns :
201
+ group_col = f"{ idgroup } - { col } "
202
+ if group_col in df_wide .columns :
203
+ ordered_columns .append (group_col )
204
+
205
+ return ordered_columns
206
+
182
207
def create_constraint_summary (df_constraint : pd .DataFrame , file_columns : List [Union [str , Dict [str , str ]]]) -> pd .DataFrame :
183
208
"""
184
209
Create a summary table for the constraint data.
@@ -273,10 +298,8 @@ def create_constraint_summary(df_constraint: pd.DataFrame, file_columns: List[Un
273
298
# Convert to wide format
274
299
df_wide = df_long .pivot (index = ["sample" ], columns = "grouped variable" , values = "Value" )
275
300
276
- # Reorder columns based on original order
277
- ordered_columns = []
278
- for col in original_columns :
279
- ordered_columns .extend ([c for c in df_wide .columns if c .endswith (f" - { col } " )])
301
+ # Reorder the columns by species-segment groups while maintaining original column order
302
+ ordered_columns = order_columns_by_idgroup (df_wide , original_columns )
280
303
df_wide = df_wide [ordered_columns ]
281
304
282
305
df_wide .reset_index (inplace = True )
@@ -439,6 +462,7 @@ def get_read_suffix(namespace: str, title: str) -> str | None:
439
462
Returns:
440
463
str | None: The read suffix if found, None otherwise
441
464
"""
465
+ title = title .strip ()
442
466
if title not in READ_DECLARATION :
443
467
return None
444
468
0 commit comments