lxrun.py 7.12 KB
Newer Older
mirandaa's avatar
mirandaa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
import os
import re

from optparse import OptionParser
#import ConfigParser

from lx.exceptions import LipidXException
from lx.project import Project
from lx.options import Options

from lx.lxMain import startMFQL

def lpdxImportCLI():

	######################################################
	###              collect the options               ###

	#confParse = ConfigParser.ConfigParser()

	optParser = OptionParser(usage="Usage: lxrun.py [options] masterscan.sc [output.csv]\n")

	# import part
	optParser.add_option("-p", "--prj", dest="prj",
					help="path of the project file", metavar = "FILE")
	optParser.add_option("-i", "--ini", dest="ini",
					 help="path of the *.ini FILE if different from './lpdxImportSettings.ini'", metavar = "FILE")
	optParser.add_option("-s", "--setting", dest="setting",
					 help="section of a setting from *.ini file")
	optParser.add_option("-r", "--resultfile", dest="resultFile",
					 help="the name of the output file")
	#optParser.add_option("-m", "--masterscan", dest="masterScan",
	#				 help="the name of the output file")
	#optParser.add_option("-d", "--dump", dest="dumpMasterScanFile",
	#				 help="the name of the output file")

	# mfql part
	#optParser.add_option("-m", "--masterscan", dest="masterScanFile",
	#           	  	help="import MasterScan from FILE", metavar="FILE")
	optParser.add_option("-d", "--dump", dest="dumpMasterScan", action="store_true",
					 help="switch for dumping the content of the MasterScan", default=False)
	#optParser.add_option("-o", "--output", dest="output",
	#				 help="output the results with default settings to FILE", metavar="FILE")
	optParser.add_option("-c", "--complement", dest="complementMasterScan", action="store_true",
					 help="generates the complement MasterScan and saves it in the origin directory", default=False)
	optParser.add_option("-q", "--queries", dest="queries", type = "string", metavar = "ARG",
					help='the list of queries to run. Format is "query1.mfql, query2.mfql, ..."', default = "")
	optParser.add_option("--tab", dest="tabLimited", action="store_true", default=False,
					 help="output format is tab-limited")
	optParser.add_option("--compress", dest="compress", action="store_true", default=False,
					 help="no output of script names")
	optParser.add_option("--nohead", dest="noHead", action="store_true", default=False,
					 help="no output of tables head")
	optParser.add_option("--noPermutations", dest="noPermutations", action="store_true", default=False,
					 help="no permutations")
	optParser.add_option("--intensityCorrection", dest="intensityCorrection", action="store_true", default=False,
					 help="intensity correction")
	optParser.add_option("--isocorrectMS", dest="isotopicCorrectionMS", action = "store_true",
					 help="switch isotopic correction on", default = False)
	optParser.add_option("--isocorrectMSMS", dest="isotopicCorrectionMSMS", action = "store_true",
					 help="switch isotopic correction on", default = False)

	# import settings
	optParser.add_option("--MStolerance", dest="MStolerance",
					 help="overwrites/specifies 'MStolerance' of the *.ini file", metavar = "ARG")

	optParser.add_option("--MSMStolerance", dest="MSMStolerance",
					 help="overwrites/specifies 'MSMStolerance' of the *.ini file", metavar = "ARG")

	optParser.add_option("--MStoleranceType", dest="MStoleranceType",
					 help="overwrites/specifies 'MStoleranceType' of the *.ini file", metavar = "ARG")

	optParser.add_option("--MSMStoleranceType", dest="MSMStoleranceType",
					 help="overwrites/specifies 'MSMStoleranceType' of the *.ini file", metavar = "ARG")

	optParser.add_option("--statistics", dest="statistics", action = "store_true", default = False,
					 help="statistics")

	optParser.add_option("--masterScanInSQL", dest="masterScanInSQL", action = "store_true", default = False,
					 help="masterScanInSQL")

	(cliOptions, args) = optParser.parse_args()

	if cliOptions.prj is None:
		if len(args) < 2:
			raise LipidXException("Wrong number of arguments")

	options = {}
	dictMFQL = {}
	project = Project()
	# the order of the following if-statements integrates prioraties

92 93
	import sys
	project.options['masterScan'] =  sys.argv[0]
mirandaa's avatar
mirandaa committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
		

	# if the settings are comming from the project file
	if not cliOptions.prj is None:
		project.load(cliOptions.prj)
		project.testOptions()
		#options = project.getOptions()

		dictMFQL = project.mfql

	else: # standard values

		project.options['setting'] = "command line"
		project.options['loopNr'] = 3
		project.options['alignmentMethodMS'] = "linear"
		project.options['alignmentMethodMSMS'] = "linear"
		project.options['scanAveragingMethod'] = "linear"

		project.options['masterScanRun'] = args[0]
		project.options['dumpMasterScanFile'] = args[0].split('.')[0] + '-dump.csv'
		project.options['importMSMS'] = True
		project.options['mzXML'] = True


	# optional option
	if cliOptions.resultFile is None:
		try:
			project.options['resultFile'] = args[1]
		except IndexError:
			print "ERROR: The result file was not specified. Use the option",
			print " -r."
	else:
		project.options['resultFile'] = cliOptions.resultFile

	# if the settings are used from the *.ini file
	if not cliOptions.ini is None:
		opts = Options()
		opts.initialize("%s" % cliOptions.ini)
		opts.setCurrentConfiguration(cliOptions.setting)
		opts.readConfiguration()
		for opt in opts.options.keys():
			project.options[opt] = opts.options[opt]

	# the settings come only from the command line (they overwrite existing options)
	if not cliOptions.dumpMasterScan is None:
		project.options['dumpMasterScan'] = cliOptions.dumpMasterScan
		project.options['dumpMasterScanFile'] = project.options['masterScan'].split('.')[0] + '-dump.csv'

	# the mfql files are comming from the commandline
	if cliOptions.__dict__.has_key("queries"):
		for q in cliOptions.queries.split(','):
			mfql_file = q.strip()
			if os.path.isdir(mfql_file): # collect the queries from the directory
				for root, dirs, files in os.walk(mfql_file):
					for f in files:
						if re.match('(.*\.mfql$)', f):
							n = os.path.join(root, f)
							l = n.split(os.sep)
							dictMFQL[l[-1]] = n
			else: # collect the queries from the string
				l = mfql_file.split(os.sep)
				dictMFQL[l[-1]] = mfql_file

	for opt in cliOptions.__dict__.keys():
		if not cliOptions.__dict__[opt] is None:
			project.options[opt] = cliOptions.__dict__[opt]

	project.options['queries'] = dictMFQL

	project.formatOptions()

	options = project.getOptions()

	# something is wrong with the result file, so I copy it again
	options['resultFile'] = project.options['resultFile']

	# overwrite the masterScanImport if given at the CLI
	#if 'masterScan' in cliOptions.__dict__.keys():
	#	options['masterScan'] = cliOptions.__dict__['masterScan']

	# print the options
	for k in sorted(options.keys()):
		if not options.isEmpty(k):
			print "{0:24} : {1:12}".format(k, str(options[k]))

	for m in sorted(dictMFQL.keys()):
		print "{0:24} : {1}".format(m, dictMFQL[m])

	###              collect the options               ###
	######################################################

	### start identification
	startMFQL(options = options, queries = dictMFQL)

if __name__ == "__main__":
	lpdxImportCLI()
	#(options, scan, importDir, output) = lpdxImportCLI()