Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48 more linting #50

Merged
merged 6 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions alpharaw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@


def register_readers():
from .ms_data_base import ms_reader_provider # noqa: F401 # TODO remove import side effect
from .legacy_msdata import mgf # noqa: F401 # TODO remove import side effect
from .ms_data_base import (
ms_reader_provider, # noqa: F401 # TODO remove import side effect
)
from .mzml import MzMLReader # noqa: F401 # TODO remove import side effect
from .wrappers import alphapept_wrapper # noqa: F401 # TODO remove import side effect
from .wrappers import (
alphapept_wrapper, # noqa: F401 # TODO remove import side effect
)

try:
from .sciex import SciexWiffData # noqa: F401 # TODO remove import side effect
from .thermo import ThermoRawData # noqa: F401 # TODO remove import side effect
from .thermo import (
ThermoRawData, # noqa: F401 # TODO remove import side effect
)
except (RuntimeError, ImportError):
print("[WARN] pythonnet is not installed")

Expand Down
31 changes: 17 additions & 14 deletions alpharaw/cli.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import click
import os

import click

import alpharaw
from alpharaw.ms_data_base import ms_reader_provider
from alpharaw.legacy_msdata import mgf # noqa: F401 # TODO remove import side effect
from alpharaw.ms_data_base import ms_reader_provider
from alpharaw.mzml import MzMLReader # noqa: F401 # TODO remove import side effect
from alpharaw.wrappers import alphapept_wrapper # noqa: F401 # TODO remove import side effect
from alpharaw.wrappers import (
alphapept_wrapper, # noqa: F401 # TODO remove import side effect
)

try:
from alpharaw.sciex import SciexWiffData # noqa: F401 # TODO remove import side effect
from alpharaw.thermo import ThermoRawData # noqa: F401 # TODO remove import side effect
from alpharaw.sciex import (
SciexWiffData, # noqa: F401 # TODO remove import side effect
)
from alpharaw.thermo import (
ThermoRawData, # noqa: F401 # TODO remove import side effect
)
except (RuntimeError, ImportError):
print("[WARN] pythonnet is not installed")

Expand All @@ -24,22 +31,18 @@
@click.version_option(alpharaw.__version__, "-v", "--version")
def run(ctx, **kwargs):
click.echo(
r"""
rf"""
___ __ __ ___
/ _ | / /__ / / ___ _/ _ \___ __ __
/ __ |/ / _ \/ _ \/ _ `/ , _/ _ `/ |/|/ /
/_/ |_/_/ .__/_//_/\_,_/_/|_|\_,_/|__,__/
/_/
....................................................
.{version}.
.{url}.
.{license}.
.{alpharaw.__version__.center(50)}.
.{alpharaw.__github__.center(50)}.
.{alpharaw.__license__.center(50)}.
....................................................
""".format(
version=alpharaw.__version__.center(50),
url=alpharaw.__github__.center(50),
license=alpharaw.__license__.center(50),
)
"""
)
if ctx.invoked_subcommand is None:
click.echo(run.get_help(ctx))
Expand Down
7 changes: 3 additions & 4 deletions alpharaw/dia/normal_dia.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import numpy as np
import typing

from collections import defaultdict

from alpharaw.ms_data_base import MSData_Base
import numpy as np
from alphatims.bruker import TimsTOF

from alpharaw.ms_data_base import MSData_Base
from alpharaw.utils.df_processing import remove_unused_peaks
from alpharaw.utils.timstof import convert_to_alphatims
from alphatims.bruker import TimsTOF


class NormalDIAGrouper:
Expand Down
9 changes: 4 additions & 5 deletions alpharaw/feature/centroids.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
from numba import njit
from alphatims.utils import threadpool
from numba import njit


@threadpool
Expand Down Expand Up @@ -47,10 +47,9 @@ def connect_centroids_unidirection(
mz_sum = mz1 + mz2
delta = 2 * 1e6 * abs(diff) / mz_sum

if delta < centroid_tol:
if scores[x, i, gap] > delta:
scores[x, i, gap] = delta
connections[x, i, gap] = (connections.shape[1] * y) + j
if delta < centroid_tol and scores[x, i, gap] > delta:
scores[x, i, gap] = delta
connections[x, i, gap] = (connections.shape[1] * y) + j

if diff > 0:
j += 1
Expand Down
11 changes: 5 additions & 6 deletions alpharaw/feature/chem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import numpy as np
from numba import int32, float32, float64, njit, types
from numba import float32, float64, int32, njit, types
from numba.experimental import jitclass
from numba.typed import Dict

Expand Down Expand Up @@ -242,7 +243,7 @@ def dict_to_dist(counted_AA: Dict, isotopes: Dict) -> IsotopeDistribution:
"""

dist = IsotopeDistribution()
for AA in counted_AA.keys():
for AA in counted_AA:
x = IsotopeDistribution()
x.add(isotopes[AA])
x = x.mult(counted_AA[AA])
Expand Down Expand Up @@ -318,7 +319,7 @@ def get_average_formula(
final_mass = 0

# Calculate integral mnumbers of atoms
for AA in averagine_aa.keys():
for AA in averagine_aa:
counted_AA[AA] = int(np.round(averagine_units * averagine_aa[AA]))
final_mass += counted_AA[AA] * isotopes[AA].m0

Expand All @@ -334,9 +335,7 @@ def test_get_average_formula():
average_formula = get_average_formula(
molecule_mass, averagine_aa, isotopes, sulphur=True
)
mass = np.sum(
[average_formula[AA] * isotopes[AA].m0 for AA in average_formula.keys()]
)
mass = np.sum([average_formula[AA] * isotopes[AA].m0 for AA in average_formula])
assert np.abs(mass - molecule_mass) < isotopes["H"].m0


Expand Down
18 changes: 9 additions & 9 deletions alpharaw/feature/finding.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import pandas as pd
import logging

import numpy as np
import pandas as pd

from alpharaw.feature.chem import averagine_aa, isotopes, maximum_offset
from alpharaw.feature.hills import (
extract_hills,
remove_duplicate_hills,
split_hills,
filter_hills,
get_hill_data,
remove_duplicate_hills,
split_hills,
)
from alpharaw.feature.isotope_pattern import (
get_pre_isotope_patterns,
get_isotope_patterns,
feature_finder_report,
get_isotope_patterns,
get_pre_isotope_patterns,
get_stats,
)

import logging


def find(
spectrum_df,
Expand Down Expand Up @@ -109,7 +109,7 @@ def find(
iso_mass_range=iso_mass_range,
cc_cutoff=iso_corr_min,
)
logging.info("Found {:,} pre isotope patterns.".format(len(pre_isotope_patterns)))
logging.info(f"Found {len(pre_isotope_patterns):,} pre isotope patterns.")

isotope_patterns, iso_idx, isotope_charges = get_isotope_patterns(
pre_isotope_patterns,
Expand All @@ -129,7 +129,7 @@ def find(
iso_split_level=iso_split_level,
callback=None,
)
logging.info("Extracted {:,} isotope patterns.".format(len(isotope_charges)))
logging.info(f"Extracted {len(isotope_charges):,} isotope patterns.")

feature_df, lookup_idx = feature_finder_report(
query_data,
Expand Down
12 changes: 5 additions & 7 deletions alpharaw/feature/hills.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
from alphatims.utils import threadpool
from numba import njit

from alpharaw.feature.centroids import connect_centroids


Expand Down Expand Up @@ -198,8 +199,8 @@ def remove_duplicate_hills(hill_ptrs, hill_data, path_node_cnt):
hill_ptrs_new = np.zeros_like(hill_ptrs)
hill_data_new = np.zeros_like(hill_data)

for i, _ in enumerate(np.argsort(path_node_cnt)[::-1]):
s, e = hill_ptrs[_], hill_ptrs[_ + 1]
for p in np.argsort(path_node_cnt)[::-1]:
s, e = hill_ptrs[p], hill_ptrs[p + 1]

point_idx = hill_data[s:e]

Expand Down Expand Up @@ -516,17 +517,14 @@ def hill_stats(
rt_min = rt_[rt_idx[idx_]].min()
rt_max = rt_[rt_idx[idx_]].max()

if len(idx_) > hill_nboot_max:
bootsize = hill_nboot_max
else:
bootsize = len(idx_)
bootsize = hill_nboot_max if len(idx_) > hill_nboot_max else len(idx_)

averages = np.zeros(hill_nboot)
average = 0

for i in range(hill_nboot):
boot = np.random.choice(len(int_), bootsize, replace=True)
boot_mz = np.sum((mz_[boot] * int_[boot])) / np.sum(int_[boot])
boot_mz = np.sum(mz_[boot] * int_[boot]) / np.sum(int_[boot])
averages[i] = boot_mz
average += boot_mz

Expand Down
65 changes: 28 additions & 37 deletions alpharaw/feature/isotope_pattern.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
from numba import njit
from numba.typed import List
import numpy as np
from alphatims.utils import threadpool
from typing import Callable, Union

import numpy as np
import pandas as pd
from numba.typed import Dict
from alphatims.utils import threadpool
from numba import njit
from numba.typed import Dict, List

# TODO: Move hardcoded constants

from alpharaw.feature.chem import (
mass_to_dist,
DELTA_M,
DELTA_S,
M_PROTON,
mass_to_dist,
)


Expand Down Expand Up @@ -110,9 +109,9 @@ def correlate(
min_one, max_one = scans_[0], scans_[-1]
min_two, max_two = scans_2[0], scans_2[-1]

if min_one + 3 > max_two: # at least an overlap of 3 elements
corr = 0
elif min_two + 3 > max_one:
if (
min_one + 3 > max_two or min_two + 3 > max_one
): # at least an overlap of 3 elements
corr = 0
else:
min_s = min(min_one, min_two)
Expand Down Expand Up @@ -401,21 +400,22 @@ def grow(
int_2 = int_data[idx_]
scans_2 = scan_idx[idx_]

if correlate(scans_, scans_2, int_, int_2) > cc_cutoff:
if check_isotope_pattern_directed(
mass1,
mass2,
delta_mass1,
delta_mass2,
charge,
-direction * index,
iso_mass_range,
):
if direction == 1:
trail.append(y)
else:
trail.insert(0, y)
index += 1 # Greedy matching: Only one edge for a specific distance, will not affect the following matches
if correlate(
scans_, scans_2, int_, int_2
) > cc_cutoff and check_isotope_pattern_directed(
mass1,
mass2,
delta_mass1,
delta_mass2,
charge,
-direction * index,
iso_mass_range,
):
if direction == 1:
trail.append(y)
else:
trail.insert(0, y)
index += 1 # Greedy matching: Only one edge for a specific distance, will not affect the following matches

delta_mass = np.abs(mass1 - mass2)

Expand Down Expand Up @@ -678,15 +678,9 @@ def truncate(
right_minima = minima[minima > seedpos]

# If the minimum is smaller than the seed
if len(left_minima) > 0:
minpos = left_minima[-1]
else:
minpos = 0
minpos = left_minima[-1] if len(left_minima) > 0 else 0

if len(right_minima) > 0:
maxpos = right_minima[0]
else:
maxpos = len(array)
maxpos = right_minima[0] if len(right_minima) > 0 else len(array)

array = array[minpos : maxpos + 1]

Expand Down Expand Up @@ -1013,10 +1007,7 @@ def get_isotope_patterns(
cc_cutoff,
iso_split_level,
)
if isotope_pattern is None:
length = 0
else:
length = len(isotope_pattern)
length = 0 if isotope_pattern is None else len(isotope_pattern)

if length > 1:
isotope_charges.append(isotope_charge)
Expand Down
Loading
Loading