Skip to content

Commit a748c94

Browse files
committed
Merge pull request #118 from squirrelo/samplesanalysis
Adds analysis.samples, another test analysis
2 parents ca65fb1 + 85890fc commit a748c94

File tree

6 files changed

+52
-27
lines changed

6 files changed

+52
-27
lines changed

qiita_db/analysis.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# The full license is in the file LICENSE, distributed with this software.
1717
# -----------------------------------------------------------------------------
1818
from __future__ import division
19+
from collections import defaultdict
1920

2021
from .sql_connection import SQLConnectionHandler
2122
from .base import QiitaStatusObject
@@ -32,6 +33,7 @@ class Analysis(QiitaStatusObject):
3233
owner
3334
name
3435
description
36+
samples
3537
biom_tables
3638
shared_with
3739
jobs
@@ -152,6 +154,24 @@ def description(self, description):
152154
"analysis_id = %s".format(self._table))
153155
conn_handler.execute(sql, (description, self._id))
154156

157+
@property
158+
def samples(self):
159+
"""The processed data and samples attached to the analysis
160+
161+
Returns
162+
-------
163+
dict
164+
Format is {processed_data_id: [sample_id, sample_id, ...]}
165+
"""
166+
conn_handler = SQLConnectionHandler()
167+
sql = ("SELECT processed_data_id, sample_id FROM qiita.analysis_sample"
168+
" WHERE analysis_id = %s ORDER BY processed_data_id")
169+
ret_samples = defaultdict(list)
170+
# turn into dict of samples keyed to processed_data_id
171+
for pid, sample in conn_handler.execute_fetchall(sql, (self._id, )):
172+
ret_samples[pid].append(sample)
173+
return ret_samples
174+
155175
@property
156176
def shared_with(self):
157177
"""The user the analysis is shared with

qiita_db/support_files/initialize.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ INSERT INTO qiita.filepath_type (filepath_type) VALUES ('raw_sequences'), ('raw_
3838
INSERT INTO qiita.checksum_algorithm (name) VALUES ('crc32');
3939

4040
-- Populate commands available
41-
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');
41+
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');

qiita_db/support_files/populate_test_db.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,19 +317,19 @@ INSERT INTO qiita.processed_filepath (processed_data_id, filepath_id) VALUES (1,
317317
INSERT INTO qiita.filepath (filepath, filepath_type_id, checksum, checksum_algorithm_id) VALUES ('job1result.txt', 8, '852952723', 1), ('job2tar.tar', 7, '852952723', 1);
318318

319319
-- Insert jobs
320-
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');
320+
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"}');
321321

322322
-- Insert Analysis
323-
INSERT INTO qiita.analysis (email, name, description, analysis_status_id, pmid) VALUES ('test@foo.bar', 'SomeAnalysis', 'A test analysis', 4, '121112');
323+
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');
324324

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

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

331331
-- Attach samples to analysis
332-
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');
332+
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');
333333

334334
--Share analysis with shared user
335335
INSERT INTO qiita.analysis_users (analysis_id, email) VALUES (1, 'shared@foo.bar');

qiita_db/test/test_analysis.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,24 @@ def test_lock_public_running(self):
3535
def test_create(self):
3636
new = Analysis.create(User("admin@foo.bar"), "newAnalysis",
3737
"A New Analysis")
38-
self.assertEqual(new.id, 2)
39-
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 2"
38+
self.assertEqual(new.id, 3)
39+
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 3"
4040
obs = self.conn_handler.execute_fetchall(sql)
41-
self.assertEqual(obs, [[2, 'admin@foo.bar', 'newAnalysis',
41+
self.assertEqual(obs, [[3, 'admin@foo.bar', 'newAnalysis',
4242
'A New Analysis', 1, None]])
4343

4444
def test_create_parent(self):
4545
new = Analysis.create(User("admin@foo.bar"), "newAnalysis",
4646
"A New Analysis", Analysis(1))
47-
self.assertEqual(new.id, 2)
48-
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 2"
47+
self.assertEqual(new.id, 3)
48+
sql = "SELECT * FROM qiita.analysis WHERE analysis_id = 3"
4949
obs = self.conn_handler.execute_fetchall(sql)
50-
self.assertEqual(obs, [[2, 'admin@foo.bar', 'newAnalysis',
50+
self.assertEqual(obs, [[3, 'admin@foo.bar', 'newAnalysis',
5151
'A New Analysis', 1, None]])
5252

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

5757
def test_retrieve_owner(self):
5858
self.assertEqual(self.analysis.owner, "test@foo.bar")
@@ -67,6 +67,11 @@ def test_set_description(self):
6767
self.analysis.description = "New description"
6868
self.assertEqual(self.analysis.description, "New description")
6969

70+
def test_retrieve_samples(self):
71+
exp = {1: ['SKB8.640193', 'SKD8.640184', 'SKB7.640196',
72+
'SKM9.640192', 'SKM4.640180']}
73+
self.assertEqual(self.analysis.samples, exp)
74+
7075
def test_retrieve_shared_with(self):
7176
self.assertEqual(self.analysis.shared_with, ["shared@foo.bar"])
7277

qiita_db/test/test_job.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,35 +55,35 @@ def test_create(self):
5555
# make first job
5656
new = Job.create("18S", "Alpha Diversity",
5757
self.options, Analysis(1))
58-
self.assertEqual(new.id, 3)
58+
self.assertEqual(new.id, 4)
5959
# make sure job inserted correctly
6060
obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.job "
61-
"WHERE job_id = 3")
62-
exp = [[3, 2, 1, 3, '{"option1":false,"option2":25,"option3":"NEW"}',
61+
"WHERE job_id = 4")
62+
exp = [[4, 2, 1, 3, '{"option1":false,"option2":25,"option3":"NEW"}',
6363
None]]
6464
self.assertEqual(obs, exp)
6565
# make sure job added to analysis correctly
6666
obs = self.conn_handler.execute_fetchall("SELECT * FROM "
6767
"qiita.analysis_job WHERE "
68-
"job_id = 3")
69-
exp = [[1, 3]]
68+
"job_id = 4")
69+
exp = [[1, 4]]
7070
self.assertEqual(obs, exp)
7171

7272
# make second job with diff datatype and command to test column insert
7373
new = Job.create("16S", "Beta Diversity",
7474
self.options, Analysis(1))
75-
self.assertEqual(new.id, 4)
75+
self.assertEqual(new.id, 5)
7676
# make sure job inserted correctly
7777
obs = self.conn_handler.execute_fetchall("SELECT * FROM qiita.job "
78-
"WHERE job_id = 4")
79-
exp = [[4, 1, 1, 2, '{"option1":false,"option2":25,"option3":"NEW"}',
78+
"WHERE job_id = 5")
79+
exp = [[5, 1, 1, 2, '{"option1":false,"option2":25,"option3":"NEW"}',
8080
None]]
8181
self.assertEqual(obs, exp)
8282
# make sure job added to analysis correctly
8383
obs = self.conn_handler.execute_fetchall("SELECT * FROM "
8484
"qiita.analysis_job WHERE "
85-
"job_id = 4")
86-
exp = [[1, 4]]
85+
"job_id = 5")
86+
exp = [[1, 5]]
8787
self.assertEqual(obs, exp)
8888

8989
# def test_create_exists(self):

qiita_db/test/test_setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ def test_processed_filepath(self):
9999
self._check_count("qiita.processed_filepath", 1)
100100

101101
def test_job(self):
102-
self._check_count("qiita.job", 2)
102+
self._check_count("qiita.job", 3)
103103

104104
def test_analysis(self):
105-
self._check_count("qiita.analysis", 1)
105+
self._check_count("qiita.analysis", 2)
106106

107107
def test_analysis_job(self):
108-
self._check_count("qiita.analysis_job", 2)
108+
self._check_count("qiita.analysis_job", 3)
109109

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

113113
def test_analysis_sample(self):
114-
self._check_count("qiita.analysis_sample", 5)
114+
self._check_count("qiita.analysis_sample", 8)
115115

116116
def test_analysis_users(self):
117117
self._check_count("qiita.analysis_users", 1)

0 commit comments

Comments
 (0)