本文整理匯總了Python中hpf.hddb.db.Session.add方法的典型用法代碼示例。如果您正苦於以下問題:Python Session.add方法的具體用法?Python Session.add怎麽用?Python Session.add使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類hpf.hddb.db.Session
的用法示例。
在下文中一共展示了Session.add方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: OIDImporter
# 需要導入模塊: from hpf.hddb.db import Session [as 別名]
# 或者: from hpf.hddb.db.Session import add [as 別名]
class OIDImporter(object):
"""
Import a set of OID files into the database
"""
def __init__(
self,
familyName,
alignFile,
alignColcullLog,
alignSeqcullLog,
treeFile,
treeDiagCharsFile,
codemlFile=None,
alignFormat="fasta",
oid_key=None,
):
self.familyName = familyName
self.treeFile = treeFile
self.treeDiagCharsFile = treeDiagCharsFile
self.alignFile = alignFile
self.alignColcullLog = alignColcullLog
self.alignSeqcullLog = alignSeqcullLog
self.codemlFile = codemlFile
self.alignFormat = alignFormat
self.oid_key = oid_key
def merge(self):
from hpf.hddb.db import Session, Family
self.session = Session()
self.family = self.session.query(Family).filter(Family.name == self.familyName).first()
if not self.family:
runtime().debug("Creating family", self.familyName)
self._family()
self._alignment()
self._tree()
else:
self.alignment = self.family.alignment
self.tree = self.alignment.tree
runtime().debug("Found family", self.family.id)
if not self.family.alignments[0].tree.codeml:
runtime().debug("Importing codeml")
self._codeml()
else:
runtime().debug("Already found codeml", self.family.alignments[0].tree.codeml.id)
# Commit the session, close, and finish
self.session.commit()
self.session.close()
def _index(self, name):
n = name.split("#")[-1]
if n.startswith("N"):
n = n[1:]
assert n.isdigit()
return n
def _tree(self):
session = self.session
# # Load the tree file and rename the taxa.
# from Bio.Nexus.Nexus import Nexus
# nex=Nexus(self.treeFile)
# self.nexus = nex.trees[0]
from Bio.Nexus.Trees import Tree as NewickTree
tree_str = open(self.treeFile).read()
self.nexus = NewickTree(tree_str)
# Rename all the taxa.
for id in self.nexus.get_terminals():
node = self.nexus.node(id)
node.data.taxon = self._index(node.data.taxon)
# Create the DB object
from hpf.hddb.db import Tree
self.tree = Tree(
alignment_key=self.alignment.id,
text=self.nexus.to_string(plain=False, plain_newick=True),
filename=self.treeFile,
)
session.add(self.tree)
session.flush()
# Now add in the node references
self.nexus.name = self.tree.id
assert self.tree.id != None
runtime().debug("Added tree", self.tree)
from hpf.hddb.db import TreeNodeFactory
nodes = list(TreeNodeFactory().create(self.nexus))
for node in nodes:
node.ancestor_node = node.ancestor.id if node.ancestor else None
# This should add the new object into the session
self.tree.nodes.append(node)
#.........這裏部分代碼省略.........
示例2: HPFPsipredWrap
# 需要導入模塊: from hpf.hddb.db import Session [as 別名]
# 或者: from hpf.hddb.db.Session import add [as 別名]
class HPFPsipredWrap():
"""
A complete wrapper for running Psipred for HPF applications and storing results
in HPF DB (hpf.sequencePsiPred). Takes sequence key, will create fasta, checkpoint,
and psipred files, and will push psipred result by sequence key and ginzu_version
to DB.
Note: In supplying a ginzu version, choose the ginzu version that contains the
version of Psipred that you run (eg, Psipred 3.2 is ginzu_version 4)
The default value for ginzu_version should reflect the version of psipred of the
system on which this code is run.
Note: Requires the location of the NR blast database to make checkpoint file
"""
def __init__(self, sequence_key, nr_db, ginzu_version="4", dir=None, autorun=True, dbstore=True, debug=True):
"""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
#.........這裏部分代碼省略.........