本文整理匯總了Python中hpf.hddb.db.Session.rollback方法的典型用法代碼示例。如果您正苦於以下問題:Python Session.rollback方法的具體用法?Python Session.rollback怎麽用?Python Session.rollback使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類hpf.hddb.db.Session
的用法示例。
在下文中一共展示了Session.rollback方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: HPFPsipredWrap
# 需要導入模塊: from hpf.hddb.db import Session [as 別名]
# 或者: from hpf.hddb.db.Session import rollback [as 別名]
#.........這裏部分代碼省略.........
"""Variables:
self.prediction - The PsipredPrediction object returned from Psipred32(...).run()
self.dbo - If dbstore=True, the Psipred ORM object (DataBaseObject) from the HPF DB
"""
from hpf.hddb.db import Session, Sequence
self.sequence_key = sequence_key
self.nr_db = os.path.abspath(os.path.expanduser(nr_db))
self.ginzu_version = ginzu_version
self.dir = dir if dir else os.getcwd()
self.dbstore = dbstore
self.debug = debug
#kdrew: commenting out because "nr" is not a file but a location
#if not os.path.isfile(self.nr_db):
# raise Exception("Must provide a valid NR database file")
self.session = Session()
self.sequence = self.session.query(Sequence).get(self.sequence_key)
if not self.sequence:
raise Exception("No sequence with key {0} exists in DB".format(self.sequence_key))
self.fasta_file = "{0}.fasta".format(self.sequence_key)
self.chkpt_file = "{0}.chk".format(self.sequence_key)
self.psipred_file = "{0}.psipred".format(self.sequence_key)
# Set in run (dbo optionally)
self.prediction = None
self.dbo = None
if autorun:
self.prediction = self.run()
def get_prediction_string(self, ):
"""Returns the prediction string from the PsipredPrediction object (if populated)"""
if not self.prediction:
return None
return self.prediction.prediction
def run(self, ):
"""
Cobbles together elements for running Psipred (writes fasta, runs blast to get checkpoint,
runs psipred and psipass2, and then parses results and uploads to DB if set to).
IF dbstore is true, adds Psipred ORM object to hpf database and sets self.dbo.
RETURNS hpf.pdb.psipred.PsipredPrediction object
"""
import subprocess
from Bio import SeqIO
from Bio.Blast.NCBIStandalone import blastpgp
from hpf.hddb.db import Psipred as PsipredORM, PsipredFactory
from sqlalchemy.exc import IntegrityError
# Write fasta file
if self.debug: print "Psipred: writing fasta file"
with open(self.fasta_file, 'w') as handle:
SeqIO.write([self.sequence.record], handle, "fasta")
# Get exe path and run blast
if self.debug: print "Psipred: running blastpgp against '{0}' DB to create checkpoint file".format(self.nr_db)
blast_cmd = subprocess.Popen(["which", "blastpgp"], stdout=subprocess.PIPE).communicate()[0].strip()
result,error = blastpgp(blastcmd=blast_cmd,
program='blastpgp',
database=self.nr_db,
infile=self.fasta_file,
npasses=3,
checkpoint_outfile=self.chkpt_file,
expectation=1e-4,
model_threshold=1e-4,
align_outfile="/dev/null")
# Note: must call something that blocks on blastpgp results (need to wait for cmd to finish)
res = result.read()
err = error.read()
if self.debug:
print "Result: ", res
print "Error/Warning: ", err
# Create Psipred options object and run Psipred 3.2 on them
if self.debug: print "Psipred: running Psipred 3.2"
options = PsipredOptions(self.fasta_file,
profile=self.chkpt_file,
output=self.psipred_file+".1",
output2=self.psipred_file+".2",
horiz=self.psipred_file,
cwd=self.dir)
self.prediction = Psipred32(options).run()
# Add to database (optional) and return
if self.dbstore:
if self.debug: print "Psipred: adding psipred DBO to hpf database"
psipred_dbo = PsipredFactory().create(self.prediction, sequence_key=self.sequence.id, ginzu_version=self.ginzu_version)
self.session.add(psipred_dbo)
try:
self.session.commit()
except IntegrityError:
print "Psipred entry for <seq {0}, ginzu_version {1}> already exists in DB. Returning existing object".format(self.sequence_key, self.ginzu_version)
self.session.rollback()
psipred_dbo = self.session.query(PsipredORM).filter_by(sequence_key=self.sequence.id, ginzu_version=self.ginzu_version).first()
self.session.refresh(psipred_dbo)
self.dbo = psipred_dbo
return self.prediction