def execute(cls, choices, galaxyFn=None, username=''): from quick.util.CommonFunctions import getFileSuffix import gold.gsuite.GSuiteComposer as GSuiteComposer from gold.gsuite.GSuite import GSuite from gold.gsuite.GSuiteTrack import registerGSuiteTrackClass, GSuiteTrack, FileGSuiteTrack registerGSuiteTrackClass(FileGSuiteTrack) gSuite = getGSuiteFromGalaxyTN(choices.gsuite) outGSuite = GSuite() for track in gSuite.allTracks(): path = track.path suffix = track.suffix if track.suffix != getFileSuffix( path) else '' uri = FileGSuiteTrack.generateURI(path=path, suffix=suffix) newTrack = GSuiteTrack(uri, title=track.title, trackType=track.trackType, genome=track.genome, attributes=track.attributes) outGSuite.addTrack(newTrack) GSuiteComposer.composeToFile(outGSuite, galaxyFn)
def testComposeLocalUrlGenomeAttributesNonAscii(self): gSuite = GSuite() gSuite.addTrack( GSuiteTrack('galaxy:/12345abc', genome=u'hg18ø', attributes=OrderedDict([('one', 'yes')]))) gSuite.addTrack( GSuiteTrack('file:/path/to/file2', genome='hg19', attributes=OrderedDict([('two', u'nø')]))) gSuite.addTrack( GSuiteTrack( HbGSuiteTrack.generateURI(trackName=[u'track', u'nøme']), genome='hg38', attributes=OrderedDict([('two', 'yes')]))) gSuite.setCustomHeader('my header', u'bø!') output = GSuiteComposer.composeToString(gSuite) targetOutput = \ '##location: local\n' \ '##file format: unknown\n' \ '##track type: unknown\n' \ '##genome: multiple\n' \ '##my header: b%C3%B8!\n' \ '###uri\ttitle\tfile_format\tgenome\tone\ttwo\n' \ 'galaxy:/12345abc\t12345abc\tunknown\thg18%C3%B8\tyes\t.\n' \ 'file:/path/to/file2\tfile2\tunknown\thg19\t.\tn%C3%B8\n' \ 'hb:/track/n%C3%B8me\tn%C3%B8me\tpreprocessed\thg38\t.\tyes\n' self.assertEquals(targetOutput, output)
def getGsuiteFromRandomRows(self, count=10): random.seed(9001) rItem = self.VOCABULARY[random.randint(0, len(self.VOCABULARY) - 1)] rRows = self.getRandomRows(rItem, count) gSuite = GSuite() self.appendGsuiteFromRows(gSuite, rRows, rItem) return gSuite
def execute(cls, choices, galaxyFn=None, username=''): basisTrackNameAsList = choices.basisTrack.split(':') extractionOptions = dict( GalaxyInterface.getTrackExtractionOptions(choices.genome, basisTrackNameAsList)) extractionFormat = extractionOptions[ choices.extFormatLbl] if extractionOptions else None gsuite = getGSuiteFromGalaxyTN(choices.gsuite) outGSuite = GSuite() for gsTrack in gsuite.allTracks(): # outputTrackFn = cls.extraGalaxyFn[gsTrack.title] # print '<br>\n<br>\n output track filename: ', outputTrackFn # print 'path: ', gsTrack.path # print 'parsed uri: ', gsTrack._parsedUri newTrackFileName = gsTrack.title + '.' + extractionFormat outGalaxyFn = ExternalTrackManager.createGalaxyFilesFn( galaxyFn, newTrackFileName) ensurePathExists(outGalaxyFn) uri = GalaxyGSuiteTrack.generateURI(galaxyFn, extraFileName=newTrackFileName) GalaxyInterface.parseExtFormatAndExtractTrackManyBins( choices.genome, basisTrackNameAsList, gsTrack.suffix, gsTrack.path, True, choices.extFormatLbl, outGalaxyFn) outGSuite.addTrack( GSuiteTrack(uri, title=gsTrack.title, fileFormat=gsTrack.fileFormat, trackType=gsTrack.trackType, genome=choices.genome, attributes=gsTrack.attributes)) GSuiteComposer.composeToFile(outGSuite, galaxyFn)
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. ''' import gold.gsuite.GSuiteComposer as GSuiteComposer from gold.gsuite.GSuite import GSuite from gold.gsuite.GSuiteTrack import GSuiteTrack, GalaxyGSuiteTrack from quick.application.UserBinSource import GlobalBinSource from quick.extra.ProgressViewer import ProgressViewer from quick.extra.TrackExtractor import TrackExtractor genome = choices.genome fullGenomeBins = GlobalBinSource(genome) gSuite = getGSuiteFromGalaxyTN(choices.gsuite) progressViewer = ProgressViewer( [(cls.PROGRESS_PROCESS_DESCRIPTION, len(gSuite))], galaxyFn) outGSuite = GSuite() hiddenStorageFn = cls.extraGalaxyFn[getGSuiteHistoryOutputName( 'storage', datasetInfo=choices.gsuite)] fileNameSet = set() for track in gSuite.allTracks(): fileName = cls._getUniqueFileName(fileNameSet, track.trackName) title = track.title attributes = track.attributes fi = cls._getFileFormatInfo(choices, gSuite, genome, track) uri = GalaxyGSuiteTrack.generateURI(galaxyFn=hiddenStorageFn, extraFileName=fileName, suffix=fi.suffix) gSuiteTrack = GSuiteTrack(uri, title=title, genome=genome, attributes=attributes) TrackExtractor.extractOneTrackManyRegsToOneFile( track.trackName, fullGenomeBins, gSuiteTrack.path, fileFormatName=fi.fileFormatName, globalCoords=True, asOriginal=fi.asOriginal, allowOverlaps=fi.allowOverlaps) outGSuite.addTrack(gSuiteTrack) progressViewer.update() primaryFn = cls.extraGalaxyFn[getGSuiteHistoryOutputName( 'primary', datasetInfo=choices.gsuite)] GSuiteComposer.composeToFile(outGSuite, primaryFn)
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 run_on_extracted_variables(cls, ts, analysisBins, choices_numberOfTimesToRandomize, choices_randAlg, choices_randType, galaxyFn, genome): assert choices_numberOfTimesToRandomize==1 #For now, since ts probably needs to be unique each time.. outputGSuite = GSuite() for i in range(0, int(choices_numberOfTimesToRandomize)): print("TF leaf nodes") print(ts.getLeafNodes()) randTvProvider = cls._createTrackViewProvider(ts, analysisBins, genome, choices_randAlg, choices_randType, False, None) # the last False and non are temporary.. randomizedTs = getRandomizedVersionOfTs(ts, randTvProvider) # output files print("Leaf nodes") print(randomizedTs.getLeafNodes()) for j, singleTrackTs in enumerate(randomizedTs.getLeafNodes()): uri = "outputfile" uri = FileGSuiteTrack.generateURI(path='/home/ivargry/outfile_' + str(randint(0,999999999)) + '_' + str(j) + ".bed", suffix='bed', doQuote=False) print("URI: " + uri) title = singleTrackTs.metadata.pop('title') gSuiteTrack = FileGSuiteTrack(uri, title=title + '.randomized', fileFormat='primary', trackType='segments', genome=genome, attributes=singleTrackTs.metadata) outputGSuite.addTrack(gSuiteTrack) singleTrackTs.metadata['trackFilePath'] = gSuiteTrack.path singleTrackTs.metadata['randomization_run'] = i spec = AnalysisSpec(TsWriterStat) res = doAnalysis(spec, analysisBins, randomizedTs) assert galaxyFn != "" GSuiteComposer.composeToFile(outputGSuite, galaxyFn)
def concatenateGSuites(gSuiteList): concatenatedGSuite = GSuite() for gSuite in gSuiteList: concatenatedGSuite.addTracks(gSuite.allTracks()) return concatenatedGSuite
def getRandomGSuite(gSuite, number, seed=9001): rGSuite = GSuite() randomTrackList = getRandomTracks(gSuite, number, seed) for track in randomTrackList: rGSuite.addTrack(track) return rGSuite
def generateSynGSuite(cls, dataOut, galaxyFn, genome): outGSuite = GSuite() g = SimulationPointIter() newData = '' chrNum = 0 for chr in dataOut: # fileName = 'syn-chr' + 'iInterR-' + str(chr[0]) + 'st-' + str(chr[1]) + 'end-' + str( # chr[2]) + 'iInterR-' + str(chr[3]) + 'iIntraR-' + str(chr[4]) + 'prob-' + str(chr[5]) + '--' + str( # chrNum) fileName = 'syn-' + str(chr[0]) + ',' + str(chr[1]) + ',' + str(chr[2]) + ',' + str(chr[3]) +',' + str(chr[4]) +',' + str(chr[5]) uri = GalaxyGSuiteTrack.generateURI(galaxyFn=galaxyFn, extraFileName=fileName, suffix='bed') gSuiteTrack = GSuiteTrack(uri) outFn = gSuiteTrack.path ensurePathExists(outFn) g.createChrTrack(genome, chr[0], PointIter, outFn, chr[3], chr[4], chr[5], chr[1], chr[2]) with open(outFn, 'r') as outputFile: newData += ''.join(outputFile.readlines()) chrNum += 1 if chrNum == len(dataOut): with open(outFn, 'w') as outputFile: outputFile.write(newData) outGSuite.addTrack(GSuiteTrack(uri, title=''.join(fileName), genome=genome)) GSuiteComposer.composeToFile(outGSuite, cls.extraGalaxyFn['synthetic GSuite'])
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. ''' gSuite = getGSuiteFromGalaxyTN(choices.gsuite) attrName = choices.attrName outputGSuite = GSuite() #Writes the information that has been changed to a new track but jumps over all the empty strings for i, track in enumerate(gSuite.allTracks()): if i < cls.MAX_NUM_OF_TRACKS: newAttrValue = getattr(choices, 'selectAttribute%s' % ((i * 2) + 1)) if (attrName == TITLE_COL): track.title = newAttrValue else: track.setAttribute(attrName, newAttrValue) outputGSuite.addTrack(track) #Creates the new GSuite GSuiteComposer.composeToFile(outputGSuite, galaxyFn)
def addResultsToInputGSuite(gsuite, results, attrNames, outputGSuiteFN): ''' Add the values from the analysis results as metadata columns and create a new GSuite. If the new attribute names in attrNames already exist as metadata columns in the GSuite, attributes with added an appropriate _[index] will be added. ''' assert isinstance(attrNames, (list, tuple)), 'attrNames must be of type list or tuple: %s' % str(attrNames) newAttrNames = [] for attrName in attrNames: newAttrNames.append(_updateAttrNameWithIndexIfDuplicate(gsuite, attrName)) outGSuite = GSuite() for gsTrack in gsuite.allTracks(): currentTrackRes = results[gsTrack.title] if len(newAttrNames) == 1: if isinstance(currentTrackRes, (list, tuple)): if currentTrackRes[0]: gsTrack.setAttribute(newAttrNames[0], str(currentTrackRes[0])) else: if currentTrackRes: gsTrack.setAttribute(newAttrNames[0], str(currentTrackRes)) else: assert isinstance(currentTrackRes, (list, tuple)), 'Expected multiple results per track. Attribute names %s' % str(attrNames) for i, resultVal in enumerate(currentTrackRes): if resultVal: gsTrack.setAttribute(newAttrNames[i], str(resultVal)) outGSuite.addTrack(gsTrack) GSuiteComposer.composeToFile(outGSuite, outputGSuiteFN)
def getGSuite(self, category, subCategory, source='All', dataTypes=[], filterFileSuffix=False, selectedFileIDs=None): if source.find('HyperBrowser') > -1: HBGSuite = getSubtracksAsGSuite( 'hg19', ['Sample data', 'Chromatin catalog', category, subCategory]) if selectedFileIDs is not None: gSuite = GSuite() for trackTitle, selected in selectedFileIDs.iteritems(): if selected: for track in HBGSuite.allTracks(): if trackTitle == track.title: gSuite.addTrack(track) break return gSuite else: return HBGSuite else: items = self.getItems(category, subCategory) #Old#gSuite = self.getGSuiteFromItems(items,source,fileTypes,selectedFileIDs) gSuite = self.getGSuiteFromItems(items, source, dataTypes, filterFileSuffix, selectedFileIDs) return gSuite
def convertArchiveToGSuite(archiveToGSuiteIterator, progressViewer=None): gSuite = GSuite() for gSuiteTrack in archiveToGSuiteIterator: gSuite.addTrack(gSuiteTrack) if progressViewer: progressViewer.update() return gSuite
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 (cls, e.g. generated image files). choices is a list of selections made by web-user in each options box. Mandatory unless isRedirectTool() returns True. """ #TODO: add functionality for single gtrack within-track randomization print 'Executing...' inputGsuite = getGSuiteFromGalaxyTN(choices.gs) outputGSuite = GSuite() genome = inputGsuite.genome ts = factory.getFlatTracksTS(genome, choices.gs) randIndex = 0 bins = GlobalBinSource(genome) if choices.randType == TsRandAlgReg.BETWEEN_TRACKS_CATEGORY and \ choices.category not in [None, 'None']: ts = ts.getSplittedByCategoryTS(choices.category) randomizedTs = TrackStructureV2() for subTsKey, subTs in ts.items(): tvProvider = cls.createTrackViewProvider( choices, subTs, bins, genome) randomizedTs[subTsKey] = getRandomizedVersionOfTs( subTs, tvProvider, randIndex) randomizedTs = randomizedTs.getFlattenedTS() else: tvProvider = cls.createTrackViewProvider(choices, ts, bins, genome) randomizedTs = getRandomizedVersionOfTs(ts, tvProvider, randIndex) for singleTrackTs in randomizedTs.getLeafNodes(): uri = GalaxyGSuiteTrack.generateURI( galaxyFn=galaxyFn, extraFileName=os.path.sep.join(singleTrackTs.track.trackName) + '.randomized', suffix='bed') title = singleTrackTs.metadata.pop('title') gSuiteTrack = GSuiteTrack(uri, title=title + '.randomized', fileFormat='primary', trackType='segments', genome=genome, attributes=singleTrackTs.metadata) outputGSuite.addTrack(gSuiteTrack) singleTrackTs.metadata['trackFilePath'] = gSuiteTrack.path spec = AnalysisSpec(TsWriterStat) res = doAnalysis(spec, bins, randomizedTs) GSuiteComposer.composeToFile(outputGSuite, galaxyFn)
def visitAllGSuiteTracksAndReturnOutputAndErrorGSuites( self, gSuite, progressViewer=None, *args, **kwArgs): from gold.gsuite.GSuite import GSuite outGSuite = GSuite() errorGSuite = GSuite() for track in gSuite.allTracks(): try: outGSuite.addTrack( self._gSuiteTrackVisitor.visit(track, *args, **kwArgs)) except Exception as e: track.comment = 'An error occurred for the following track: ' + str( e).partition('\n')[0] errorGSuite.addTrack(track) if progressViewer: progressViewer.update() return outGSuite, errorGSuite
def testSelectAttributesEmpty(self): gSuite = GSuite(trackList=[self.track1, self.track2, self.track3]) outGSuite = GSuiteEditor.selectColumnsFromGSuite(gSuite, selectedAttributes=[], selectTitle=False) self.assertEquals([], outGSuite.attributes) tracks = list(outGSuite.allTracks()) self.assertEquals(OrderedDict(), tracks[0].attributes) self.assertEquals(OrderedDict(), tracks[1].attributes) self.assertEquals(OrderedDict(), tracks[2].attributes)
def testEmptyCompose(self): gSuite = GSuite() output = GSuiteComposer.composeToString(gSuite) targetOutput = \ '##location: unknown\n' \ '##file format: unknown\n' \ '##track type: unknown\n' \ '##genome: unknown\n' self.assertEquals(targetOutput, output)
def testFullCompose(self): gSuite = GSuite() gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/path/to/file1.bed', title='Track', attributes=OrderedDict([('cell', 'k562'), ('antibody', 'cMyb')]))) gSuite.addTrack( GSuiteTrack('http://server.other.com/path/to/file2.bed', title='Track2', attributes=OrderedDict([('cell', 'GM12878'), ('antibody', 'cMyc')]))) gSuite.addTrack( GSuiteTrack('https://server.other.com/path/to/file3.bed', attributes=OrderedDict([('cell', 'GM12878'), ('antibody', 'cMyb')]))) gSuite.addTrack( GSuiteTrack('rsync://server.other.com/path/to/file4;wig', title='Track4', attributes=OrderedDict([('cell', 'NHFL')]))) gSuite.addTrack(GSuiteTrack('hb:/track/name/hierarchy', title='Track')) gSuite.addTrack( GSuiteTrack('galaxy:/ad123dd12fg;btrack?track=track:name', title='Track', attributes=OrderedDict([('cell', 'k562'), ('antibody', 'cMyb')]))) gSuite.addTrack( GSuiteTrack('file:/path/to/file.btrack?track=track:name', title='Track name7', attributes=OrderedDict([('antibody', 'cMyb'), ('extra', 'yes')]))) gSuite.setGenomeOfAllTracks('hg18') gSuite.setCustomHeader('My header', 'Some value') output = GSuiteComposer.composeToString(gSuite) targetOutput = \ '##location: multiple\n' \ '##file format: multiple\n' \ '##track type: unknown\n' \ '##genome: hg18\n' \ '##my header: Some value\n' \ '###uri\ttitle\tfile_format\tcell\tantibody\textra\n' \ 'ftp://server.somewhere.com/path/to/file1.bed\tTrack\tprimary\tk562\tcMyb\t.\n' \ 'http://server.other.com/path/to/file2.bed\tTrack2\tprimary\tGM12878\tcMyc\t.\n' \ 'https://server.other.com/path/to/file3.bed\tfile3.bed\tprimary\tGM12878\tcMyb\t.\n' \ 'rsync://server.other.com/path/to/file4;wig\tTrack4\tprimary\tNHFL\t.\t.\n' \ 'hb:/track/name/hierarchy\tTrack (2)\tpreprocessed\t.\t.\t.\n' \ 'galaxy:/ad123dd12fg;btrack?track=track%3Aname\tTrack (3)\tpreprocessed\tk562\tcMyb\t.\n' \ 'file:/path/to/file.btrack?track=track%3Aname\tTrack name7\tpreprocessed\t.\tcMyb\tyes\n' self.assertEquals(targetOutput, output)
def testSimpleTitleDuplicate(self): gSuite = GSuite() gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/path/to/file1.bed')) gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/other/path/to/file1.bed')) gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/third/path/to/file1.bed')) gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/last/path/to/file1.bed')) self.assertEqual( ['file1.bed', 'file1.bed (2)', 'file1.bed (3)', 'file1.bed (4)'], gSuite.allTrackTitles())
def concatenateGSuitesAddingCategories(gSuiteList, categoryColumnTitle, categoryList): concatenatedGSuite = GSuite() assert len(gSuiteList) == len(categoryList) for i, gSuite in enumerate(gSuiteList): tracksToAdd = [track for track in gSuite.allTracks()] for track in tracksToAdd: track.setAttribute(categoryColumnTitle, categoryList[i]) concatenatedGSuite.addTracks(tracksToAdd) return concatenatedGSuite
def parseLines(gSuiteLines, allowTrackSummaryToOverrideHeaders=True): """ :return GSuite: """ colNames = None headerVars = {} gSuite = GSuite() trackLines = [] level = 0 for line in gSuiteLines: line = line.rstrip(' \t\r\n') _checkCharUsageOfPhrase(line) if line.startswith( '####'): # Deprecated, but kept for backwards compatibility level = _setLevelAndCheckOrder(level, 4) headerVars[GENOME_HEADER] = _parseGenomeLine(line) elif line.startswith('###'): level = _setLevelAndCheckOrder(level, 3) colNames = _parseColumnSpecLine(line) elif line.startswith('##'): level = _setLevelAndCheckOrder(level, 2) key, val = _parseHeaderLine(line) headerVars = _updateHeaderVars(headerVars, key, val) elif line == '' or line.startswith('#'): pass else: level = _setLevelAndCheckOrder(level, 5) trackLines.append(line) # headerVars = _updateUnsetHeaderVarsWithDefaultVals(headerVars) if not colNames: colNames = _getDefaultColNames() for trackLine in trackLines: gSuite.addTrack(_parseTrackLine(trackLine, colNames, headerVars), allowDuplicateTitles=False) _setCustomHeaders(gSuite, headerVars) if not allowTrackSummaryToOverrideHeaders: _compareTextHeadersWithTrackSummaryHeaders(headerVars, gSuite) return gSuite
def testEmptyGSuite(self): gSuite = GSuite() self.assertEqual('unknown', gSuite.location) self.assertEqual('unknown', gSuite.fileFormat) self.assertEqual('unknown', gSuite.trackType) self.assertEqual('unknown', gSuite.genome) self.assertEqual([], gSuite.attributes) self.assertEqual(False, gSuite.isPreprocessed()) self.assertEqual(False, gSuite.hasCustomTitles()) self.assertEqual(0, gSuite.numTracks()) self.assertEqual(0, len(list(gSuite.allTracks()))) self.assertEqual(0, len(list(gSuite.allTrackTitles()))) self.assertEqual(0, len(list(gSuite.allTrackTypes())))
def testSelectRows(self): gSuite = GSuite(trackList=[self.track1, self.track2, self.track3]) outGSuite = GSuiteEditor.selectRowsFromGSuiteByIndex(gSuite, [0,2]) self.assertEquals([self.track1, self.track3], list(outGSuite.allTracks())) outGSuite = GSuiteEditor.selectRowsFromGSuiteByIndex(gSuite, []) self.assertEquals([], list(outGSuite.allTracks())) outGSuite = GSuiteEditor.selectRowsFromGSuiteByTitle(gSuite, ['Track1', 'Track3']) self.assertEquals([self.track1, self.track3], list(outGSuite.allTracks())) outGSuite = GSuiteEditor.selectRowsFromGSuiteByIndex(gSuite, []) self.assertEquals([], list(outGSuite.allTracks())) self.assertRaises(IndexError, GSuiteEditor.selectRowsFromGSuiteByIndex, gSuite, [3]) self.assertRaises(KeyError, GSuiteEditor.selectRowsFromGSuiteByTitle, gSuite, ['Track4'])
def testComposeRemoteOnlyUrl(self): gSuite = GSuite() gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/path/to/file1')) gSuite.addTrack(GSuiteTrack('http://server.other.com/path/to/file2')) output = GSuiteComposer.composeToString(gSuite) targetOutput = \ '##location: remote\n' \ '##file format: unknown\n' \ '##track type: unknown\n' \ '##genome: unknown\n' \ '###uri\ttitle\n' \ 'ftp://server.somewhere.com/path/to/file1\tfile1\n' \ 'http://server.other.com/path/to/file2\tfile2\n' self.assertEquals(targetOutput, output)
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. ''' import gold.gsuite.GSuiteComposer as GSuiteComposer from gold.gsuite.GSuite import GSuite from gold.gsuite.GSuiteTrack import GSuiteTrack, GalaxyGSuiteTrack inGSuite = getGSuiteFromGalaxyTN(choices.gsuite) outGSuite = GSuite() newSuffix = cls._getNewSuffixIfAny(choices) for track in cls._getSelectedTracks(choices, inGSuite): title, suffix = cls._getExportTrackTitleAndSuffix(track, newSuffix) trackGalaxyFn = cls.extraGalaxyFn[title] if choices.changeFormat == cls.OUTPUT_FORMAT_CONVERT: from gold.origdata.FileFormatComposer import getComposerClsFromFileFormatName geSource = track.getGenomeElementSource(printWarnings=False) composerCls = getComposerClsFromFileFormatName( choices.outputFormat) composer = composerCls(geSource) composer.composeToFile(trackGalaxyFn) else: shutil.copy(track.path, trackGalaxyFn) uri = GalaxyGSuiteTrack.generateURI(galaxyFn=trackGalaxyFn, suffix=suffix) gSuiteTrack = GSuiteTrack(uri, title=title, genome=track.genome, attributes=track.attributes) outGSuite.addTrack(gSuiteTrack) GSuiteComposer.composeToFile(outGSuite, galaxyFn)
def selectColumnsFromGSuite(gSuite, selectedAttributes, selectTitle=True): reducedGSuite = GSuite() for attribute in selectedAttributes: if attribute not in gSuite.attributes: raise KeyError('Attribute "%s" is not defined in the GSuite file' % attribute) for track in gSuite.allTracks(): reducedTrack = copy(track) reducedTrack.attributes = \ OrderedDict([(key, reducedTrack.attributes[key]) for key in selectedAttributes if key in reducedTrack.attributes]) if not selectTitle: reducedTrack.title = reducedTrack.uri reducedGSuite.addTrack(reducedTrack) return reducedGSuite
def getGSuiteFromItems(self, items, source='All', dataTypes=[], filterFileSuffix=False, selectedFileIDs=None): source = source.strip() sourceTool = 'None' gSuite = GSuite() for key, value in self.SOURCE.items(): if source == value: sourceTool = key ##return source+'--'+sourceTool for item in items: if not 'All' in source and item.sourceTool != sourceTool: continue #attribute_col_name = self.DB.getAttributeNameFromReadableName(item.sourceTable,item.toolAttr) try: Rows = self.getRows(item, filterFileSuffix, False) except Exception as e: print item raise e #Old#rows = self.filterRowsByFileTypes(Rows,fileTypes) rows = self.filterRowsByDataTypes(Rows, dataTypes) ##rows = [] ##if len(fileTypes)== 0: ## rows = allRows ##else: ## for row in allRows: ## if self._isValidRow(row,fileTypes): ## rows.append(row) #Pass gSuite byref (which is the default in python if you will change, not reset): self.appendGsuiteFromRows(gSuite, rows, item, selectedFileIDs) return gSuite
def testComposeUrlTitleLocationTrackType(self): gSuite = GSuite() gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/path/to/file1', title='Track1', trackType='points')) gSuite.addTrack( GSuiteTrack('file:/path/to/file2', trackType='segments')) output = GSuiteComposer.composeToString(gSuite) targetOutput = \ '##location: multiple\n' \ '##file format: unknown\n' \ '##track type: multiple\n' \ '##genome: unknown\n' \ '###uri\ttitle\ttrack_type\n' \ 'ftp://server.somewhere.com/path/to/file1\tTrack1\tpoints\n' \ 'file:/path/to/file2\tfile2\tsegments\n' self.assertEquals(targetOutput, output)
def testComposeRemoteUrlGenomeFileFormat(self): gSuite = GSuite() gSuite.addTrack( GSuiteTrack('ftp://server.somewhere.com/path/to/file1.bed', genome='hg18')) gSuite.addTrack( GSuiteTrack('http://server.other.com/path/to/file2', genome='hg18')) output = GSuiteComposer.composeToString(gSuite) targetOutput = \ '##location: remote\n' \ '##file format: unknown\n' \ '##track type: unknown\n' \ '##genome: hg18\n' \ '###uri\ttitle\tfile_format\n' \ 'ftp://server.somewhere.com/path/to/file1.bed\tfile1.bed\tprimary\n' \ 'http://server.other.com/path/to/file2\tfile2\tunknown\n' self.assertEquals(targetOutput, output)