Skip to content

Commit

Permalink
Merge a674a8d into 28ad6d8
Browse files Browse the repository at this point in the history
  • Loading branch information
josenavas committed Jun 19, 2014
2 parents 28ad6d8 + a674a8d commit 7ab38c4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 3 deletions.
25 changes: 25 additions & 0 deletions qiita_db/commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

import pandas as pd
from functools import partial
try:
# Python 2
Expand All @@ -8,6 +17,7 @@

from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_db.metadata_template import SampleTemplate


def make_study_from_cmd(owner, title, info):
Expand Down Expand Up @@ -55,3 +65,18 @@ def make_study_from_cmd(owner, title, info):
efo_ids = [x.strip() for x in efo_ids.split(',')]

Study.create(User(owner), title, efo_ids, infodict)


def sample_template_adder(sample_temp_path, study_id):
r"""Adds a sample template to the database
Parameters
----------
sample_temp_path : str
Path to the sample template file
study_id : int
The study id to wich the sample template belongs to
"""
sample_temp = pd.DataFrame.from_csv(sample_temp_path, sep='\t',
infer_datetime_format=True)
return SampleTemplate.create(sample_temp, Study(study_id))
8 changes: 8 additions & 0 deletions qiita_db/metadata_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
MetadataTemplate
SampleTemplate
PrepTemplate
Methods
-------
..autosummary::
:toctree: generated/
sample_template_adder
"""

# -----------------------------------------------------------------------------
Expand Down
57 changes: 55 additions & 2 deletions qiita_db/test/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from unittest import TestCase, main
from future.utils.six import StringIO
try:
Expand All @@ -7,10 +15,10 @@
# Python 3
from configparser import NoOptionError

from qiita_db.commands import make_study_from_cmd
from qiita_db.study import StudyPerson
from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_core.util import qiita_test_checker
from qiita_db.commands import make_study_from_cmd, sample_template_adder


@qiita_test_checker()
Expand All @@ -35,6 +43,38 @@ def test_make_study_from_cmd(self):
with self.assertRaises(NoOptionError):
make_study_from_cmd('test@test.com', 'newstudy2', fh2)


@qiita_test_checker()
class SampleTemplateAdderTests(TestCase):
def setUp(self):
# Create a sample template file
self.st_contents = SAMPLE_TEMPLATE

# create a new study to attach the sample template
info = {
"timeseries_type_id": 1,
"metadata_complete": True,
"mixs_compliant": True,
"number_samples_collected": 4,
"number_samples_promised": 4,
"portal_type_id": 3,
"study_alias": "TestStudy",
"study_description": "Description of a test study",
"study_abstract": "No abstract right now...",
"emp_person_id": StudyPerson(2),
"principal_investigator_id": StudyPerson(3),
"lab_person_id": StudyPerson(1)
}
self.study = Study.create(User('test@foo.bar'),
"Test study", [1], info)

def test_sample_template_adder(self):
"""Correctly adds a sample template to the DB"""
fh = StringIO(self.st_contents)
st = sample_template_adder(fh, self.study.id)
self.assertEqual(st.id, self.study.id)


CONFIG_1 = """[required]
timeseries_type_id = 1
metadata_complete = True
Expand Down Expand Up @@ -72,5 +112,18 @@ def test_make_study_from_cmd(self):
vamps_id = vamps_id
"""

SAMPLE_TEMPLATE = (
"#SampleID\trequired_sample_info_status_id\tcollection_timestamp\t"
"sample_type\thas_physical_specimen\tphysical_location\thas_extracted_data"
"\thost_subject_id\tTreatment\tDOB\tDescription\n"
"PC.354\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.354"
"\tControl\t20061218\tControl_mouse_I.D._354\n"
"PC.593\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.593"
"\tControl\t20071210\tControl_mouse_I.D._593\n"
"PC.607\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.607"
"\tFast\t20071112\tFasting_mouse_I.D._607\n"
"PC.636\t1\t2014-06-18 16:44\ttype_1\tTrue\tLocation_1\tTrue\tHS_ID_PC.636"
"\tFast\t20080116\tFasting_mouse_I.D._636")

if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions qiita_db/test/test_data/sample_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#SampleID required_sample_info_status_id collection_timestamp sample_type has_physical_specimen physical_location has_extracted_data host_subject_id Treatment DOB Description
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
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
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
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
9 changes: 8 additions & 1 deletion scripts/qiita_db
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from qiita_db.environment_manager import (make_test_environment,
drop_demo_environment,
DFLT_BASE_DATA_FOLDER,
DFLT_BASE_WORK_FOLDER)
from qiita_db.commands import make_study_from_cmd
from qiita_db.commands import sample_template_adder, make_study_from_cmd


@click.group()
Expand Down Expand Up @@ -119,6 +119,13 @@ def drop_demo_env(user, host):
"""Drops the demo database environment."""
drop_demo_environment(user, None, host)


@click.command()
def add_sample_template():
"""Adds a sample template to the database"""
sample_template_adder()


qiita_db.add_command(make_test_env)
qiita_db.add_command(clean_test_env)
qiita_db.add_command(drop_test_env)
Expand Down

0 comments on commit 7ab38c4

Please sign in to comment.