def runIntegrationTest(): track = Track(['melting']) track2 = Track(['melting']) geSource = GenomeElementSource('/usit/titan/u1/bjarnej/new_hb', 'old_NCBI') print geSource coll = GlobalCollectorPP(geSource, track, track2, ZipperStat, CountStat, MeanStat) coll = XBinnerPP(coll, 5) # for x,y in coll: # print x,y coll = YSummarizerPP(coll, lmean) results = [[result[i] for result in coll] for i in range(2)] print results slide = 3 x = results[0] y = results[1] for i in range(len(x) - (slide - 1)): print(y[i] + y[i + 1] + y[i + 2]) / slide
def runIntegrationTest(): # myTrack = Track(['bs','wenjie1']) # myTrack2 = Track(['melting']) # myTrack = Track(['melting','2state']) myTrack = Track(['melting', '2state']) myTrack2 = Track(['melting']) # userBinSource = AutoBinner(parseRegSpec('chr1:1-1000000', genome), 100000) #fixme: do a conversion from binSpecification to binSource.. # userBinSource = UserBinSource('chr1:1-10000','1000') #fixme: do a conversion from binSpecification to binSource.. # userBinSource = UserBinSource('chr1:184916000-184936000','10000') #fixme: do a conversion from binSpecification to binSource.. # userBinSource = UserBinSource('chr1:11913000-11916000','1000') #fixme: do a conversion from binSpecification to binSource.. userBinSource = UserBinSource( 'chrM', '10000') #fixme: do a conversion from binSpecification to binSource.. #regionIter = GenomeElementSource('/Volumes/insilico.titan.uio.no/HyperBrowser/new_hb/data/2sSegs.bed','hg18') #genomeAnchor = GenomeRegion(genome='hg18', chr='chrM', start=0, end=14) #trackView = TrackView([2, 6, 12], [3, 9, 14], None, None, genomeAnchor) #trackView2 = TrackView(None, None, [79, 70, 76, 68, 69, 71, 79, 79, 80, 73, 68, 69, 78, 80], None, genomeAnchor) #myTrack = MyTrack(trackView) #myTrack2 = MyTrack(trackView2) print StatRunner.run(userBinSource, myTrack, myTrack2, MeanInsideOutsideTwoTailRandStat)
def _compute(self): kwArgs = copy(self._kwArgs) if 'rawStatistic' in kwArgs: del kwArgs['rawStatistic'] matrixElRes = [] tr1Subtypes = ProcTrackOptions.getSubtypes(self.getGenome(), self._track.trackName, True) assert len(tr1Subtypes) > 0 for subtype1 in tr1Subtypes:#['0','1']: for subtype2 in ['0','1']: tn1 = self._track.trackName + [subtype1] tn2 = self._track2.trackName + [subtype2] if not os.path.exists(createDirPath(tn1,self.getGenome())) or not os.path.exists(createDirPath(tn2,self.getGenome())): raise IncompatibleTracksError #print ',' track1 = Track( tn1) track1.formatConverters = self._track.formatConverters track2 = Track( tn2) track2.formatConverters = self._track2.formatConverters #self._addChild(self._rawStatistic(self._region, track1, track2, **kwArgs) ) matrixElRes.append( self._rawStatistic(self._region, track1, track2, **kwArgs).getResult() ) ResultsMemoizer.flushStoredResults() #assert len(self._children) == 7 #return dict(zip( '00,01,10,11'.split(','), [x.getResult() for x in self._children[3:]])) allChildRes = array(matrixElRes) #allChildRes = array([x.getResult() for x in self._children[3:]]) allChildRes = allChildRes.reshape((-1,2)) return OrderedDict([('Matrix', allChildRes.tolist()), ('Rows', tr1Subtypes), ('Cols', ['Case','Control'])])
def _createChildren(self): kwArgs = copy(self._kwArgs) if 'rawStatistic' in kwArgs: del kwArgs['rawStatistic'] track2 = self._track2 if hasattr(self, '_track2') else None self._addChild( FormatSpecStat(self._region, self._track, TrackFormatReq(dense=False, val='tc') ) ) #self._track.formatConverters = 'Dummy' #to avoid check of tracks not being used.. #self._track2.formatConverters = 'Dummy' #to avoid check of tracks not being used.. #self._addChild( RawDataStat(self._region, self._track2, TrackFormatReq(dense=False, val='tc') ) ) self._addChild( self._rawStatistic(self._region, self._track, track2, **kwArgs) ) #This will actually compute, without any use for it. self._indexOfFirstSubCatChild = len(self._children) for subtype1 in ['0','1']: #for subtype2 in ['0','1']: tn1 = self._track.trackName + [subtype1] if not os.path.exists(createDirPath(tn1, self.getGenome())): #logMessage('DID NOT EXIST.. '+createOrigPath(self.getGenome(),tn1)) raise IncompatibleTracksError #else: # logMessage('DID EXIST') track1 = Track( tn1) track1.formatConverters = self._track.formatConverters #track2 = Track( self._track2.trackName + [subtype2]) #track2.formatConverters = self._track2.formatConverters self._addChild(self._rawStatistic(self._region, track1, track2, **kwArgs) )
def _createChildren(self): kwArgs = copy(self._kwArgs) if 'rawStatistic' in kwArgs: del kwArgs['rawStatistic'] track2 = self._track2 if hasattr(self, '_track2') else None self._addChild( FormatSpecStat(self._region, self._track, TrackFormatReq(dense=False, val='tc'))) #self._track.formatConverters = 'Dummy' #to avoid check of tracks not being used.. #self._track2.formatConverters = 'Dummy' #to avoid check of tracks not being used.. #self._addChild( RawDataStat(self._region, self._track2, TrackFormatReq(dense=False, val='tc') ) ) self._addChild( self._rawStatistic( self._region, self._track, track2, ** kwArgs)) #This will actually compute, without any use for it. self._indexOfFirstSubCatChild = len(self._children) for subtype1 in ['0', '1']: #for subtype2 in ['0','1']: tn1 = self._track.trackName + [subtype1] if not os.path.exists(createDirPath(tn1, self.getGenome())): #logMessage('DID NOT EXIST.. '+createOrigPath(self.getGenome(),tn1)) raise IncompatibleTracksError #else: # logMessage('DID EXIST') track1 = Track(tn1) track1.formatConverters = self._track.formatConverters #track2 = Track( self._track2.trackName + [subtype2]) #track2.formatConverters = self._track2.formatConverters self._addChild( self._rawStatistic(self._region, track1, track2, **kwArgs))
def _buildTestTrees(self): # inputTree splitOnA splitOnB pairwise (A vs B) # / \ | / \ / \ # A B C D E t1_t2 t1_t3 # | / \ / \ /\ /\ / \ / \ # C D E A B A B A B Q R Q R # | | | | / \ | | | | | | | | # t1 t2 t3 t1 D E C t2 C t3 t1 t2 t1 t2 # | | | | # t2 t3 t1 t1 self.t1 = SingleTrackTS(Track(['t1']), {'field 1': 'value 1'}) self.t2 = SingleTrackTS(Track(['t2']), { 'field 1': 'value 2', 'field 2': '6' }) self.t3 = SingleTrackTS(Track(['t3']), { 'field 1': 'value 2', 'field 3': 'None' }) self.inputTree = TrackStructureV2() self.inputTree['A'] = TrackStructureV2() self.inputTree['A']['C'] = self.t1 self.inputTree['B'] = TrackStructureV2() self.inputTree['B']['D'] = self.t2 self.inputTree['B']['E'] = self.t3 # correct result of the input tree splitted on node A self.splittedOnNodeA = TrackStructureV2() self.splittedOnNodeA['C'] = TrackStructureV2() self.splittedOnNodeA['C']['A'] = self.t1 self.splittedOnNodeA['C']['B'] = TrackStructureV2() self.splittedOnNodeA['C']['B']['D'] = self.t2 self.splittedOnNodeA['C']['B']['E'] = self.t3 # correct result of the input tree splitted on node B self.splittedOnNodeB = TrackStructureV2() self.splittedOnNodeB['D'] = TrackStructureV2() self.splittedOnNodeB['D']['A'] = TrackStructureV2() self.splittedOnNodeB['D']['A']['C'] = self.t1 self.splittedOnNodeB['D']['B'] = self.t2 self.splittedOnNodeB['E'] = TrackStructureV2() self.splittedOnNodeB['E']['A'] = TrackStructureV2() self.splittedOnNodeB['E']['A']['C'] = self.t1 self.splittedOnNodeB['E']['B'] = self.t3 self.pairwiseCombinations = TrackStructureV2() self.pairwiseCombinations["['t1']_['t2']"] = TrackStructureV2() self.pairwiseCombinations["['t1']_['t2']"]['query'] = self.t1 self.pairwiseCombinations["['t1']_['t2']"]['reference'] = self.t2 self.pairwiseCombinations["['t1']_['t3']"] = TrackStructureV2() self.pairwiseCombinations["['t1']_['t3']"]['query'] = self.t1 self.pairwiseCombinations["['t1']_['t3']"]['reference'] = self.t3 self.flatTrackStructure = FlatTracksTS() self.flatTrackStructure['A'] = self.t1 self.flatTrackStructure['B'] = self.t2 self.flatTrackStructure['C'] = self.t3
def test2(): myTrack = Track(['melting']) myTrack2 = Track(['genes', 'refseq']) userBinSource = UserBinSource('chr1:0-10', '10') res = StatRunner.run(userBinSource, myTrack, myTrack2, wrapClass(CustomRStat, {'scriptFn': fn})) resColl = ResultsCollection() resColl.addResults(None, res) print resColl.getHtmlString()
def testSplitMagic(self): self.assertTrue( isinstance( SumStat([GenomeRegion('TestGenome', 'chr21', 0, 100)], Track(['dummy'])), SumStatSplittable)) self.assertTrue( isinstance( SumStat(GenomeRegion('TestGenome', 'chr21', 0, 150), Track(['dummy'])), SumStatSplittable)) self.assertTrue( isinstance( SumStat(GenomeRegion('TestGenome', 'chr21', 0, 100), Track(['dummy'])), SumStatUnsplittable))
def testMemoMagic(self): stat1v1 = SumStat(GenomeRegion('TestGenome', 'chr21', 0, 100), Track(['dummy'])) stat1v1._result = 1 self.assertEqual(stat1v1._result, 1) stat1v2 = SumStat(GenomeRegion('TestGenome', 'chr21', 0, 100), Track(['dummy'])) self.assertEqual(stat1v2._result, 1) stat2 = SumStat(GenomeRegion('TestGenome', 'chr21', 0, 99), Track(['dummy'])) self.assertFalse(stat2.hasResult())
def test1(): #myTrack = Track(['melting']) #myTrack2 = Track(['curvature']) myTrack = Track(['genes', 'refseq']) myTrack2 = Track(['repeats']) rStat = CustomRStatUnsplittable(GenomeRegion('hg18', 'chr1', 0, 100000000), myTrack, myTrack2, scriptFn=fn) #rStat._codeLines = scriptLines #rStat.compute() print rStat.getResult()
def getTrackCombinations(inputList): '''inputList is a list of tracks and/or GSuites. The return value of the method is each product element of the given input elements.''' expandedTrackList = [None] * len(inputList) for index, trackListElement in enumerate(inputList): if isinstance(trackListElement, GSuite): expandedTrackList[index] = [ Track(gSuiteTrack.trackName) for gSuiteTrack in trackListElement.allTracks() ] else: expandedTrackList[index] = [Track(trackListElement)] import itertools trackCombinations = itertools.product(expandedTrackList) return trackCombinations
def runIntegrationTest(): track = Track(['melting']) track2 = Track(['melting']) geSource = GenomeElementSource('M:\\Hyperbrowser\\new_hb\\2sSegs.bed', 'hg18') # Randomized p-value distribution data = StatRunner.run(geSource, track, track2, RandomizationManagerStat, MeanStat, 5) print data l = [index for index in range(len(data)) if data[index] < 1.0] d2 = [data[index] for index in range(len(data)) if data[index] < 1.0] hist(d2, 100) show()
def run(self): tracks = [t.trackName for t in self._gsuite.allTracks()] trackTitles = self._gsuite.allTrackTitles() results = OrderedDict() analysisSpec = AnalysisSpec(SummarizedInteractionWithOtherTracksStat) analysisSpec.addParameter('rawStatistic', self._rawStatistic) analysisSpec.addParameter('summaryFunc', self._summaryFunction) analysisSpec.addParameter('reverse', self._reversed) for t1Title, t1 in zip(trackTitles, tracks): for t2Title, t2 in zip(trackTitles, tracks): if t1Title != t2Title: result = doAnalysis(analysisSpec, self._analysisBins, [Track(t1), Track(t2)]) resultDict = result.getGlobalResult() # if 'Result' in resultDict: results[(t1Title, t2Title)] = resultDict['Result']
def updateTrackInfoToVersion15(genome, trackName): "genome trackName" if isinstance(trackName, str): trackName = qcf.convertTNstrToTNListFormat(trackName) ti = TrackInfo(genome, trackName) ti.preProcOverlapRules = [] trackFormatList = [] for allowOverlaps in [True, False]: from gold.origdata.PreProcessUtils import PreProcessUtils if PreProcessUtils.preProcFilesExist(genome, trackName, allowOverlaps): ti.preProcOverlapRules.append(allowOverlaps) if PreProcessUtils.mergedPreProcFilesExist(genome, trackName, allowOverlaps): mergedPrefixInfoDict = PreProcessUtils.getMergedPrefixInfoDict(genome, trackName, allowOverlaps) ti.mergedPrefixInfoDictPerOverlapRule[allowOverlaps] = mergedPrefixInfoDict trackFormatList.append(TrackFormat.createInstanceFromPrefixInfoDict(mergedPrefixInfoDict)) if trackFormatList: ti.trackFormatHash = hash(tuple(trackFormatList)) if not ti.geSourceVersion: ti.geSourceVersion = ti.preProcVersion ti.preProcVersion = '1.5' origPath = qcf.createOrigPath(genome, trackName) if os.path.exists(origPath): ti.id = ti.constructIdFromPath(genome, qcf.createOrigPath(genome, trackName), ti.geSourceVersion, ti.preProcVersion) print ti.id, ti.preProcOverlapRules, ti.mergedPrefixInfoDictPerOverlapRule, ti.trackFormatHash, ti.geSourceVersion, ti.preProcVersion ti.store() print Track(trackName).getUniqueKey(genome)
def __init__(self, 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. ''' analysisDefString = REPLACE_TEMPLATES[ '$MCFDRv3$'] + ' -> CollectionBinnedHypothesisWrapperStat' analysisSpec = AnalysisDefHandler(analysisDefString) analysisSpec.setChoice('MCFDR sampling depth', choices.mcfdrDepth) analysisSpec.addParameter('assumptions', 'PermutedSegsAndIntersegsTrack_') if choices.question == "question 8": analysisSpec.addParameter('rawStatistic', 'MultitrackRawBinnedOverlapV2Stat') else: analysisSpec.addParameter('rawStatistic', 'MultitrackRawSingleBinV2Stat') # analysisSpec.addParameter('pairwiseStatistic', choices.stat) analysisSpec.addParameter('summaryFunc', choices.summaryFunc) analysisSpec.addParameter('tail', 'right-tail') analysisSpec.addParameter('localBinSize', choices.binSize) analysisSpec.addParameter('question', choices.question) analysisBins = GlobalBinSource(choices.genome) gsuite = getGSuiteFromGalaxyTN(choices.tracks) tracks = [Track(x.trackName) for x in gsuite.allTracks()] tracks = tracks[0:2] results = doAnalysis(analysisSpec, analysisBins, tracks) print results
def getGSuiteRipleysKData(self, bpWindow=1000, analysisBins=None): resDict = OrderedDict() ripleysK = AnalysisSpec(RipleysKStat) ripleysK.addParameter('bpWindow', str(bpWindow)) for track in self._gSuite.allTracks(): ripleysKResults = doAnalysis(ripleysK, analysisBins, [Track(track.trackName)]) resDict[track.title] = ripleysKResults.getGlobalResult()['Result'] return resDict
def handleSameTrack(cls, trackName, regSpec, binSpec, genome, galaxyFn): analysisSpec = AnalysisSpec(RawOverlapToSelfStat) analysisBins = GalaxyInterface._getUserBinSource( regSpec, binSpec, genome) return doAnalysis(analysisSpec, analysisBins, [Track(trackName)]).getGlobalResult()
def __init__(self, name, motifid): self.motifid = motifid self.name = str(name[4]) self.motif = motifs[ motifid] # motifs should already be a dict with motifs self.peaks = [] # Peaks in this transcription factor self.peakTrack = Track(name) self.trackName = name self._addPeaks()
def runMultipleSingleValStatsOnTracks(gsuite, stats, analysisBins, queryTrack=None): ''' gsuite: The gsuite of tracks stats: List of statistics analysisBins: BinSource object queryTrack: should be defined if there are stats that need to run on two tracks (e.g. overlap) Returns an OrderedDict: Track title -> OrderedDict: Stat name -> single value''' assert stats is not None, 'stats argument not defined' assert type(stats) in [str, list ], '''stats argument must be a list of statistics or ^-separated string of statistic names''' resultsDict = OrderedDict() from quick.statistic.GenericResultsCombinerStat import GenericResultsCombinerStat additionalAnalysisSpec = AnalysisSpec(GenericResultsCombinerStat) statsParam = stats if isinstance(stats, basestring) else "^".join( [x.__name__ for x in stats]) additionalAnalysisSpec.addParameter( 'rawStatistics', statsParam) #use ^ separator to add additional stat classes. for refTrack in gsuite.allTracks(): if refTrack.title not in resultsDict: resultsDict[refTrack.title] = OrderedDict() tracks = [Track(refTrack.trackName), queryTrack ] if queryTrack else [Track(refTrack.trackName)] additionalResult = doAnalysis(additionalAnalysisSpec, analysisBins, tracks).getGlobalResult() for statClassName, res in additionalResult.iteritems(): statPrettyName = CommonConstants.STATISTIC_CLASS_NAME_TO_NATURAL_NAME_DICT[ statClassName] if statClassName in CommonConstants.STATISTIC_CLASS_NAME_TO_NATURAL_NAME_DICT else statClassName resultsDict[refTrack.title][statPrettyName] = res return resultsDict
def _createChildren(self): kwArgs = copy(self._kwArgs) if 'rawStatistic' in kwArgs: del kwArgs['rawStatistic'] self._addChild( RawDataStat(self._region, self._track, TrackFormatReq(dense=False, val='tc') ) ) self._addChild( RawDataStat(self._region, self._track2, TrackFormatReq(dense=False, val='tc') ) ) self._addChild( self._rawStatistic(self._region, self._track, self._track2, **kwArgs) ) #try: for subtype1 in ['0','1']: for subtype2 in ['0','1']: tn1 = self._track.trackName + [subtype1] tn2 = self._track2.trackName + [subtype2] if not os.path.exists(createDirPath(tn1, self.getGenome())) or not os.path.exists(createDirPath(tn2,self.getGenome())): raise IncompatibleTracksError track1 = Track( tn1) track1.formatConverters = self._track.formatConverters track2 = Track( tn2) track2.formatConverters = self._track2.formatConverters self._addChild(self._rawStatistic(self._region, track1, track2, **kwArgs) )
def doAnalysisGSuitePairwise(analysisSpec, analysisBins, gSuite): ''' For each pair of tracks in a GSuite run the analysis (analysisSpec). Returns an OrderedDict where the key is a tuple of track titles, the value is a Results object. ''' results = OrderedDict() for trackPair in GSuiteAPI.generateAllTrackPairsInGSuite(gSuite): tracks = [Track(t.trackName) for t in trackPair] result = doAnalysis(analysisSpec, analysisBins, tracks) results[(t.title for t in trackPair)] = result return results
def runIntegrationTest(): track = Track(['melting']) track2 = Track(['melting']) #regionIter = [_getRegion(c,s,e) for c,s,e in [('M',1000,2000),('M',2000,5000),('M',1000,15000)]]#('M',4000,4000)] ] regionIter = GenomeElementSource('Z:\\new_hb\\2sSegs.bed', 'hg18') # segments: genomeAnchor = GenomeRegion(genome='hg18', chr='chrM', start=0, end=50) trackView = TrackView(genomeAnchor, [2, 16, 23, 40], [9, 20, 26, 45], None, 4, None) trackView2 = TrackView(genomeAnchor, [4, 8, 22], [6, 16, 24], None, 3, None) # data = StatRunner.run(regionIter, track, track2, RawOverlapStat, trackView, trackView2) # data = StatRunner.run(regionIter, track, track2, DerivedOverlapStat, trackView, trackView2) data = StatRunner.run(regionIter, track, track2, AccuracyStat, trackView, trackView2) print data param = "cc" for el in data: s = SingleValExtractor(el, param) print s.getVal()
def testComputeDivideByZero(self): self.statParentDivByZero = MyStatistic(GenomeRegion('TestGenome','chr1',0,1), \ Track(['dummy']), True) self.statParentDivByZero.compute() self.assertEqual(True, self.statParentDivByZero.hasResult()) self.assertRaises(NoneResultError, self.statParentDivByZero.getResult) self.assertEqual( 2, len(self.statParentDivByZero._children[1]._childResults)) self.assertEqual([None, None], self.statParentDivByZero._children[1]._childResults) self.assertRaises(NoneResultError, self.statParentDivByZero._children[1].getResult)
def oldTest(): results = [] for chr in ['chr' + str(i) for i in range(1, 21)]: userBinSource = AutoBinner( parseRegSpec(chr, DEFAULT_GENOME), 1e9) #fixme: do a conversion from binSpecification to binSource.. trackName1 = ['hpv_200kb'] trackName2 = ['allTss'] #trackName2 = ['melting','zvals','11mers'] #trackName2 = ['melting'] #trackName1 = ['melting'] res = StatRunner.run(userBinSource, Track(trackName1), Track(trackName2), RawOverlapStat) results.append(res[0]) print res globResults = {} for key in results[0]: globResults[key] = sum(res[key] for res in results) print globResults tp, fp, tn, fn = [globResults[key] for key in 'TP,FP,TN,FN'.split(',')] print 'freq near HPV: ', 1.0 * tp / (tp + fp) print 'freq far from HPV: ', 1.0 * fn / (tn + fn)
def _collectTracks(self): tracks = [self._track, self._track2] #Map trackNameIntensity over to extraTrack, if extraTrack does not exist in itself (to allow selection of third track in GUI and through some existing code interfaces) if 'trackNameIntensity' in self._kwArgs: if not 'extraTracks' in self._kwArgs: self._kwArgs['extraTracks'] = self._kwArgs[ 'trackNameIntensity'] if 'extraTracks' in self._kwArgs: from gold.util.CommonConstants import MULTIPLE_EXTRA_TRACKS_SEPARATOR extraTracks = self._kwArgs['extraTracks'] if isinstance(extraTracks, basestring): extraTracks = extraTracks.split( MULTIPLE_EXTRA_TRACKS_SEPARATOR) for extraT in extraTracks: #HACK for tests if isinstance( extraT, (Track, SampleTrack)): #SampleTrack is used by tests if str(extraT) not in self._trackDict: self._trackDict[str(extraT)] = extraT tracks.append(extraT) else: tracks.append(self._trackDict[str(extraT)]) ##################################### else: if isinstance(extraT, basestring): #extraT = extraT.split('|') #extraT = re.split('\^|\|',extraT) extraT = convertTNstrToTNListFormat(extraT, doUnquoting=False) if type(extraT) == list: from urllib import unquote extraT = [unquote(part) for part in extraT] from quick.application.ExternalTrackManager import ExternalTrackManager if ExternalTrackManager.isGalaxyTrack(extraT): extraT = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN( self.getGenome(), extraT) #extraT = PlainTrack(extraT) #extraT = Track(extraT) #tracks.append(extraT) if str(extraT) not in self._trackDict: extraTrack = Track(extraT) self._trackDict[str(extraT)] = extraTrack tracks.append(extraTrack) else: tracks.append(self._trackDict[str(extraT)]) return tracks
def __init__(self, choices): global motifs, snps, peaks, transcription_factors, regions, genome, snpsTrack genome = choices.genome transcription_factors = [] # Add all internal motifs to a dictionary self._addAllMotifsFromInternalPWMs() # Preprocess and store snp data file #trackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(choices.genome, choices.snp) #snpsTrack = PlainTrack(trackName) #print snpsTrack # Do it the "old" way since I do not know how to get the 4th column from trackview #print trackName self._getSnpData(choices.snp) #.split(":")[2]) #tv = snpsTrack.getTrackView(GenomeRegion(genome, "chr1", 0, 10000)) #print tv.allExtrasAsDictOfNumpyArrays() #print snps # Add some fake mutations #snps[1].append(SNP(1, 1708718, "C", "A")) #snps[1].append(SNP(1, 1708720, "G", "C")) # Add all transcription factor tracks from gsuite file gsuite = getGSuiteFromGalaxyTN(choices.gsuite) for x in gsuite.allTracks(): if not "pwm" in x.attributes: print "Error: pwm attribute is missing from track " + str( x.trackName ) + ". The transcription factor was removed from analysis." #elif x.attributes["pwm"] not in motifs: # print "Error: pwm " + x.attributes["pwm"] + " was not found in the list of supported pwms. The transcription factor " + str(x.trackName) + " was removed from analysis." else: for pwm in x.attributes["pwm"].split(","): if pwm not in motifs: print "<p>Warning: pwm " + pwm + " was not found in the list of supported pwms. The transcription factor " + str( x.trackName ) + " will not be analysed with this pwm.</p>" else: transcription_factors.append( TranscriptionFactor(x.trackName, pwm)) Track(x.trackName) self.runAnalysis()
def _compute(self): kwArgs = copy(self._kwArgs) if 'rawStatistic' in kwArgs: del kwArgs['rawStatistic'] matrixElRes = [] tr1Subtypes = ProcTrackOptions.getSubtypes(self.getGenome(), self._track.trackName, True) tr2Subtypes = ProcTrackOptions.getSubtypes(self.getGenome(), self._track2.trackName, True) assert len(tr1Subtypes) > 0, str(self._track.trackName) assert len(tr2Subtypes) > 0, str(self._track2.trackName) if 'minimal' in self._kwArgs: tr1Subtypes = tr1Subtypes[:1] tr2Subtypes = tr2Subtypes[:1] for subtype1 in tr1Subtypes: #['0','1']: #for subtype2 in ['0','1']: for subtype2 in tr2Subtypes: # print ',' tn1 = self._track.trackName + [subtype1] tn2 = self._track2.trackName + [subtype2] if not os.path.exists(createDirPath( tn1, self.getGenome())) or not os.path.exists( createDirPath(tn2, self.getGenome())): raise IncompatibleTracksError #print ',' track1 = Track(tn1) track1.formatConverters = self._track.formatConverters track2 = Track(tn2) track2.formatConverters = self._track2.formatConverters #self._addChild(self._rawStatistic(self._region, track1, track2, **kwArgs) ) matrixElRes.append( self._rawStatistic(self._region, track1, track2, **kwArgs).getResult()) ResultsMemoizer.flushStoredResults() #assert len(self._children) == 7 #return dict(zip( '00,01,10,11'.split(','), [x.getResult() for x in self._children[3:]])) allChildRes = array(matrixElRes) #allChildRes = array([x.getResult() for x in self._children[3:]]) allChildRes = allChildRes.reshape((len(tr1Subtypes), len(tr2Subtypes))) return { 'Result': OrderedDict([('Matrix', allChildRes.tolist()), ('Rows', tr1Subtypes), ('Cols', tr2Subtypes)]) }
def _validateAllTracksRead(self): if not self.hasResult(): raise ShouldNotOccurError("At this stage, statistic should either have result, " "or exception should have been raised") tracks = self.getAllTracks() trackUniqueKeys = [Track(tr.trackName).getUniqueKey(self.getGenome()) for tr in tracks] for trackIndex, restTrackIndexes in allElementsVersusRest(xrange(len(trackUniqueKeys))): track = tracks[trackIndex] if track is not None and not track.hasBeenFlaggedAsRead(): uniqueKeyForRestTracks = \ set(trackUniqueKeys[i] for i in restTrackIndexes) # If several tracks are the same, memory memoization will only result # in one RawDataStat being created, for one Track object. This is a # wanted optimization. In other cases, something is probably wrong if # a track has not been touched. However, this rule may be revisited # when track structure functionality is implemented. if trackUniqueKeys[trackIndex] not in uniqueKeyForRestTracks: raise IncompatibleTracksError( 'Track ' + prettyPrintTrackName(track.trackName) + ' was created, but not touched by statistic')
def _createChildren(self): kwArgs = copy(self._kwArgs) if 'rawStatistic' in kwArgs: del kwArgs['rawStatistic'] self._addChild( RawDataStat(self._region, self._track, TrackFormatReq(dense=False, val='tc') ) ) self._addChild( RawDataStat(self._region, self._track2, TrackFormatReq(dense=False, val='tc') ) ) self._addChild( self._rawStatistic(self._region, self._track, self._track2, **kwArgs) ) #try: for subtype1 in ['0','1']: for subtype2 in ['0','1']: tn1 = self._track.trackName + [subtype1] tn2 = self._track2.trackName + [subtype2] if not os.path.exists(createDirPath(tn1, self.getGenome())) or not os.path.exists(createDirPath(tn2,self.getGenome())): raise IncompatibleTracksError track1 = Track( tn1) track1.formatConverters = self._track.formatConverters track2 = Track( tn2) track2.formatConverters = self._track2.formatConverters self._addChild(self._rawStatistic(self._region, track1, track2, **kwArgs) )
def _setTracks(self, trackName1, trackName2): self._track = Track(trackName1) self._track2 = Track(trackName2)
def _getTrack(self): track = Track(self._trackName) track.addFormatReq(TrackFormatReq(allowOverlaps=self._allowOverlaps)) return track
def _getTrackView(self, trackName, region, allowOverlaps): track = Track(trackName) track.addFormatReq(TrackFormatReq(allowOverlaps=allowOverlaps)) return track.getTrackView(region)
def _getTrack(self): track = Track(self._trackName) track.addFormatReq(TrackFormatReq(allowOverlaps=self._allowOverlaps)) return track
from gold.track.Track import Track from gold.track.GenomeRegion import GenomeRegion from gold.track.TrackFormat import TrackFormatReq if __name__ == "__main__": track = Track(['test']) #trackView = track.getTrackView(None, GenomeRegion('hg18','chrM',1000,10000)) #trackView2 = track.getTrackView(None, GenomeRegion('hg18','chrM',4000,4000)) trackView = track.getTrackView(GenomeRegion('TestGenome', 'chr1', -1, 1000)) count = 0 for el in trackView: print el.start(), el.end() count += 1 if count > 50: break print len([el for el in trackView]) #print len([el for el in trackView2])
def execute(cls, choices, galaxyFn=None, username=''): cls._setDebugModeIfSelected(choices) genome = choices.genome genomicRegions = choices.genomicRegions genomicRegionsTracks = choices.genomicRegionsTracks sourceTfs = choices.sourceTfs sourceTfsDetails = choices.sourceTfsDetails tfTracks = choices.tfTracks # Get Genomic Region track name: if genomicRegions == cls.REGIONS_FROM_HISTORY: galaxyTN = genomicRegionsTracks.split(':') genElementTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN( genome, galaxyTN) #queryGSuite = getGSuiteFromGalaxyTN(genomicRegionsTracks) #queryTrackList = [Track(x.trackName, x.title) for x in queryGSuite.allTracks()] elif genomicRegions == 'Hyperbrowser repository': selectedGenRegTrack = TfbsTrackNameMappings.getTfbsTrackNameMappings( genome)[genomicRegionsTracks] if isinstance(selectedGenRegTrack, dict): genElementTrackName = selectedGenRegTrack.values() else: genElementTrackName = selectedGenRegTrack elif genomicRegions == 'Hyperbrowser repository (cell-type-specific)': genElementTrackName = ['Private', 'Antonio' ] + genomicRegionsTracks.split(':') else: return # Get TF track names: if isinstance(tfTracks, dict): selectedTfTracks = [ key for key, val in tfTracks.iteritems() if val == 'True' ] else: selectedTfTracks = [tfTracks] queryTrackTitle = '--'.join(genElementTrackName) trackTitles = [queryTrackTitle] tracks = [Track(genElementTrackName, trackTitle=queryTrackTitle)] for i in selectedTfTracks: if sourceTfs == 'Hyperbrowser repository': tfTrackName = TfTrackNameMappings.getTfTrackNameMappings( genome)[sourceTfsDetails] + [i] tracks.append( Track(tfTrackName, trackTitle=tfTrackName[len(tfTrackName) - 1])) trackTitles.append(tfTrackName[len(tfTrackName) - 1]) else: tfTrackName = i.split(':') queryGSuite = getGSuiteFromGalaxyTN(sourceTfsDetails) for x in queryGSuite.allTracks(): selectedTrackNames = (':'.join(x.trackName)) if i == selectedTrackNames: tracks.append(Track(x.trackName, x.title)) trackTitles.append(x.trackName[-1]) # queryGSuite = getGSuiteFromGalaxyTN(sourceTfsDetails) # tfTrackName = [x.trackName for x in queryGSuite.allTracks()] + [i] # tracks += [Track(x.trackName, x.title) for x in queryGSuite.allTracks()] # trackTitles += tfTrackName # print tfTrackName # print tracks # print trackTitles trackTitlesForStat = trackTitles trackTitles = CommonConstants.TRACK_TITLES_SEPARATOR.join(trackTitles) ##first statistic for Q2 resultsForStatistics = OrderedDict() similarityFunc = [ #GSuiteStatUtils.T7_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP, GSuiteStatUtils.T5_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP ] for similarityStatClassName in similarityFunc: regSpec, binSpec = UserBinMixin.getRegsAndBinsSpec(choices) analysisBins = GalaxyInterface._getUserBinSource(regSpec, binSpec, genome=genome) mcfdrDepth = AnalysisDefHandler( REPLACE_TEMPLATES['$MCFDR$']).getOptionsAsText().values()[0][0] analysisDefString = REPLACE_TEMPLATES[ '$MCFDR$'] + ' -> GSuiteSimilarityToQueryTrackRankingsAndPValuesWrapperStat' analysisSpec = AnalysisDefHandler(analysisDefString) analysisSpec.setChoice('MCFDR sampling depth', mcfdrDepth) analysisSpec.addParameter('assumptions', 'PermutedSegsAndIntersegsTrack_') analysisSpec.addParameter( 'rawStatistic', GSuiteStatUtils. PAIRWISE_STAT_LABEL_TO_CLASS_MAPPING[similarityStatClassName]) analysisSpec.addParameter( 'pairwiseStatistic', GSuiteStatUtils. PAIRWISE_STAT_LABEL_TO_CLASS_MAPPING[similarityStatClassName] ) #needed for call of non randomized stat for assertion analysisSpec.addParameter('tail', 'more') analysisSpec.addParameter('trackTitles', trackTitles) #that need to be string analysisSpec.addParameter('queryTracksNum', str(len(tracks))) results = doAnalysis(analysisSpec, analysisBins, tracks).getGlobalResult() if not similarityStatClassName in resultsForStatistics: resultsForStatistics[similarityStatClassName] = {} resultsForStatistics[similarityStatClassName] = results keyTitle = [ #'Normalized ratio of observed to expected overlap (normalized Forbes similarity measure)', 'Ratio of observed to expected overlap (Forbes similarity measure)' ] # 'Normalized Forbes coefficient: ratio of observed to expected overlap normalized in relation to the reference GSuite', # 'Forbes coefficient: ratio of observed to expected overlap' keyTitle = [ #GSuiteStatUtils.T7_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP, GSuiteStatUtils.T5_RATIO_OF_OBSERVED_TO_EXPECTED_OVERLAP ] resultDict = AllTfsOfRegions.countStatistics(similarityFunc, choices, genome, tracks, trackTitlesForStat) resultDictShow = AllTfsOfRegions.countStatisticResults( resultDict, keyTitle, trackTitlesForStat) # print resultsForStatistics '''selectedTrackNames = [] if sourceTfs == 'History (user-defined)': if selectedTfTracks.split(":")[1] == "gsuite": gSuite = getGSuiteFromGalaxyTN(selectedTfTracks) for track in gSuite.allTracks(): selectedTrackNames.append(track.trackName) else: galaxyTN = selectedTfTracks.split(':') gRegTrackName = ExternalTrackManager.getPreProcessedTrackFromGalaxyTN(genome, galaxyTN) selectedTrackNames.append(gRegTrackName) else:''' tfNameList = [] #Intersection between TF Tracks and selected region (Table 1): n = 0 allTargetBins = [] alltfNames = [] table1 = [] for i in selectedTfTracks: n = n + 1 #newGalaxyFn = galaxyFn.split(".")[0] + str(n) + "." + "dat" if sourceTfs == 'Hyperbrowser repository': tfTrackName = TfTrackNameMappings.getTfTrackNameMappings( genome)[sourceTfsDetails] + [i] else: tfTrackName = i.split(':') tfTrackName.pop(0) #tfIntersection.expandReferenceTrack(upFlankSize, downFlankSize) tfIntersection = TrackIntersection(genome, genElementTrackName, tfTrackName, galaxyFn, str(n)) regFileNamer = tfIntersection.getIntersectedRegionsStaticFileWithContent( ) targetBins = tfIntersection.getIntersectedReferenceBins() #regSpec, targetBins = UserBinSelector.getRegsAndBinsSpec(choices) tfHits = [i] * len(targetBins) fixedTargetBins = [str(a).split(" ")[0] for a in targetBins] extendedTargetBins = [ list(a) for a in zip(fixedTargetBins, tfHits) ] allTargetBins = allTargetBins + extendedTargetBins tfName = i alltfNames = alltfNames + [tfName] # Save output table: tfNameList.append(tfName) line = [tfName] + [len(targetBins)] + [ regFileNamer.getLink('Download bed-file') ] + [ regFileNamer.getLoadToHistoryLink('Send bed-file to History') ] table1 = table1 + [line] # Computing totals: fullCase = ','.join(alltfNames) firstColumn = [item[0] for item in allTargetBins] uniqueAllTargetBins = list(set(firstColumn)) # Group TFs by bound region: d1 = defaultdict(list) for k, v in allTargetBins: d1[k].append(v) allTFTargetBins = dict((k, ','.join(v)) for k, v in d1.iteritems()) allTFTargetList = [] fullCaseTFTargetList = [] for key, value in allTFTargetBins.iteritems(): allTFTargetList = allTFTargetList + [[key, value]] if value == fullCase: fullCaseTFTargetList = fullCaseTFTargetList + [[key, value]] analysis3 = TrackIntersection.getFileFromTargetBins( allTFTargetList, galaxyFn, str(3)) analysis4 = TrackIntersection.getFileFromTargetBins( fullCaseTFTargetList, galaxyFn, str(4)) # Print output to table: title = 'TF targets and co-occupancy of ' + genElementTrackName[ -1] + ' genomic regions' htmlCore = HtmlCore() pf = plotFunction(tableId='resultsTable') htmlCore.begin() htmlCore.header(title) htmlCore.divBegin('resultsDiv') htmlCore.line(pf.createButton(bText='Show/Hide more results')) # htmlCore.tableHeader(['Transcription Factor', 'Normalized ratio of observed to expected overlap (normalized Forbes similarity measure) -- Similarity to genomic regions track', 'Normalized ratio of observed to expected overlap (normalized Forbes similarity measure) -- p-value','Ratio of observed to expected overlap (Forbes similarity measure) -- Similarity to genomic regions track', 'Ratio of observed to expected overlap (Forbes similarity measure) -- p-value', 'Number of TF-Target Track Regions', 'File of TF Target Regions', 'File of TF Target Regions', 'Number of TF-co-occupied Regions', 'File of TF co-occupied Regions', 'File of TF co-occupied Regions', 'Rank of TF co-occupancy motifs', 'Rank of TF co-occupancy motifs'], sortable=True, tableId='resultsTable') #previous ordering # htmlCore.tableHeader(['Transcription Factor', 'Normalized Forbes index --overlap score', # 'Normalized Forbes index --p-value', # 'Forbes index --overlap score', 'Forbes index --p-value', # 'Number of TF-Target Track Regions', 'File of TF Target Regions', # 'File of TF Target Regions', 'Number of target track regions occupied by this TF', # 'File of TF co-occupied Regions', 'File of TF co-occupied Regions', # 'Rank of TF co-occupancy motifs', 'Rank of TF co-occupancy motifs'], # sortable=True, tableId='resultsTable') htmlCore.tableHeader( [ 'Transcription Factor', 'Number of TF-Target Track Regions', 'File of TF Track Regions', 'Number of target track regions occupied by this TF', 'File of TF Target Regions', 'Forbes index --overlap score', 'Forbes index --p-value', #'Normalized Forbes index --overlap score', 'Normalized Forbes index --p-value', 'File of TF co-occupied Regions', 'Rank of TF co-occupancy motifs' ], sortable=True, tableId='resultsTable') # Adding co-occupancy results to table: n = 1000 genRegionNumElements = [ int(x) for x in getTrackRelevantInfo.getNumberElements( genome, genElementTrackName) ] for key0, it0 in resultsForStatistics.iteritems(): for el in tfNameList: if el not in it0: resultsForStatistics[key0][el] = [None, None] resultsPlotDict = {} resultPlotCat = [] resultsPlot = [] resultsForStatisticsProper = {} for key0, it0 in resultsForStatistics.iteritems(): if not key0 in resultsPlotDict: resultsPlotDict[key0] = {} resultsPlotPart = [] for key1, it1 in it0.iteritems(): resultsPlotPart.append(it1[0]) if not key1 in resultsForStatisticsProper: resultsForStatisticsProper[key1] = [] if not key1 in resultsPlotDict[key0]: resultsPlotDict[key0][key1] = None for el in it1: resultsForStatisticsProper[key1].append(el) resultsPlotDict[key0][key1] = it1[0] resultPlotCat.append(tfNameList) resultPlotCat.append(tfNameList) #resultPlotCatPart = tfNameList # print resultPlotCatPart for key0, it0 in resultsPlotDict.iteritems(): resultsPlotPart = [] for el in tfNameList: if el in it0: resultsPlotPart.append(it0[el]) else: resultsPlotPart.append(None) resultsPlot.append(resultsPlotPart) for i in table1: thisCaseTFTargetList = [] for key, value in allTFTargetList: if i[0] in value and ',' in value: thisCaseTFTargetList = thisCaseTFTargetList + [[ key, value ]] n = n + 1 thisAnalysis = TrackIntersection.getFileFromTargetBins( thisCaseTFTargetList, galaxyFn, str(n)) thisCaseCoCountsList = [] thing = [x[1] for x in thisCaseTFTargetList] for k in list(set(thing)): thisCount = thing.count(k) thisCaseCoCountsList = thisCaseCoCountsList + \ [[k, thisCount, 100*float(thisCount)/float(sum(genRegionNumElements)), 100*float(thisCount)/float(len(thisCaseTFTargetList))]] thisCaseCoCountsList.sort(key=lambda x: x[2], reverse=True) n = n + 1 thisCoCountsAnalysis = TrackIntersection.getOccupancySummaryFile( thisCaseCoCountsList, galaxyFn, str(n)) thisLine = [len(thisCaseTFTargetList)] + \ [thisAnalysis.getLink('Download file')] + [thisAnalysis.getLoadToHistoryLink('Send file to History')] + \ [thisCoCountsAnalysis.getLink('Download file')] + [thisCoCountsAnalysis.getLoadToHistoryLink('Send file to History')] newLineI = [] tfName = i[0] newLineI.append(tfName) for el in resultsForStatisticsProper[tfName]: newLineI.append(el) for elN in range(1, len(i)): newLineI.append(i[elN]) # htmlCore.tableLine(i + thisLine) # htmlCore.tableHeader(['Transcription Factor', 'Normalized Forbes index --overlap score', # 'Normalized Forbes index --p-value', # 'Forbes index --overlap score', 'Forbes index --p-value', # 'Number of TF-Target Track Regions', 'File of TF Target Regions', # 'File of TF Target Regions', 'Number of target track regions occupied by this TF', # 'File of TF co-occupied Regions', 'File of TF co-occupied Regions', # 'Rank of TF co-occupancy motifs', 'Rank of TF co-occupancy motifs'], # sortable=True, tableId='resultsTable') # htmlCore.tableHeader(['Transcription Factor', 'Number of TF-Target Track Regions', 'File of TF Track Regions', # 'Number of target track regions occupied by this TF', 'File of TF Target Regions', # 'Forbes index --overlap score', 'Forbes index --p-value', # 'Normalized Forbes index --overlap score', 'Normalized Forbes index --p-value', # 'File of TF co-occupied Regions', 'Rank of TF co-occupancy motifs'], # sortable=True, tableId='resultsTable') tl = newLineI + thisLine # previous ordering tl - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 # actual ordering - 0, 5, 7, 8, 7, 3, 4, 1, 2, 9, 11 #ordering = [0, 5, 7, 8, 10, 3, 4, 1, 2, 10, 12] ordering = [0, 3, 5, 6, 8, 1, 2, 8, 10] #1, 2, => delete eoList = [] for eo in ordering: eoList.append(tl[eo]) htmlCore.tableLine(eoList) totalCoOccupancyTargetList = [] n = 2000 for key, value in allTFTargetList: n = n + 1 if ',' in value: totalCoOccupancyTargetList = totalCoOccupancyTargetList + [[ key, value ]] #newGalaxyFn = galaxyFn.split(".")[0] + str(n) + "." + "dat" totalCoOccupancyAnalysis = TrackIntersection.getFileFromTargetBins( totalCoOccupancyTargetList, galaxyFn, str(n)) #line = ['Total reported regions'] + [len(allTargetBins)] + [''] + [''] + [''] + [''] + [''] #line = ['Full co-occupancy of ' + fullCase] + ['-'] + ['-'] + ['-'] + ['-'] + ['-'] + ['-'] + ['-'] + [len(fullCaseTFTargetList)] + [analysis4.getLink('Download file')] + [analysis4.getLoadToHistoryLink('Send file to History')] + ['-'] + ['-'] line = ['Full co-occupancy of ' + fullCase] + \ ['-'] + \ ['-'] + \ [len(fullCaseTFTargetList)] + \ ['-'] + \ ['-'] + \ ['-'] + \ [analysis4.getLoadToHistoryLink('Send file to History')] + \ ['-'] htmlCore.tableLine(line) #line = ['Total unique regions'] + ['-'] + ['-'] + ['-'] + ['-'] + [len(allTFTargetList)] + [analysis3.getLink('Download bed-file')] + [analysis3.getLoadToHistoryLink('Send bed-file to History')] + [len(totalCoOccupancyTargetList)] + [totalCoOccupancyAnalysis.getLink('Download file')] + [totalCoOccupancyAnalysis.getLoadToHistoryLink('Send file to History')] + ['-'] + ['-'] line = ['Total unique regions'] + \ [len(allTFTargetList)] + \ ['-'] + \ [len(totalCoOccupancyTargetList)] + \ [analysis3.getLoadToHistoryLink('Send bed-file to History')] + \ ['-'] +\ ['-'] + \ [totalCoOccupancyAnalysis.getLoadToHistoryLink('Send file to History')] + \ ['-'] htmlCore.tableLine(line) htmlCore.tableFooter() htmlCore.divEnd() # htmlCore.line(pf.hideColumns(indexList=[2, 4])) # sumRes = 0 for r in resultsPlot[0]: if r != None: sumRes += r if sumRes != 0: vg = visualizationGraphs() result = vg.drawColumnCharts( [resultsPlot[0]], height=300, categories=resultPlotCat, legend=False, addOptions='width: 90%; float:left; margin: 0 4%;', #titleText=['Overlap between TFs and genomic region using normalized Forbes', 'Overlap between TFs and genomic region using Forbes'], titleText=[ 'Overlap between TFs and genomic region using Forbes' ], xAxisRotation=90, xAxisTitle='TF', yAxisTitle='value') htmlCore.line(result) for key0, it0 in resultDictShow.iteritems(): htmlCore.divBegin('resultsDiv' + str(key0)) htmlCore.header(key0) htmlCore.tableHeader(it0[0], sortable=True, tableId='resultsTable' + str(key0)) for elN in range(1, len(it0)): htmlCore.tableLine(it0[elN]) htmlCore.tableFooter() htmlCore.divEnd() htmlCore.hideToggle(styleClass='debug') htmlCore.end() print htmlCore