comparison msp_split.py @ 5:f2683ec717fe draft default tip

planemo upload for repository https://github.com/computational-metabolomics/mspurity-galaxy commit a164f06c09dc1614c2909c247ebf390aab433527-dirty
author tomnl
date Wed, 18 Sep 2019 05:46:09 -0400
parents 89f33758ad22
children
comparison
equal deleted inserted replaced
4:769ec2496d14 5:f2683ec717fe
4 import os 4 import os
5 import re 5 import re
6 import csv 6 import csv
7 import math 7 import math
8 8
9 def msp_split(i, o, n): 9 def msp_split(i, o, x, n):
10 spec_total = lcount('NAME', i) 10 spec_total = lcount(x, i)
11 spec_lim = math.ceil(spec_total/float(n)) 11 spec_lim = math.ceil(spec_total/float(n))
12 spec_c = 0 12 spec_c = 0
13 filelist = [] 13 filelist = []
14 header = '' 14 header = ''
15 print('spec_lim', spec_lim) 15 print('spec_lim', spec_lim)
23 line = msp_in.readline() 23 line = msp_in.readline()
24 24
25 if not line: 25 if not line:
26 break # end of file 26 break # end of file
27 27
28 if re.match('^NAME:.*$', line, re.IGNORECASE): 28 if re.match('^{}.*$'.format(x), line, re.IGNORECASE):
29 header = line 29 header = line
30 spec_c += 1 30 spec_c += 1
31 else: 31 else:
32 msp_out.write(line) 32 msp_out.write(line)
33 spec_c = 1 33 spec_c = 1
34 34
35 return filelist 35 return filelist
36 36
37 def lcount(keyword, fname): 37 def lcount(keyword, fname):
38 with open(fname, 'r') as fin: 38 with open(fname, 'r') as fin:
44 formatter_class=argparse.RawDescriptionHelpFormatter, 44 formatter_class=argparse.RawDescriptionHelpFormatter,
45 description='''split msp files''', 45 description='''split msp files''',
46 ) 46 )
47 47
48 p.add_argument('-i', dest='i', help='msp file', required=True) 48 p.add_argument('-i', dest='i', help='msp file', required=True)
49 p.add_argument('-x', dest='x', help='name', default='RECORD_TITLE:')
49 p.add_argument('-o', dest='o', help='out dir', required=True) 50 p.add_argument('-o', dest='o', help='out dir', required=True)
50 p.add_argument('-n', dest='n',) 51 p.add_argument('-n', dest='n',)
51 52
52 53
53 args = p.parse_args() 54 args = p.parse_args()
54 55
55 if not os.path.exists(args.o): 56 if not os.path.exists(args.o):
56 os.makedirs(args.o) 57 os.makedirs(args.o)
57 print('in file', args.i) 58 print('in file', args.i)
58 print('out dir', args.o) 59 print('out dir', args.o)
60 print('Name of starting value in MSP', args.x)
59 print('nm files', args.n) 61 print('nm files', args.n)
60 62
61 msp_split(args.i, args.o, int(args.n)) 63 msp_split(args.i, args.o, args.x, int(args.n))
62 64
63 65
64 if __name__ == '__main__': 66 if __name__ == '__main__':
65 main() 67 main()
66 68