Skip to content

Commit

Permalink
Merge 28b8b63 into 0c5a7e4
Browse files Browse the repository at this point in the history
  • Loading branch information
teravest committed Jun 18, 2014
2 parents 0c5a7e4 + 28b8b63 commit eb068db
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 16 deletions.
58 changes: 58 additions & 0 deletions qiita_db/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from functools import partial
try:
# Python 2
from ConfigParser import ConfigParser
except ImportError:
# Python 3
from configparser import ConfigParser

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


def make_study_from_cmd(owner, title, info):

# Parse the configuration file
config = ConfigParser()
config.readfp(info)

optional = dict(config.items('optional'))
get_optional = lambda name: optional.get(name, None)
get_required = partial(config.get, 'required')
infodict = {}
infodict['funding'] = get_optional('funding')
infodict['timeseries_type_id'] = get_required('timeseries_type_id')
infodict['metadata_complete'] = get_required('metadata_complete')
infodict['mixs_compliant'] = get_required('mixs_compliant')
infodict['most_recent_contact'] = get_optional('most_recent_contact')
infodict['number_samples_collected'] = get_required(
'number_samples_collected')
infodict['number_samples_promised'] = get_required(
'number_samples_promised')
infodict['portal_type_id'] = get_required('portal_type_id')
infodict['reprocess'] = get_required('reprocess')
infodict['spatial_series'] = get_optional('spatial_series')
infodict['study_alias'] = get_required('study_alias')
infodict['study_description'] = get_required('study_description')
infodict['study_abstract'] = get_required('study_abstract')
infodict['vamps_id'] = get_optional('vamps_id')
emp_person_name_email = get_optional('emp_person_name')
if emp_person_name_email is not None:
emp_name, emp_email = emp_person_name_email.split(',')
infodict['emp_person_id'] = StudyPerson.create(emp_name.strip(),
emp_email.strip())
lab_name_email = get_optional('lab_person')
if lab_name_email is not None:
lab_name, lab_email = lab_name_email.split(',')
infodict['lab_person_id'] = StudyPerson.create(lab_name.strip(),
lab_email.strip())
pi_name_email = get_required('principal_investigator')
pi_name, pi_email = pi_name_email.split(',')
infodict['principal_investigator_id'] = StudyPerson.create(
pi_name.strip(), pi_email.strip())
# this will eventually change to using the Experimental Factory Ontolgoy
# names
efo_ids = get_required('efo_ids')
efo_ids = [x.strip() for x in efo_ids.split(',')]

Study.create(User(owner), title, efo_ids, infodict)
23 changes: 11 additions & 12 deletions qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,22 +551,21 @@ def create(cls, name, email, address=None, phone=None):
-------
New StudyPerson object
Raises
------
QiitaDBDuplicateError
Person already exists
"""
if cls.exists(name, email):
raise QiitaDBDuplicateError(
"StudyPerson", "name: %s, email: %s" % (name, email))
sql = ("SELECT study_person_id from qiita.{0} WHERE name = %s and"
" email = %s".format(cls._table))
conn_handler = SQLConnectionHandler()
spid = conn_handler.execute_fetchone(sql, (name, email))

# Doesn't exist so insert new person
sql = ("INSERT INTO qiita.{0} (name, email, address, phone) VALUES"
" (%s, %s, %s, %s) RETURNING "
"study_person_id".format(cls._table))
conn_handler = SQLConnectionHandler()
spid = conn_handler.execute_fetchone(sql, (name, email, address,
phone))
else:
sql = ("INSERT INTO qiita.{0} (name, email, address, phone) VALUES"
" (%s, %s, %s, %s) RETURNING "
"study_person_id".format(cls._table))
conn_handler = SQLConnectionHandler()
spid = conn_handler.execute_fetchone(sql, (name, email, address,
phone))
return cls(spid[0])

# Properties
Expand Down
74 changes: 74 additions & 0 deletions qiita_db/test/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from unittest import TestCase, main
from StringIO import StringIO
try:
# Python 2
from ConfigParser import NoOptionError
except ImportError:
# Python 3
from configparser import NoOptionError

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


@qiita_test_checker()
class TestMakeStudyFromCmd(TestCase):
def setUp(self):
StudyPerson.create('SomeDude', 'somedude@foo.bar',
'111 fake street', '111-121-1313')
User.create('test@test.com', 'password')
self.config1 = """
[required]
timeseries_type_id = 1
metadata_complete = True
mixs_compliant = True
number_samples_collected = 50
number_samples_promised = 25
portal_type_id = 3
principal_investigator = SomeDude, somedude@foo.bar
reprocess = False
study_alias = 'test study'
study_description = 'test study description'
study_abstract = 'study abstract'
efo_ids = 1,2,3,4
[optional]
lab_person = SomeDude, somedude@foo.bar
funding = 'funding source'
vamps_id = vamps_id
"""
self.config2 = """
[required]
timeseries_type_id = 1
metadata_complete = True
number_samples_collected = 50
number_samples_promised = 25
portal_type_id = 3
principal_investigator = SomeDude, somedude@foo.bar
reprocess = False
study_alias = 'test study'
study_description = 'test study description'
study_abstract = 'study abstract'
efo_ids = 1,2,3,4
[optional]
lab_person = SomeDude, somedude@foo.bar
funding = 'funding source'
vamps_id = vamps_id
"""

def test_make_study_from_cmd(self):
fh = StringIO(self.config1)
make_study_from_cmd('test@test.com', 'newstudy', fh)
sql = ("select study_id from qiita.study where email = %s and "
"study_title = %s")
study_id = self.conn_handler.execute_fetchone(sql, ('test@test.com',
'newstudy'))
self.assertTrue(study_id is not None)

fh2 = StringIO(self.config2)
with self.assertRaises(NoOptionError):
make_study_from_cmd('test@test.com', 'newstudy2', fh2)

if __name__ == "__main__":
main()
8 changes: 4 additions & 4 deletions qiita_db/test/test_study.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from qiita_db.study import Study, StudyPerson
from qiita_db.investigation import Investigation
from qiita_db.user import User
from qiita_db.exceptions import (QiitaDBDuplicateError, QiitaDBColumnError,
QiitaDBStatusError)
from qiita_db.exceptions import QiitaDBColumnError, QiitaDBStatusError

# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
Expand All @@ -36,8 +35,9 @@ def test_create_studyperson(self):
'111 fake street', '111-121-1313']])

def test_create_studyperson_already_exists(self):
with self.assertRaises(QiitaDBDuplicateError):
StudyPerson.create('LabDude', 'lab_dude@foo.bar')
obs = StudyPerson.create('LabDude', 'lab_dude@foo.bar')
self.assertEqual(obs.name, 'LabDude')
self.assertEqual(obs.email, 'lab_dude@foo.bar')

def test_retrieve_name(self):
self.assertEqual(self.studyperson.name, 'LabDude')
Expand Down
12 changes: 12 additions & 0 deletions scripts/qiita_db
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +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


@click.group()
Expand Down Expand Up @@ -85,6 +86,16 @@ def make_production_env():
make_production_environment()


@click.command()
@click.option('--owner', help="The email address of the ower of the study")
@click.option('--title', help="The title of the study")
@click.option('--info', type=click.File(mode='r'),
help="filepath of file with study information in python"
"config file format")
def insert_study_to_db(owner, title, info):
make_study_from_cmd(owner, title, info)


@click.command()
@click.option('--base_data_folder', default=DFLT_BASE_DATA_FOLDER,
help="The folder where the test data files are stored")
Expand Down Expand Up @@ -114,6 +125,7 @@ qiita_db.add_command(drop_test_env)
qiita_db.add_command(make_production_env)
qiita_db.add_command(make_demo_env)
qiita_db.add_command(drop_demo_env)
qiita_db.add_command(insert_study_to_db)

if __name__ == '__main__':
qiita_db()

0 comments on commit eb068db

Please sign in to comment.