<commit_before># Copyright (c) 2009-2010 <NAME> <<EMAIL>>
#
# This module is free software. You can redistribute it and/or modify it under 
# the terms of the MIT License, see the file COPYING included with this 
# distribution.
""" This module contains the core GimmeMotifs functionality """

# Python imports
import os
import sys
import shutil
import logging
import logging.handlers
from datetime import datetime
# External imports
import kid
# GimmeMotifs imports
from gimmemotifs.config import *
from gimmemotifs.fasta import *
from gimmemotifs.utils import *
from gimmemotifs.prediction import *
from gimmemotifs.background import *
from gimmemotifs.comparison import *
from gimmemotifs import genome_index
from gimmemotifs.cluster import *

import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import matplotlib.cm as cm

def job_server_ok():
	return True

def run_command(cmd):
	#print args
	from subprocess import Popen
	p = Popen(cmd, shell=True)
	p.communicate()

def motif_localization(fastafile, motif, width, outfile):
	NR_HIST_MATCHES = 100
	from gimmemotifs.utils import plot_histogram, ks_pvalue
	from gimmemotifs.fasta import Fasta
	from numpy import array
	
	matches = motif.pwm_scan(Fasta(fastafile), cutoff=0.9, nreport=NR_HIST_MATCHES)
	if len(matches) > 0:
		ar = []
		for a in matches.values():
			ar += a
		matches = array(ar)
		p = ks_pvalue(matches, width - len(motif))
		plot_histogram(matches - width / 2 + len(motif) / 2, outfile, xrange=(-width / 2, width / 2), breaks=21, title="%s (p=%0.2e)" % (motif.id, p), xlabel="Position")
		return motif.id, p
	else:
		return motif.id, 1.0

def scan_fasta_file_with_motifs(fastafile, motiffile, threshold, gfffile):
	error = None
	try:
		from gimmemotifs.fasta import Fasta
		from gimmemotifs.motif import pwmfile_to_motifs
		motifs = pwmfile_to_motifs(motiffile)
		fa = Fasta(fastafile)
		for motif in motifs:
			motif.pwm_scan_to_gff(fa, gfffile, nreport=1, cutoff=float(threshold), append=True)
	except Exception,e :
		error = e
	return error

def get_scores(motif, fg_file, bg_file):
	error = None
	auc = None
	mncp = None
	max_f = None
	y = None
	try:
		from gimmemotifs.fasta import Fasta
		from gimmemotifs.rocmetrics import ROC_values,ROC_AUC,MNCP,max_fmeasure

		fg_result = motif.pwm_scan_score(Fasta(fg_file), cutoff=0.0, nreport=1)
		fg_vals = [sorted(x)[-1] for x in fg_result.values()]

		bg_result = motif.pwm_scan_score(Fasta(bg_file), cutoff=0.0, nreport=1)
		bg_vals = [sorted(x)[-1] for x in bg_result.values()]

		(x, y) = ROC_values(fg_vals, bg_vals)
		auc = ROC_AUC(fg_vals, bg_vals)
		mncp = MNCP(fg_vals, bg_vals)
		max_f, y = max_fmeasure(x,y)

	except Exception,e:
		error = e
	return (error, auc, mncp, max_f, y)

def get_roc_values(motif, fg_file, bg_file):<commit_msg>Some changes<commit_after># Copyright (c) 2009-2010 <NAME> <<EMAIL>>
#
# This module is free software. You can redistribute it and/or modify it under 
# the terms of the MIT License, see the file COPYING included with this 
# distribution.
""" This module contains the core GimmeMotifs functionality """

# Python imports
import os
import sys
import shutil
import logging
import logging.handlers
from datetime import datetime
# External imports
import kid
# GimmeMotifs imports
from gimmemotifs.config import *
from gimmemotifs.fasta import *
from gimmemotifs.utils import *
from gimmemotifs.prediction import *
from gimmemotifs.background import *
from gimmemotifs.comparison import *
from gimmemotifs import genome_index
from gimmemotifs.cluster import *

import matplotlib
matplotlib.use('Agg')

import matplotlib.pyplot as plt
import matplotlib.cm as cm

def job_server_ok():
	return True

def run_command(cmd):
	#print args
	from subprocess import Popen
	p = Popen(cmd, shell=True)
	p.communicate()

def motif_localization(fastafile, motif, width, outfile):
	NR_HIST_MATCHES = 100
	from gimmemotifs.utils import plot_histogram, ks_pvalue
	from gimmemotifs.fasta import Fasta
	from numpy import array
	
	matches = motif.pwm_scan(Fasta(fastafile), cutoff=0.9, nreport=NR_HIST_MATCHES)
	if len(matches) > 0:
		ar = []
		for a in matches.values():
			ar += a
		matches = array(ar)
		p = ks_pvalue(matches, width - len(motif))
		plot_histogram(matches - width / 2 + len(motif) / 2, outfile, xrange=(-width / 2, width / 2), breaks=21, title="%s (p=%0.2e)" % (motif.id, p), xlabel="Position")
		return motif.id, p
	else:
		return motif.id, 1.0

def scan_fasta_file_with_motifs(fastafile, motiffile, threshold, gfffile, scan_rc=True):
	error = None
	try:
		from gimmemotifs.fasta import Fasta
		from gimmemotifs.motif import pwmfile_to_motifs
		motifs = pwmfile_to_motifs(motiffile)
		fa = Fasta(fastafile)
		for motif in motifs:
			motif.pwm_scan_to_gff(fa, gfffile, nreport=1, cutoff=float(threshold), scan_rc=scan_rc, append=True)
	except Exception,e :
		error = e
	return error

def get_scores(motif, fg_file, bg_file):
	error = None
	auc = None
	mncp = None
	max_f = None
	y = None
	try:
		from gimmemotifs.fasta import Fasta
		from gimmemotifs.rocmetrics import ROC_values,ROC_AUC,MNCP,max_fmeasure

		fg_result = motif.pwm_scan_score(Fasta(fg_file), cutoff=0.0, nreport=1)
		fg_vals = [sorted(x)[-1] for x in fg_result.values()]

		bg_result = motif.pwm_scan_score(Fasta(bg_file), cutoff=0.0, nreport=1)
		bg_vals = [sorted(x)[-1] for x in bg_result.values()]

		(x, y) = ROC_values(fg_vals, bg_vals)
		auc = ROC_AUC(fg_vals, bg_vals)
		mncp = MNCP(fg_vals, bg_vals)
		max_f, y = max_fmeasure(x,y)

	except Exception,e:
		error = e
	return (error, auc, mncp, max_f, y)

def get_roc_values(motif, fg_file, bg_file):
{'fraction_non_alphanumeric': 0.10096325365679629, 'fraction_numerical': 0.011773100249732429, 'mean_word_length': 4.889705882352941, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4359027', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2107, 'n_words': 566}