Skip to content

Commit fb65166

Browse files
committed
Merge pull request #116 from josenavas/STadder
Adds the sample template adder command
2 parents 7588f11 + fc105ee commit fb65166

File tree

5 files changed

+108
-5
lines changed

5 files changed

+108
-5
lines changed

qiita_db/commands.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2014--, The Qiita Development Team.
3+
#
4+
# Distributed under the terms of the BSD 3-clause License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# -----------------------------------------------------------------------------
8+
9+
import pandas as pd
110
from functools import partial
211
try:
312
# Python 2
@@ -10,6 +19,7 @@
1019
from .user import User
1120
from .util import get_filetypes, get_filepath_types
1221
from .data import RawData
22+
from .metadata_template import SampleTemplate
1323

1424

1525
def make_study_from_cmd(owner, title, info):
@@ -59,6 +69,21 @@ def make_study_from_cmd(owner, title, info):
5969
Study.create(User(owner), title, efo_ids, infodict)
6070

6171

72+
def sample_template_adder(sample_temp_path, study_id):
73+
r"""Adds a sample template to the database
74+
75+
Parameters
76+
----------
77+
sample_temp_path : str
78+
Path to the sample template file
79+
study_id : int
80+
The study id to wich the sample template belongs to
81+
"""
82+
sample_temp = pd.DataFrame.from_csv(sample_temp_path, sep='\t',
83+
infer_datetime_format=True)
84+
return SampleTemplate.create(sample_temp, Study(study_id))
85+
86+
6287
def load_raw_data_cmd(filepaths, filepath_types, filetype, study_ids):
6388
"""Add new raw data by populating the relevant tables
6489

qiita_db/metadata_template.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919
MetadataTemplate
2020
SampleTemplate
2121
PrepTemplate
22+
23+
Methods
24+
-------
25+
26+
..autosummary::
27+
:toctree: generated/
28+
29+
sample_template_adder
2230
"""
2331

2432
# -----------------------------------------------------------------------------

qiita_db/test/test_commands.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2014--, The Qiita Development Team.
3+
#
4+
# Distributed under the terms of the BSD 3-clause License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# -----------------------------------------------------------------------------
8+
19
from os import remove, close
2-
from os.path import exists, abspath, join, basename
10+
from os.path import exists, join, basename
311
from tempfile import mkstemp
412
from unittest import TestCase, main
513
from future.utils.six import StringIO
@@ -10,8 +18,9 @@
1018
# Python 3
1119
from configparser import NoOptionError
1220

13-
from qiita_db.commands import make_study_from_cmd, load_raw_data_cmd
14-
from qiita_db.study import StudyPerson
21+
from qiita_db.commands import (make_study_from_cmd, load_raw_data_cmd,
22+
sample_template_adder)
23+
from qiita_db.study import Study, StudyPerson
1524
from qiita_db.user import User
1625
from qiita_db.util import get_count, check_count, get_db_files_base_dir
1726
from qiita_core.util import qiita_test_checker
@@ -40,6 +49,37 @@ def test_make_study_from_cmd(self):
4049
make_study_from_cmd('test@test.com', 'newstudy2', fh2)
4150

4251

52+
@qiita_test_checker()
53+
class SampleTemplateAdderTests(TestCase):
54+
def setUp(self):
55+
# Create a sample template file
56+
self.st_contents = SAMPLE_TEMPLATE
57+
58+
# create a new study to attach the sample template
59+
info = {
60+
"timeseries_type_id": 1,
61+
"metadata_complete": True,
62+
"mixs_compliant": True,
63+
"number_samples_collected": 4,
64+
"number_samples_promised": 4,
65+
"portal_type_id": 3,
66+
"study_alias": "TestStudy",
67+
"study_description": "Description of a test study",
68+
"study_abstract": "No abstract right now...",
69+
"emp_person_id": StudyPerson(2),
70+
"principal_investigator_id": StudyPerson(3),
71+
"lab_person_id": StudyPerson(1)
72+
}
73+
self.study = Study.create(User('test@foo.bar'),
74+
"Test study", [1], info)
75+
76+
def test_sample_template_adder(self):
77+
"""Correctly adds a sample template to the DB"""
78+
fh = StringIO(self.st_contents)
79+
st = sample_template_adder(fh, self.study.id)
80+
self.assertEqual(st.id, self.study.id)
81+
82+
4383
@qiita_test_checker()
4484
class TestLoadRawDataFromCmd(TestCase):
4585
def setUp(self):
@@ -79,7 +119,6 @@ def test_load_data_from_cmd(self):
79119
initial_raw_count = get_count('qiita.raw_data')
80120
initial_fp_count = get_count('qiita.filepath')
81121
initial_raw_fp_count = get_count('qiita.raw_filepath')
82-
initial_study_raw_data_count = get_count('qiita.study_raw_data')
83122

84123
new = load_raw_data_cmd(filepaths, filepath_types, filetype,
85124
study_ids)
@@ -146,5 +185,18 @@ def test_load_data_from_cmd(self):
146185
vamps_id = vamps_id
147186
"""
148187

188+
SAMPLE_TEMPLATE = (
189+
"#SampleID\trequired_sample_info_status_id\tcollection_timestamp\t"
190+
"sample_type\thas_physical_specimen\tphysical_location\thas_extracted_data"
191+
"\thost_subject_id\tTreatment\tDOB\tDescription\n"
192+
"PC.354\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.354"
193+
"\tControl\t20061218\tControl_mouse_I.D._354\n"
194+
"PC.593\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.593"
195+
"\tControl\t20071210\tControl_mouse_I.D._593\n"
196+
"PC.607\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.607"
197+
"\tFast\t20071112\tFasting_mouse_I.D._607\n"
198+
"PC.636\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.636"
199+
"\tFast\t20080116\tFasting_mouse_I.D._636")
200+
149201
if __name__ == "__main__":
150202
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#SampleID required_sample_info_status_id collection_timestamp sample_type has_physical_specimen physical_location has_extracted_data host_subject_id Treatment DOB Description
2+
PC.354 1 2014-06-18 16:44 type_1 True Location_1 True HS_ID_PC.354 Control 20061218 Control_mouse_I.D._354
3+
PC.593 1 2014-06-18 16:44 type_1 True Location_1 True HS_ID_PC.593 Control 20071210 Control_mouse_I.D._593
4+
PC.607 1 2014-06-18 16:44 type_1 True Location_1 True HS_ID_PC.607 Fast 20071112 Fasting_mouse_I.D._607
5+
PC.636 1 2014-06-18 16:44 type_1 True Location_1 True HS_ID_PC.636 Fast 20080116 Fasting_mouse_I.D._636

scripts/qiita_db

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import click
1212

1313
from qiita_db.util import get_filetypes, get_filepath_types
14-
from qiita_db.commands import make_study_from_cmd, load_raw_data_cmd
14+
from qiita_db.commands import (sample_template_adder, make_study_from_cmd,
15+
load_raw_data_cmd)
1516

1617

1718
@click.group()
@@ -48,5 +49,17 @@ def insert_study_to_db(owner, title, info):
4849
make_study_from_cmd(owner, title, info)
4950

5051

52+
@qiita_db.command()
53+
@click.option('--fp', required=True,
54+
type=click.Path(resolve_path=True, readable=True, exists=True),
55+
help='Path to the tab delimited file holding the sample '
56+
'template information')
57+
@click.option('--study', required=True, type=int,
58+
help='Associate the sample template with this study')
59+
def add_sample_template(fp, study):
60+
"""Adds a sample template to the database"""
61+
sample_template_adder(fp, study)
62+
63+
5164
if __name__ == '__main__':
5265
qiita_db()

0 commit comments

Comments
 (0)