def listen(cls,
p_socket,
p_nbThreads=10,
p_tls=False,
p_cacert=None,
p_cert=None,
p_key=None):
if not cls.ms_initialized:
raise XtdError(__name__, "you must initialize server manager first")
l_server = cherrypy._cpserver.Server()
p_socket = urlparse(p_socket)
if p_socket.scheme == "tcp":
l_server.socket_host = p_socket.hostname
l_port = p_socket.port
if not l_port:
l_port = 8080
l_server.socket_port = l_port
elif p_socket.scheme == "unix":
l_server.bind_addr = p_socket.path
l_server.thread_pool = p_nbThreads
if p_tls:
cherrypy.log("Enabling TLS support")
l_server.ssl_module = "builtin"
#l_server.ssl_certificate_chain = p_cacert
l_server.ssl_certificate = p_cert
l_server.ssl_private_key = p_key
l_server.subscribe()
return l_server
python类log()的实例源码
def initialize(cls, p_logger):
if cls.ms_initialized:
return None
cherrypy.tools.counter_start = \
cherrypy._cptools.Tool("on_start_resource", tools.perf_begin())
cherrypy.tools.counter_stop = \
cherrypy._cptools.Tool("on_end_request", tools.perf_end())
cherrypy.tools.log_request = \
cherrypy._cptools.Tool('on_start_resource', tools.request_logger())
cherrypy.tools.log_response = \
cherrypy._cptools.Tool('on_end_resource', tools.response_logger())
cherrypy.server.unsubscribe()
l_filterAccess = cls.LoggerFilter(p_logger + ".access", True)
l_filterError = cls.LoggerFilter(p_logger + ".error", True)
logging.getLogger("cherrypy.acccess").addFilter(l_filterAccess)
logging.getLogger("cherrypy.error").addFilter(l_filterError)
cherrypy.config.update({
"environment" : "production",
"engine.autoreload.on" : False,
"log.screen" : True,
"log.access_file" : "",
"log.error_file" : ""
})
cherrypy.engine.signals.subscribe()
cls.ms_initialized = True
def mount(cls, p_handler, p_path, p_conf=None, p_logger="cherrypy"):
if p_conf is None:
p_conf = {}
if not cls.ms_initialized:
raise XtdError(__name__, "you must initialize server manager first")
l_res = mergedicts({
'/' : {
"tools.log_request.on" : True,
"tools.log_request.module" : p_logger + ".error",
"tools.log_request.level" : "debug",
"tools.log_response.on" : True,
"tools.log_response.module" : p_logger + ".error",
"tools.log_response.level" : "debug",
"tools.counter_start.on" : True,
"tools.counter_start.ns" : p_logger,
"tools.counter_start.name" : "rtt",
"tools.counter_stop.on" : True,
"tools.counter_stop.ns" : p_logger,
"tools.counter_stop.name" : "rtt",
}
}, p_conf)
l_app = cherrypy.tree.mount(p_handler, p_path, dict(l_res))
l_filterAccess = cls.LoggerFilter(p_logger + ".access")
l_filterError = cls.LoggerFilter(p_logger + ".error")
l_loggerAccess = logging.getLogger(p_logger + ".access")
l_loggerError = logging.getLogger(p_logger + ".error")
l_loggerError.addFilter(l_filterError)
l_loggerAccess.addFilter(l_filterAccess)
l_app.log.error_log = l_loggerError
l_app.log.access_log = l_loggerAccess
return l_app
def createJSONforD3(node_value,link_value):
'''
This function takes the variables created in the functions nodes() and links() and creates a json file that contains all the necessary information for plotting the netwrok of associations between the different OTUs with the color of the links reflecting the prediction of the types of interactions occurring between pairs of OTUs.
:param node_value: list of dictionaries returned from function nodes
:param link_value: list of dictionaries returned from function links
:return data4plot_json: file in json format in site folder that contains the information required for plotting network of associations between OTUs and the predicted interactions occurring between pairs of species.
'''
import json
cherrypy.log('Finally, we will dump the information about the nodes and links of our network to a json file.')
'''
@summary: put the nodes values and the links values into the final dataset that is in the dictionary format
'''
dataDict = {'nodes': node_value, 'links': link_value}
'''
@summary: dump the new data into a json file
'''
with open('data4plot_json','w') as outfile:
cherrypy.log('The information will be dumped into the file %s' %outfile)
json.dump(dataDict, outfile)
cherrypy.log('We finished creating the json file.')
def createEXmodel(EXreactions):
'''
This function takes the list of exchange reactions created using the function totalEXRxns and creates a Model object using cobrapy composed of those reactions with the upper bound flux values of 1000, lower bound flux values of -1000, and objective coefficient of 0, and one metabolite as being uptaken by the reaction (stoichiometric coefficient of -1). This is a model composed solely of exchange reactions and it's the model for the extra compartment created for the full community model
:param EXreactions: list of reactions that are the output of the function totalEXRxns (above)
:return exchange_model: cobrapy Model object for the compartment that will serve as an extra compartment in the full community model.
'''
cherrypy.log("Started the function that creates the exchange reactions for the community model")
exchange_model = cobra.Model('Model with the exchange reactions only')
cherrypy.log("Created the base exchange model object")
for i in EXreactions:
new_i = str(i)
new_i = new_i[3:]
new_met = cobra.Metabolite(new_i)
rxn = cobra.Reaction(i)
rxn.lower_bound = -1000.000
rxn.upper_bound = 1000.000
rxn.objective_coefficient = 0.000
rxn.add_metabolites({new_met:-1.0})
exchange_model.add_reaction(rxn)
cherrypy.log('Finished adding all exchange reactions in exchange model object. There are %d of them' %(len(exchange_model.reactions)))
return exchange_model
def createReverseEXmodel(EXreactions):
'''
This function takes the list of exchange reactions created using the function totalEXRxns and creates a Model object using cobrapy composed of those reactions with the upper bound flux values of 1000, lower bound flux values of -1000, and objective coefficient of 0, and one metabolite as being produced by the reaction (stoichiometric coefficient of 1). This is a model composed solely of exchange reactions. The metabolite information for these reactions will be used to update the metabolites of the exchange reactions for models A and B.
:param EXreactions: list of reactions that are the output of the function totalEXRxns (above)
:return exchange_modelRev: cobrapy Model object containing only exchange reactions with the production of their respective metabolites
'''
cherrypy.log("Started the function that creates the reverse exchange reactions for the community model")
exchange_modelRev = cobra.Model('Model with the exchange reactions only with reversed stoi coefficient')
cherrypy.log("Created the base reverse exchange model object")
for i in EXreactions:
new_i = str(i)
new_i = new_i[3:]
new_met = cobra.Metabolite(new_i)
rxn = cobra.Reaction(i)
rxn.lower_bound = -1000.000
rxn.upper_bound = 1000.000
rxn.objective_coefficient = 0.000
rxn.add_metabolites({new_met:1.0})
exchange_modelRev.add_reaction(rxn)
cherrypy.log('Finished adding all exchange reactions in reverse exchange model object. There are %d of them' %(len(exchange_modelRev.reactions)))
return exchange_modelRev
def addEXMets2SpeciesEX(reverseEXmodel,speciesModel):
'''
This function takes the model with exchange reactions where the metabolite is produced (output from function createReverseEXmodel) and a species model, and adds the metabolite from the reverse model to the exhange reactions of the species model. For instance:
Reaction : modelB_EX_cpd11588_e0 got the cpd11588_e0[u] added.
'model_B_cpd11588_e0 <=> cpd11588_e0[u]'
This way, when a compound is exported to the extracellular environment, it is automatically transformed into a form that is common to all members in the community.
:param reverseEXmodel: cobrapy Model object containing only exchange reactions with the production of their respective metabolites
:param speciesModel: Model object of a particular species.
:return speciesModel: Model object of a particular species with updated exchange reactions are updated.
'''
cherrypy.log('Started function to add metabolites to the exchange reactions of the reverse exchange model') #not right
for j in range(len(reverseEXmodel.reactions)):
exRxn = str(reverseEXmodel.reactions[j])
for i in range(len(speciesModel.reactions)):
rxn = str(speciesModel.reactions[i])
if rxn in exRxn:
new_met = reverseEXmodel.reactions[j].metabolites
speciesModel.reactions[i].add_metabolites(new_met)
speciesModel.reactions[i].lower_bound = -1000.000
speciesModel.reactions[i].upper_bound = 1000.000
cherrypy.log('Finished adding metabolites to the exchange reactions of the reverse exchange model')
return speciesModel
def getUniqueOTU(corrs):
'''
In this function we use a file with three columns: columns 1 and 2 are lists of identifiers of OTUs from the microbiome study the user is interested in exploring. The third column has some value of associaltion between each pair of OTUs. The usual measure is correlation or co-occurrence, but it can be any measure of association really. This function goes through all the identifiers and creates a list of unique identifiers in the table, that is, all the different OTUs that are going to be part of the analsysi.
:param corrs: file with the values of association between pairs of OTUs
:returns uniqueOTUs: list with unique identifiers of OTUs
'''
cherrypy.log('We started the function to get the list of all unique OTUs from the file that lists how pairs of OTUs are correlated. The file we are using is %s .' %corrs)
correlations = open(corrs,'r')
correlations.readline()
OTUs = []
for line in correlations:
line = line.rstrip().split()
OTUs.append(line[0])
OTUs.append(line[1])
uniqueOTUs = list(set(OTUs))
cherrypy.log('We created a list with %d unique OTUS.' %len(uniqueOTUs))
return uniqueOTUs
def getSeqs(sequences):
'''
In this function, we "clean up" a FASTA file. The input is a FASTA file containing the 16S rDNA sequences of all the OTUs in the microbiome study the user is exploring. This function makes sure that each genomic sequence only spans one line in the file.
:param sequences: FASTA file with 16S dRNA sequences spanning multiple lines in addition to the sequence identifier line.
:returns seqs: list of 16s dRNA sequences in the FASTA format spanning only one line in addition to the sequence identifier line.
'''
cherrypy.log("We are now going to clean up the FASTA file given (%s) so that each DNA sequence does not span multiple lines."%sequences)
userSeqs = open(sequences,'r')
seq = ''
for line in userSeqs:
if line.startswith('>'):
seq += '+++++\n'
seq += line
else:
seq += line.rstrip()
seqs = seq.split('+++++\n')
userSeqs.close()
cherrypy.log("Clean up is finished. We are ready to fetch only the representative OTU sequences that we are interested in using in the rest of our analysis." )
return seqs
def workingOTUs(uniqueOTUs,seqs, output):
'''
This function takes the outputs of the functions getUniqueOTU() and getSeqs(). It then creates a FASTA file that contains the 16S dRNA sequences for only the OTUs that are listed in the file with the association values between OTUs. This reduces the size of the files to ge used in further analysis in MMinte.
:param uniqueOTUs: list of OTUs present in the dataset with association information, output of getUniqueOTU()
:param seqs: set of sequences in a FASTA file where the sequences themselves only take one line and do not span multiple lines
:param output: FASTA file with only the sequences that are needed for running Widget 2
:returns output: FASTA file with only the sequences that are needed for running Widget 2
'''
cherrypy.log('Finally, we are going to create a FASTA file with only the representative OTUs that we will use in our analysis.')
cherrypy.log('The full path to the FASTA file created is %s .' %output)
reprOtusForAnalysis = open(output,'w')
counter = 0
for line in seqs:
for item in uniqueOTUs:
new_item = '>'+item+' '
if line.startswith(new_item):
counter += 1
print>>reprOtusForAnalysis, line
reprOtusForAnalysis.close()
cherrypy.log("There are a total of %d sequences to BLAST against the local database. The path to this file is %s"%(counter,output))
def blastSeqs(seqsToBlast):
'''
This function uses an input FASTA file with genomic sequences of 16S dRNA and BLASTs each sequence against a local database (found in supportFiles/db/) that contains the 16S dRNA sequences of all the bacterial species in the PATRIC database that have a whole genome sequence available. It output only the top hit to a tab-delimeted text file. The 16S sequences present in the database were provided by Maulik Shukla on the 3rd of November of 2015.
:param seqsToBlast: FASTA file containing the sequences that will be matched to the sequences in the local database. Called in blastn from parameter query.
:param dbase: local database on ../supportFiles/db/16Sdb. Called in blastn from parameter db.
:param tempOutputFile: global variable defined by file on '../tempFiles/blastOutput.txt'. Raw BLAST results are temporarily stored in this file and will be processed in function listTaxId4ModelSEED(). Called in blastn from parameter out.
:returns tempOutputFile
'''
from Bio.Blast.Applications import NcbiblastnCommandline
import os
cherrypy.log('We will blast the sequences in the FASTA file provided, %s, against a local custom database that contains the 16S sequences of the species in the NCBI that have whole genome sequences.' %(seqsToBlast))
dbase = '../supportFiles/db/16Sdb'
'''
@summary: run a local blast of the user's representative OTUs, the output format is tabular, only one match is shown
'''
blastn_cline = NcbiblastnCommandline(cmd='../ncbi-blast-2.2.22+/bin/blastn', query= seqsToBlast, db= dbase, outfmt= 6, out= tempOutputFile, max_target_seqs = 1, num_threads = 5)
cherrypy.log('The BLAST command that will be run is: %s' %blastn_cline)
os.system(str(blastn_cline))
#check if the tempOutputFile file was created. if not, shout out an error.
if os.stat(tempOutputFile).st_size != 0:
cherrypy.log('The %s was created and it is not empty' %tempOutputFile)
else:
cherrypy.log('Something is wrong because the %s appears to be empty' %tempOutputFile)
exit()
def data4plot_json(self):
ROOT_DIR = os.path.dirname(os.path.realpath('data4plot_json'))
full_path = ROOT_DIR + '/data4plot_json'
with open(full_path) as data:
return data.read()
#cherrypy.config.update({"response.timeout":1000000,'log.access_file': '../fullRun/supportFiles/logs/logAccess_file.txt','log.error_file': '../fullRun/supportFiles/logs/logError_file.txt','log.screen':True})
def start(self):
self.bus.log("Starting WebSocket processing")
self.bus.subscribe('stop', self.cleanup)
self.bus.subscribe('handle-websocket', self.handle)
self.bus.subscribe('websocket-broadcast', self.broadcast)
self.manager.start()
def stop(self):
self.bus.log("Terminating WebSocket processing")
self.bus.unsubscribe('stop', self.cleanup)
self.bus.unsubscribe('handle-websocket', self.handle)
self.bus.unsubscribe('websocket-broadcast', self.broadcast)
def index(self):
cherrypy.log("Handler created: %s" % repr(cherrypy.request.ws_handler))
def index():
cherrypy.log("CHERRYPY LOG: /")
return render_template('index.html')
def patient_gif(patient_index):
patient_index = int(patient_index)
if patient_index > NUMBER_PATIENTS:
abort(BAD_REQUEST)
cherrypy.log("CHERRYPY LOG: /gif/<patient_index>")
gif_url = manage_gif(patient_index)
return make_response(jsonify({'status': STATUS_OK, 'gif_url': gif_url}), STATUS_OK)
def predict_patient(patient_index):
patient_index = int(patient_index)
if patient_index > NUMBER_PATIENTS:
abort(BAD_REQUEST)
cherrypy.log("CHERRYPY LOG: /predict/<patient_index>")
prob = manage_prediction(patient_index)
return make_response(jsonify({'status': STATUS_OK, 'prob': prob}), STATUS_OK)
def patient_info():
cherrypy.log("CHERRYPY LOG: /patient_info")
patient_index = manage_request_patient_index(request.form['patient_index'])
gif_url = manage_gif(patient_index)
return render_template('patient.html', patient_index=patient_index, gif_url=gif_url)
def reopen_files(self):
"""Close and reopen all file handlers."""
for log in (self.error_log, self.access_log):
for h in log.handlers:
if isinstance(h, logging.FileHandler):
h.acquire()
h.stream.close()
h.stream = open(h.baseFilename, h.mode)
h.release()