Skip to content

Commit 9f5a29a

Browse files
committed
Fixing study.py and tests. Removing add_raw_data as it is no longer needed
1 parent 4e5b9ce commit 9f5a29a

File tree

2 files changed

+31
-40
lines changed

2 files changed

+31
-40
lines changed

qiita_db/study.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,11 @@ def data_types(self):
642642
list of str
643643
"""
644644
conn_handler = SQLConnectionHandler()
645-
sql = ("SELECT DISTINCT DT.data_type FROM qiita.study_raw_data SRD "
646-
"JOIN qiita.prep_template PT ON SRD.raw_data_id = "
647-
"PT.raw_data_id JOIN qiita.data_type DT ON PT.data_type_id = "
648-
"DT.data_type_id WHERE SRD.study_id = %s")
645+
sql = """SELECT DISTINCT data_type
646+
FROM qiita.study_prep_template
647+
JOIN qiita.prep_template USING (prep_template_id)
648+
JOIN qiita.data_type USING (data_type_id)
649+
WHERE study_id = %s"""
649650
return [x[0] for x in conn_handler.execute_fetchall(sql, (self._id,))]
650651

651652
@property
@@ -759,36 +760,30 @@ def raw_data(self, data_type=None):
759760

760761
return [x[0] for x in conn_handler.execute_fetchall(sql, (self._id,))]
761762

762-
def add_raw_data(self, raw_data):
763-
""" Adds raw_data to the current study
763+
def prep_templates(self, data_type=None):
764+
"""Return list of prep template ids
764765
765766
Parameters
766767
----------
767-
raw_data : list of RawData
768-
The RawData objects to be added to the study
768+
data_type : str, optional
769+
If given, retrieve only prep templates for given datatype.
770+
Default None.
769771
770-
Raises
771-
------
772-
QiitaDBError
773-
If the raw_data is already linked to the current study
772+
Returns
773+
-------
774+
list of PrepTemplate ids
774775
"""
775-
conn_handler = SQLConnectionHandler()
776-
self._lock_non_sandbox(conn_handler)
777-
queue = "%d_add_raw_data" % self.id
778-
sql = ("SELECT EXISTS(SELECT * FROM qiita.study_raw_data WHERE "
779-
"study_id=%s AND raw_data_id=%s)")
780-
conn_handler.create_queue(queue)
781-
sql_args = [(self.id, rd.id) for rd in raw_data]
782-
conn_handler.add_to_queue(queue, sql, sql_args, many=True)
783-
linked = conn_handler.execute_queue(queue)
784-
785-
if any(linked):
786-
raise QiitaDBError("Some of the passed raw datas have been already"
787-
" linked to the study %s" % self.id)
776+
spec_data = ""
777+
if data_type:
778+
spec_data = " AND data_type_id = %s" % convert_to_id(data_type,
779+
"data_type")
788780

789-
conn_handler.executemany(
790-
"INSERT INTO qiita.study_raw_data (study_id, raw_data_id) "
791-
"VALUES (%s, %s)", sql_args)
781+
conn_handler = SQLConnectionHandler()
782+
sql = """SELECT prep_template_id
783+
FROM qiita.study_prep_template
784+
JOIN qiita.prep_template USING (prep_template_id)
785+
WHERE study_id = %s{0}""".format(spec_data)
786+
return [x[0] for x in conn_handler.execute_fetchall(sql, (self._id,))]
792787

793788
def preprocessed_data(self, data_type=None):
794789
""" Returns list of data ids for preprocessed data info

qiita_db/test/test_study.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime
33

44
from future.utils import viewitems
5+
import pandas as pd
56

67
from qiita_core.exceptions import IncompetentQiitaDeveloperError
78
from qiita_core.util import qiita_test_checker
@@ -11,6 +12,7 @@
1112
from qiita_db.user import User
1213
from qiita_db.data import RawData
1314
from qiita_db.util import convert_to_id
15+
from qiita_db.metadata_template import PrepTemplate
1416
from qiita_db.exceptions import (
1517
QiitaDBColumnError, QiitaDBStatusError, QiitaDBError,
1618
QiitaDBUnknownIDError)
@@ -586,26 +588,20 @@ def test_retrieve_data_types_none(self):
586588
self.assertEqual(new.data_types, [])
587589

588590
def test_retrieve_raw_data(self):
589-
self.assertEqual(self.study.raw_data(), [1, 2, 3, 4])
591+
self.assertEqual(self.study.raw_data(), [1])
590592

591593
def test_retrieve_raw_data_none(self):
592594
new = Study.create(User('test@foo.bar'), 'NOT Identification of the '
593595
'Microbiomes for Cannabis Soils', [1], self.info)
594596
self.assertEqual(new.raw_data(), [])
595597

596-
def test_add_raw_data(self):
597-
self._change_processed_data_status('awaiting_approval')
598+
def test_retrieve_prep_templates(self):
599+
self.assertEqual(self.study.prep_templates(), [1])
600+
601+
def test_retrieve_prep_templates_none(self):
598602
new = Study.create(User('test@foo.bar'), 'NOT Identification of the '
599603
'Microbiomes for Cannabis Soils', [1], self.info)
600-
new.add_raw_data([RawData(1), RawData(2)])
601-
obs = self.conn_handler.execute_fetchall(
602-
"SELECT * FROM qiita.study_raw_data WHERE study_id=%s",
603-
(new.id,))
604-
self.assertEqual(obs, [[new.id, 1], [new.id, 2]])
605-
606-
def test_add_raw_data_private(self):
607-
with self.assertRaises(QiitaDBStatusError):
608-
self.study.add_raw_data([RawData(2)])
604+
self.assertEqual(new.prep_templates(), [])
609605

610606
def test_retrieve_preprocessed_data(self):
611607
self.assertEqual(self.study.preprocessed_data(), [1, 2])

0 commit comments

Comments
 (0)