def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' resultLines = [] outputFile=open(galaxyFn,"w") fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[2].split(':')) fnDB = ExternalTrackManager.extractFnFromGalaxyTN(choices[3].split(':')) intersectingFactor = 'id' if choices[4] == 'Element id' else 'position' colsToAdd = [] colsToAddDict = choices[5] for key in colsToAddDict: if colsToAddDict[key]: colsToAdd.append(key) genome = choices[1] if choices[0] == 'Yes' else None try: complementGtrackFileAndWriteToFile(fnSource, fnDB, galaxyFn, intersectingFactor, colsToAdd, genome) except Exception, e: import sys print >> sys.stderr, e
def execute(choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' # get population format if choices.format == 'File': pop = []; popfile = choices.population; inFn = ExternalTrackManager.extractFnFromGalaxyTN(popfile.split(":")); infile = open(inFn); for line in infile: pop.append(line.rstrip('\n')); else: pop = map(str.strip,choices.population.split(",")); # read in file inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices.vcf.split(":")); data = open(inFn).read(); # convert and write to GTrack file outfile = open(galaxyFn, 'w'); outfile.write(addHeader(choices.genome)); outfile.write(convertToGtrackFile(data, pop, choices.genome)); outfile.close();
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' outputFile=open(galaxyFn,"w") fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(':')) inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(':')), 'r') if choices[2] == 'Filter on exact values': if choices[3]!='Select column..': column = int(choices[3][7:]) filterSet = set([key for key,val in choices[4].items() if val]) for i in inputFile: if i.split('\t')[column] in filterSet: print>>outputFile, i else: for i in inputFile: temptab = i.split('\t') for index in range(len(temptab)): locals()['c'+str(index)] = temptab[index] if eval(choices[5]): print>>outputFile, i inputFile.close() outputFile.close()
def execute(cls, choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' try: historyInputTN = choices[0].split(':') #from history historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( historyInputTN) #same as galaxyFn in execute of create benchmark.. randomStatic = RunSpecificPickleFile(historyGalaxyFn) #finds path to static file created for a previous history element, and directs to a pickle file myInfo = randomStatic.loadPickledObject() except: return None galaxyTN = myInfo[3].split(':') myFileName = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) genome = myInfo[0] gtrackSource = GtrackGenomeElementSource(myFileName, genome) regionList = [] for obj in gtrackSource: regionList.append(GenomeRegion(obj.genome, obj.chr, obj.start, obj.end)) extractor = TrackExtractor() fn = extractor.extract(GenomeInfo.getSequenceTrackName(genome), regionList, galaxyFn, 'fasta')
def getOptionsBox6(prevChoices): if prevChoices[3]: extraDbColumnsDict = OrderedDict() fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[2].split(':')) fnDB = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[3].split(':')) gtrackDB = GtrackGenomeElementSource(fnDB) gtrackSource = GtrackGenomeElementSource(fnSource) extraDbColumns = [v for v in gtrackDB.getColumns() if not v in gtrackSource.getColumns()] #list(set(gtrackDBColumnSpec) - set(gtrackSourceColumnSpec)) for column in extraDbColumns: extraDbColumnsDict[column] = False return extraDbColumnsDict
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' from time import time startTime = time() from quick.application.ExternalTrackManager import ExternalTrackManager from quick.util.StaticFile import GalaxyRunSpecificFile import os motifFn = ExternalTrackManager.extractFnFromGalaxyTN( choices[0].split(':')) observedFasta = ExternalTrackManager.extractFnFromGalaxyTN( choices[1].split(':')) randomGalaxyTN = choices[2].split(':') randomName = ExternalTrackManager.extractNameFromHistoryTN(randomGalaxyTN) randomGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( randomGalaxyTN) randomStatic = GalaxyRunSpecificFile(['random'],randomGalaxyFn) #finds path to static file created for a previous history element (randomFn), and directs to a folder containing several files.. #print os.listdir(randomStatic.getDiskPath()) randomFastaPath = randomStatic.getDiskPath() #motifFn, observedFasta, randomFastaPath = '/Users/sandve/egne_dokumenter/_faglig/NullModels/DnaSeqExample/liver.pwm', 'liver.fa', 'randomFastas' testStatistic = choices[3] if testStatistic == 'Average of max score per sequence': scoreFunc = scoreMotifOnFastaAsAvgOfBestScores elif testStatistic == 'Sum of scores across all positions of all sequences': scoreFunc = scoreMotifOnFastaAsSumOfAllScores elif testStatistic == 'Score of Frith et al. (2004)': scoreFunc = lr4 elif testStatistic == 'Product of max per sequence': scoreFunc = scoreMotifOnFastaAsProductOfBestScores else: raise pvals = mcPvalFromMotifAndFastas(motifFn, observedFasta, randomFastaPath, scoreFunc) print 'Pvals for motifs (%s) against observed (%s) vs random (%s - %s) sequences.' % (motifFn, observedFasta, randomName, randomFastaPath) for motif,pval in sorted(pvals.items()): print motif+'\t'+('%.4f'%pval) from quick.util.StaticFile import GalaxyRunSpecificFile from gold.application.RSetup import r, robjects histStaticFile = GalaxyRunSpecificFile(['pvalHist.png'],galaxyFn) #histStaticFile.openRFigure() histStaticFile.plotRHist(pvals.values(), [x/40.0 for x in range(41)], 'Histogram of p-values', xlim=robjects.FloatVector([0.0, 1.0])) #r.hist(robjects.FloatVector(pvals.values()), breaks=robjects.FloatVector([x/40.0 for x in range(41)]), xlim=robjects.FloatVector([0.0, 1.0]), main='Histogram of p-values' ) #histStaticFile.closeRFigure() print histStaticFile.getLink('Histogram') print 'Time (s):', time()-startTime
def execute(choices, galaxyFn=None, username=''): #'Genome:','Source of seed TF:','Seed TF: ','Flank size: ', 'Source of potentially cooperative TFs:' #genome genome = choices[0] seedSource = choices[1] seedTfTnInput = choices[2] if seedSource == 'TFBS from history': seedFn = ExternalTrackManager.extractFnFromGalaxyTN(seedTfTnInput.split(':')) else: tfTrackNameMappings = TfInfo.getTfTrackNameMappings(genome) seedTfTn = tfTrackNameMappings[seedSource] + [seedTfTnInput] #tfTrackName = tfTrackNameMappings[tfSource] + [selectedTF] seedFns = getOrigFns(genome, seedTfTn, '') assert len(seedFns) == 1 seedFn = seedFns[0] flankSize = choices[3] flankSize = int(flankSize) if flankSize != '' else 0 cooperativeTfSource = choices[4] #flankSize = int(choices[4]) #TFsFromGenes.findOverrepresentedTFsFromGeneSet('hg18', 'UCSC tfbs conserved', ['ENSGflankSizeflankSizeflankSizeflankSizeflankSize2flankSize8234','ENSGflankSizeflankSizeflankSizeflankSizeflankSize199674'],flankSize, flankSize, galaxyFn) #TFsFromGenes.findOverrepresentedTFsFromGeneSet('hg18', tfSource, choices[5].split(','),flankSize, flankSize, 'Ensembl', galaxyFn) #TFsFromRegions.findOverrepresentedTFsFromGeneSet(genome, tfSource, ensembleGeneIdList,upFlankSize, downFlankSize, geneSource, galaxyFn) #TFsFromGenes.findTFsTargetingGenes('hg18', tfSource, choices[5].split(','),flankSize, flankSize, 'Ensembl', galaxyFn) TFsFromRegions.findTFsOccurringInRegions(genome, cooperativeTfSource, seedFn, flankSize, flankSize, galaxyFn)
def _getTempChromosomeNames(galaxyTn): if isinstance(galaxyTn, basestring): galaxyTn = galaxyTn.split(":") tempinfofile = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn) #abbrv=GenomeImporter.getGenomeAbbrv(tempinfofile) #return os.linesep.join(GenomeInfo(abbrv).sourceChrNames) return os.linesep.join(GenomeImporter.getChromosomeNames(tempinfofile))
def getGeSourceList(cls, genome, tracks): from quick.application.ExternalTrackManager import ExternalTrackManager from gold.origdata.BedGenomeElementSource import BedGenomeElementSource, BedCategoryGenomeElementSource from gold.origdata.GtrackGenomeElementSource import GtrackGenomeElementSource from gold.origdata.TrackGenomeElementSource import FullTrackGenomeElementSource geSourceList = [] trackNamesWithoutPath = [] for track in tracks: try: fileType = ExternalTrackManager.extractFileSuffixFromGalaxyTN( track) fn = ExternalTrackManager.extractFnFromGalaxyTN(track) if fileType == 'category.bed': geSourceList.append(BedCategoryGenomeElementSource(fn)) elif fileType == 'gtrack': geSourceList.append(GtrackGenomeElementSource(fn)) else: geSourceList.append(BedGenomeElementSource(fn)) trackNamesWithoutPath.append( ExternalTrackManager.extractNameFromHistoryTN(track)) except: # it is not a history, must be in HB track repository geSourceList.append( FullTrackGenomeElementSource(genome, track, allowOverlaps=True)) trackNamesWithoutPath.append(':'.join(track)) return geSourceList, trackNamesWithoutPath
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' genome = choices.genome if choices.selectGenome == 'Yes' else None onlyNonDefault = choices.allHeaders == 'Only non-default headers' try: if choices.history: inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices.history.split(':')) expandHeadersOfGtrackFileAndWriteToFile(inFn, galaxyFn, genome, onlyNonDefault) else: if choices.whitespace == 'Keep whitespace exact': input = choices.input else: input = '' for line in choices.input.split(os.linesep): line = line.strip() if (line.startswith('###') and len(line) > 3 and line[3] != '#') \ or not line.startswith('#'): line = line.replace(' ', '\t') else: line = line.replace('\t', ' ') input += line + os.linesep composer = expandHeadersOfGtrackFileAndReturnComposer('', genome, strToUseInsteadOfFn=input) composer.composeToFile(galaxyFn, onlyNonDefault=onlyNonDefault) except Exception, e: import sys print >> sys.stderr, e
def _getHeaders(prevChoices): numCols = TabularToGtrackTool._getFileContentsInfo(prevChoices).numCols if prevChoices.columnSelection == 'Select individual columns': header = [] for i in xrange(numCols): if hasattr(prevChoices, 'column%s' % i): colHeader = getattr(prevChoices, 'column%s' % i) if colHeader is None or colHeader == '-- ignore --': header.append('') elif colHeader == '-- custom --': header.append(getattr(prevChoices, 'customColumn%s' % i).strip()) else: header.append(colHeader) else: header.append('') return header else: genome = prevChoices.genome if prevChoices.selectGenome == 'Yes' else None inFn = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices.colSpecFile.split(':')) try: geSource = GtrackGenomeElementSource(inFn, genome=genome) geSource.parseFirstDataLine() return geSource.getColumns()[:numCols] except Exception, e: return []
def getOptionsBoxFileContentsInfo(prevChoices): if prevChoices.history or prevChoices.input: if prevChoices.history: inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(prevChoices.history.split(':')), 'r') else: inputFile = StringIO(prevChoices.input) for i in xrange(TabularToGtrackTool._getNumSkipLines(prevChoices)): inputFile.readline() table = [] splitChar = TabularToGtrackTool._getSplitChar(prevChoices) numCols = None error = None for i,line in enumerate(inputFile): row = [x.strip() for x in line.strip().split(splitChar)] if numCols == None: numCols = len(row) elif numCols != len(row): numCols = max(numCols, len(row)) # error = 'Error: the number of columns varies over the rows of the tabular file.' table.append(row) if i == TabularToGtrackTool.NUM_ROWS_IN_TABLE: break numCols = max(len(row) for row in table) if len(table) > 0 else 0 if error is None: if numCols > TabularToGtrackTool.NUM_COLUMN_FUNCTIONS: error = 'Error: the tabular file has more columns than is allowed by the tool (%s > %s).' % (numCols, TabularToGtrackTool.NUM_COLUMN_FUNCTIONS) return ('__hidden__', FileContentsInfo(table=table, numCols=numCols, error=error))
def execute(cls, choices, galaxyFn=None, username=''): #'Genome:','Source of seed TF:','Seed TF: ','Flank size: ', 'Source of potentially cooperative TFs:' #genome genome = choices[0] seedSource = choices[1] seedTfTnInput = choices[2] if seedSource == 'TFBS from history': seedFn = ExternalTrackManager.extractFnFromGalaxyTN( seedTfTnInput.split(':')) else: tfTrackNameMappings = TfInfo.getTfTrackNameMappings(genome) seedTfTn = tfTrackNameMappings[seedSource] + [seedTfTnInput] #tfTrackName = tfTrackNameMappings[tfSource] + [selectedTF] seedFns = getOrigFns(genome, seedTfTn, '') assert len(seedFns) == 1 seedFn = seedFns[0] flankSize = choices[3] flankSize = int(flankSize) if flankSize != '' else 0 cooperativeTfSource = choices[4] #flankSize = int(choices[4]) #TFsFromGenes.findOverrepresentedTFsFromGeneSet('hg18', 'UCSC tfbs conserved', ['ENSGflankSizeflankSizeflankSizeflankSizeflankSize2flankSize8234','ENSGflankSizeflankSizeflankSizeflankSizeflankSize199674'],flankSize, flankSize, galaxyFn) #TFsFromGenes.findOverrepresentedTFsFromGeneSet('hg18', tfSource, choices[5].split(','),flankSize, flankSize, 'Ensembl', galaxyFn) #TFsFromRegions.findOverrepresentedTFsFromGeneSet(genome, tfSource, ensembleGeneIdList,upFlankSize, downFlankSize, geneSource, galaxyFn) #TFsFromGenes.findTFsTargetingGenes('hg18', tfSource, choices[5].split(','),flankSize, flankSize, 'Ensembl', galaxyFn) TFsFromRegions.findTFsOccurringInRegions(genome, cooperativeTfSource, seedFn, flankSize, flankSize, galaxyFn)
def execute(choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' resultsFN = ExternalTrackManager.extractFnFromGalaxyTN( choices.resultsFile) selectedTasks = [key for key, val in choices.tasks.iteritems() if val] examAnalysis = TaskCorrelationAnalysis(resultsFN, galaxyFn) examAnalysis.run(selectedTasks) core = HtmlCore() core.begin() for plotUrl in examAnalysis.getPlotUrls(): core.divBegin(divId='plot') core.image(plotUrl) core.divEnd() core.end() print core
def _getHeaders(prevChoices): numCols = TabularToGtrackTool._getFileContentsInfo(prevChoices).numCols if prevChoices.columnSelection != 'Base columns on existing GTrack file': header = [] for i in xrange(numCols): if hasattr(prevChoices, 'column%s' % i): colHeader = getattr(prevChoices, 'column%s' % i) if colHeader is None or colHeader == '-- ignore --': header.append('') elif colHeader == '-- custom --': header.append(getattr(prevChoices, 'customColumn%s' % i).strip()) else: header.append(colHeader) else: header.append('') return header else: genome = prevChoices.genome if prevChoices.selectGenome == 'Yes' else None try: inFn = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices.colSpecFile.split(':')) geSource = GtrackGenomeElementSource(inFn, genome=genome) geSource.parseFirstDataLine() return geSource.getColumns()[:numCols] except Exception, e: return []
def getOptionsBoxFileContentsInfo(prevChoices): if prevChoices.history or prevChoices.input: if prevChoices.history: inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(prevChoices.history.split(':')), 'r') else: inputFile = StringIO(prevChoices.input) for i in xrange(TabularToGtrackTool._getNumSkipLines(prevChoices)): inputFile.readline() table = [] splitChar = TabularToGtrackTool._getSplitChar(prevChoices) numCols = None error = None for i,line in enumerate(inputFile): row = [x.strip() for x in line.strip().split(splitChar)] if numCols == None: numCols = len(row) elif numCols != len(row): numCols = max(numCols, len(row)) # error = 'Error: the number of columns varies over the rows of the tabular file.' table.append(row) if i == TabularToGtrackTool.NUM_ROWS_IN_TABLE: break numCols = max(len(row) for row in table) if len(table) > 0 else 0 if error is None: if numCols > TabularToGtrackTool.NUM_COLUMN_FUNCTIONS: error = 'Error: the tabular file has more columns than is allowed by the tool (%s > %s).' % (numCols, TabularToGtrackTool.NUM_COLUMN_FUNCTIONS) return '__hidden__', FileContentsInfo(table=table, numCols=numCols, error=error)
def returnColumnList(file, type=None): inputFile = open( ExternalTrackManager.extractFnFromGalaxyTN(file.split(':')), 'r') column = [] i = 0 with inputFile as f: for x in f.readlines(): if i == 0: for el in list(x.strip('\n').split('\t')): column.append(el) if type == 'Number': if i == 1: keys = column.keys() j = 0 for el in list(x.strip('\n').split('\t')): try: el = float(el) except: inx = column.keys[j] del column[inx] j += 1 i += 1 inputFile.close() return column
def _getTempChromosomeNames(galaxyTn): if isinstance(galaxyTn, str): galaxyTn = galaxyTn.split(":") tempinfofile=ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn) abbrv=GenomeImporter.getGenomeAbbrv(tempinfofile) return os.linesep.join(GenomeInfo(abbrv).sourceChrNames)
def execute(choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' # Retrieve the pickled benchmark object from history try: historyInputTN = choices[0].split(':') #same as galaxyFn in execute of create benchmark.. historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN(historyInputTN) #finds path to static file created for a previous history element, and directs to a pickle file randomStatic = RunSpecificPickleFile(historyGalaxyFn) benchmarkSpecification = randomStatic.loadPickledObject() except: return None genome = benchmarkSpecification[0] benchmarkLevel = benchmarkSpecification[2] regionTrackName = benchmarkSpecification[3] benchmarkUtil = BenchmarkUtil(galaxyFn, genome) if benchmarkLevel == 'Base pair probability level': return benchmarkUtil.retrieveFeatureTrack(genome, galaxyFn, regionTrackName, benchmarkSpecification[5]) elif type(regionTrackName) is str: # If string, we're dealing with a single track so just retrieve it return benchmarkUtil.retrieveTrack(regionTrackName, galaxyFn) elif type(regionTrackName) is list: # If list, we're dealing with a benchmark suite which will have to be zipped print benchmarkUtil.retrieveBenchmarkSuiteAsZipFile(regionTrackName) else: raise Exception('Invalid benchmark')
def _getResultsLists(histChoices): if len([x for x in histChoices.values() if x is not None]) == 0: return [], [] galaxyTNs = [ x.split(':') for x in histChoices.values() if x is not None ] galaxyFns = [ ExternalTrackManager.extractFnFromGalaxyTN(tn) for tn in galaxyTNs ] historyNames = [ ExternalTrackManager.extractNameFromHistoryTN(tn) for tn in galaxyTNs ] staticFiles = [ GalaxyRunSpecificFile(['results.pickle'], gfn) for gfn in galaxyFns ] #fileSpecificFile = [GalaxyRunSpecificFile([], gfn) for gfn in galaxyFns] #paths = [x.getDiskPath()+'/0' for x in fileSpecificFile] #pngList = [[v for v in x[2] if v.find('.png')>0] for x in os.walk(paths[0])] try: resultsLists = [load(sf.getFile('r')) for sf in staticFiles] except: resultsLists = [] return resultsLists, historyNames
def execute(cls, choices, galaxyFn=None, username=''): genome = choices[0] outputFile = open(galaxyFn, "w") index2name = ast.literal_eval(choices[2]) name2index = dict([(name, idx) for idx, name in index2name.iteritems()]) galaxyTnList = [tn.split(':') for tn in choices[1].values() if tn] #print>>outputFile,'\n'.join([str(i)+':\t'+str(v) for i, v in enumerate(choices)]) #trackIndexStart = cls.NUM_CATEGORY_FIELDS*2 + 2 for ind, galaxyTn in enumerate(galaxyTnList): name = ExternalTrackManager.extractNameFromHistoryTN(galaxyTn) fnSource = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn) category = choices[2 * name2index[name] + 4] for i in open(fnSource, 'r'): if any(i.startswith(x) for x in ['#', 'track']): continue linetab = i.strip().split('\t') linetab.insert(3, category) if len(linetab) > 4: linetab.pop(4) print >> outputFile, '\t'.join(linetab) outputFile.close()
def execute(cls, choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' orig_case = ExternalTrackManager.extractFnFromGalaxyTN( choices.case.split(':')) orig_control = ExternalTrackManager.extractFnFromGalaxyTN( choices.control.split(':')) out_case = orig_case out_control = orig_control output = galaxyFn if choices.shared in ['removed', 'returned as control regions']: case_minus_control = tempfile.NamedTemporaryFile() cls.subtract_files(orig_case, orig_control, case_minus_control.name) out_case = case_minus_control.name if choices.shared in ['removed', 'returned as case regions']: control_minus_case = tempfile.NamedTemporaryFile() cls.subtract_files(orig_control, orig_case, control_minus_case.name) out_control = control_minus_case.name sys.stdout = open(output, "w", 0) GalaxyInterface.combineToTargetControl(out_case, out_control, output)
def createRSquareGraph(cls, ldGraphTrackName, r2_threshold): """ Creates a dictionary of all pairs in a linked point track. Variants in LD must have rsquare >= the rsquare threshold passed to the function. :param ldGraphTrackName: linked point track, as chosen in tool (choices.ldtrack) :param r2_threshold: Lower limit of square value :return: Dictionary of all ld-pairs with sorted key = (rsid1, rsid2), value = rSquare """ from quick.application.ExternalTrackManager import ExternalTrackManager from gold.origdata.GtrackGenomeElementSource import GtrackGenomeElementSource fileName = ExternalTrackManager.extractFnFromGalaxyTN(ldGraphTrackName) suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(ldGraphTrackName) gtSource = GtrackGenomeElementSource(fileName, suffix=suffix) r2graph = {} for ge in gtSource: rsid = ge.id edges = ge.edges weights = ge.weights for i in range(0, len(edges)): ldRsid = edges[i] r2 = weights[i] if r2 >= float(r2_threshold): cls.addEdge(r2graph, rsid, ldRsid, r2) return r2graph
def execute(cls, choices, galaxyFn=None, username=''): from quick.application.ExternalTrackManager import ExternalTrackManager genome = choices[0] preProcTN1 = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN( genome, choices[2].split( ':')) if choices[1] == 'history' else choices[2].split(':') chrSizeDict = dict([(chrom, GenomeInfo.getChrLen(genome, chrom)) for chrom in GenomeInfo.getChrList(genome)]) trackType = choices[3].split(':')[1] fnSource = ExternalTrackManager.extractFnFromGalaxyTN( choices[3].split(':')) if trackType in ['valued.bed', 'category.bed', 'bed']: geSource = GenomeElementSorter( BedGenomeElementSource(fnSource, genome=genome)).__iter__() elif trackType == 'gtrack': geSource = GenomeElementSorter( GtrackGenomeElementSource(fnSource, genome=genome)).__iter__() #headLinesStr = geSource.getHeaderLines().replace('##','\n##') else: raise InvalidFormatError( 'The Binning must be of the following formats: gtrack, valued.bed, category.bed ,bed ...' ) cls.PrintResultToHistItem(galaxyFn, geSource, preProcTN1, genome, username)
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' print 'Executing...' tempinfofile=ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(":")) abbrv=GenomeImporter.getGenomeAbbrv(tempinfofile) gi = GenomeInfo(abbrv) chrNamesInFasta=gi.sourceChrNames chromNamesDict={} chrDict = InstallGenomeTool._getRenamedChrDictWithSelection(choices) for i, key in enumerate(chrDict.keys()): if chrDict[key]: chromNamesDict[chrNamesInFasta[i]]=key print 'All chromosomes chosen: ' + str(chromNamesDict) stdChrDict = InstallGenomeTool._getRenamedChrDictWithSelection(choices, stdChrs=True) stdChrs = [x for x in stdChrDict if stdChrDict[x]] print 'Standard chromosomes chosen: ' + ", ".join(stdChrs) GenomeImporter.createGenome(abbrv, gi.fullName, chromNamesDict, stdChrs, username=username) gi.installedBy = username gi.timeOfInstallation = datetime.now() gi.store()
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[2].split(':')) core = HtmlCore() core.begin() valid = False try: core.header('Validating GTrack headers') core.styleInfoBegin(styleClass='debug') print str(core) core = HtmlCore() gtrackSource = GtrackGenomeElementSource(fnSource, choices[1] if choices[0]=='Yes' else None, printWarnings=True) core.append('Done') core.styleInfoEnd() core.header('Validating complete GTrack file') core.styleInfoBegin(styleClass='debug') print str(core) core = HtmlCore() try: for ge in gtrackSource: pass except Exception, e: pass else:
def execute(cls, choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' if choices[0] == cls.INPUT_FIELD: batchStr = choices[1] else: from quick.application.ExternalTrackManager import ExternalTrackManager batchStr = open( ExternalTrackManager.extractFnFromGalaxyTN( choices[2].split(':'))).read() batchLines = [x.strip() for x in batchStr.strip().split(os.linesep)] #batchLines = [x.strip() for x in choices[0].strip().split(os.linesep)] legalBatchLines = cls._removeIllegalLines(batchLines, username) if len(legalBatchLines) > 0: GalaxyInterface.runBatchLines(batchLines=legalBatchLines, galaxyFn=galaxyFn, \ genome=None, username=username) else: return 'Your user does not have access to run these command lines.'
def execute(cls, choices, galaxyFn=None, username=''): ''' Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' from quick.application.ExternalTrackManager import ExternalTrackManager from quick.application.GalaxyInterface import GalaxyInterface from gold.application.LogSetup import setupDebugModeAndLogging #setupDebugModeAndLogging() genome = choices[0] tn1, tn2 = choices[1:] fnTn1 = ExternalTrackManager.extractFnFromGalaxyTN(tn1) track1 = [ 'Sequence', 'DNA' ] #ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, tn1.split(':')) track2 = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN( genome, tn2.split(':')) analysisDef = 'dummy -> GetMutatedSequenceStat' res = GalaxyInterface.runManual([track1, track2], analysisDef, 'bed', fnTn1, genome) with open(galaxyFn, 'w') as utfil: for region, sequence in res.items(): print >> utfil, '>' + region.chr + ' ' + str(region.start + 1) + '-' + str( region.end) print >> utfil, sequence['Result']
def execute(cls, choices, galaxyFn=None, username=''): from gold.gsuite.GSuite import GSuite from gold.gsuite.GSuiteTrack import GalaxyGSuiteTrack, GSuiteTrack import gold.gsuite.GSuiteComposer as GSuiteComposer from quick.application.ExternalTrackManager import ExternalTrackManager genome = choices.genome if choices.selectGenome == 'Yes' else None selectedHistories = [ key for key, val in choices.history.iteritems() if val is not None ] gSuite = GSuite() for histGalaxyId in selectedHistories: galaxyTrackName = choices.history[histGalaxyId].split(':') histGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( galaxyTrackName) histName = ExternalTrackManager.extractNameFromHistoryTN( galaxyTrackName) histSuffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN( galaxyTrackName) uri = GalaxyGSuiteTrack.generateURI(galaxyFn=histGalaxyFn, suffix=histSuffix) gSuite.addTrack(GSuiteTrack(uri, title=histName, genome=genome)) GSuiteComposer.composeToFile(gSuite, galaxyFn)
def _getContentsOfHistoryFile(cls, historyChoice): from quick.application.ExternalTrackManager import ExternalTrackManager histFileName = ExternalTrackManager.extractFnFromGalaxyTN( historyChoice) with open(histFileName) as histFile: contents = [row.split() for row in histFile] return contents
def retrieveFeatureTrack(self, genome, galaxyFn, regionTrackName, featureTrack): regionTrackName = regionTrackName.split(':') regionFileName = ExternalTrackManager.extractFnFromGalaxyTN(regionTrackName) bins = GalaxyInterface._getUserBinSource('gtrack', regionFileName, genome, featureTrack.split(':')) return self._extractor.extract(featureTrack.split(':'), bins, galaxyFn, 'gtrack')
def getOptionsBox3(prevChoices): '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box. prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case) ''' if prevChoices[1] is not None: galaxyTN = prevChoices[1].split(':') fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) return fn, True
def getOptionsBox6(prevChoices): if prevChoices[3]: extraDbColumnsDict = OrderedDict() fnSource = ExternalTrackManager.extractFnFromGalaxyTN( prevChoices[2].split(':')) fnDB = ExternalTrackManager.extractFnFromGalaxyTN( prevChoices[3].split(':')) gtrackDB = GtrackGenomeElementSource(fnDB) gtrackSource = GtrackGenomeElementSource(fnSource) extraDbColumns = [ v for v in gtrackDB.getColumns() if not v in gtrackSource.getColumns() ] #list(set(gtrackDBColumnSpec) - set(gtrackSourceColumnSpec)) for column in extraDbColumns: extraDbColumnsDict[column] = False return extraDbColumnsDict
def returnGSuiteDict3LevelDept(par): from quick.application.ExternalTrackManager import ExternalTrackManager with open(ExternalTrackManager.extractFnFromGalaxyTN(par), 'r') as f: data = [x.strip('\n') for x in f.readlines()] f.closed ll = [] for i in data: new = [x for x in i.split('\t')] ll.append(new) i = 0 targetTracksDict = [] ddList = [] dFv1List = [] for l in ll: if i > 4: if i == 5: fv1 = l[2] #genome fv2 = l[3] #dir_level_1 dfV0 = {'folderName1': fv2, 'dataFolderValue1': dFv1List} if fv2 == l[3]: path = l[0].split('/') path.pop(0) dFv1 = { 'folderName2': l[4], 'trackName': l[1], 'trackPath': path } dFv1List.append(dFv1) else: ddList.append(dfV0) fv2 = l[3] dFv1List = [] path = l[0].split('/') path.pop(0) dFv1 = { 'folderName2': l[4], 'trackName': l[1], 'trackPath': path } dFv1List.append(dFv1) if fv1 != l[2]: dd = {'genome': fv1, 'dataFolderValue0': ddList} targetTracksDict.append(dd) fv1 = l[2] ddList = [] if len(ll) - 1 == i: ddList.append(dfV0) dd = {'genome': fv1, 'dataFolderValue0': ddList} targetTracksDict.append(dd) i += 1 return targetTracksDict
def _getGESource(choices): genome = choices.genome if choices.selectGenome == 'Yes' else None galaxyTN = choices.history.split(':') suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(galaxyTN) fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) return GenomeElementSource(fn, genome=genome, printWarnings=False, suffix=suffix)
def getOptionsBoxTasks(prevChoices): if prevChoices.resultsFile: taskNames = ExamResultsReader.readTaskNames( ExternalTrackManager.extractFnFromGalaxyTN( prevChoices.resultsFile), TaskCorrelationAnalysis.REQUIRED_COLUMNS, TaskCorrelationAnalysis.OPTIONAL_COLUMNS) selected = [True] * len(taskNames) return OrderedDict(zip(taskNames, selected)) return None
def getOptionsBox3(prevChoices): '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box. prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case) ''' if prevChoices[-2]!=None and prevChoices[0]: try: fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[0].split(':')) return (open(fnSource,'r').readlines()[int(prevChoices[-2])], 2, True) except: pass
def getOptionsBox2(prevChoices): try: historyInputTN = prevChoices[0].split(':') #from history historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( historyInputTN) #same as galaxyFn in execute of create benchmark.. randomStatic = RunSpecificPickleFile(historyGalaxyFn) #finds path to static file created for a previous history element, and directs to a pickle file myInfo = randomStatic.loadPickledObject() return [myInfo[0]] except: return None
def execute(choices, galaxyFn=None, username=''): historyInputTN = choices[0].split(':') #from history historyGalaxyFn = ExternalTrackManager.extractFnFromGalaxyTN( historyInputTN) #same as galaxyFn in execute of create benchmark.. randomStatic = RunSpecificPickleFile(historyGalaxyFn) #finds path to static file created for a previous history element, and directs to a pickle file myInfo = randomStatic.loadPickledObject() if myInfo[1] == choices[2]: print "Your answer was correct!" else: print "You answered %s but the right answer was %s" % (myInfo[1], choices[2])
def validateAndReturnErrors(choices): from quick.application.ExternalTrackManager import ExternalTrackManager if not choices[0]: return 'No tabular history elements selected. Please select a history element with 4-column tabular data.' dataFn = ExternalTrackManager.extractFnFromGalaxyTN(choices[0]) if len(open(dataFn).readline().split('\t')) != 4: return 'Selected history element does not contain 4-column tabular data.'
def getOptionsBoxCelltype(cls, prevChoices): if prevChoices.history and prevChoices.history != '----- Select -----': resultSet = set([ line.split('\t')[1] for line in open( ExternalTrackManager.extractFnFromGalaxyTN( prevChoices.history.split(':'))).readlines()[1:] if line.find('\t') > 0 ]) #return ['Hello World'] return OrderedDict([(v, False) for v in resultSet])
def execute(cls, choices, galaxyFn=None , username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' #print 'Executing... with choices %s'%str(choices) abbrv = choices[0] name = choices[1] #Should test that the genome is not in hyperbrowser. gi = GenomeInfo(abbrv) if gi.hasOrigFiles(): sys.stderr.write( "Genome "+abbrv+ " is already in the Genomic HyperBrowser. Remove the old first.") else: gi.fullName = name if choices[2] == 'URL': urls = choices[3].split() gi.sourceUrls = urls for url in urls: try: GenomeImporter.downloadGenomeSequence(abbrv, url) except InvalidFormatError: return else: basePath = os.sep.join([NONSTANDARD_DATA_PATH, abbrv] + GenomeInfo.getSequenceTrackName(abbrv)) fnSource = ExternalTrackManager.extractFnFromGalaxyTN(choices[4].split(':')) fnDest = basePath+'/'+abbrv+'Sequence.fasta' ensurePathExists(fnDest) copyfile(fnSource, fnDest) chrs=GenomeImporter.extractChromosomesFromGenome(abbrv) gi.sourceChrNames = chrs gi.installedBy = username gi.genomeBuildSource = choices[5] gi.genomeBuildName = choices[6] gi.species = choices[7] gi.speciesTaxonomyUrl = choices[8] gi.assemblyDetails = choices[9] gi.privateAccessList = [v.strip() for v in choices[10].replace(os.linesep, ' ').replace(',', ' ').split(' ') if v.find('@')>0] gi.isPrivate = (choices[11] != 'All') gi.isExperimental = (choices[12] != 'All') gi.ucscClade = choices[13] gi.ucscGenome = choices[14] gi.ucscAssembly = choices[15] galaxyFile=open(galaxyFn, "w") galaxyFile.write( 'Genome abbreviation: ' + abbrv + os.linesep) galaxyFile.write( 'Genome full name: ' + name + os.linesep) galaxyFile.write( 'Track name: ' + ':'.join(GenomeInfo.getSequenceTrackName(abbrv)) + os.linesep) galaxyFile.write( 'Temp chromosome names: ' + ' || '.join(chrs) + os.linesep) #GenomeImporter.saveTempInfo(abbrv, name, chrs) #print 'Chromosomes: '+chrs gi.store()
def openBedFile(self, bedRegions): bedFile = ExternalTrackManager.extractFnFromGalaxyTN(bedRegions.split(':')) with open(bedFile, 'r') as f: lines = f.readlines() bedData = OrderedDict() for l in lines: l = l.strip('\n').split('\t') strL = l[0] + '-' + l[1] + '-' + l[2] if not strL in bedData.keys(): bedData[strL] = [] return bedData
def validateAndReturnErrors(cls, choices): ''' Should validate the selected input parameters. If the parameters are not valid, an error text explaining the problem should be returned. The GUI then shows this text to the user (if not empty) and greys out the execute button (even if the text is empty). If all parameters are valid, the method should return None, which enables the execute button. ''' errorString = cls._checkHistoryTrack(choices, 'snp', choices.genome) if errorString: return errorString from quick.application.ExternalTrackManager import ExternalTrackManager fileName = choices.snp if fileName != None and fileName != "": fName = ExternalTrackManager.extractFnFromGalaxyTN(fileName) suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN(fileName) from gold.origdata.GenomeElementSource import GenomeElementSource geSource = GenomeElementSource(fName, suffix=suffix) # Hacky way to check validity: # Check for errors when reading first column # Probably more correct ways to do this? try: for ge in geSource: chr = ge.chr start = ge.mutated_from_allele from_allele = ge.mutated_to_allele to_allele = ge.mutated_to_allele break except: return "Invalid SNP data file. The SNP data file should as a minimum contain the following columns:" + \ " seqid, start, end, mutated_from_allele, mutated_to_allele" errorString = cls._checkGSuiteFile(choices.gsuite) if errorString: return errorString gSuite = getGSuiteFromGalaxyTN(choices.gsuite) errorString = cls._checkGSuiteRequirements( gSuite, allowedLocations=cls.GSUITE_ALLOWED_LOCATIONS, allowedFileFormats=cls.GSUITE_ALLOWED_FILE_TYPES, allowedTrackTypes=cls.GSUITE_ALLOWED_TRACK_TYPES) if errorString: return errorString errorString = cls._validateGenome(choices.genome) if errorString: return errorString
def getOptionsBox2(prevChoices): '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box. prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case) ''' if prevChoices[0]: fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[0].split(':')) inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[0].split(':')), 'r') firstLineStr = inputFile.readline() count = 0 resultStrList = [] while count< 4 and firstLineStr: if firstLineStr[0]!='#' and firstLineStr.strip()!='': resultStrList.append(firstLineStr) count+=1 firstLineStr = inputFile.readline() firstLineStr = ''.join(resultStrList) else: return None return (firstLineStr, 4, True)
def retrieveTrack(self, regionTrackName, fastaFileName): regionTrackName = regionTrackName.split(':') myFileName = ExternalTrackManager.extractFnFromGalaxyTN(regionTrackName) gtrackSource = GtrackGenomeElementSource(myFileName, self._genome) regionList = [] for obj in gtrackSource: regionList.append(GenomeRegion(obj.genome, obj.chr, obj.start, obj.end)) return self._extractor.extract(GenomeInfo.getSequenceTrackName(self._genome), regionList, fastaFileName, 'fasta')
def getOptionsBox5(prevChoices): '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box. prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case) ''' if prevChoices[2] == 'Filter on exact values' and prevChoices[3] and prevChoices[3]!='Select column..': resultDict = {} column = int(prevChoices[3][7:]) fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[0].split(':')) inputFile = open(ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[0].split(':')), 'r') for i in inputFile: tempVal = i.split('\t')[column] if not resultDict.has_key(tempVal): resultDict[tempVal] = None if len(resultDict.keys())<2000: return dict([(v,False) for v in sorted(resultDict.keys())]) else: return ('Too many values too choose from: %d' % len(resultDict), 1, True) else: return None
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' binSize = parseShortenedSizeSpec(choices[1]) geSource = BedGenomeElementSource(ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(':'))) splittedGeSource = ListGESourceWrapper(geSource, list(SplitSegmentsTool.splitGEs(geSource, binSize))) BedComposer(splittedGeSource).composeToFile(galaxyFn)
def _validateGSuiteFile(galaxyTN): import gold.gsuite.GSuiteParser as GSuiteParser from quick.application.ExternalTrackManager import ExternalTrackManager from cStringIO import StringIO galaxyFn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) outFile = StringIO() ok = GSuiteParser.validate(galaxyFn, outFile=outFile, printHelpText=False) if not ok: return outFile.getvalue()
def getOptionsBox3(prevChoices): '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box. prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case) ''' if prevChoices[-2] != None and prevChoices[0]: try: fnSource = ExternalTrackManager.extractFnFromGalaxyTN( prevChoices[0].split(':')) return (open(fnSource, 'r').readlines()[int(prevChoices[-2])], 2, True) except: pass
def execute(cls, choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' #print 'Executing...' genome = choices[0] galaxyTN = choices[1].split(':') #print 'galaxyTN: ',galaxyTN , type(galaxyTN) fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) print 'Disk path for selected history element: <br>%s<br><br>' % fn from proto.hyperbrowser.StaticFile import GalaxyRunSpecificFile staticFile = GalaxyRunSpecificFile([],fn) print 'HyperBrowser data path (static file path) corresponding to selected history element: <br>%s<br><br>' % staticFile.getDiskPath() from config.Config import EXT_PROCESSED_DATA_PATH try: tn = ExternalTrackManager.getStdTrackNameFromGalaxyTN(galaxyTN)[:-1] preProcPath = '/'.join([EXT_PROCESSED_DATA_PATH,'100000','noOverlaps',genome] + tn) import os lastTnPart = os.listdir(preProcPath) assert len(lastTnPart)==1, lastTnPart #preProcPath += '/'+lastTnPart[0] tn += lastTnPart preProcPath = '/'.join([EXT_PROCESSED_DATA_PATH,'100000','noOverlaps',genome] + tn) print 'HyperBrowser pre-processed track name (if applicable): <br>%s<br><br>' % tn print 'HyperBrowser pre-processed track path (if applicable): <br>%s<br><br>' % preProcPath except: print '(Not printing track name, as history element does not appear to be a (valid) track.)<br>' print 'HyperBrowser URL path (static file URL) corresponding to selected history element: <br>%s<br><br>' % staticFile.getURL() # Does not work correctly, as the id needs to be the history id, and not the dataset id, # which may be different. Dataset id may perhaps be found from history id in the following manner (untested): # # data = trans.sa_session.query( trans.app.model.HistoryDatasetAssociation ).get( trans.security.decode_id( history_id ) ) # dataset_id = extractIdFromGalaxyFn(data.file_name) # # Not sure how to go the other way. # #from quick.util.CommonFunctions import extractIdFromGalaxyFn #from galaxy.web.security import SecurityHelper #from config.Config import GALAXY_ID_SECRET #secHelper = SecurityHelper(id_secret=GALAXY_ID_SECRET) #id = extractIdFromGalaxyFn(galaxyFn) #encodedId = secHelper.encode_id(id[1]) #print 'Galaxy URL id: <br>%s<br><br>' % encodedId import os.path, time print "Time of creation for Galaxy history file: %s" % time.ctime(os.path.getctime(fn)) print "Last modification for Galaxy history file: %s" % time.ctime(os.path.getmtime(fn))
def validateAndReturnErrors(choices): ''' Should validate the selected input parameters. If the parameters are not valid, an error text explaining the problem should be returned. The GUI then shows this text to the user (if not empty) and greys out the execute button (even if the text is empty). If all parameters are valid, the method should return None, which enables the execute button. ''' if not choices.resultsFile: return 'Please select a results file from history' resultsFN = ExternalTrackManager.extractFnFromGalaxyTN( choices.resultsFile) errors = ExamResultsValidator.validateExamResultsDataFile( resultsFN, IndividualTaskAnalysis.REQUIRED_COLUMNS, IndividualTaskAnalysis.OPTIONAL_COLUMNS) if errors: if len(errors) > 5: errors = errors[0:5] return 'First 5 errors reported:<br><br>' + '<br>'.join( [str(x) for x in errors]).replace('\n', '<br>') selectedTasks = [key for key, val in choices.tasks.iteritems() if val] if not selectedTasks: return 'Please select at least one task' if choices.analysis == IndividualTaskAnalysis.ANALYSIS_BIN_AVG_SMOOTHED_PLOT: if not choices.bins: return 'Please specify number of bins' try: bins = int(choices.bins) if bins < 1: return "Number of bins must be 1 or more" except: return "Bins must be an integer number" if choices.analysis in [ IndividualTaskAnalysis.ANALYSIS_BIN_AVG_SMOOTHED_PLOT, IndividualTaskAnalysis.ANALYSIS_MOVING_AVG_SMOOTHED_PLOT ]: if not choices.spar: return 'Please specify the smoothing parameter' else: try: spar = float(choices.spar) if spar < 0 or spar > 1: return 'The smoothing parameter must be between 0.0 and 1.0' except: return 'The smoothing parameter must be a floating point number between 0.0 and 1.0' return None
def execute(cls, choices, galaxyFn=None, username=''): from gold.util.RandomUtil import random outputFile = open(galaxyFn, 'w') genome = choices[0] histItem = choices[2] trackItem = choices[3] chromRegsPath = GenomeInfo.getChrRegsFn(genome) chrSizeDict = dict([(chrom, GenomeInfo.getChrLen(genome, chrom)) for chrom in GenomeInfo.getChrList(genome)]) geSource = headLinesStr = None if choices[1] == 'history': trackType = choices[2].split(':')[1] username = ''.join( [chr(random.randint(97, 122)) for i in range(6)]) tempFn = createCollectedPath( genome, [], username + '_'.join([str(v) for v in time.localtime()[:6]]) + '.' + trackType) fnSource = ExternalTrackManager.extractFnFromGalaxyTN( choices[2].split(':')) open(tempFn, 'w').write(open(fnSource, 'r').read()) if trackType in ['valued.bed', 'category.bed', 'bed']: geSource = GenomeElementSorter( BedGenomeElementSource(tempFn, genome=genome)).__iter__() #elif trackType == 'gtrack': # geSource = GenomeElementSorter(GtrackGenomeElementSource(tempFn, genome=genome)).__iter__() # headLinesStr = geSource.getHeaderLines().replace('##','\n##') cls.WriteExpandedElementsToFile(geSource, chrSizeDict, outputFile, headLinesStr, writeHeaderFlag=True) os.remove(tempFn) else: writeHeaderFlag = True for chrom in GenomeInfo.getChrList(genome): gRegion = GenomeRegion(genome, chrom, 0, chrSizeDict[chrom]) plTrack = PlainTrack(trackItem.split(':')) geSource = GenomeElementTvWrapper( plTrack.getTrackView(gRegion)).__iter__() cls.WriteExpandedElementsToFile(geSource, chrSizeDict, outputFile, headLinesStr, writeHeaderFlag) writeHeaderFlag = False outputFile.close()
def _validateFirstLine(galaxyTN, genome=None, fileStr='file'): try: from quick.application.ExternalTrackManager import ExternalTrackManager from gold.origdata.GenomeElementSource import GenomeElementSource suffix = ExternalTrackManager.extractFileSuffixFromGalaxyTN( galaxyTN) fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) GenomeElementSource(fn, genome, suffix=suffix).parseFirstDataLine() except Exception, e: return fileStr.capitalize() + ' invalid: ' + str(e)
def getOptionsBox5(prevChoices): '''Returns a list of options to be displayed in the second options box, which will be displayed after a selection is made in the first box. prevChoices is a list of selections made by the web-user in the previous input boxes (that is, list containing only one element for this case) ''' if prevChoices[2] and prevChoices[3]: fnSource = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[2].split(':')) fnDB = ExternalTrackManager.extractFnFromGalaxyTN(prevChoices[3].split(':')) gtrackDBColumnSpec = GtrackGenomeElementSource(fnDB).getColumnSpec().keys() gtrackSourceColumnSpec = GtrackGenomeElementSource(fnSource).getColumnSpec().keys() resultlist = ['Element id'] if 'id' in gtrackDBColumnSpec and 'id' in gtrackSourceColumnSpec else [] commonColumns = list(set(gtrackDBColumnSpec) & set(gtrackSourceColumnSpec)) tupleKey = True if any(x in commonColumns for x in ['start', 'end']) else False resultlist += ['Positional information'] if tupleKey else [] return resultlist return None
def createLinkedPointTrack(cls, rsids, isUndirected, trackFn, r2): from quick.webtools.clustering.CreateLDTrack import CreateLDTrack from quick.application.ExternalTrackManager import ExternalTrackManager # Create file for GTrack galaxyTN = ExternalTrackManager.constructGalaxyTnFromSuitedFn(trackFn, fileEnding='gtrack', name='ld_graph') fn = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTN) f = open(fn, 'w') # Get LD information and create linked point track ldDict = CreateLDTrack.getLDDict(r2) expansionDict = CreateLDTrack.getExpansionDict(rsids, ldDict) f.write(CreateLDTrack.formatLinkedPointTrack(expansionDict, isUndirected))
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' if username =='': username = ''.join([chr(random.randint(97,122)) for i in range(6)]) dirForDataset = AddFilesToStorebioinfoDataset.STOREBIO_ROOT_PATH+username + '_'.join([str(v) for v in time.localtime()[:6]]) + str(time.time()).split('.')[-1] print dirForDataset makedirs(dirForDataset) url = 'scp://invitro.titan.uio.no:' if choices[0] == 'from tracks': sourceLink = createOrigPath(genome=choices[1], trackName=choices[2].split(':')) symboliclink = dirForDataset + sep + choices[2].split(':')[-1] copytree(sourceLink, symboliclink)# symlink(sourceLink, symboliclink) else: sourceLink = dirForDataset galaxyTnList = [unquote(v).split(':') for v in choices[1].values() if v] for galaxyTn in galaxyTnList: fnSource = ExternalTrackManager.extractFnFromGalaxyTN(galaxyTn) fnDestination = dirForDataset+'/' + galaxyTn[-1].replace(' ','_')+'.'+galaxyTn[1] copyfile(fnSource, fnDestination) url = url + fnDestination #AddFileToDataSet(dataSetId, subtype, pathInSubtype, url, userName, password) #['Username','password', 'Select dataset', 'select subtype', 'write path in subtype', 'UIO username', 'UIO password'] userName, password = choices[3], choices[4] #uioUser, uioPwd = choices[-2], choices[-1] dataSetId, subtype = choices[5].split('(')[-1].split(')')[0] , choices[6] pathInSubtype = choices[7] if choices[7] else '' params = ['username:='******'password:='******'operation:=AddFileToDataSet', 'class:=dataStorageService',\ 'params:='+'<#>'.join([dataSetId, subtype, pathInSubtype, url])] print params AddFilesToStorebioinfoDataset.socket.send(messageSep.join(params)) message = AddFilesToStorebioinfoDataset.socket.recv_unicode().encode('utf-8')#, 'ignore' if message != 'something went wrong...': print 'Dataset successfully upploaded to Storebio server' else: print message
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices.history.split(':')) treatTrackAs = ExpandBedSegmentsTool._TRACK_TYPE_CONVERSION_OPTIONS[choices.conversion] GalaxyInterface.expandBedSegments(inFn, galaxyFn, choices.genome, \ int(choices.upstream), int(choices.downstream), \ treatTrackAs, removeChrBorderCrossing=(choices.chrBorderHandling=='Removing'))
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn.gtr If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' genome = choices[1] if choices[0] == 'Yes' else None inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices[2].split(':')) try: sortGtrackFileAndWriteToFile(inFn, galaxyFn, genome) except Exception, e: import sys print >> sys.stderr, e
def execute(choices, galaxyFn=None, username=''): '''Is called when execute-button is pushed by web-user. Should print output as HTML to standard out, which will be directed to a results page in Galaxy history. If getOutputFormat is anything else than HTML, the output should be written to the file with path galaxyFn. If needed, StaticFile can be used to get a path where additional files can be put (e.g. generated image files). choices is a list of selections made by web-user in each options box. ''' #print 'Executing...' idsToUse = map(str.strip,choices[1].split(",")) inFn = ExternalTrackManager.extractFnFromGalaxyTN(choices[0].split(":")) data = open(inFn).read() outputFile = open(galaxyFn, "w") outputFile.write(convertToGtrackFile(data, idsToUse)) outputFile.close()