def test_filename(db): session = Session(db, from_id='testid') assert session.filename is None session.filename = 'test' assert session.filename == 'test' session = Session(db, from_file='othertest') assert session.filename == 'othertest'
def test_coordinates(db): session = Session(db, from_id='testid') assert session.from_coord == 0 assert session.to_coord == 0 session.from_coord = 23 session.to_coord = 42 assert session.from_coord == 23 assert session.to_coord == 42
def test_region(db): session = Session(db, from_id='testid') assert session.region == {} region = { 'from': 23, 'to': 42, } session.region = region assert session.region == region
def test_last_changed(db, fake_session): session_id = 12345678 db_session = fake_session(session_id) session = Session(db, session_id=session_id) assert session.last_changed == db_session['last_changed'] now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') session.state = 'done' assert session.last_changed == now
def test_last_changed_datetime(db, fake_session): session_id = 12345678 db_session = fake_session(session_id) session = Session(db, session_id=session_id) now = datetime.strptime(db_session['last_changed'], '%Y-%m-%d %H:%M:%S') assert session.last_changed_datetime == now
def test_next(db): queue = Queue(db, 'test') session = Session(db, from_id='test123') queue.submit(session) key = queue.next() assert key == session._session_key key = queue.next() assert key is None
def test_genome(db): session = Session(db, from_id='testid') assert session.genome == {} genome = { 'locus': 'NC_003888', 'definition': 'Streptomyces coelicolor A3(2) chromosome, complete genome.', 'source': 'Streptomyces coelicolor A3(2)', 'orfs': [ { 'id': 'SCO4711', 'start': 5132708, 'end': 5132995, 'strand': 1, 'gene': 'rpsQ' }, ], 'clusters': [ { 'start': 235986, 'end': 271084, 'name': "Cluster 3", 'type': "Lantipeptide", 'description': "Sanglifehrin_A_biosynthetic_gene_cluster (4% of genes show similarity)" }, ], } session.genome = genome assert session.genome == genome
def test_init_from_id(db, fake_session): session_id = 123456789 db_session = fake_session(session_id) session = Session(db, session_id=session_id) assert session.asid == db_session['asid'] assert session.filename is None assert session.state == db_session['state'] assert session.error == db_session['error'] assert session.added == db_session['added'] assert session.last_changed == db_session['last_changed'] assert session.genome == {} assert session.region == {} assert session.derived is False assert session.pam == 'GG' assert session.uniq_size == 13 assert session.full_size == 23
def test_init(db): now = datetime.utcnow() session = Session(db, from_id='testid') session_key = 'crispy:session:{0:039}'.format(session._session_id) assert session._session_key == session_key db_session = db.hgetall(session_key) assert db_session['state'] == 'pending' assert db_session['asid'] == 'testid' assert db_session['filename'] == 'None' assert db_session['added'] == now.strftime('%Y-%m-%d %H:%M:%S') assert db_session['genome'] == '{}' assert db_session['region'] == '{}' assert db_session['from'] == '0' assert db_session['to'] == '0' assert db_session['derived'] == 'false' assert db_session['pam'] == 'GG' assert db_session['uniq_size'] == '13' assert db_session['full_size'] == '23'
def test_full_size(db): session = Session(db, from_id='testid') assert session.full_size == 23 session.full_size = 17 assert session.full_size == 17
def test_uniq_size(db): session = Session(db, from_id='testid') assert session.uniq_size == 13 session.uniq_size = 17 assert session.uniq_size == 17
def test_asid(db): session = Session(db, from_id='testid') assert session.asid == 'testid' session.asid = 'otherid' assert session.asid == 'otherid'
def test_state(db): session = Session(db, from_id='testid') assert session.state == 'pending' session.state = 'done' assert session.state == 'done'
def test_init_without_id_or_file(db): with pytest.raises(ValueError): session = Session(db)
def test_error(db): session = Session(db, from_id='testid') assert session.error == '' session.error = 'something bad happened' assert session.error == 'something bad happened'
def test_init_from_invalid_id(db): with pytest.raises(ValueError): session = Session(db, session_id=172342)
def test_derived(db): session = Session(db, from_id='testid') assert session.derived is False session.derived = True assert session.derived is True
def test_init_with_bot_id_and_file(db): with pytest.raises(ValueError): session = Session(db, from_id=12345, from_file='test')
def test_derived_raises(db): session = Session(db, from_id='testid') with pytest.raises(ValueError): session.derived = 5
def test_submit(db): queue = Queue(db, 'test') assert queue.length == 0 session = Session(db, from_id='test123') queue.submit(session) assert queue.length == 1
def test_pam(db): session = Session(db, from_id='testid') assert session.pam == 'GG' session.pam = 'TT' assert session.pam == 'TT'
def test_repeatability(db): first = Queue(db, 'test') second = Queue(db, 'test') session = Session(db, from_id='test123') first.submit(session) assert second.length == 1
def main(): parser = argparse.ArgumentParser( description='CRISPy sequence preparation service') parser.add_argument('-d', '--debug', dest='debug', action='store_true', default=False, help='print diagnostic messages while running') parser.add_argument('-q', '--queue', dest='queue', default='redis://127.0.0.1:6379/0', help='URI of the Queue server') parser.add_argument( '-u', '--upload-dir', dest='upload_dir', default='../uploads', help='Path to directory where the session files are stored') parser.add_argument('-V', '--version', action="version", version=version.__version__) args = parser.parse_args() if args.debug: logging.basicConfig(format='%(levelname)s %(asctime)s: %(message)s', level=logging.INFO) UPLOAD_PATH = path.abspath(args.upload_dir) db = redis.Redis.from_url(args.queue, decode_responses=True) queue = Queue(db, 'prepare') while True: job_key = queue.next() if job_key is None: time.sleep(1) continue logging.info('processing {}'.format(job_key)) job_id = int(job_key.split(':')[-1]) job = Session(db, session_id=job_id) dirname = path.join(UPLOAD_PATH, str(job_id)) if job.asid != 'None': logging.info('grabbing file for id {}'.format(job.asid)) try: json_rec, filename = fetch(job.asid, dirname) job.filename = filename except requests.HTTPError: job.state = 'error' job.error = 'Downloading sequence file failed. Is the ID spelled correctly?' continue except Exception: job.state = 'error' job.error = 'Failed to load the sequence' continue else: logging.info('parsing uploaded file {}'.format(job.filename)) full_path = path.join(dirname, job.filename) if job.filename.endswith('.gbk') or job.filename.endswith('.gb'): try: rec = list(SeqIO.parse(full_path, 'genbank'))[0] except (IndexError, ValueError, AssertionError, AttributeError, UnicodeDecodeError): job.state = 'error' job.error = 'Parsing the uploaded file failed. Please check it is a valid GenBank file' continue try: json_rec = genome_json(rec) except (TypeError): job.state = 'error' job.error = 'Invalid input file. Please check the file contains a nucleotide sequence.' continue elif job.filename.endswith('.json'): with open(full_path, 'r') as handle: as_json = json.load(handle) json_rec = convert_json(as_json) gbk_record = json_to_gbk(as_json) job.filename = 'input.gbk' SeqIO.write([gbk_record], path.join(dirname, job.filename), 'genbank') else: job.state = 'error' job.error = 'Invalid input file' continue job.genome = json_rec job.state = 'loaded' logging.info('done with {}'.format(job_key))