85
85
from .base import QiitaObject
86
86
from .logger import LogEntry
87
87
from .sql_connection import SQLConnectionHandler
88
- from .exceptions import QiitaDBError , QiitaDBUnknownIDError
88
+ from .exceptions import QiitaDBError , QiitaDBUnknownIDError , QiitaDBStatusError
89
89
from .util import (exists_dynamic_table , insert_filepaths , convert_to_id ,
90
90
convert_from_id , purge_filepaths , get_filepath_id ,
91
- get_mountpoint , move_filepaths_to_upload_folder )
91
+ get_mountpoint , move_filepaths_to_upload_folder ,
92
+ infer_status )
92
93
93
94
94
95
class BaseData (QiitaObject ):
@@ -615,6 +616,58 @@ def remove_filepath(self, fp):
615
616
# Delete the files, if they are not used anywhere
616
617
purge_filepaths (conn_handler )
617
618
619
+ def status (self , study ):
620
+ """The status of the raw data within the given study
621
+
622
+ Parameters
623
+ ----------
624
+ study : Study
625
+ The study that is looking to the raw data status
626
+
627
+ Returns
628
+ -------
629
+ str
630
+ The status of the raw data
631
+
632
+ Raises
633
+ ------
634
+ QiitaDBStatusError
635
+ If the raw data does not belong to the passed study
636
+
637
+ Notes
638
+ -----
639
+ Given that a raw data can be shared by multiple studies, we need to
640
+ know under which context (study) we want to check the raw data status.
641
+ The rationale is that a raw data object can contain data from multiple
642
+ studies, so the raw data can have multiple status at the same time.
643
+ We then check the processed data generated to infer the status of the
644
+ raw data.
645
+ """
646
+ if self ._id not in study .raw_data ():
647
+ raise QiitaDBStatusError (
648
+ "The study %s does not have access to the raw data %s"
649
+ % (study .id , self .id ))
650
+
651
+ conn_handler = SQLConnectionHandler ()
652
+ sql = """SELECT processed_data_status
653
+ FROM qiita.processed_data_status pds
654
+ JOIN qiita.processed_data pd
655
+ USING (processed_data_status_id)
656
+ JOIN qiita.preprocessed_processed_data ppd_pd
657
+ USING (processed_data_id)
658
+ JOIN qiita.prep_template_preprocessed_data pt_ppd
659
+ USING (preprocessed_data_id)
660
+ JOIN qiita.prep_template pt
661
+ USING (prep_template_id)
662
+ JOIN qiita.raw_data rd
663
+ USING (raw_data_id)
664
+ JOIN qiita.study_processed_data spd
665
+ USING (processed_data_id)
666
+ WHERE pt.raw_data_id=%s AND spd.study_id=%s"""
667
+ pd_statuses = conn_handler .execute_fetchall (sql , (self ._id , study .id ))
668
+
669
+ return infer_status (pd_statuses )
670
+
618
671
619
672
class PreprocessedData (BaseData ):
620
673
r"""Object for dealing with preprocessed data
@@ -995,6 +1048,34 @@ def processing_status(self, state):
995
1048
"UPDATE qiita.{0} SET processing_status=%s WHERE "
996
1049
"preprocessed_data_id=%s" .format (self ._table ), (state , self .id ))
997
1050
1051
+ @property
1052
+ def status (self ):
1053
+ """The status of the preprocessed data
1054
+
1055
+ Returns
1056
+ -------
1057
+ str
1058
+ The status of the preprocessed_data
1059
+
1060
+ Notes
1061
+ -----
1062
+ The status of a preprocessed data is inferred by the status of the
1063
+ processed data generated from this preprocessed data. If no processed
1064
+ data has been generated with this preprocessed data; then the status
1065
+ is 'sandbox'.
1066
+ """
1067
+ conn_handler = SQLConnectionHandler ()
1068
+ sql = """SELECT processed_data_status
1069
+ FROM qiita.processed_data_status pds
1070
+ JOIN qiita.processed_data pd
1071
+ USING (processed_data_status_id)
1072
+ JOIN qiita.preprocessed_processed_data ppd_pd
1073
+ USING (processed_data_id)
1074
+ WHERE ppd_pd.preprocessed_data_id=%s"""
1075
+ pd_statuses = conn_handler .execute_fetchall (sql , (self ._id ,))
1076
+
1077
+ return infer_status (pd_statuses )
1078
+
998
1079
999
1080
class ProcessedData (BaseData ):
1000
1081
r"""Object for dealing with processed data
@@ -1020,6 +1101,61 @@ class ProcessedData(BaseData):
1020
1101
_study_processed_table = "study_processed_data"
1021
1102
_preprocessed_processed_table = "preprocessed_processed_data"
1022
1103
1104
+ @classmethod
1105
+ def get_by_status (cls , status ):
1106
+ """Returns id for all ProcessedData with given status
1107
+
1108
+ Parameters
1109
+ ----------
1110
+ status : str
1111
+ Status to search for
1112
+
1113
+ Returns
1114
+ -------
1115
+ list of int
1116
+ All the processed data ids that match the given status
1117
+ """
1118
+ conn_handler = SQLConnectionHandler ()
1119
+ sql = """SELECT processed_data_id FROM qiita.processed_data pd
1120
+ JOIN qiita.processed_data_status pds
1121
+ USING (processed_data_status_id)
1122
+ WHERE pds.processed_data_status=%s"""
1123
+ result = conn_handler .execute_fetchall (sql , (status ,))
1124
+ if result :
1125
+ pds = set (x [0 ] for x in result )
1126
+ else :
1127
+ pds = set ()
1128
+
1129
+ return pds
1130
+
1131
+ @classmethod
1132
+ def get_by_status_grouped_by_study (cls , status ):
1133
+ """Returns id for all ProcessedData with given status grouped by study
1134
+
1135
+ Parameters
1136
+ ----------
1137
+ status : str
1138
+ Status to search for
1139
+
1140
+ Returns
1141
+ -------
1142
+ dict of list of int
1143
+ A dictionary keyed by study id in which the values are the
1144
+ processed data ids that belong to that study and match the given
1145
+ status
1146
+ """
1147
+ conn_handler = SQLConnectionHandler ()
1148
+ sql = """SELECT spd.study_id,
1149
+ array_agg(pd.processed_data_id ORDER BY pd.processed_data_id)
1150
+ FROM qiita.processed_data pd
1151
+ JOIN qiita.processed_data_status pds
1152
+ USING (processed_data_status_id)
1153
+ JOIN qiita.study_processed_data spd
1154
+ USING (processed_data_id)
1155
+ WHERE pds.processed_data_status = %s
1156
+ GROUP BY spd.study_id;"""
1157
+ return dict (conn_handler .execute_fetchall (sql , (status ,)))
1158
+
1023
1159
@classmethod
1024
1160
def create (cls , processed_params_table , processed_params_id , filepaths ,
1025
1161
preprocessed_data = None , study = None , processed_date = None ,
@@ -1173,3 +1309,40 @@ def processed_date(self):
1173
1309
return conn_handler .execute_fetchone (
1174
1310
"SELECT processed_date FROM qiita.{0} WHERE "
1175
1311
"processed_data_id=%s" .format (self ._table ), (self .id ,))[0 ]
1312
+
1313
+ @property
1314
+ def status (self ):
1315
+ conn_handler = SQLConnectionHandler ()
1316
+ sql = """SELECT pds.processed_data_status
1317
+ FROM qiita.processed_data_status pds
1318
+ JOIN qiita.processed_data pd
1319
+ USING (processed_data_status_id)
1320
+ WHERE pd.processed_data_id=%s"""
1321
+ return conn_handler .execute_fetchone (sql , (self ._id ,))[0 ]
1322
+
1323
+ @status .setter
1324
+ def status (self , status ):
1325
+ """Set the status value
1326
+
1327
+ Parameters
1328
+ ----------
1329
+ status : str
1330
+ The new status
1331
+
1332
+ Raises
1333
+ ------
1334
+ QiitaDBStatusError
1335
+ If the processed data status is public
1336
+ """
1337
+ if self .status == 'public' :
1338
+ raise QiitaDBStatusError (
1339
+ "Illegal operation on public processed data" )
1340
+
1341
+ conn_handler = SQLConnectionHandler ()
1342
+
1343
+ status_id = convert_to_id (status , 'processed_data_status' ,
1344
+ conn_handler = conn_handler )
1345
+
1346
+ sql = """UPDATE qiita.{0} SET processed_data_status_id = %s
1347
+ WHERE processed_data_id=%s""" .format (self ._table )
1348
+ conn_handler .execute (sql , (status_id , self ._id ))
0 commit comments