def makePWMscorefiles(fastafiles, pwmfiles, destdir, both_strands=True): for fastaf in fastafile: thisseqname = fastaf.split('/')[-1].split('.')[0] handle = open(fastaf, "r") records = list(Bio.SeqIO.parse(handle, "fasta")) handle.close() thisseq = records[0].seq print 'Doing sequence ', thisseqname, 'length=', len(thisseq) for pwmf in pwmfiles: thispwmname = pwmf.split('/')[-1] print ' Doing pwm ', thispwmname thispwm = MOODS.load_matrix(pwmf) thispwmcomplement = MOODS.reverse_complement(thispwm) print ' strand 1' onestrandindexvector=getMOODSscore(thisseq, thispwm) print ' strand 2' otherstrandindexvecor=getMOODSscore(thisseq, thispwmcomplement) print ' finding best score per bp' bothstrandsindexvector = np.append( onestrandindexvector, otherstrandindexvecor, axis=0) bestscorevector = getMaxPWMScore( bothstrandsindexvector, len(thispwm[0])) for strandnbr in range(len(bothstrandsindexvector)): print ' writing wiggle for strand', str(strandnbr) vegardswritewiggle(bothstrandsindexvector[strandnbr,:], name=thispwmname, chr=thisseqname, destpath=destdir + '/' + 'start_index_score/'+ thispwmname+ '/strand_'+str(strandnbr)) print ' writing wiggle for bestscore' vegardswritewiggle(bestscorevector, name=thispwmname, chr=thisseqname, destpath=destdir + '/' + 'best_score_in_window/'+thispwmname)
def getMOODSscore(seqfile, pwmfiles, both_strands=False): handle = open(seqfile, "r") records = list(Bio.SeqIO.parse(handle, "fasta")) handle.close() seq = records[0].seq print 'len(seq)=', len(seq) matrixlist = list() for f in pwmfiles: matrix = MOODS.load_matrix(f) print 'pwm ', f, 'windowlength=', len(matrix[0]) matrixlist.append(matrix) if both_strands: matrixlist.append( MOODS.reverse_complement(matrix) ) # both_strand option in MOODS returned a akward result. print 'starting MOODS.search', datetime.now() results = MOODS.search(seq, matrixlist, thresholds=1, absolute_threshold=False) print 'done MOODS.search', datetime.now() reslist = [] for n in range(len(pwmfiles)): thisind = n * (1 + both_strands) reslist.append(vegardparseMOODSres(results[thisind], len(seq))) if both_strands: reslist[n] = np.append(reslist[n], vegardparseMOODSres(results[thisind + 1], len(seq)), axis=0) return (reslist)
def getMOODSscore(seqfile, pwmfiles, both_strands=False): handle = open(seqfile, "r") records = list(Bio.SeqIO.parse(handle, "fasta")) handle.close() seq = records[0].seq print 'len(seq)=',len(seq) matrixlist=list() for f in pwmfiles: matrix = MOODS.load_matrix(f) print 'pwm ', f , 'windowlength=', len(matrix[0]) matrixlist.append(matrix) if both_strands: matrixlist.append(MOODS.reverse_complement(matrix)) # both_strand option in MOODS returned a akward result. print 'starting MOODS.search', datetime.now() results = MOODS.search(seq, matrixlist, thresholds=1, absolute_threshold=False) print 'done MOODS.search', datetime.now() reslist=[] for n in range(len(pwmfiles)): thisind = n * (1 + both_strands) reslist.append(vegardparseMOODSres( results[thisind] , len(seq))) if both_strands: reslist[n] = np.append( reslist[n] , vegardparseMOODSres( results[thisind+1] , len(seq)), axis=0) return(reslist)
for i in results: for (position, score) in i: print("Position: " + str(position) + " Score: "+ str(score)) print('one way') results = MOODS.search(teststring, [matrix], thresholds=1, absolute_threshold=False, both_strands=False) for i in results: for (position, score) in i: print("Position: " + str(position) + " Score: "+ str(score)) print('other way') results = MOODS.search(teststring, [ ,MOODS.reverse_complement(matrix)], thresholds=1, absolute_threshold=False, both_strands=False) results = MOODS.search(teststring, [ matrix , matrix], thresholds=1, absolute_threshold=False, both_strands=False) MOODS.search(teststring, [ matrix ], thresholds=1, absolute_threshold=False, both_strands=True) for i in results: for (position, score) in i: print("Position: " + str(position) + " Score: "+ str(score)) # /xanadu/home/vegardny/prosjekter/hyperbrowser/pwm_vs_snp/MOODS/src/find_pssm_dna 1 -algorithm=naive /xanadu/home/vegardny/prosjekter/hyperbrowser/pwm_vs_snp/vegard_debug_MOODS/examples/vegardtest2.fasta /xanadu/home/vegardny/prosjekter/hyperbrowser/pwm_vs_snp/vegard_debug_MOODS/examples/vegardtest2.pfm