83b08588cf3f1cf3282402cbb89c7d6d79a47f55
[qpalma.git] / qpalma / SettingsParser.py
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
5 #
6 # Written (W) 2008 Fabio De Bona
7 # Copyright (C) 2008 Max-Planck-Society
8
9 #
10 # This file contains the main interface to the QPalma pipeline.
11 #
12
13 import os
14 import re
15 import os.path
16 import sys
17 import pdb
18
19 jp = os.path.join
20
21
22 def parseSettingsFile(filename):
23 """
24 This function parse all key value pairs from the given filename
25 """
26 settings = {}
27 for line in open(filename):
28 if (not line.strip()) or line.startswith('#'):
29 pass
30 else:
31 line = line.strip()
32 line = re.sub('\s', '', line)
33 key, val = line.split('=')
34
35 settings[key] = val
36
37 return settings
38
39
40 def makeSettings(settings):
41 """
42
43 """
44 assert os.path.exists(settings['result_dir']),'Error: You have to specify a existing result directory!'
45 result_dir = settings['result_dir']
46 settings['approximation_dir'] = jp(result_dir, 'approximation')
47 settings['dataset_dir'] = jp(result_dir, 'dataset')
48 settings['preproc_dir'] = jp(result_dir, 'preprocessing')
49 settings['postproc_dir'] = jp(result_dir, 'postprocessing')
50 settings['prediction_dir'] = jp(result_dir, 'prediction')
51 settings['training_dir'] = jp(result_dir, 'training')
52 settings['alignment_dir'] = jp(result_dir, 'alignment')
53
54 for dir_name in ['approximation_dir','dataset_dir', 'preproc_dir', 'postproc_dir',\
55 'prediction_dir', 'training_dir', 'alignment_dir']:
56 try:
57 os.mkdir(settings[dir_name])
58 continue
59 except:
60 print ('Error: There was a problem generating the subdirectory: %s' % dir_name)
61
62 #assert checkSettings(settings),'Check your settings some entries were invalid!'
63
64 ddir = settings['dataset_dir']
65 settings['prediction_dataset_fn'] = jp(ddir,'prediction_data.pickle')
66 settings['prediction_dataset_keys_fn'] = jp(ddir,'prediction_data.keys.pickle')
67 settings['training_dataset_fn'] = jp(ddir,'training_data.pickle')
68 settings['training_dataset_keys_fn'] = jp(ddir,'training_data.keys.pickle')
69
70 try:
71 os.mknod(settings['global_log_fn'])
72 except:
73 print 'Error: There was a problem generating the logfile %s' % settings['global_log_fn']
74
75 # addd checking support!!
76 settings['allowed_fragments'] = eval(settings['allowed_fragments'])
77
78 TF = [True,False]
79
80 # convert and check all boolean parameters
81 for parameter in ['enable_quality_scores', 'enable_intron_length', 'enable_splice_scores',\
82 'remove_duplicate_scores', 'print_matrix']:
83
84 try:
85 settings[parameter] = eval(settings[parameter])
86 assert settings[parameter] in TF
87 except:
88 print "Unexpected error:", sys.exc_info()[0]
89 print 'The value of %s has to be either True or False!'%parameter
90
91 # now convert and check all integer parameters
92 for parameter in ['matchmatrixCols', 'matchmatrixRows', 'numAccSuppPoints', 'numConstraintsPerRound',\
93 'numDonSuppPoints', 'numLengthSuppPoints', 'numQualPlifs', 'numQualSuppPoints', 'anzpath', 'iter_steps',\
94 'max_intron_len', 'max_qual', 'min_intron_len', 'min_qual', 'totalQualSuppPoints','C','num_splits','prb_offset','half_window_size']:
95
96 try:
97 settings[parameter] = int(settings[parameter])
98 except:
99 print 'Error: %s has to be an integer!'%parameter
100
101 # now convert and check all integer parameters
102 for parameter in ['max_svm_score','min_svm_score']:
103
104 try:
105 settings[parameter] = float(settings[parameter])
106 except:
107 print 'Error: %s has to be an float!'%parameter
108
109 return settings
110
111
112 def checkSettings(settings):
113 for (key, val,) in settings.items():
114 if key.endswith('_fn') or key.endswith('_dir'):
115 assert os.path.exists(val), 'Error: Path/File %s with value %s does not seem to exist!' % (key,val)
116 return True
117
118
119 def parseSettings(filename):
120 settings = parseSettingsFile(filename)
121 settings = makeSettings(settings)
122 #assert checkSettings(settings)
123 return settings
124
125 if __name__ == '__main__':
126 print parseSettings(sys.argv[1])