Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds analysis.samples, another test analysis #118

Merged
merged 5 commits into from
Jun 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions qiita_db/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from __future__ import division
from collections import defaultdict

from .sql_connection import SQLConnectionHandler
from .base import QiitaStatusObject
Expand All @@ -32,6 +33,7 @@ class Analysis(QiitaStatusObject):
owner
name
description
samples
biom_tables
shared_with
jobs
Expand Down Expand Up @@ -152,6 +154,24 @@ def description(self, description):
"analysis_id = %s".format(self._table))
conn_handler.execute(sql, (description, self._id))

@property
def samples(self):
"""The processed data and samples attached to the analysis

Returns
-------
dict
Format is {processed_data_id: [sample_id, sample_id, ...]}
"""
conn_handler = SQLConnectionHandler()
sql = ("SELECT processed_data_id, sample_id FROM qiita.analysis_sample"
" WHERE analysis_id = %s ORDER BY processed_data_id")
ret_samples = defaultdict(list)
# turn into dict of samples keyed to processed_data_id
for pid, sample in conn_handler.execute_fetchall(sql, (self._id, )):
ret_samples[pid].append(sample)
return ret_samples

@property
def shared_with(self):
"""The user the analysis is shared with
Expand Down
2 changes: 1 addition & 1 deletion qiita_db/support_files/initialize.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ INSERT INTO qiita.filepath_type (filepath_type) VALUES ('raw_sequences'), ('raw_
INSERT INTO qiita.checksum_algorithm (name) VALUES ('crc32');

-- Populate commands available
INSERT INTO qiita.command (name, command) VALUES ('Summarize Taxa', 'summarize_taxa_through_plots.py'), ('Beta Diversity', 'beta_diversity_through_plots.py'), ('Alpha Diversity', 'alpha_diversity_through_plots.py');
INSERT INTO qiita.command (name, command) VALUES ('Summarize Taxa', 'summarize_taxa_through_plots.py'), ('Beta Diversity', 'beta_diversity_through_plots.py'), ('Alpha Diversity', 'alpha_rarefaction_through_plots.py');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change name to Alpha Rarefaction

8 changes: 4 additions & 4 deletions qiita_db/support_files/populate_test_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,19 @@ INSERT INTO qiita.processed_filepath (processed_data_id, filepath_id) VALUES (1,
INSERT INTO qiita.filepath (filepath, filepath_type_id, checksum, checksum_algorithm_id) VALUES ('job1result.txt', 8, '852952723', 1), ('job2tar.tar', 7, '852952723', 1);

-- Insert jobs
INSERT INTO qiita.job (data_type_id, job_status_id, command_id, options) VALUES (1, 3, 1, '{"option1":true,"option2":12,"option3":"FCM"}'), (1, 3, 2, 'options2');
INSERT INTO qiita.job (data_type_id, job_status_id, command_id, options) VALUES (1, 1, 1, '{"option1":true,"option2":12,"option3":"FCM"}'), (1, 3, 2, 'options2'), (1, 1, 2, '{"option1":true,"option2":12,"option3":"FCM"}');

-- Insert Analysis
INSERT INTO qiita.analysis (email, name, description, analysis_status_id, pmid) VALUES ('test@foo.bar', 'SomeAnalysis', 'A test analysis', 4, '121112');
INSERT INTO qiita.analysis (email, name, description, analysis_status_id, pmid) VALUES ('test@foo.bar', 'SomeAnalysis', 'A test analysis', 4, '121112'), ('test@foo.bar', 'SomeSecondAnalysis', 'Another test analysis', 1, '22221112');

-- Attach jobs to analysis
INSERT INTO qiita.analysis_job (analysis_id, job_id) VALUES (1, 1), (1, 2);
INSERT INTO qiita.analysis_job (analysis_id, job_id) VALUES (1, 1), (1, 2), (2, 3);

-- Attach filepath to analysis
INSERT INTO qiita.analysis_filepath (analysis_id, filepath_id) VALUES (1, 7);

-- Attach samples to analysis
INSERT INTO qiita.analysis_sample (analysis_id, processed_data_id, sample_id) VALUES (1,1,'SKB8.640193'), (1,1,'SKD8.640184'), (1,1,'SKB7.640196'), (1,1,'SKM9.640192'), (1,1,'SKM4.640180');
INSERT INTO qiita.analysis_sample (analysis_id, processed_data_id, sample_id) VALUES (1,1,'SKB8.640193'), (1,1,'SKD8.640184'), (1,1,'SKB7.640196'), (1,1,'SKM9.640192'), (1,1,'SKM4.640180'), (2,1,'SKB8.640193'), (2,1,'SKD8.640184'), (2,1,'SKB7.640196');

--Share analysis with shared user
INSERT INTO qiita.analysis_users (analysis_id, email) VALUES (1, 'shared@foo.bar');
Expand Down
21 changes: 13 additions & 8 deletions qiita_db/test/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ def test_lock_public_running(self):
def test_create(self):
new = Analysis.create(User("admin@foo.bar"), "newAnalysis",
"A New Analysis")
self.assertEqual(new.id, 2)
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 2"
self.assertEqual(new.id, 3)
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 3"
obs = self.conn_handler.execute_fetchall(sql)
self.assertEqual(obs, [[2, 'admin@foo.bar', 'newAnalysis',
self.assertEqual(obs, [[3, 'admin@foo.bar', 'newAnalysis',
'A New Analysis', 1, None]])

def test_create_parent(self):
new = Analysis.create(User("admin@foo.bar"), "newAnalysis",
"A New Analysis", Analysis(1))
self.assertEqual(new.id, 2)
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 2"
self.assertEqual(new.id, 3)
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 3"
obs = self.conn_handler.execute_fetchall(sql)
self.assertEqual(obs, [[2, 'admin@foo.bar', 'newAnalysis',
self.assertEqual(obs, [[3, 'admin@foo.bar', 'newAnalysis',
'A New Analysis', 1, None]])

sql = "SELECT * FROM qiita.analysis_chain WHERE child_id = 2"
sql = "SELECT * FROM qiita.analysis_chain WHERE child_id = 3"
obs = self.conn_handler.execute_fetchall(sql)
self.assertEqual(obs, [[1, 2]])
self.assertEqual(obs, [[1, 3]])

def test_retrieve_owner(self):
self.assertEqual(self.analysis.owner, "test@foo.bar")
Expand All @@ -67,6 +67,11 @@ def test_set_description(self):
self.analysis.description = "New description"
self.assertEqual(self.analysis.description, "New description")

def test_retrieve_samples(self):
exp = {1: ['SKB8.640193', 'SKD8.640184', 'SKB7.640196',
'SKM9.640192', 'SKM4.640180']}
self.assertEqual(self.analysis.samples, exp)

def test_retrieve_shared_with(self):
self.assertEqual(self.analysis.shared_with, ["shared@foo.bar"])

Expand Down
20 changes: 10 additions & 10 deletions qiita_db/test/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,35 +55,35 @@ def test_create(self):
# make first job
new = Job.create("18S", "Alpha Diversity",
self.options, Analysis(1))
self.assertEqual(new.id, 3)
self.assertEqual(new.id, 4)
# make sure job inserted correctly
obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.job "
"WHERE job_id = 3")
exp = [[3, 2, 1, 3, '{"option1":false,"option2":25,"option3":"NEW"}',
"WHERE job_id = 4")
exp = [[4, 2, 1, 3, '{"option1":false,"option2":25,"option3":"NEW"}',
None]]
self.assertEqual(obs, exp)
# make sure job added to analysis correctly
obs = self.conn_handler.execute_fetchall("SELECT * FROM "
"qiita.analysis_job WHERE "
"job_id = 3")
exp = [[1, 3]]
"job_id = 4")
exp = [[1, 4]]
self.assertEqual(obs, exp)

# make second job with diff datatype and command to test column insert
new = Job.create("16S", "Beta Diversity",
self.options, Analysis(1))
self.assertEqual(new.id, 4)
self.assertEqual(new.id, 5)
# make sure job inserted correctly
obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.job "
"WHERE job_id = 4")
exp = [[4, 1, 1, 2, '{"option1":false,"option2":25,"option3":"NEW"}',
"WHERE job_id = 5")
exp = [[5, 1, 1, 2, '{"option1":false,"option2":25,"option3":"NEW"}',
None]]
self.assertEqual(obs, exp)
# make sure job added to analysis correctly
obs = self.conn_handler.execute_fetchall("SELECT * FROM "
"qiita.analysis_job WHERE "
"job_id = 4")
exp = [[1, 4]]
"job_id = 5")
exp = [[1, 5]]
self.assertEqual(obs, exp)

# def test_create_exists(self):
Expand Down
8 changes: 4 additions & 4 deletions qiita_db/test/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ def test_processed_filepath(self):
self._check_count("qiita.processed_filepath", 1)

def test_job(self):
self._check_count("qiita.job", 2)
self._check_count("qiita.job", 3)

def test_analysis(self):
self._check_count("qiita.analysis", 1)
self._check_count("qiita.analysis", 2)

def test_analysis_job(self):
self._check_count("qiita.analysis_job", 2)
self._check_count("qiita.analysis_job", 3)

def test_analysis_filepath(self):
self._check_count("qiita.analysis_filepath", 1)

def test_analysis_sample(self):
self._check_count("qiita.analysis_sample", 5)
self._check_count("qiita.analysis_sample", 8)

def test_analysis_users(self):
self._check_count("qiita.analysis_users", 1)
Expand Down