Skip to content

Commit ff5552e

Browse files
committed
fixing mqc output table
1 parent 158fc77 commit ff5552e

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

bin/utils/module_data_processing.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,31 @@ def get_anchor(mqc: object, module: str) -> str | None:
179179
return m.anchor
180180
return None
181181

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+
182207
def create_constraint_summary(df_constraint: pd.DataFrame, file_columns: List[Union[str, Dict[str, str]]]) -> pd.DataFrame:
183208
"""
184209
Create a summary table for the constraint data.
@@ -273,10 +298,8 @@ def create_constraint_summary(df_constraint: pd.DataFrame, file_columns: List[Un
273298
# Convert to wide format
274299
df_wide = df_long.pivot(index=["sample"], columns="grouped variable", values="Value")
275300

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)
280303
df_wide = df_wide[ordered_columns]
281304

282305
df_wide.reset_index(inplace=True)
@@ -439,6 +462,7 @@ def get_read_suffix(namespace: str, title: str) -> str | None:
439462
Returns:
440463
str | None: The read suffix if found, None otherwise
441464
"""
465+
title = title.strip()
442466
if title not in READ_DECLARATION:
443467
return None
444468

0 commit comments

Comments
 (0)