Commit ee4686c6 authored by Pauline Pommeret's avatar Pauline Pommeret

some docstrings

parent 414f61a6
#!/usr/bin/env python2.7 #!/usr/bin/env python2.7
"""
# XXX
"""
import psycopg2 import psycopg2
import psycopg2.extras import psycopg2.extras
...@@ -10,7 +12,15 @@ class PGCursor(object): ...@@ -10,7 +12,15 @@ class PGCursor(object):
def __init__(self): def __init__(self):
""" """
Starts a psycopg2 cursor. Starts a psycopg2 cursor and populates:
- ``self._conn``
- ``self._cur``
Parameters:
- ``self``
Returns:
- nothing
""" """
self._conn = psycopg2.connect(database='itpp') self._conn = psycopg2.connect(database='itpp')
self._conn.set_session(autocommit = True) self._conn.set_session(autocommit = True)
...@@ -18,37 +28,62 @@ class PGCursor(object): ...@@ -18,37 +28,62 @@ class PGCursor(object):
def store_sequence(self, seq): def store_sequence(self, seq):
""" """
Store the sequence data in database Stores the sequence data in database
Parameters:
- ``self``
- ``seq`` : sequence object (:py:mod:`sequence`)
Returns:
- nothing
""" """
# This query is designed to add the sequence metadata in the database.
# We give explicitly the columns of the table we want to fill, and we also give the dict keys for the VALUES,
# so we only have to give seq to cur.execute() as if seq was a dict.
main_seq_query = """INSERT INTO main_seq_query = """INSERT INTO
sequences sequences
(accession, name, description, sequence, label, alphabet, alpha, sliding, centering, trx_scale_path) (accession, name, description, sequence, label, alphabet, alpha, sliding, centering, trx_scale_path)
VALUES VALUES
(%(accession)s, %(name)s, %(description)s, %(sequence)s, %(label)s, %(alphabet)s, %(alpha)s, %(sliding)s, %(centering)s, %(trx_scale_path)s) (%(accession)s, %(name)s, %(description)s, %(sequence)s, %(label)s, %(alphabet)s,\
%(alpha)s, %(sliding)s, %(centering)s, %(trx_scale_path)s)
RETURNING RETURNING
id;""" id;"""
self._cur.execute(main_seq_query, seq) self._cur.execute(main_seq_query, seq)
# The query returns the id of the new recorded data. Will be useful for the other queries.
seq_id = self._cur.fetchone()[0] seq_id = self._cur.fetchone()[0]
# We do a record for each helicoidal param, recording all the frames in the good table.
for helicoidal_parameter in seq.mdd: for helicoidal_parameter in seq.mdd:
for (frame_num, values) in seq.mdd[helicoidal_parameter].iteritems(): for (frame_num, values) in seq.mdd[helicoidal_parameter].iteritems():
# this query is formatted twice, once for data outside from value dict and for the table name,
# once again for the dict value itself. This is why there is %%(attr)s instead of %(attr)s.
md_query = """INSERT INTO md_query = """INSERT INTO
md_%s md_%s
(seq_id, frame_num, complete_peak_freq, complete_peak, complete_size, center_peak_freq, center_peak, center_size, sliding_peak_freq, sliding_peak, sliding_size) (seq_id, frame_num, complete_peak_freq, complete_peak, complete_size, center_peak_freq,\
center_peak, center_size, sliding_peak_freq, sliding_peak, sliding_size)
VALUES VALUES
(%s, %s, %%(complete_peak_freq)s, %%(complete_peak)s, %%(complete_size)s, %%(center_peak_freq)s, %%(center_peak)s, %%(center_size)s, %%(sliding_peak_freq)s, %%(sliding_peak)s, %%(sliding_size)s);""" % (helicoidal_parameter, seq_id, frame_num) (%s, %s, %%(complete_peak_freq)s, %%(complete_peak)s, %%(complete_size)s, %%(center_peak_freq)s,\
%%(center_peak)s, %%(center_size)s, %%(sliding_peak_freq)s, %%(sliding_peak)s,\
%%(sliding_size)s);""" % (helicoidal_parameter, seq_id, frame_num)
# We execute the query.
self._cur.execute(md_query, values) self._cur.execute(md_query, values)
# Trx data.
trx_query = """INSERT INTO trx_query = """INSERT INTO
trx trx
(seq_id, complete_peak_freq, complete_peak, complete_size, center_peak_freq, center_peak, center_size, sliding_peak_freq, sliding_peak, sliding_size) (seq_id, complete_peak_freq, complete_peak, complete_size, center_peak_freq,\
center_peak, center_size, sliding_peak_freq, sliding_peak, sliding_size)
VALUES VALUES
(%(seq_id)s, %(complete_peak_freq)s, %(complete_peak)s, %(complete_size)s, %(center_peak_freq)s, %(center_peak)s, %(center_size)s, %(sliding_peak_freq)s, %(sliding_peak)s, %(sliding_size)s);""" (%(seq_id)s, %(complete_peak_freq)s, %(complete_peak)s, %(complete_size)s,\
%(center_peak_freq)s, %(center_peak)s, %(center_size)s, %(sliding_peak_freq)s, %(sliding_peak)s, %(sliding_size)s);"""
dic_to_sql = dict(seq.trx) dic_to_sql = dict(seq.trx)
dic_to_sql.update({'seq_id': seq_id}) dic_to_sql.update({'seq_id': seq_id})
self._cur.execute(trx_query, dic_to_sql) self._cur.execute(trx_query, dic_to_sql)
#Correlation data
for (correl_types, bunch_of_data) in seq.correlation.iteritems(): for (correl_types, bunch_of_data) in seq.correlation.iteritems():
type_a, type_b = correl_types.split("/") type_a, type_b = correl_types.split("/")
for (frame_num, data) in bunch_of_data.iteritems(): for (frame_num, data) in bunch_of_data.iteritems():
...@@ -56,7 +91,8 @@ class PGCursor(object): ...@@ -56,7 +91,8 @@ class PGCursor(object):
correlations correlations
(seq_id, frame_num, type_a, type_b, spearman_complete, spearman_center, pearson_complete, pearson_center) (seq_id, frame_num, type_a, type_b, spearman_complete, spearman_center, pearson_complete, pearson_center)
VALUES VALUES
(%(seq_id)s, %(frame_num)s, %(type_a)s, %(type_b)s, %(spearman_complete)s, %(spearman_center)s, %(pearson_complete)s, %(pearson_center)s);""" (%(seq_id)s, %(frame_num)s, %(type_a)s, %(type_b)s, %(spearman_complete)s,\
%(spearman_center)s, %(pearson_complete)s, %(pearson_center)s);"""
dic_to_sql = dict(data) dic_to_sql = dict(data)
dic_to_sql.update({ dic_to_sql.update({
"frame_num": frame_num, "frame_num": frame_num,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment