|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Exporter of MS Annika CSM Results to xiFDR 2.2.1 input format |
| 4 | +# 2024 (c) Micha Johannes Birklbauer |
| 5 | +# https://github.com/michabirklbauer/ |
| 6 | +# micha.birklbauer@gmail.com |
| 7 | + |
| 8 | +import argparse |
| 9 | +import pandas as pd |
| 10 | + |
| 11 | +__version = "1.0.2" |
| 12 | +__date = "202405017" |
| 13 | + |
| 14 | +""" |
| 15 | +DESCRIPTION: |
| 16 | +A script to export MS Annika CSM results (.xlsx) to a xiFDR input file (.csv). |
| 17 | +This version only works for xiFDR 2.2.1 and later! |
| 18 | +CSMs should be unfiltered, therefore include decoys and not be validated for any |
| 19 | +FDR. |
| 20 | +USAGE: |
| 21 | +xiFdr2.2.1Exporter_msannika.py f [f] |
| 22 | + [-o OUTPUT] |
| 23 | + [-h] |
| 24 | + [--version] |
| 25 | +positional arguments: |
| 26 | + f Crosslink-Spectrum-Matches (CSMs) exported from |
| 27 | + MS Annika in Microsoft Excel (.xlsx) format. |
| 28 | +optional arguments: |
| 29 | + -o OUTPUT, --output OUTPUT |
| 30 | + Prefix of the output file. |
| 31 | + -h, --help show this help message and exit |
| 32 | + --version show program's version number and exit |
| 33 | +""" |
| 34 | + |
| 35 | +# Exporter class with constructor that takes one MS Annika CSM result file as |
| 36 | +# input. CSMs should not be in any way filtered and exported to Microsoft Excel |
| 37 | +# .xlsx format from Proteome Discoverer. |
| 38 | +class MSAnnika_Exporter: |
| 39 | + |
| 40 | + def __init__(self, input_file: str): |
| 41 | + self.input_file = input_file |
| 42 | + |
| 43 | + # static method to generate pandas dataframe of xiFDR export without class |
| 44 | + # instance. Takes the file name of the CSM file as input. |
| 45 | + @staticmethod |
| 46 | + def generate_df(input_file: str) -> pd.DataFrame: |
| 47 | + |
| 48 | + df = pd.read_excel(input_file) |
| 49 | + df.rename(columns = {"Spectrum File": "run", |
| 50 | + "First Scan": "scan", |
| 51 | + "Sequence A": "peptide1", |
| 52 | + "Sequence B": "peptide2", |
| 53 | + "Crosslinker Position A": "peptide link 1", |
| 54 | + "Crosslinker Position B": "peptide link 2", |
| 55 | + "Charge": "precursor charge", |
| 56 | + "Combined Score": "score", |
| 57 | + "Score Alpha": "peptide1 score", |
| 58 | + "Score Beta": "peptide2 score", |
| 59 | + "Accession A": "accession1", |
| 60 | + "Accession B": "accession2", |
| 61 | + "A in protein": "peptide position 1", |
| 62 | + "B in protein": "peptide position 2"}, |
| 63 | + inplace = True, |
| 64 | + errors = "raise") |
| 65 | + df["is decoy 1"] = df["Alpha T/D"].apply(lambda x: "false" if "t" in str(x).lower() else "true") |
| 66 | + df["is decoy 2"] = df["Beta T/D"].apply(lambda x: "false" if "t" in str(x).lower() else "true") |
| 67 | + df["peptide position 1"] = df["peptide position 1"].apply(lambda x: ";".join([str(int(y) + 1) for y in str(x).split(";")])) |
| 68 | + df["peptide position 2"] = df["peptide position 2"].apply(lambda x: ";".join([str(int(y) + 1) for y in str(x).split(";")])) |
| 69 | + |
| 70 | + return df |
| 71 | + |
| 72 | + # classmethod implementation of the static generate_df |
| 73 | + def __generate_csv_df(self) -> pd.DataFrame: |
| 74 | + return self.generate_df(self.input_file) |
| 75 | + |
| 76 | + # export function, takes one argument "output_file" which sets the prefix |
| 77 | + # of generated output file |
| 78 | + def export(self, output_file: str = None) -> pd.DataFrame: |
| 79 | + csv = self.__generate_csv_df() |
| 80 | + |
| 81 | + if output_file is None: |
| 82 | + output_file = ".".join(self.input_file.split(".")[:-1]) |
| 83 | + |
| 84 | + csv.to_csv(output_file + "_xiFDR.csv", index = False) |
| 85 | + |
| 86 | + return csv |
| 87 | + |
| 88 | +# initialize exporter and export xiFDR csv file |
| 89 | +def main() -> None: |
| 90 | + parser = argparse.ArgumentParser() |
| 91 | + parser.add_argument(metavar = "f", |
| 92 | + dest = "file", |
| 93 | + help = "Name/Path of the MS Annika CSM result file (in .xlsx format) to process.", |
| 94 | + type = str, |
| 95 | + nargs = 1) |
| 96 | + parser.add_argument("-o", "--output", |
| 97 | + dest = "output", |
| 98 | + default = None, |
| 99 | + help = "Prefix of the output file.", |
| 100 | + type = str) |
| 101 | + parser.add_argument("--version", |
| 102 | + action = "version", |
| 103 | + version = __version) |
| 104 | + args = parser.parse_args() |
| 105 | + |
| 106 | + exporter = MSAnnika_Exporter(args.file[0]) |
| 107 | + |
| 108 | + exporter.export(args.output) |
| 109 | + |
| 110 | +if __name__ == "__main__": |
| 111 | + main() |
0 commit comments