diff COBRAxy/utils/general_utils.py @ 427:4a385fdb9e58 draft

Uploaded
author francesco_lapi
date Wed, 10 Sep 2025 11:38:08 +0000
parents ed2c1f9e20ba
children a6e45049c1b9
line wrap: on
line diff
--- a/COBRAxy/utils/general_utils.py	Wed Sep 10 09:25:32 2025 +0000
+++ b/COBRAxy/utils/general_utils.py	Wed Sep 10 11:38:08 2025 +0000
@@ -505,6 +505,39 @@
     """
     with open(path.show(), "r", newline = "") as fd: return list(csv.reader(fd, delimiter = delimiter))[skipHeader:]
 
+def findIdxByName(header: List[str], name: str, colName="name") -> Optional[int]:
+    """
+    Find the indices of the 'ReactionID' column and a user-specified column name
+    within the header row of a tabular file.
+
+    Args:
+        header (List[str]): The header row, as a list of column names.
+        name (str): The name of the column to look for (e.g. 'GPR').
+        colName (str, optional): Label used in error messages for clarity. Defaults to "name".
+
+    Returns:
+        Tuple[int, int]: A tuple containing:
+            - The index of the 'ReactionID' column.
+            - The index of the requested column `name`.
+
+    Raises:
+        ValueError: If 'ReactionID' or the requested column `name` is not found in the header.
+
+    Notes:
+        Both 'ReactionID' and the requested column are mandatory for downstream processing.
+    """
+
+    col_index = {col_name: idx for idx, col_name in enumerate(header)}
+
+    if name not in col_index or "ReactionID" not in col_index:
+        raise ValueError(f"Tabular file must contain 'ReactionID' and {name} columns.")
+
+    id_idx = col_index["ReactionID"]
+    idx_gpr = col_index[name]
+
+    return id_idx, idx_gpr
+
+
 def readSvg(path :FilePath, customErr :Optional[Exception] = None) -> ET.ElementTree:
     """
     Reads the contents of a .svg file, which needs to exist at the given path.