Skip to content

Commit

Permalink
Merge pull request #116 from josenavas/STadder
Browse files Browse the repository at this point in the history
Adds the sample template adder command
  • Loading branch information
ElDeveloper committed Jun 20, 2014
2 parents 7588f11 + fc105ee commit fb65166
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 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 @@ -10,6 +19,7 @@
from .user import User
from .util import get_filetypes, get_filepath_types
from .data import RawData
from .metadata_template import SampleTemplate


def make_study_from_cmd(owner, title, info):
Expand Down Expand Up @@ -59,6 +69,21 @@ def make_study_from_cmd(owner, title, info):
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))


def load_raw_data_cmd(filepaths, filepath_types, filetype, study_ids):
"""Add new raw data by populating the relevant tables
Expand Down
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
60 changes: 56 additions & 4 deletions qiita_db/test/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# -----------------------------------------------------------------------------
# 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 os import remove, close
from os.path import exists, abspath, join, basename
from os.path import exists, join, basename
from tempfile import mkstemp
from unittest import TestCase, main
from future.utils.six import StringIO
Expand All @@ -10,8 +18,9 @@
# Python 3
from configparser import NoOptionError

from qiita_db.commands import make_study_from_cmd, load_raw_data_cmd
from qiita_db.study import StudyPerson
from qiita_db.commands import (make_study_from_cmd, load_raw_data_cmd,
sample_template_adder)
from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_db.util import get_count, check_count, get_db_files_base_dir
from qiita_core.util import qiita_test_checker
Expand Down Expand Up @@ -40,6 +49,37 @@ def test_make_study_from_cmd(self):
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)


@qiita_test_checker()
class TestLoadRawDataFromCmd(TestCase):
def setUp(self):
Expand Down Expand Up @@ -79,7 +119,6 @@ def test_load_data_from_cmd(self):
initial_raw_count = get_count('qiita.raw_data')
initial_fp_count = get_count('qiita.filepath')
initial_raw_fp_count = get_count('qiita.raw_filepath')
initial_study_raw_data_count = get_count('qiita.study_raw_data')

new = load_raw_data_cmd(filepaths, filepath_types, filetype,
study_ids)
Expand Down Expand Up @@ -146,5 +185,18 @@ def test_load_data_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
15 changes: 14 additions & 1 deletion scripts/qiita_db
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import click

from qiita_db.util import get_filetypes, get_filepath_types
from qiita_db.commands import make_study_from_cmd, load_raw_data_cmd
from qiita_db.commands import (sample_template_adder, make_study_from_cmd,
load_raw_data_cmd)


@click.group()
Expand Down Expand Up @@ -48,5 +49,17 @@ def insert_study_to_db(owner, title, info):
make_study_from_cmd(owner, title, info)


@qiita_db.command()
@click.option('--fp', required=True,
type=click.Path(resolve_path=True, readable=True, exists=True),
help='Path to the tab delimited file holding the sample '
'template information')
@click.option('--study', required=True, type=int,
help='Associate the sample template with this study')
def add_sample_template(fp, study):
"""Adds a sample template to the database"""
sample_template_adder(fp, study)


if __name__ == '__main__':
qiita_db()

0 comments on commit fb65166

Please sign in to comment.