Mercurial > repos > recetox > target_screen
comparison target_screen.py @ 0:d4c2d5bc0524 draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/misc commit 94322884bede7ddb9f2a9166952dd0115bdb4e49
| author | recetox |
|---|---|
| date | Thu, 26 Sep 2024 13:03:05 +0000 |
| parents | |
| children | 6d51be3d7bb5 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:d4c2d5bc0524 |
|---|---|
| 1 import argparse | |
| 2 | |
| 3 import numpy as np | |
| 4 import pandas as pd | |
| 5 | |
| 6 | |
| 7 def mz_match(marker, peak, ppm): | |
| 8 return np.abs(marker - peak) <= ((peak + marker) / 2) * ppm * 1e-06 | |
| 9 | |
| 10 | |
| 11 def rt_match(marker, peak, tol): | |
| 12 return np.abs(marker - peak) <= tol | |
| 13 | |
| 14 | |
| 15 def find_matches(peaks, markers, ppm, rt_tol): | |
| 16 # Create a meshgrid of all combinations of mz and rt values | |
| 17 marker_mz = markers['mz'].values[:, np.newaxis] | |
| 18 peak_mz = peaks['mz'].values | |
| 19 marker_rt = markers['rt'].values[:, np.newaxis] | |
| 20 peak_rt = peaks['rt'].values | |
| 21 | |
| 22 # Calculate mz and rt matches | |
| 23 mz_matches = mz_match(marker_mz, peak_mz, ppm) | |
| 24 rt_matches = rt_match(marker_rt, peak_rt, rt_tol) | |
| 25 | |
| 26 # Find the indices where both mz and rt match | |
| 27 match_indices = np.where(mz_matches & rt_matches) | |
| 28 | |
| 29 # Create a DataFrame of hits | |
| 30 matched_markers = markers.iloc[match_indices[0]].reset_index(drop=True) | |
| 31 matched_peaks = peaks.iloc[match_indices[1]].reset_index(drop=True) | |
| 32 hits = pd.concat([matched_markers[['formula']].reset_index(drop=True), matched_peaks], axis=1) | |
| 33 | |
| 34 # Calculate mz and rt differences | |
| 35 hits['mz_diff'] = np.abs(matched_markers['mz'].values - matched_peaks['mz'].values) | |
| 36 hits['rt_diff'] = np.abs(matched_markers['rt'].values - matched_peaks['rt'].values) | |
| 37 | |
| 38 return hits | |
| 39 | |
| 40 | |
| 41 def main(): | |
| 42 parser = argparse.ArgumentParser(description='Find matches between peaks and markers.') | |
| 43 parser.add_argument('--peaks', required=True, help='Path to the peaks parquet file.') | |
| 44 parser.add_argument('--markers', required=True, help='Path to the markers CSV file.') | |
| 45 parser.add_argument('--output', required=True, help='Path to the output TSV file.') | |
| 46 parser.add_argument('--ppm', type=int, default=5, help='PPM tolerance for mz matching.') | |
| 47 parser.add_argument('--rt_tol', type=int, default=10, help='RT tolerance for rt matching.') | |
| 48 args = parser.parse_args() | |
| 49 | |
| 50 peaks = pd.read_parquet(args.peaks) | |
| 51 markers = pd.read_csv(args.markers, sep='\t') | |
| 52 | |
| 53 hits = find_matches(peaks, markers, args.ppm, args.rt_tol) | |
| 54 | |
| 55 hits.to_csv(args.output, sep='\t', index=False) | |
| 56 | |
| 57 | |
| 58 if __name__ == "__main__": | |
| 59 main() |
