Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
haeussma committed Aug 13, 2024
1 parent dce445c commit adbb80e
Show file tree
Hide file tree
Showing 15 changed files with 1,103 additions and 283 deletions.
7 changes: 0 additions & 7 deletions chromatopy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
import os

from .core.chromatogram import Chromatogram
from .core.measurement import Measurement
from .core.peak import Peak
from .core.role import Role
from .core.signaltype import SignalType
from .core.standard import Standard
22 changes: 14 additions & 8 deletions chromatopy/core/chromatogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .signaltype import SignalType

if TYPE_CHECKING:
from chromatopy.tools.species import Species
from chromatopy.tools.molecule import Species


class Chromatogram(
Expand Down Expand Up @@ -231,6 +231,8 @@ def fit(
# self.peaks = self._map_hplcpy_peaks(fitter.peaks)
self.processed_signal = np.sum(fitter.unmixed_chromatograms, axis=1)

print(fitter.peaks.to_dict(orient="records"))

for record in fitter.peaks.to_dict(orient="records"):
for species in molecules:
tol_interval = (
Expand All @@ -249,13 +251,17 @@ def fit(
height=record["amplitude"],
)
self.peaks.append(peak)
else:
continue

print(
f"Assigned peak at [bold]{record['retention_time']} min[/bold] to"
f" [green]{species.name}[/green]"
)
print(
f"Assigned peak at [bold]{record['retention_time']} min[/bold] to"
f" [green]{species.name}[/green]"
)

self.add_to_peaks(
retention_time=record["retention_time"],
area=record["area"],
height=record["amplitude"],
)

def _map_hplcpy_peaks(self, fitter_peaks: pd.DataFrame) -> List[Peak]:
peaks = fitter_peaks.to_dict(orient="records")
Expand All @@ -278,7 +284,7 @@ def to_dataframe(self) -> pd.DataFrame:
"time": self.times,
"signal": self.signals,
}
)
).dropna()

def visualize(self) -> go.Figure:
"""
Expand Down
1 change: 0 additions & 1 deletion chromatopy/readers/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .chromreader import ChromReader
65 changes: 40 additions & 25 deletions chromatopy/readers/chromeleon.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import re

import pandas as pd

from chromatopy.core import Chromatogram, Measurement
from chromatopy.model import Chromatogram, Measurement
from chromatopy.units import h, min, ul


def read_chromeleon_file(path: str):
def read_chromeleon_file(file_path: str):
with open(file_path, "r", encoding="ISO-8859-1") as file:
content = file.read()

Expand All @@ -22,27 +25,54 @@ def read_chromeleon_file(path: str):

content_dict["Raw Data"] = transpose_data(content_dict["Raw Data"])

return content_dict
measurement = map_measurement(content_dict, file_path)

return measurement


def map_measurement(content: dict) -> Measurement:
def map_measurement(content: dict, file_name: str) -> Measurement:
chromatogram = Chromatogram(
id=file_name,
wavelength=int(content["Signal Parameter Information"][1][1].split(" ")[0]),
times=content["Raw Data"]["time"],
signals=content["Raw Data"]["value"],
time_unit="min",
time_unit=min,
)

reaction_time, unit = extract_reaction_time(file_name)

return Measurement(
id=content["Sample Information"][2][1],
chromatograms=[chromatogram],
injection_volume=float(content["Sample Information"][13][1].replace(",", ".")),
injection_volume_unit="µL",
injection_volume_unit=ul,
signal_parameter_information=content["Signal Parameter Information"],
dilution_factor=float(content["Sample Information"][14][1].replace(",", ".")),
reaction_time=reaction_time,
time_unit=unit,
)


def extract_reaction_time(file_name: str) -> tuple[float, str]:
pattern = r"\b(\d+(?:\.\d+)?)\s*(h|min)\b"

matches = re.findall(pattern, file_name)

if len(matches) == 0:
return None, None

reaction_time, unit_str = matches[0]

if unit_str == "h":
unit = h
elif unit_str == "min":
unit = min
else:
raise ValueError(f"Unit '{unit_str}' not recognized")

return reaction_time, unit


def transpose_data(data: list) -> pd.DataFrame:
data = data
df = pd.DataFrame(data[1:], columns=["time", "step", "value"])
Expand All @@ -54,23 +84,8 @@ def transpose_data(data: list) -> pd.DataFrame:
return df


if __name__ == "__main__":
dir_path = "/Users/max/Documents/jan-niklas/MjNK/Standards"
file_path = (
"/Users/max/Documents/jan-niklas/MjNK/Standards/Adenosine Stadards_ 0.5 mM.txt"
)

import matplotlib.pyplot as plt

content = read_chromeleon_file(file_path)
print(content.keys())
print(content["Raw Data"][0:3])

measurement = map_measurement(content)
# if __name__ == "__main__":
# dir_path = "/Users/max/Documents/jan-niklas/MjNK/Standards"
# file_path = "/Users/max/Documents/jan-niklas/MjNK/adenosine_std/Adenosine Stadards_ 0.5 mM.txt"

print(measurement)

plt.scatter(
measurement.chromatograms[0].times, measurement.chromatograms[0].signals
)
plt.show()
# content = read_chromeleon_file(file_path)
Loading

0 comments on commit adbb80e

Please sign in to comment.