Commit ee4686c6 authored by Pauline Pommeret's avatar Pauline Pommeret

some docstrings

parent 414f61a6
#!/usr/bin/env python2.7
"""
# XXX
"""
import psycopg2
import psycopg2.extras
......@@ -10,7 +12,15 @@ class PGCursor(object):
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.set_session(autocommit = True)
......@@ -18,37 +28,62 @@ class PGCursor(object):
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
sequences
(accession, name, description, sequence, label, alphabet, alpha, sliding, centering, trx_scale_path)
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
id;"""
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]
# We do a record for each helicoidal param, recording all the frames in the good table.
for helicoidal_parameter in seq.mdd:
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_%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
(%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)
# Trx data.
trx_query = """INSERT INTO
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
(%(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.update({'seq_id': seq_id})
self._cur.execute(trx_query, dic_to_sql)
#Correlation data
for (correl_types, bunch_of_data) in seq.correlation.iteritems():
type_a, type_b = correl_types.split("/")
for (frame_num, data) in bunch_of_data.iteritems():
......@@ -56,7 +91,8 @@ class PGCursor(object):
correlations
(seq_id, frame_num, type_a, type_b, spearman_complete, spearman_center, pearson_complete, pearson_center)
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.update({
"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