annotate mztab_reader.py @ 1:dc31e153fe6c draft default tip

"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
author galaxyp
date Fri, 15 Jan 2021 21:09:34 +0000
parents a46d857e25c2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
1 #!/usr/bin/env python
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
2
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
3 import argparse
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
4 import os
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
5
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
6 import pandas as pd
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
7 from pyteomics.mztab import MzTab
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
8
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
9
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
10 def read_mztab(input_path, output_path):
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
11 """
1
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
12 Read and process mztab file
0
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
13 """
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
14 mztab = MzTab(input_path)
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
15 mtd = pd.DataFrame.from_dict(mztab.metadata, orient='index')
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
16 mtd.to_csv(os.path.join(output_path, "mtd.tsv"), sep="\t")
1
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
17 for name, tab in mztab:
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
18 if not tab.empty:
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
19 tab.to_csv(os.path.join(output_path, f"{name.lower()}.tsv"), sep="\t")
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
20 else:
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
21 with open(os.path.join(output_path, f"{name.lower()}.tsv"), "w"):
dc31e153fe6c "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 58fe8232b7f4659b37e8725197d63e81efae0683"
galaxyp
parents: 0
diff changeset
22 pass
0
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
23
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
24
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
25 if __name__ == "__main__":
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
26 # Create the parser
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
27 my_parser = argparse.ArgumentParser(description='List of paths')
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
28 # Add the arguments
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
29 my_parser.add_argument('--path_in',
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
30 metavar='path',
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
31 type=str,
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
32 required=True,
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
33 help='the path of input .mztab file')
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
34 my_parser.add_argument('--path_out',
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
35 metavar='path',
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
36 type=str,
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
37 default=os.getcwd(),
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
38 help='the path of folder for output .tsv file')
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
39
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
40 # Execute parse_args()
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
41 args = my_parser.parse_args()
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
42
a46d857e25c2 "planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/pyteomics commit 49b21b01937067ffc7cf088e615d68177644640b"
galaxyp
parents:
diff changeset
43 read_mztab(args.path_in, args.path_out)