class Controller: def __init__(self,viewType=None): ''' construct an instance of the controller class to use invoke the method initialize ''' ## determine location cwd = os.getcwd() if os.path.split(cwd)[1] == 'gdiqt4': self.baseDir = cwd elif os.path.split(cwd)[1] == 'tests': self.baseDir = os.path.join(os.path.split(cwd)[0],'gdiqt4') else: print "ERROR: cannot determine current working directory" ## basic application wide variables self.viewType = viewType self.appName = "Gene Data Integrator" self.fontName = 'Arial' #'Helvetica' self.verbose = True self.reset_workspace() def reset_workspace(self): self.projectID = None self.homeDir = None self.model = Model() self.log = Logger() self.subsampleIndices = None def save(self): self.log.write() def initialize_project(self,projectID,loadExisting=False): self.projectID = projectID self.homeDir = os.path.join(self.baseDir,"projects",self.projectID) self.log.initialize(self.projectID,self.homeDir,load=loadExisting) self.model.initialize(self.projectID,self.homeDir) ################################################################################################## # # data dealings -- handling file, project, model and figure data # ################################################################################################## def create_new_project(self,view=None,projectID=None): #fcsFileName = str(fcsFileName) createNew = True ## create projects dir if necssary if os.path.isdir(os.path.join(self.baseDir,'projects')) == False: print "INFO: projects dir did not exist. creating..." os.mkdir(os.path.join(self.baseDir,'projects')) ## get project id if projectID != None: pass elif createNew == True and projectID == None: projectID, ok = QtGui.QInputDialog.getText(view, self.appName, 'Enter the name of your new project:') projectID = str(projectID) if ok == False: createNew = False else: createNew = False print "ERROR: creating a new project" if createNew == True: print 'initializing project...' self.initialize_project(projectID) else: print "WARNING: did not initialize project" return False # remove previous if self.homeDir != None and os.path.exists(self.homeDir) == True and createNew == True: print 'overwriting...', self.homeDir self.remove_project(self.homeDir) if createNew == True and self.homeDir != None: os.mkdir(self.homeDir) os.mkdir(os.path.join(self.homeDir,"data")) os.mkdir(os.path.join(self.homeDir,"figs")) os.mkdir(os.path.join(self.homeDir,"models")) os.mkdir(os.path.join(self.homeDir,"results")) return True def remove_project(self,homeDir): for fileOrDir in os.listdir(homeDir): if os.path.isfile(os.path.join(homeDir,fileOrDir)) == True: os.remove(os.path.join(homeDir,fileOrDir)) elif os.path.isdir(os.path.join(homeDir,fileOrDir)) == True: for fileOrDir2 in os.listdir(os.path.join(homeDir,fileOrDir)): if os.path.isfile(os.path.join(homeDir,fileOrDir,fileOrDir2)) == True: os.remove(os.path.join(homeDir,fileOrDir,fileOrDir2)) elif os.path.isdir(os.path.join(homeDir,fileOrDir,fileOrDir2)) == True: for fileOrDir3 in os.listdir(os.path.join(homeDir,fileOrDir,fileOrDir2)): if os.path.isfile(os.path.join(homeDir,fileOrDir,fileOrDir2,fileOrDir3)) == True: os.remove(os.path.join(homeDir,fileOrDir,fileOrDir2,fileOrDir3)) elif os.path.isdir(os.path.join(homeDir,fileOrDir,fileOrDir2,fileOrDir3)) == True: for fileName in os.listdir(os.path.join(homeDir,fileOrDir,fileOrDir2,fileOrDir3)): os.remove(os.path.join(homeDir,fileOrDir,fileOrDir2,fileOrDir3,fileName)) os.rmdir(os.path.join(homeDir,fileOrDir,fileOrDir2,fileOrDir3)) os.rmdir(os.path.join(homeDir,fileOrDir,fileOrDir2)) os.rmdir(os.path.join(homeDir,fileOrDir)) os.rmdir(homeDir) def rm_file(self,fileName): if os.path.isfile(fileName) == False: print "ERROR: could not rm file: %s"%fileName else: os.remove(fileName) self.view.status.set("file removed") def load_fcs_files(self,fileList,dataType='fcs',transform='log'): if type(fileList) != type([]): print "INPUT ERROR: load_fcs_files: takes as input a list of file paths" script = os.path.join(self.baseDir,"LoadFile.py") self.log.log['transform'] = transform for filePath in fileList: proc = subprocess.Popen("%s %s -f %s -h %s -d %s -t %s"%(pythonPath,script,filePath,self.homeDir,dataType,transform), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) while True: try: next_line = proc.stdout.readline() if next_line == '' and proc.poll() != None: break ## to debug uncomment the following line print next_line except: break ## check to see that files were made newFileName = re.sub('\s+','_',os.path.split(filePath)[-1]) newFileName = re.sub('\.fcs|\.txt|\.out','',newFileName) newDataFileName = newFileName +"_data_original.pickle" newChanFileName = newFileName +"_channels_original.pickle" newFileName = re.sub('\s+','_',filePath[-1]) if filePath == fileList[0]: self.log.log['selectedFile'] = re.sub("\.pickle","",newDataFileName) if os.path.isfile(os.path.join(self.homeDir,'data',newDataFileName)) == False: print "ERROR: data file was not successfully created", os.path.join(self.homeDir,'data',newDataFileName) if os.path.isfile(os.path.join(self.homeDir,'data',newChanFileName)) == False: print "ERROR: channel file was not successfully created", os.path.join(self.homeDir,'data',newChanFileName) #def load_additional_fcs_files(self,fileName=None,view=None): # loadFile = True # fcsFileName = None # if fileName == None: # fileName = QtGui.QFileDialog.getOpenFileName(self, 'Open FCS file') # # if not re.search('\.fcs',fileName): # fcsFileName = None # view.display_warning("File '%s' was not of type *.fcs"%fileName) # else: # fcsFileName = fileName # # if fcsFileName != None: # self.load_fcs_file(fcsFileName) # return True # else: # print "WARNING: bad attempt to load file name" # return False def get_component_states(self): try: return self.view.resultsNavigationLeft.get_component_states() except: return None ################################################################################################## # # log files # ################################################################################################## def run_selected_model(self,progressBar=None,view=None): numItersMCMC = 1100 selectedModel = self.log.log['modelToRun'] numComponents = self.log.log['numComponents'] if self.subsampleIndices == None: fileList = get_fcs_file_names(self.homeDir) elif self.subsampleIndices != None: fileList = get_fcs_file_names(self.homeDir,getPickles=True) percentDone = 0 totalIters = float(len(fileList)) * numItersMCMC percentagesReported = [] for fileName in fileList: if selectedModel == 'dpmm': script = os.path.join(self.baseDir,"RunDPMM.py") if os.path.isfile(script) == False: print "ERROR: Invalid model run file path ", script proc = subprocess.Popen("%s %s -h %s -f %s -k %s"%(pythonPath,script,self.homeDir,fileName,numComponents), shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) while True: try: next_line = proc.stdout.readline() if next_line == '' and proc.poll() != None: break ## to debug uncomment the following 2 lines if not re.search("it =",next_line): print next_line if re.search("it =",next_line): progress = 1.0 / totalIters percentDone+=progress * 100.0 if progressBar != None: progressBar.move_bar(int(round(percentDone))) else: if int(round(percentDone)) not in percentagesReported: percentagesReported.append(int(round(percentDone))) if int(round(percentDone)) != 100: print "\r",int(round(percentDone)),"percent complete", else: print "\r",int(round(percentDone)),"percent complete" except: break else: print "ERROR: invalid selected model", selectedModel
class Controller: def __init__(self,debug=False): ''' construct an instance of the controller class to use invoke the method initialize ''' ## basic application wide variables self.appName = "lpEdit" self.debug = debug self.maxDocuments = 16 self.version = __version__ self.baseDir = __basedir__ if self.debug == True: self.verbose = True else: self.verbose = False ## initialize self.reset_all() def reset_all(self): """ reset all variables and the layout """ self.log = Logger() self.currentFileIndex = 0 self.sphinxProjectBase = None self.fileNameList = [None for i in range(self.maxDocuments)] self.filePathList = [None for i in range(self.maxDocuments)] self.fileLangList = [None for i in range(self.maxDocuments)] self.editorList = [None for i in range(16)] self.background = [] def load_file(self,filePath,verbose=True,fileLang=None): ''' takes a file path and loads the file into lpEdit ''' ## ensure we have absolute file path if filePath != None: filePath = os.path.realpath(filePath) if os.path.exists(filePath) == False: print "WARNING: controller.load_file -- bad file path specified" ## check to see if already loaded fileName = os.path.split(filePath)[-1] if fileName in self.fileNameList: if verbose == True: print "WARNING: file of same name already loaded -- aborting load" return False numActive = self.get_number_active_files() if numActive >= self.maxDocuments: if verbose == True: print "WARNING: too many files already open -- aborting load" self.currentFileIndex = numActive self.filePathList[self.currentFileIndex] = filePath self.fileNameList[self.currentFileIndex] = fileName if fileLang != None: self.fileLangList[self.currentFileIndex] = fileLang elif re.search("\.Rnw|\.rnw",fileName): self.fileLangList[self.currentFileIndex] = 'R' else: self.fileLangList[self.currentFileIndex] = 'Python' ## set the sphinx project path if self.sphinxProjectBase == None: self.sphinxProjectBase = os.path.split(filePath)[0] return True def remove_file(self,fileName): ''' removes a file from the list of loaded files' ''' if fileName not in self.fileNameList: print "ERROR: Controller cannot remove file that is not present -- ignoring" return ## variables to change fileToRemoveIndex = self.fileNameList.index(fileName) fileName = self.fileNameList[fileToRemoveIndex] filePath = self.filePathList[fileToRemoveIndex] editor = self.editorList[fileToRemoveIndex] ## close the editor if editor != None: editor.close() self.fileNameList.remove(fileName) self.filePathList.remove(filePath) self.editorList.pop(fileToRemoveIndex) self.fileLangList.pop(fileToRemoveIndex) self.fileNameList = self.fileNameList + [None] self.filePathList = self.filePathList + [None] self.editorList = self.editorList + [None] self.fileLangList = self.fileLangList + [None] def clean_file(self,filePath): ''' function to be used with unittests to ensure examples are working may also be used to ensure a new project ''' filePathBase = os.path.split(filePath)[0] fileName = os.path.split(filePath)[-1] pdfFilePath = re.sub("\.rnw|\.nw|.rst",".pdf",filePath,flags=re.IGNORECASE) if re.search("\.rnw|\.nw",fileName,flags=re.IGNORECASE): dirName = "_latex" else: dirName = "_sphinx" dirPath = os.path.join(filePathBase,dirName) if os.path.isdir(dirPath) == False: return ## remove pdf file if present if os.path.exists(pdfFilePath): os.remove(pdfFilePath) ## remove all files in lp generated dir if os.path.isdir(dirPath): shutil.rmtree(dirPath) def save(self): ''' saves changes to the log file ''' self.log.write() def get_python_path(self,mainWindow=None): ''' attempts to find the python path based on the system path and by searching ''' if self.log.log['python_path'] != None: pythonPath = self.log.log['python_path'] if os.path.exists(pythonPath) == False: msg = "ERROR: Controller -- given python path does not exist -- using default" if mainWindow != None: mainWindow.display_info(msg) print msg else: return pythonPath cmdsToTry = ['python','python2.8','python2.7','python2.6'] for cmd in cmdsToTry: pythonPath = self.find_command_path(cmd) if pythonPath != None and os.path.exists(pythonPath): return pythonPath return None def find_command_path(self,cmd): """ used to search for a given command on a multiple operating systems """ ## try to see if cmd is in the system path cmdPath = None try: if sys.platform == 'win32': p = os.popen('where %s'%cmd) else: p = os.popen('which %s'%cmd) cmdPath = p.readline() cmdPath = cmdPath[:-1] except: cmdPath = None if cmdPath != None and os.path.exists(cmdPath): return cmdPath ## look for cmd in several commonly installed places if sys.platform == 'win32': pathsToTry = [] if cmd == 'python': pathsToTry = ["C:\Python28\python.exe", "C:\Python27\python.exe", "C:\Python26\python.exe"] elif sys.platform == 'darwin': pathsToTry = [os.path.join("/","usr","bin",cmd), os.path.join("/","usr","local","bin",cmd), os.path.join("/","opt","local","bin",cmd)] else: pathsToTry = [os.path.join("/","usr","bin",cmd), os.path.join("/","usr","local","bin",cmd)] for cmdPath in pathsToTry: if cmdPath != None and os.path.exists(cmdPath) == True: return cmdPath return None def get_sphinx_path(self,mainWindow=None): """ attempts to find the sphinx (sphinx-build) path based on the system path and by searching """ if self.log.log['sphinx_path'] != None: sphinxPath = self.log.log['sphinx_path'] if os.path.exists(sphinxPath) == False: msg = "ERROR: Controller -- given sphinx path does not exist -- using default" if mainWindow != None: mainWindow.display_info(msg) print msg else: return sphinxPath cmdsToTry = ['sphinx-build','sphinx-build-2.8','sphinx-build-2.7','sphinx-build-2.6'] for cmd in cmdsToTry: sphinxPath = self.find_command_path(cmd) if sphinxPath != None and os.path.exists(sphinxPath): return sphinxPath return None def get_latex_path(self,mainWindow=None): ''' returns the latex path based on the system path or a provided one ''' if self.log.log['latex_path'] != None: latexPath = self.log.log['latex_path'] if os.path.exists(latexPath) == False: msg = "ERROR: Controller -- given latex path does not exist -- using default" if mainWindow != None: mainWindow.display_info(msg) print msg else: return latexPath cmdsToTry = ['pdflatex'] for cmd in cmdsToTry: latexPath = self.find_command_path(cmd) if latexPath != None and os.path.exists(latexPath): return latexPath return None def find_r_path_windows(self): dirsToTry = ["C:/Program Files/R","C:/Program Files (x86)/R"] r_path = None for rbaseDir in dirsToTry: if os.path.isdir(rbaseDir) == False: continue installedInstances = os.listdir(rbaseDir) if len(installedInstances) > 0: installedInstances.sort() rdir = installedInstances[-1] r_path = os.path.join(rbaseDir,rdir,"bin","R.exe") break return r_path def get_latex2html_path(self): """ returns the path of the specified program to convert latex to html """ if self.log.log['latex2html_path'] != None: rPath = self.log.log['latex2html_path'] if sys.platform == 'win32': pthsToTry = ["C:/Program Files/latex2html.exe", "C:/Program Files (x86)/latex2html.exe", "C:/Program Files/tth.exe", "C:/Program Files (x86)/tth.exe"] else: pthsToTry = ["/usr/bin/latex2html", "/usr/local/bin/latex2html", "/opt/local/bin/latex2html", "/usr/bin/tth", "/usr/local/bin/tth", "/opt/local/bin/tth"] latex2html_path = None for pth in pthsToTry: if os.path.exists(pth) == True: latex2html_path = pth break return latex2html_path def get_r_path(self,mainWindow=None): ''' returns the r path based on the system path or a provided one ''' if self.log.log['r_path'] != None: rPath = self.log.log['r_path'] if os.path.exists(rPath) == False: msg = "ERROR: Controller -- given R path does not exist -- Please install R or specify a new path" if mainWindow != None: mainWindow.display_info(msg) print msg else: return rPath ## windows if sys.platform == 'win32': rPath = self.find_r_path_windows() return rPath cmdsToTry = ['R'] for cmd in cmdsToTry: rPath = self.find_command_path(cmd) if rPath != None and os.path.exists(rPath): return rPath return None def find_adobe_path_windows(self): """ finds the adobe acrobat program file path in windows """ dirsToTry = ["C:/Program Files/Adobe","C:/Program Files (x86)/Adobe"] adobe_path = None for abaseDir in dirsToTry: if os.path.isdir(abaseDir) == False: continue installedInstances = os.listdir(abaseDir) if len(installedInstances) > 0: installedInstances.sort() adir = installedInstances[-1] adobe_path = os.path.join(abaseDir,adir,"Reader","AcroRd32.exe") break return adobe_path def get_pdfviewer_path(self,mainWindow=None): """ finds the pdfviewer path (on windows it looks for adobe) """ if self.log.log['pdfviewer_path'] != None: pdfviewerPath = self.log.log['pdfviewer_path'] ## exceptions if pdfviewerPath == 'open' and sys.platform == 'darwin': return pdfviewerPath if os.path.exists(pdfviewerPath) == False: msg = "ERROR: the pdfviewer path does not exist -- using default" if mainWindow != None: mainWindow.display_info(msg) print msg else: return pdfviewerPath if sys.platform == 'win32': return self.find_adobe_path_windows() elif sys.platform == 'darwin': return "open" cmdsToTry = ['okular','evince','acroread'] for cmd in cmdsToTry: pdfviewerPath = self.find_command_path(cmd) if pdfviewerPath != None and os.path.exists(pdfviewerPath): return pdfviewerPath return None def ensure_dir_present(self,filePath): """ Given a file path create a dir next to the Rnw or otherwise valid file It is assumed that the same root used for all open rst files """ fileName = os.path.split(filePath)[-1] if re.search("\.rnw|\.nw",fileName,flags=re.IGNORECASE): lpDirName = "_latex" filePathBase = os.path.split(filePath)[0] elif re.search("\.rst",fileName,flags=re.IGNORECASE): lpDirName = "_sphinx" filePathBase = self.sphinxProjectBase lpDirPath = os.path.join(filePathBase,lpDirName) ## create the directory if necessary if os.path.isdir(lpDirPath) == False: os.mkdir(lpDirPath) return lpDirPath def get_number_active_files(self): ''' returns the number of active files ''' totalActive = 0 for fileName in self.fileNameList: if fileName != None: totalActive += 1 return totalActive def sanitize_check(self,script): """ standard function to sanitize file name inputs """ if re.search(">|<|\*|\||^\$|;|#|\@|\&",script): return False else: return True def get_templates_dir(self): """ return the directory path for templates """ templatesDir = os.path.realpath(os.path.join(self.baseDir,'templates')) return templatesDir def get_styfiles_dir(self): """ return the directory path for sty files """ styfilesDir = os.path.realpath(os.path.join(self.baseDir,'styfiles')) return styfilesDir def get_sphinx_files_dir(self): """ return the directory path for sphinx files """ sphinxfilesDir = os.path.realpath(os.path.join(self.baseDir,'sphinxfiles')) return sphinxfilesDir def initialize_sphinx_project(self,filePath): """ If necessary create index.rst and conf.py and subdirectories """ ## variables sphinxDir = self.get_sphinx_files_dir() filePathBase,fileName = os.path.split(filePath) ## check to see if index.rst and conf.py need to be created for fName in ["conf.py"]: fPath = os.path.join(self.sphinxProjectBase,fName) if os.path.exists(fPath) == False: shutil.copy(os.path.join(sphinxDir,fName),fPath) createIndex = False for fName in ["index.rst"]: fPath = os.path.join(self.sphinxProjectBase,fName) if os.path.exists(fPath) == False: createIndex = True ## create the _build and _source dirs if necessary for dirName in ["_build", "_source","_static",]: if os.path.isdir(os.path.join(self.sphinxProjectBase,'_sphinx',dirName)) == False: os.mkdir(os.path.join(self.sphinxProjectBase,'_sphinx',dirName)) ## add to any newly created index.rst if one does not exist if createIndex == True: indexFilePath = os.path.join(self.sphinxProjectBase,'index.rst') fid = open(indexFilePath,'w') fid.write(".. master file, created automatically by lpEdit\n") fid.write("\nContents\n=========================\n\n") fid.write(".. toctree::\n :maxdepth: 1\n\n") for rstFile in os.listdir(os.path.join(self.sphinxProjectBase)): if not re.search("\.rst",rstFile): continue if rstFile == 'index.rst': continue fid.write(" %s\n"%re.sub("\.rst","",rstFile,flags=re.IGNORECASE)) fid.close() shutil.copy(indexFilePath,os.path.join(dirPath,'index.rst')) def copy_sphinx_files(self): """ Move all source files into target dir unless already present sphinx files can exist in a directory without a leading underscore directory trees can be only 1 subdirectory deep however """ def create_link(source,target): if os.path.islink(target): return os.symlink(source,target) dirRoot = self.sphinxProjectBase for fName in os.listdir(dirRoot): sourceFilePath = os.path.join(dirRoot,fName) targetFilePath = os.path.join(dirRoot,'_sphinx',fName) if re.search("\.rst",fName,flags=re.IGNORECASE): if re.search("\~$|\.backup",fName): continue if os.path.exists(targetFilePath) == False: shutil.copy(sourceFilePath,targetFilePath) elif os.path.isdir(fName) and fName[0] != '_': subdirRoot = os.path.join(self.sphinxProjectBase,fName) ## create subdir if needed subdirName = fName if not os.path.isdir(os.path.join(dirRoot,'_sphinx',subdirName)): os.mkdir(os.path.join(dirRoot,'_sphinx',subdirName)) for ffName in os.listdir(subdirRoot): sourceFilePath = os.path.join(subdirRoot,ffName) subdirName = os.path.basename(os.path.dirname(sourceFilePath)) targetFilePath = os.path.join(dirRoot,'_sphinx',subdirName,ffName) if re.search("\.rst",ffName,flags=re.IGNORECASE): if re.search("\~$|\.backup",ffName): continue if os.path.exists(targetFilePath) == False: shutil.copy(sourceFilePath,targetFilePath) elif not os.path.isdir(sourceFilePath): create_link(sourceFilePath,targetFilePath) else: if re.search("\~$|\.backup|^\.",fName) or fName[0] == '_': continue create_link(sourceFilePath,targetFilePath) def language_quickcheck(self,chunksFilePath,fileLanguage): """ look into the extracted code and see if there are obvious signs that the language selected is incorrect """ ## variables pythonSpecific = ["import","from"] rSpecific = ["<-"] fileLanguage = fileLanguage.lower() if os.path.exists(chunksFilePath) == False: return fid = open(chunksFilePath,'r') isValid = True for linja in fid: if fileLanguage == 'python': for kw in rSpecific: if re.search(kw,linja): isValid = False if fileLanguage == 'r': for kw in pythonSpecific: if re.search(kw,linja): isValid = False fid.close() return isValid