-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python | ||
# Written by Jonas Scheid under the MIT license | ||
|
||
import logging | ||
import csv | ||
import argparse | ||
import matplotlib.pyplot as plt | ||
|
||
import pandas as pd | ||
import pyopenms as oms | ||
|
||
# Setup logging | ||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s") | ||
|
||
|
||
def parse_arguments(): | ||
parser = argparse.ArgumentParser(description='Exctract TICs of MS1 Spectra') | ||
parser.add_argument('-in','--input', type=str, help='Path to the spectrum file') | ||
parser.add_argument('-out', '--output', type=str, help='Path to the output CSV file containing RT and TIC of Precursors') | ||
return parser.parse_args() | ||
|
||
def main(): | ||
args = parse_arguments() | ||
input_file = args.input | ||
output_file = args.output | ||
|
||
# Load the mzML file | ||
logging.info(f'Loading file: {input_file}') | ||
exp = oms.MSExperiment() | ||
mzml_file = oms.MzMLFile() | ||
mzml_file.load(input_file, exp) | ||
|
||
# Get RT and Spectrum TIC of MS1 Spectra | ||
chromatogram = [(spectrum.getRT() / 60 , spectrum.calculateTIC()) for spectrum in exp.getSpectra() if spectrum.getMSLevel() == 1] | ||
logging.info(f'Found {len(chromatogram)} MS1 Spectra') | ||
logging.info(f'RT range: {round(chromatogram[0][0],2)} - {round(chromatogram[-1][0],2)} [min]') | ||
# Create pandas df | ||
chromatogram_df = pd.DataFrame(chromatogram, columns=['RT', 'TIC']) | ||
# bin data into minutes and take the mean of the TIC | ||
chromatogram_df = chromatogram_df.groupby('RT').mean().reset_index() | ||
# Add RT=0 and Intensity=0 to start and end of chromatogram_df | ||
start = pd.DataFrame([{'RT': 0, 'TIC': 0}]) | ||
end = pd.DataFrame([{'RT': chromatogram_df['RT'].max(), 'TIC': 0}]) | ||
# Concatenate the DataFrames | ||
chromatogram_df = pd.concat([start, chromatogram_df, end], ignore_index=True) | ||
|
||
# Write to csv | ||
chromatogram_df.to_csv(output_file, index=False, header=False) | ||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
process PYOPENMS_CHROMATOGRAMEXTRACTOR { | ||
tag "$meta.id" | ||
label 'process_single' | ||
|
||
conda "bioconda::pyopenms=3.1.0" | ||
container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ? | ||
'https://depot.galaxyproject.org/singularity/pyopenms:3.1.0--py311h9b8898c_0' : | ||
'biocontainers/pyopenms:3.1.0--py311h9b8898c_0' }" | ||
|
||
input: | ||
tuple val(meta), path(mzml) | ||
|
||
output: | ||
tuple val(meta), path("*.csv") , emit: csv | ||
path "versions.yml" , emit: versions | ||
|
||
when: | ||
task.ext.when == null || task.ext.when | ||
|
||
script: | ||
def prefix = task.ext.prefix ?: "${mzml.baseName}" | ||
def args = task.ext.args ?: '' | ||
|
||
""" | ||
chromatogram_extractor.py \\ | ||
-in $mzml \\ | ||
-out ${prefix}_chrom.csv \\ | ||
cat <<-END_VERSIONS > versions.yml | ||
"${task.process}": | ||
pyOpenMS: \$(pip show pyopenms | grep Version | cut -d ' ' -f 2) | ||
END_VERSIONS | ||
""" | ||
|
||
stub: | ||
def args = task.ext.args ?: '' | ||
def prefix = task.ext.prefix ?: "${mzml.baseName}" | ||
|
||
""" | ||
touch ${prefix}_chrom.csv | ||
cat <<-END_VERSIONS > versions.yml | ||
"${task.process}": | ||
pyOpenMS: \$(pip show pyopenms | grep Version | cut -d ' ' -f 2) | ||
END_VERSIONS | ||
""" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.