Skip to content

Commit adf3b26

Browse files
committed
Fixing merge conflicts
2 parents 4332597 + 6f326eb commit adf3b26

File tree

20 files changed

+256
-138
lines changed

20 files changed

+256
-138
lines changed

qiita_db/analysis.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,12 @@ def jobs(self):
425425
Returns
426426
-------
427427
list of ints
428-
Job ids for jobs in analysis
428+
Job ids for jobs in analysis. Empty list if no jobs attached.
429429
"""
430430
conn_handler = SQLConnectionHandler()
431431
sql = ("SELECT job_id FROM qiita.analysis_job WHERE "
432432
"analysis_id = %s".format(self._table))
433433
job_ids = conn_handler.execute_fetchall(sql, (self._id, ))
434-
if job_ids == []:
435-
return None
436434
return [job_id[0] for job_id in job_ids]
437435

438436
@property

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,11 @@ def _add_common_creation_steps_to_queue(cls, md_template, obj_id,
637637
for col, dtype in zip(headers, datatypes)]
638638
conn_handler.add_to_queue(
639639
queue_name,
640-
"CREATE TABLE qiita.{0} (sample_id varchar NOT NULL, {1})".format(
640+
"CREATE TABLE qiita.{0} ("
641+
"sample_id varchar NOT NULL, {1}, "
642+
"CONSTRAINT fk_{0} FOREIGN KEY (sample_id) "
643+
"REFERENCES qiita.study_sample (sample_id) "
644+
"ON UPDATE CASCADE)".format(
641645
table_name, ', '.join(column_datatype)))
642646

643647
# Insert values on custom table
@@ -703,9 +707,9 @@ def _add_common_extend_steps_to_queue(self, md_template, conn_handler,
703707

704708
if existing_samples:
705709
warnings.warn(
706-
"No values have been modified for samples '%s'. However, "
707-
"the following columns have been added to them: '%s'"
708-
% (", ".join(existing_samples), ", ".join(new_cols)),
710+
"No values have been modified for existing samples (%s). "
711+
"However, the following columns have been added to them: "
712+
"'%s'" % (len(existing_samples), ", ".join(new_cols)),
709713
QiitaDBWarning)
710714
# The values for the new columns are the only ones that get
711715
# added to the database. None of the existing values will be
@@ -727,8 +731,8 @@ def _add_common_extend_steps_to_queue(self, md_template, conn_handler,
727731
conn_handler.add_to_queue(queue_name, sql, values, many=True)
728732
elif existing_samples:
729733
warnings.warn(
730-
"The following samples already exist in the template and "
731-
"will be ignored: %s" % ", ".join(existing_samples),
734+
"%d samples already exist in the template and "
735+
"their values won't be modified" % len(existing_samples),
732736
QiitaDBWarning)
733737

734738
if new_samples:

qiita_db/metadata_template/prep_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def delete(cls, id_):
232232
conn_handler.execute(
233233
"DROP TABLE qiita.{0}".format(table_name))
234234

235-
# Remove the rows from common_prep_info
235+
# Remove the rows from prep_template_samples
236236
conn_handler.execute(
237237
"DELETE FROM qiita.{0} where {1} = %s".format(cls._table,
238238
cls._id_column),

qiita_db/metadata_template/sample_template.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def metadata_headers():
8484
return [x[0] for x in
8585
conn_handler.execute_fetchall(
8686
"SELECT DISTINCT column_name FROM qiita.study_sample_columns "
87-
"UNION SELECT column_name FROM information_schema.columns "
88-
"WHERE table_name = 'required_sample_info' "
8987
"ORDER BY column_name")]
9088

9189
@classmethod

qiita_db/metadata_template/test/test_prep_template.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,9 @@ def test_add_common_creation_steps_to_queue(self):
627627
'emp_status varchar, experiment_design_description varchar, '
628628
'library_construction_protocol varchar, '
629629
'linkerprimersequence varchar, platform varchar, '
630-
'run_prefix varchar, str_column varchar)')
630+
'run_prefix varchar, str_column varchar, '
631+
'CONSTRAINT fk_prep_2 FOREIGN KEY (sample_id) REFERENCES '
632+
'qiita.study_sample (sample_id) ON UPDATE CASCADE)')
631633

632634
sql_insert_dynamic = (
633635
'INSERT INTO qiita.prep_2 '

qiita_db/metadata_template/test/test_sample_template.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,19 @@ class TestSampleTemplateReadOnly(BaseTestSampleTemplate):
574574
def setUp(self):
575575
self._set_up()
576576

577+
def test_metadata_headers(self):
578+
obs = SampleTemplate.metadata_headers()
579+
exp = {'physical_specimen_location', 'physical_specimen_remaining',
580+
'dna_extracted', 'sample_type', 'collection_timestamp',
581+
'host_subject_id', 'description', 'season_environment',
582+
'assigned_from_geo', 'texture', 'taxon_id', 'depth',
583+
'host_taxid', 'common_name', 'water_content_soil', 'elevation',
584+
'temp', 'tot_nitro', 'samp_salinity', 'altitude', 'env_biome',
585+
'country', 'ph', 'anonymized_name', 'tot_org_carb',
586+
'description_duplicate', 'env_feature', 'latitude', 'longitude',
587+
'sample_id'}
588+
self.assertEqual(set(obs), exp)
589+
577590
def test_study_id(self):
578591
"""Ensure that the correct study ID is returned"""
579592
self.assertEqual(self.tester.study_id, 1)
@@ -795,12 +808,14 @@ def test_add_common_creation_steps_to_queue(self):
795808

796809
sql_crate_table = (
797810
'CREATE TABLE qiita.sample_2 (sample_id varchar NOT NULL, '
798-
'collection_timestamp timestamp, ''description varchar, '
811+
'collection_timestamp timestamp, description varchar, '
799812
'dna_extracted bool, host_subject_id varchar, int_column integer, '
800813
'latitude float8, longitude float8, '
801814
'physical_specimen_location varchar, '
802815
'physical_specimen_remaining bool, sample_type varchar, '
803-
'str_column varchar)')
816+
'str_column varchar, '
817+
'CONSTRAINT fk_sample_2 FOREIGN KEY (sample_id) REFERENCES '
818+
'qiita.study_sample (sample_id) ON UPDATE CASCADE)')
804819

805820
sql_insert_dynamic = (
806821
'INSERT INTO qiita.sample_2 '
@@ -1111,7 +1126,7 @@ def test_create_int_prefix(self):
11111126
# The returned object has the correct id
11121127
self.assertEqual(st.id, new_id)
11131128

1114-
# The relevant rows to required_sample_info have been added.
1129+
# The relevant rows to study_sample have been added.
11151130
obs = self.conn_handler.execute_fetchall(
11161131
"SELECT * FROM qiita.study_sample WHERE study_id=%s", (new_id,))
11171132
exp = [["%s.12.Sample1" % new_id, new_id],

qiita_db/metadata_template/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def prefix_sample_names_with_id(md_template, study_id):
113113
# Create a new column on the metadata template that includes the
114114
# metadata template indexes prefixed with the study id
115115
md_template['sample_name_with_id'] = (study_ids + '.' +
116-
md_template.index)
116+
md_template.index.values)
117117
md_template.index = md_template.sample_name_with_id
118118
del md_template['sample_name_with_id']
119119
# The original metadata template had the index column unnamed - remove
@@ -221,13 +221,13 @@ def load_template_to_dataframe(fn, strip_whitespace=True):
221221
parse_dates=True, index_col=False, comment='\t',
222222
mangle_dupe_cols=False, converters={
223223
'sample_name': lambda x: str(x).strip(),
224-
# required_sample_info
224+
# required sample template information
225225
'physical_location': str,
226226
'sample_type': str,
227227
# collection_timestamp is not added here
228228
'host_subject_id': str,
229229
'description': str,
230-
# common_prep_info
230+
# common prep template information
231231
'center_name': str,
232232
'center_projct_name': str})
233233

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
1-
-- May 6, 2015
2-
-- We attach the prep template directly to the study. The raw data is no longer
3-
-- attached to the study directly, the prep template point to them. This will
4-
-- make the RawData to be effectively just a container for the raw files,
5-
-- which is how it was acting previously.
6-
7-
CREATE TABLE qiita.study_prep_template (
8-
study_id bigint NOT NULL,
9-
prep_template_id bigint NOT NULL,
10-
CONSTRAINT idx_study_raw_data PRIMARY KEY ( study_id, prep_template_id )
11-
) ;
12-
13-
CREATE INDEX idx_study_prep_template_0 ON qiita.study_prep_template ( study_id ) ;
14-
15-
CREATE INDEX idx_study_prep_template_1 ON qiita.study_prep_template ( prep_template_id ) ;
16-
17-
COMMENT ON TABLE qiita.study_prep_template IS 'links study to its prep templates';
18-
19-
ALTER TABLE qiita.study_prep_template ADD CONSTRAINT fk_study_prep_template_study FOREIGN KEY ( study_id ) REFERENCES qiita.study( study_id ) ;
20-
21-
ALTER TABLE qiita.study_prep_template ADD CONSTRAINT fk_study_prep_template_pt FOREIGN KEY ( prep_template_id ) REFERENCES qiita.prep_template( prep_template_id ) ;
22-
23-
-- Connect the existing prep templates in the system with their studies
24-
WITH pt_st AS (SELECT prep_template_id, study_id
25-
FROM qiita.prep_template
26-
JOIN qiita.study_raw_data USING (raw_data_id))
27-
28-
29-
DROP TABLE qiita.study_raw_data;
30-
1+
-- May 7, 2015
2+
-- This patch adds the ON UPDATE CASCADE constraint to all the FK
3+
-- that are referencing the sample ids
4+
5+
DO $do$
6+
DECLARE
7+
dyn_t varchar;
8+
fk_vals RECORD;
9+
BEGIN
10+
11+
-- The dynamic tables do not have a FK set on their sample ID
12+
-- We need to find the dynamic tables existing in the system and we add the
13+
-- FK constraint to them.
14+
FOR dyn_t IN
15+
SELECT DISTINCT table_name
16+
FROM information_schema.columns
17+
WHERE (SUBSTR(table_name, 1, 7) = 'sample_'
18+
OR SUBSTR(table_name, 1, 5) = 'prep_')
19+
AND table_schema = 'qiita'
20+
AND table_name NOT IN ('prep_template',
21+
'prep_template_preprocessed_data',
22+
'prep_template_filepath',
23+
'prep_columns',
24+
'sample_template_filepath',
25+
'prep_template_sample')
26+
LOOP
27+
EXECUTE 'ALTER TABLE qiita.' || dyn_t || '
28+
ADD CONSTRAINT fk_' || dyn_t || '
29+
FOREIGN KEY (sample_id)'
30+
'REFERENCES qiita.study_sample (sample_id);';
31+
END LOOP;
32+
33+
-- Search for all the tables that are pointing to the sample_id
34+
-- and add the FK constraint with ON UPDATE CASCADE
35+
FOR fk_vals IN
36+
SELECT r.table_name, r.column_name, fk.constraint_name
37+
FROM information_schema.constraint_column_usage u
38+
INNER JOIN information_schema.referential_constraints fk
39+
ON u.constraint_catalog = fk.unique_constraint_catalog
40+
AND u.constraint_schema = fk.unique_constraint_schema
41+
AND u.constraint_name = fk.unique_constraint_name
42+
INNER JOIN information_schema.key_column_usage r
43+
ON r.constraint_catalog = fk.constraint_catalog
44+
AND r.constraint_schema = fk.constraint_schema
45+
AND r.constraint_name = fk.constraint_name
46+
WHERE u.column_name = 'sample_id'
47+
AND u.table_schema = 'qiita'
48+
AND u.table_name = 'study_sample'
49+
LOOP
50+
EXECUTE 'ALTER TABLE qiita.' || fk_vals.table_name || '
51+
DROP CONSTRAINT ' || fk_vals.constraint_name || ',
52+
ADD CONSTRAINT ' || fk_vals.constraint_name ||'
53+
FOREIGN KEY (' || fk_vals.column_name ||')
54+
REFERENCES qiita.study_sample( sample_id )
55+
ON UPDATE CASCADE;';
56+
END LOOP;
57+
END $do$;

qiita_db/support_files/populate_test_db.sql

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,26 @@ INSERT INTO qiita.study_sample (study_id, sample_id) VALUES
107107
-- Add the study sample columns for study 1
108108
INSERT INTO qiita.study_sample_columns (study_id, column_name, column_type) VALUES
109109
(1, 'sample_id', 'varchar'),
110-
(1, 'SEASON_ENVIRONMENT', 'varchar'),
111-
(1, 'ASSIGNED_FROM_GEO', 'varchar'),
112-
(1, 'TEXTURE', 'varchar'),
113-
(1, 'TAXON_ID', 'varchar'),
114-
(1, 'DEPTH', 'float8'),
115-
(1, 'HOST_TAXID', 'varchar'),
116-
(1, 'COMMON_NAME', 'varchar'),
117-
(1, 'WATER_CONTENT_SOIL', 'float8'),
118-
(1, 'ELEVATION', 'float8'),
119-
(1, 'TEMP', 'float8'),
120-
(1, 'TOT_NITRO', 'float8'),
121-
(1, 'SAMP_SALINITY', 'float8'),
122-
(1, 'ALTITUDE', 'float8'),
123-
(1, 'ENV_BIOME', 'varchar'),
124-
(1, 'COUNTRY', 'varchar'),
125-
(1, 'PH', 'float8'),
126-
(1, 'ANONYMIZED_NAME', 'varchar'),
127-
(1, 'TOT_ORG_CARB', 'float8'),
128-
(1, 'Description_duplicate', 'varchar'),
129-
(1, 'ENV_FEATURE', 'varchar'),
110+
(1, 'season_environment', 'varchar'),
111+
(1, 'assigned_from_geo', 'varchar'),
112+
(1, 'texture', 'varchar'),
113+
(1, 'taxon_id', 'varchar'),
114+
(1, 'depth', 'float8'),
115+
(1, 'host_taxid', 'varchar'),
116+
(1, 'common_name', 'varchar'),
117+
(1, 'water_content_soil', 'float8'),
118+
(1, 'elevation', 'float8'),
119+
(1, 'temp', 'float8'),
120+
(1, 'tot_nitro', 'float8'),
121+
(1, 'samp_salinity', 'float8'),
122+
(1, 'altitude', 'float8'),
123+
(1, 'env_biome', 'varchar'),
124+
(1, 'country', 'varchar'),
125+
(1, 'ph', 'float8'),
126+
(1, 'anonymized_name', 'varchar'),
127+
(1, 'tot_org_carb', 'float8'),
128+
(1, 'description_duplicate', 'varchar'),
129+
(1, 'env_feature', 'varchar'),
130130
(1, 'physical_specimen_location', 'varchar'),
131131
(1, 'physical_specimen_remaining', 'bool'),
132132
(1, 'dna_extracted', 'bool'),
@@ -140,26 +140,26 @@ INSERT INTO qiita.study_sample_columns (study_id, column_name, column_type) VALU
140140
-- Crate the sample_1 dynamic table
141141
CREATE TABLE qiita.sample_1 (
142142
sample_id varchar,
143-
SEASON_ENVIRONMENT varchar,
144-
ASSIGNED_FROM_GEO varchar,
145-
TEXTURE varchar,
146-
TAXON_ID varchar,
147-
DEPTH float8,
148-
HOST_TAXID varchar,
149-
COMMON_NAME varchar,
150-
WATER_CONTENT_SOIL float8,
151-
ELEVATION float8,
152-
TEMP float8,
153-
TOT_NITRO float8,
154-
SAMP_SALINITY float8,
155-
ALTITUDE float8,
156-
ENV_BIOME varchar,
157-
COUNTRY varchar,
158-
PH float8,
159-
ANONYMIZED_NAME varchar,
160-
TOT_ORG_CARB float8,
161-
Description_duplicate varchar,
162-
ENV_FEATURE varchar,
143+
season_environment varchar,
144+
assigned_from_geo varchar,
145+
texture varchar,
146+
taxon_id varchar,
147+
depth float8,
148+
host_taxid varchar,
149+
common_name varchar,
150+
water_content_soil float8,
151+
elevation float8,
152+
temp float8,
153+
tot_nitro float8,
154+
samp_salinity float8,
155+
altitude float8,
156+
env_biome varchar,
157+
country varchar,
158+
ph float8,
159+
anonymized_name varchar,
160+
tot_org_carb float8,
161+
description_duplicate varchar,
162+
env_feature varchar,
163163
physical_specimen_location varchar,
164164
physical_specimen_remaining bool,
165165
dna_extracted bool,
@@ -173,7 +173,7 @@ CREATE TABLE qiita.sample_1 (
173173
);
174174

175175
-- Populates the sample_1 dynamic table
176-
INSERT INTO qiita.sample_1 (sample_id, SEASON_ENVIRONMENT, ASSIGNED_FROM_GEO, TEXTURE, TAXON_ID, DEPTH, HOST_TAXID, COMMON_NAME, WATER_CONTENT_SOIL, ELEVATION, TEMP, TOT_NITRO, SAMP_SALINITY, ALTITUDE, ENV_BIOME, COUNTRY, PH, ANONYMIZED_NAME, TOT_ORG_CARB, Description_duplicate, ENV_FEATURE, physical_specimen_location, physical_specimen_remaining, dna_extracted, sample_type, collection_timestamp, host_subject_id, description, latitude, longitude) VALUES
176+
INSERT INTO qiita.sample_1 (sample_id, season_environment, assigned_from_geo, texture, taxon_id, depth, host_taxid, common_name, water_content_soil, elevation, temp, tot_nitro, samp_salinity, altitude, env_biome, country, ph, anonymized_name, tot_org_carb, description_duplicate, env_feature, physical_specimen_location, physical_specimen_remaining, dna_extracted, sample_type, collection_timestamp, host_subject_id, description, latitude, longitude) VALUES
177177
('1.SKM7.640188', 'winter', 'n', '63.1 sand, 17.7 silt, 19.2 clay', '1118232', 0.15, '3483', 'root metagenome', 0.101, 114, 15, 1.3, 7.44, 0, 'ENVO:Temperate grasslands, savannas, and shrubland biome', 'GAZ:United States of America', 6.82, 'SKM7', 3.31, 'Bucu Roots', 'ENVO:plant-associated habitat', 'ANL', TRUE, TRUE, 'ENVO:soil', '2011-11-11 13:00', '1001:B6', 'Cannabis Soil Microbiome', 60.1102854322, 74.7123248382),
178178
('1.SKD9.640182', 'winter', 'n', '66 sand, 16.3 silt, 17.7 clay', '1118232', 0.15, '3483', 'root metagenome', 0.178, 114, 15, 1.51, 7.1, 0, 'ENVO:Temperate grasslands, savannas, and shrubland biome', 'GAZ:United States of America', 6.82, 'SKD9', 4.32, 'Diesel Root', 'ENVO:plant-associated habitat', 'ANL', TRUE, TRUE, 'ENVO:soil', '2011-11-11 13:00', '1001:D3', 'Cannabis Soil Microbiome', 23.1218032799, 42.838497795),
179179
('1.SKM8.640201', 'winter', 'n', '63.1 sand, 17.7 silt, 19.2 clay', '1118232', 0.15, '3483', 'root metagenome', 0.101, 114, 15, 1.3, 7.44, 0, 'ENVO:Temperate grasslands, savannas, and shrubland biome', 'GAZ:United States of America', 6.82, 'SKM8', 3.31, 'Bucu Roots', 'ENVO:plant-associated habitat', 'ANL', TRUE, TRUE, 'ENVO:soil', '2011-11-11 13:00', '1001:D8', 'Cannabis Soil Microbiome', 3.21190859967, 26.8138925876),

0 commit comments

Comments
 (0)