lximport.py 8.67 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 92 93 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
#!/usr/bin/python

import os
import sys
import re
from optparse import OptionParser
import ConfigParser

from lx.lxMain import startImport

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

#import logging

### logging levels ###
#DEBUG - debug
#INFO - info for user
#WARNING - warning for user
#ERROR - error, when exception araise
#CRITICAL - when program exits

#logging.basicConfig(level=logging.DEBUG,
#                    format='%(asctime)s %(levelname)s %(funcName)s(): %(message)s',
#					datefmt = '%H:%M:%S',
#                    filename='lpdxlog.txt',
#                    filemode='a')

#log = logging.getLogger("lpdxUITypes.py")

# define a Handler which writes INFO messages or higher to the sys.stderr
#console = logging.StreamHandler()
#console.setLevel(logging.INFO)
# set a format which is simpler for console use
#formatter = logging.Formatter('(log) %(name)-12s: %(message)s')
# tell the handler to use this format
#console.setFormatter(formatter)
# add the handler to the root logger
#log.addHandler(console)

def lpdxImportCLI():

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

	confParse = ConfigParser.ConfigParser()

	optParser = OptionParser(usage="\nlpdxCLI.py [options] [experiment/] [output.sc]\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("-m", "--masterscan", dest="masterScan",
					 help="The name of the masterscan which should be output.")

	optParser.add_option("-i", "--importdir", dest="importDir",
					 help="the directory with the samples which should be imported.")

	optParser.add_option("--pis", dest="pisSpectra", action = "store_true", default = False,
					 help="switches to precursor ion scan (PIS) mode.")
	#optParser.add_option("-g", "--groups", dest="groups", default = "",
	#				 help="select a group file. Without, no groups will be defined.")

	# import settings
	optParser.add_option("-t", "--timerange", dest="timerange", type = "string",
					 help="overwrites/specifies the timerange of retention time \
						in sec with '(start, end)'", metavar = "ARG")
	optParser.add_option("--MSmassrange", dest="MSmassrange", type = "string",
					 help="overwrites/specifies the range of masses with '(start, end)'", metavar = "ARG")

	optParser.add_option("--MSMSmassrange", dest="MSMSmassrange", type = "string",
					 help="overwrites/specifies the range of masses with '(start, end)'", metavar = "ARG")
	optParser.add_option("--MSresolutionDelta", dest="MSresolutionDelta", type = "string",
					 help="overwrites/specifies the change of resolution per 1da in MS", metavar = "ARG")

	optParser.add_option("--MSMSresolutionDelta", dest="MSMSresolutionDelta", type = "string",
					 help="overwrites/specifies the change of resolution per 1da in MS/MS", metavar = "ARG")

	optParser.add_option("--MSminOccupation", dest="MSminOccupation", type = "string",
					 help="defines the percentage MS occupation of masses through all samples ", metavar = "ARG")

	optParser.add_option("--MSMSminOccupation", dest="MSMSminOccupation", type = "string",
					 help="defines the percentage MS occupation of masses through all samples ", metavar = "ARG")

	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("--MSresolution", dest="MSresolution",
					 help="overwrites/specifies 'MSresolution' of the *.ini file", metavar = "ARG")

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

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

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

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

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

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

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

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

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

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

	optParser.add_option("--alignmentMethodMS", dest="alignmentMethodMS", default = "linear",
					 help="linear (default) or heuristic", metavar = "ARG")

	optParser.add_option("--alignmentMethodMSMS", dest="alignmentMethodMSMS", default = "linear",
					 help="linear (default) or heuristic", metavar = "ARG")

	optParser.add_option("--fileType", dest="spectraFormat", type = "string",
					 help="Choose the spectra file type")

	optParser.add_option("--prefix", dest="settingsPrefix", action = "store_true", default = False,
					 help="have the settings as a prefix")

	(cliOptions, args) = optParser.parse_args()

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

	options = optionsDict()
	# the order of the following if-statements integrates prioraties

	project = Project()

	# if the settings are comming from the project file
	if not cliOptions.prj is None:
		#project = Project()
		project.load(cliOptions.prj)
		project.testOptions()
		#options = project.getOptions()
		#project.options['importDir'] = args[0]
		#project.options['masterScan'] = args[1]

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

		project.options['importDir'] = args[0]
		project.options['masterScan'] = args[1]
		project.options['dumpMasterScanFileImport'] = args[1].split('.')[0] + '-dump.csv'
		project.options['importMSMS'] = True
		project.options['mzXML'] = True


	# 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]
	#else:
	project.options['ini'] = "None"

	# the settings come only from the command line (they overwrite existing options)
	if cliOptions.masterScan is None:
		try:
			project.options['masterScan'] = args[1]#cliOptions.masterScanFile
		except IndexError:
			print "ERROR: The masterscan file was not specified. Use the ",
			print "option -m"

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

	project.formatOptions()

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

	options = project.getOptions()
	# print the options
	for k in options:
		try:
			print "{0:24} : {1:12}".format(k, str(options[k]))
		except:
			print "{0:24} : None".format(k)

	if 'masterScan' in cliOptions.__dict__.keys():
		options['masterScan'] = cliOptions.__dict__['masterScan']

	### start import ###
	startImport(options = options, lipidxplorer = True)

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