Mercurial > repos > galaxyp > maxquant_mqpar
comparison mqwrapper.py @ 0:256cc0e17454 draft
planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/maxquant commit ab4e4f1817080cbe8a031a82cb180610ff140847
| author | galaxyp | 
|---|---|
| date | Sat, 20 Jul 2019 04:53:23 -0400 | 
| parents | |
| children | 26693e21c3c8 | 
   comparison
  equal
  deleted
  inserted
  replaced
| -1:000000000000 | 0:256cc0e17454 | 
|---|---|
| 1 """ | |
| 2 Run MaxQuant on a modified mqpar.xml. | |
| 3 Use maxquant conda package. | |
| 4 TODO: add support for parameter groups | |
| 5 | |
| 6 Authors: Damian Glaetzer <d.glaetzer@mailbox.org> | |
| 7 | |
| 8 based on the maxquant galaxy tool by John Chilton: | |
| 9 https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/maxquant | |
| 10 """ | |
| 11 | |
| 12 import argparse | |
| 13 import os | |
| 14 import shutil | |
| 15 import subprocess | |
| 16 | |
| 17 import mqparam | |
| 18 | |
| 19 # build parser | |
| 20 parser = argparse.ArgumentParser() | |
| 21 | |
| 22 # input, special outputs and others | |
| 23 other_args = ('raw_files', 'mzxml_files', 'fasta_files', | |
| 24 'description_parse_rule', 'identifier_parse_rule', | |
| 25 'mqpar_in', 'output_all', | |
| 26 'mqpar_out', 'infile_names', 'mzTab', | |
| 27 'version', 'substitution_rx') | |
| 28 | |
| 29 # txt result files | |
| 30 txt_output = ('evidence', 'msms', 'parameters', | |
| 31 'peptides', 'proteinGroups', 'allPeptides', | |
| 32 'libraryMatch', 'matchedFeatures', | |
| 33 'modificationSpecificPeptides', 'ms3Scans', | |
| 34 'msmsScans', 'mzRange', 'peptideSection', | |
| 35 'summary') | |
| 36 | |
| 37 global_simple_args = ('num_threads',) | |
| 38 | |
| 39 arguments = ['--' + el for el in (txt_output | |
| 40 + other_args | |
| 41 + global_simple_args)] | |
| 42 | |
| 43 for arg in arguments: | |
| 44 parser.add_argument(arg) | |
| 45 | |
| 46 args = vars(parser.parse_args()) | |
| 47 | |
| 48 # link infile datasets to names with correct extension | |
| 49 # for maxquant to accept them | |
| 50 files = (args['raw_files'] if args['raw_files'] | |
| 51 else args['mzxml_files']).split(',') | |
| 52 ftype = ".thermo.raw" if args['raw_files'] else ".mzXML" | |
| 53 filenames = args['infile_names'].split(',') | |
| 54 fnames_with_ext = [(a if a.endswith(ftype) | |
| 55 else os.path.splitext(a)[0] + ftype) | |
| 56 for a in filenames] | |
| 57 | |
| 58 for f, l in zip(files, fnames_with_ext): | |
| 59 os.link(f, l) | |
| 60 | |
| 61 # build mqpar.xml | |
| 62 mqpar_temp = os.path.join(os.getcwd(), 'mqpar.xml') | |
| 63 mqpar_out = args['mqpar_out'] if args['mqpar_out'] != 'None' else mqpar_temp | |
| 64 mqpar_in = args['mqpar_in'] | |
| 65 | |
| 66 exp_design = None | |
| 67 m = mqparam.MQParam(mqpar_out, mqpar_in, exp_design, | |
| 68 substitution_rx=args['substitution_rx']) | |
| 69 if m.version != args['version']: | |
| 70 raise Exception('mqpar version is ' + m.version + | |
| 71 '. Tool uses version {}.'.format(args['version'])) | |
| 72 | |
| 73 # modify parameters, interactive mode if no mqpar_in was specified | |
| 74 m.add_infiles([os.path.join(os.getcwd(), name) for name in fnames_with_ext], False) | |
| 75 m.add_fasta_files(args['fasta_files'].split(','), | |
| 76 identifier=args['identifier_parse_rule'], | |
| 77 description=args['description_parse_rule']) | |
| 78 | |
| 79 m.write() | |
| 80 | |
| 81 # build and run MaxQuant command | |
| 82 cmd = ['maxquant', mqpar_out] | |
| 83 | |
| 84 subprocess.run(cmd, check=True, cwd='./') | |
| 85 | |
| 86 # copy results to galaxy database | |
| 87 for el in txt_output: | |
| 88 destination = args[el] | |
| 89 source = os.path.join(os.getcwd(), "combined", "txt", "{}.txt".format(el)) | |
| 90 if destination != 'None' and os.path.isfile(source): | |
| 91 shutil.copy(source, destination) | |
| 92 | |
| 93 if args['mzTab'] != 'None': | |
| 94 source = os.path.join(os.getcwd(), "combined", "txt", "mzTab.mzTab") | |
| 95 if os.path.isfile(source): | |
| 96 shutil.copy(source, args['mzTab']) | |
| 97 | |
| 98 if args['output_all'] != 'None': | |
| 99 subprocess.run(('tar', '-zcf', args['output_all'], './combined/txt/')) | 
