def test_getitem(self): file_name = (('ACBA.0917.00019', '.fna'), ('ESCO001.C.00001.C001', '.fst')) for seq_name, ext in file_name: replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) exp = read_multi_prot_fasta( self.find_data( os.path.join('Gembase', 'Proteins', seq_name + '.prt'))) specie, date, strain, contig = replicon.id.split('.') pattern = '{}\.{}\.{}\.\w?{}'.format(specie, date, strain, contig) for prot_expected in exp: if re.match(pattern, prot_expected.id): prot_received = db[prot_expected.id] self.assertEqual(prot_received.id, prot_expected.id) self.assertEqual(prot_received.seq, prot_expected.seq) with self.assertRaises(KeyError) as ctx: db['nimport_naoik'] self.assertEqual(str(ctx.exception), "'nimport_naoik'")
def test_make_protfile(self): file_name = (('ACBA.0917.00019', '.fna', 3870), ('ESCO001.C.00001.C001', '.fst', 3870)) for seq_name, ext, seq_nb in file_name: replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) for seq_nb, seqs in enumerate( zip( read_multi_prot_fasta( self.find_data( os.path.join('Gembase', 'Proteins', seq_name + '.prt'))), read_multi_prot_fasta(db.protfile)), 1): expected, test = seqs self.assertEqual(expected.id, test.id) self.assertEqual(seq_nb, seq_nb)
def test_getitem(self): file_name = (('ACBA.0917.00019', '.fna'), ('ESCO001.C.00001.C001', '.fst')) for seq_name, ext in file_name: replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) exp = read_multi_prot_fasta(self.find_data(os.path.join('Gembase', 'Proteins', seq_name + '.prt'))) specie, date, strain, contig = replicon.id.split('.') pattern = '{}\.{}\.{}\.\w?{}'.format(specie, date, strain, contig) for prot_expected in exp: if re.match(pattern, prot_expected.id): prot_received = db[prot_expected.id] self.assertEqual(prot_received.id, prot_expected.id) self.assertEqual(prot_received.seq, prot_expected.seq) with self.assertRaises(KeyError) as ctx: db['nimport_naoik'] self.assertEqual(str(ctx.exception), "'nimport_naoik'")
def test_iter(self): # test Gembase Draft seq_name = 'ACBA.0917.00019' ext = '.fna' replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path db = GembaseDB(replicon, cfg) idx = SeqIO.index(self.find_data( os.path.join('Gembase', 'Proteins', seq_name + '.prt')), 'fasta', alphabet=Seq.IUPAC.extended_protein) specie, date, strain, contig = replicon.id.split('.') pattern = '{}\.{}\.{}\.\w?{}'.format(specie, date, strain, contig) self.assertListEqual(sorted([i for i in idx if re.match(pattern, i)]), sorted([i for i in db])) # test Gembase Complet seq_name = 'ESCO001.C.00001.C001' ext = '.fst' replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path with self.catch_log(): db = GembaseDB(replicon, cfg) idx = SeqIO.index(self.find_data( os.path.join('Gembase', 'Proteins', seq_name + '.prt')), 'fasta', alphabet=Seq.IUPAC.extended_protein) specie, date, strain, contig = replicon.id.split('.') pattern = '{}\.{}\.{}\.\w?{}'.format(specie, date, strain, contig) seqid_from_gembase_protfile = set( [i for i in idx if re.match(pattern, i)]) seqid_from_if = set([i for i in db]) non_common_seq = seqid_from_gembase_protfile ^ seqid_from_if # in Gembase complete the annotation from lstinfo provided from genbank # it appear some times that some CDS are not translate in proteins # So in data I have 3 genes from LSTINFO are not in .prt file diff = { 'ESCO001.C.00001.C001_03974', 'ESCO001.C.00001.C001_01509', 'ESCO001.C.00001.C001_04162' } self.assertSetEqual(non_common_seq, diff)
def test_find_gembase_file_basename_file_not_in_gembase(self): """ test if find_gembase_file_basename get the the right basename for files not located in gembase and file name is the output of split operation a file containing one contig a file representing a chunk """ gembase_path = self.find_data('Gembase') file_names = { 'ACBA.0917.00019': self.find_data( os.path.join('Replicons', 'ACBA.0917.00019.0001.fst')), 'ESCO001.C.00001.C001.fst': os.path.join(self.tmp_dir, 'ESCO001.C.00001.C001_chunk_1.fst') } shutil.copyfile( os.path.join(gembase_path, 'Replicons', 'ESCO001.C.00001.C001.fst'), file_names['ESCO001.C.00001.C001.fst']) for base_file_name, replicon_path in file_names.items(): self.args.replicon = replicon_path self.args.gembase_path = gembase_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg, gembase_path=gembase_path) self.assertTrue( db._find_gembase_file_basename(gembase_path, replicon_path), base_file_name) replicon_path = self.find_data( os.path.join('Replicons', 'acba.007.p01.13.fst')) self.args.replicon = replicon_path self.args.gembase_path = gembase_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.assertRaises(FileNotFoundError) as ctx: with self.catch_log(): GembaseDB(replicon, cfg, gembase_path=gembase_path) self.assertEqual( str(ctx.exception), 'cannot find lst file matching {} sequence'.format(replicon_path))
def test_get_description(self): # SeqDesc(id, strand, strat, stop) file_name = {('ACBA.0917.00019', '.fna'): {'ACBA.0917.00019.b0001_00001': SeqDesc('ACBA.0917.00019.b0001_00001', -1, 266, 1480), 'ACBA.0917.00019.i0001_03957': SeqDesc('ACBA.0917.00019.i0001_03957', -1, 4043755, 4044354)}, } for seq_name, ext in file_name: replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = GembaseDB(replicon, cfg) descriptions = file_name[(seq_name, ext)] for seq_id, desc in descriptions.items(): self.assertEqual(desc, db.get_description(seq_id)) with self.assertRaises(IntegronError) as ctx: db.get_description('nimport_naoik') self.assertEqual(str(ctx.exception), "'nimport_naoik' is not a valid Gembase protein identifier.") with self.assertRaises(KeyError) as ctx: db.get_description('FOO.BAR.00019.i0001_03924') self.assertEqual(str(ctx.exception), "'FOO.BAR.00019.i0001_03924'")
def test_read_multi_prot_fasta(self): replicon_id = 'ACBA.007.P01_13' replicon_path = self.find_data(os.path.join('Proteins', replicon_id + '.prt')) replicon = utils.read_multi_prot_fasta(replicon_path) expected_seq_id = ['{}_{}'.format(replicon_id, i) for i in range(1, 24)] received_seq_id = [seq.id for seq in replicon] self.assertListEqual(expected_seq_id, received_seq_id)
def test_make_protfile_no_dir(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path db = ProdigalDB(replicon, cfg) for seq_nb, seqs in enumerate(zip( read_multi_prot_fasta(self.find_data(os.path.join('Proteins', prot_name))), read_multi_prot_fasta(db.protfile)), 1): expected, test = seqs self.assertEqual(expected.id, test.id) self.assertEqual(seq_nb, 23)
def test_iter(self): # test Gembase Draft seq_name = 'ACBA.0917.00019' ext = '.fna' replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path db = GembaseDB(replicon, cfg) idx = SeqIO.index(self.find_data(os.path.join('Gembase', 'Proteins', seq_name + '.prt')), 'fasta', alphabet=Seq.IUPAC.extended_protein) specie, date, strain, contig = replicon.id.split('.') pattern = '{}\.{}\.{}\.\w?{}'.format(specie, date, strain, contig) self.assertListEqual(sorted([i for i in idx if re.match(pattern, i)]), sorted([i for i in db])) # test Gembase Complet seq_name = 'ESCO001.C.00001.C001' ext = '.fst' replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path with self.catch_log(): db = GembaseDB(replicon, cfg) idx = SeqIO.index(self.find_data(os.path.join('Gembase', 'Proteins', seq_name + '.prt')), 'fasta', alphabet=Seq.IUPAC.extended_protein) specie, date, strain, contig = replicon.id.split('.') pattern = '{}\.{}\.{}\.\w?{}'.format(specie, date, strain, contig) seqid_from_gembase_protfile = set([i for i in idx if re.match(pattern, i)]) seqid_from_if = set([i for i in db]) non_common_seq = seqid_from_gembase_protfile ^ seqid_from_if # in Gembase complete the annotation from lstinfo provided from genbank # it appear some times that some CDS are not translate in proteins # So in data I have 3 genes from LSTINFO are not in .prt file diff = {'ESCO001.C.00001.C001_03974', 'ESCO001.C.00001.C001_01509', 'ESCO001.C.00001.C001_04162'} self.assertSetEqual(non_common_seq, diff)
def test_find_gembase_file_basename_file_not_in_gembase(self): """ test if find_gembase_file_basename get the the right basename for files not located in gembase and file name is the output of split operation a file containing one contig a file representing a chunk """ gembase_path = self.find_data('Gembase') file_names = {'ACBA.0917.00019': self.find_data(os.path.join('Replicons', 'ACBA.0917.00019.0001.fst')), 'ESCO001.C.00001.C001.fst': os.path.join(self.tmp_dir, 'ESCO001.C.00001.C001_chunk_1.fst') } shutil.copyfile(os.path.join(gembase_path, 'Replicons', 'ESCO001.C.00001.C001.fst'), file_names['ESCO001.C.00001.C001.fst']) for base_file_name, replicon_path in file_names.items(): self.args.replicon = replicon_path self.args.gembase_path = gembase_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg, gembase_path=gembase_path) self.assertTrue(db._find_gembase_file_basename(gembase_path, replicon_path), base_file_name) replicon_path = self.find_data(os.path.join('Replicons', 'acba.007.p01.13.fst')) self.args.replicon = replicon_path self.args.gembase_path = gembase_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.assertRaises(FileNotFoundError) as ctx: with self.catch_log(): GembaseDB(replicon, cfg, gembase_path=gembase_path) self.assertEqual(str(ctx.exception), 'cannot find lst file matching {} sequence'.format(replicon_path))
def test_read_hmm_evalue(self): """ Test that the hmm hits are well read, and returned only if evalue is < to the given threshold. """ rep_name = "acba.007.p01.13" replicon_id = 'ACBA.007.P01_13' replicon_path = self.find_data( os.path.join('Replicons', rep_name + '.fst')) prot_file = self.find_data( os.path.join('Proteins', replicon_id + '.prt')) args = argparse.Namespace() args.gembase = False args.replicon = replicon_path cfg = Config(args) sequences_db = read_multi_prot_fasta(replicon_path) replicon = next(sequences_db) prot_db = ProdigalDB(replicon, cfg, prot_file=prot_file) infile = self.find_data( os.path.join("Results_Integron_Finder_{}".format(rep_name), "tmp_{}".format(replicon_id), "{}_intI.res".format(replicon_id))) df1 = read_hmm(rep_name, prot_db, infile, cfg, evalue=1.95e-25) exp1 = pd.DataFrame(data={ "Accession_number": rep_name, "query_name": "intI_Cterm", "ID_query": "-", "ID_prot": "ACBA.007.P01_13_1", "strand": 1, "pos_beg": 55, "pos_end": 1014, "evalue": 1.9e-25 }, index=[0]) exp1 = exp1[[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]] pdt.assert_frame_equal(df1, exp1) df2 = read_hmm(replicon_id, prot_db, infile, cfg, evalue=1.9e-25) exp2 = pd.DataFrame(columns=[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]) intcols = ["pos_beg", "pos_end", "strand"] floatcol = ["evalue"] exp2[intcols] = exp2[intcols].astype(int) exp2[floatcol] = exp2[floatcol].astype(float) pdt.assert_frame_equal(df2, exp2)
def test_make_protfile(self): file_name = (('ACBA.0917.00019', '.fna', 3870), ('ESCO001.C.00001.C001', '.fst', 3870)) for seq_name, ext, seq_nb in file_name: replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) for seq_nb, seqs in enumerate(zip( read_multi_prot_fasta(self.find_data(os.path.join('Gembase', 'Proteins', seq_name + '.prt'))), read_multi_prot_fasta(db.protfile)), 1): expected, test = seqs self.assertEqual(expected.id, test.id) self.assertEqual(seq_nb, seq_nb)
def test_read_multi_prot_fasta(self): replicon_id = 'ACBA.007.P01_13' replicon_path = self.find_data( os.path.join('Proteins', replicon_id + '.prt')) replicon = utils.read_multi_prot_fasta(replicon_path) expected_seq_id = [ '{}_{}'.format(replicon_id, i) for i in range(1, 24) ] received_seq_id = [seq.id for seq in replicon] self.assertListEqual(expected_seq_id, received_seq_id)
def test_make_protfile_no_dir(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path db = ProdigalDB(replicon, cfg) for seq_nb, seqs in enumerate( zip( read_multi_prot_fasta( self.find_data(os.path.join('Proteins', prot_name))), read_multi_prot_fasta(db.protfile)), 1): expected, test = seqs self.assertEqual(expected.id, test.id) self.assertEqual(seq_nb, 23)
def test_ProteinDB(self): file_name = 'acba.007.p01.13' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) self.assertTrue(db.replicon.id, replicon.id)
def test_make_protfile_no_prodigal(self): file_name = 'acba.007.p01.13' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path self.args.prodigal = 'foo_bar' cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path with self.assertRaises(RuntimeError) as ctx: ProdigalDB(replicon, cfg)
def test_ProteinDB_no_prodigal(self): file_name = 'acba.007.p01.13' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) self.args.prodigal = None with self.assertRaises(RuntimeError) as ctx: ProdigalDB(replicon, cfg)
def test_getitem(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) exp = read_multi_prot_fasta(self.find_data(os.path.join('Proteins', prot_name))) for prot_expected in exp: prot_received = db[prot_expected.id] self.assertEqual(prot_received.id, prot_expected.id) self.assertEqual(prot_received.seq, prot_expected.seq) with self.assertRaises(KeyError) as ctx: db['nimport_naoik'] self.assertEqual(str(ctx.exception), "'nimport_naoik'")
def test_ProteinDB(self): file_name = 'acba.007.p01.13' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) self.assertTrue(db.replicon.id, replicon.id)
def test_protfile(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) self.assertEqual(os.path.join(cfg.tmp_dir(replicon.id), prot_name), db.protfile)
def test_make_protfile_no_prodigal(self): file_name = 'acba.007.p01.13' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path self.args.prodigal = 'foo_bar' cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path with self.assertRaises(RuntimeError) as ctx: ProdigalDB(replicon, cfg)
def test_getitem(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) exp = read_multi_prot_fasta( self.find_data(os.path.join('Proteins', prot_name))) for prot_expected in exp: prot_received = db[prot_expected.id] self.assertEqual(prot_received.id, prot_expected.id) self.assertEqual(prot_received.seq, prot_expected.seq) with self.assertRaises(KeyError) as ctx: db['nimport_naoik'] self.assertEqual(str(ctx.exception), "'nimport_naoik'")
def test_ProteinDB_no_prodigal(self): file_name = 'acba.007.p01.13' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) self.args.prodigal = None with self.assertRaises(RuntimeError) as ctx: ProdigalDB(replicon, cfg)
def test_protfile(self): file_name = (('ACBA.0917.00019', '.fna'), ('ESCO001.C.00001.C001', '.fst')) for seq_name, ext in file_name: replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) self.assertEqual(os.path.join(cfg.tmp_dir(replicon.id), replicon.id + '.prt'), db.protfile)
def test_protfile(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) self.assertEqual(os.path.join(cfg.tmp_dir(replicon.id), prot_name), db.protfile)
def test_ProteinDB(self): # From Gembase Draft , Gembase Complete file_names = ('ACBA.0917.00019.fna', 'ESCO001.C.00001.C001.fst') for file_name in file_names: replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', file_name)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) self.assertTrue(db.replicon.id, replicon.id)
def test_ProteinDB(self): # From Gembase Draft , Gembase Complete file_names = ('ACBA.0917.00019.fna', 'ESCO001.C.00001.C001.fst') for file_name in file_names: replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', file_name)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) self.assertTrue(db.replicon.id, replicon.id)
def test_iter(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) idx = SeqIO.index(self.find_data(os.path.join('Proteins', prot_name)), 'fasta', alphabet=Seq.IUPAC.extended_protein) for exp_seq_id, get_seq_id in zip(idx, db): self.assertEqual(exp_seq_id, get_seq_id)
def test_read_multi(self): """ Test reading hmm results when there are multiple hits: 2 hits on the same protein: keep only the one with the best evalue. 2 hits on 2 different proteins: keep the 2 proteins. """ replicon_id = 'ACBA.0917.00019' contig_id = 'ACBA.0917.00019.0001' result_dir_expected = self.find_data( "Results_Integron_Finder_{}.gembase".format(replicon_id)) replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', replicon_id + '.fna')) prot_file = os.path.join(result_dir_expected, "tmp_{}".format(contig_id), contig_id + '.prt') args = argparse.Namespace() args.gembase = True args.replicon = replicon_path cfg = Config(args) sequences_db = read_multi_prot_fasta(replicon_path) replicon = next(sequences_db) prot_db = GembaseDB(replicon, cfg, prot_file=prot_file) infile = self.find_data( os.path.join('fictive_results', "{}_intI_multi.res".format(contig_id))) df = read_hmm(contig_id, prot_db, infile, cfg) exp = pd.DataFrame(data={ "Accession_number": [contig_id] * 2, "query_name": ["Phage_integrase"] * 2, "ID_query": ["PF00589.16"] * 2, "ID_prot": ["ACBA.0917.00019.i0001_00298", "ACBA.0917.00019.i0001_00338"], "strand": [-1, -1], "pos_beg": [311597, 350328], "pos_end": [312631, 351248], "evalue": [5.5e-66, 3.4e-51] }, index=[0, 1]) exp = exp[[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]] pdt.assert_frame_equal(df, exp)
def test_get_description(self): # SeqDesc(id, strand, strat, stop) file_name = 'acba.007.p01.13' replicon_path = self.find_data(os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) descriptions = {'ACBA.007.P01_13_23': SeqDesc('ACBA.007.P01_13_23', -1, 19721, 20254), 'ACBA.007.P01_13_1': SeqDesc('ACBA.007.P01_13_1', 1, 55, 1014)} for seq_id, desc in descriptions.items(): self.assertEqual(desc, db.get_description(seq_id))
def test_protfile(self): file_name = (('ACBA.0917.00019', '.fna'), ('ESCO001.C.00001.C001', '.fst')) for seq_name, ext in file_name: replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) self.assertEqual( os.path.join(cfg.tmp_dir(replicon.id), replicon.id + '.prt'), db.protfile)
def test_iter(self): file_name = 'acba.007.p01.13' prot_name = 'ACBA.007.P01_13.prt' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) idx = SeqIO.index(self.find_data(os.path.join('Proteins', prot_name)), 'fasta', alphabet=Seq.IUPAC.extended_protein) for exp_seq_id, get_seq_id in zip(idx, db): self.assertEqual(exp_seq_id, get_seq_id)
def test_read_hmm_gembase(self): """ Test that the hmm hits are well read, when the gembase format is used (.prt file is provided, prodigal is not used to find the proteins). """ replicon_id = 'ACBA.0917.00019' contig_id = 'ACBA.0917.00019.0001' result_dir_expected = self.find_data( "Results_Integron_Finder_{}.gembase".format(replicon_id)) replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', replicon_id + '.fna')) prot_file = os.path.join(result_dir_expected, "tmp_{}".format(contig_id), contig_id + '.prt') infile = os.path.join(result_dir_expected, "tmp_{}".format(contig_id), "{}_intI.res".format(contig_id)) args = argparse.Namespace() args.gembase = True args.replicon = replicon_path cfg = Config(args) sequences_db = read_multi_prot_fasta(replicon_path) replicon = next(sequences_db) prot_db = GembaseDB(replicon, cfg, prot_file=prot_file) df = read_hmm(contig_id, prot_db, infile, cfg) exp = pd.DataFrame(data={ "Accession_number": contig_id, "query_name": "intI_Cterm", "ID_query": "-", "ID_prot": "ACBA.0917.00019.i0001_00298", "strand": -1, "pos_beg": 311597, "pos_end": 312631, "evalue": 3.6e-25 }, index=[0]) exp = exp[[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]] pdt.assert_frame_equal(df, exp)
def test_read_hmm(self): """ Test that the hmm hits are well read """ rep_name = "acba.007.p01.13" replicon_id = 'ACBA.007.P01_13' replicon_path = self.find_data( os.path.join('Replicons', rep_name + '.fst')) prot_file = self.find_data( os.path.join('Proteins', replicon_id + '.prt')) args = argparse.Namespace() args.gembase = False args.replicon = replicon_path cfg = Config(args) sequences_db = read_multi_prot_fasta(replicon_path) replicon = next(sequences_db) prot_db = ProdigalDB(replicon, cfg, prot_file=prot_file) infile = self.find_data( os.path.join("Results_Integron_Finder_{}".format(rep_name), "tmp_{}".format(replicon_id), "{}_intI.res".format(replicon_id))) df = read_hmm(rep_name, prot_db, infile, cfg) exp = pd.DataFrame(data={ "Accession_number": rep_name, "query_name": "intI_Cterm", "ID_query": "-", "ID_prot": "ACBA.007.P01_13_1", "strand": 1, "pos_beg": 55, "pos_end": 1014, "evalue": 1.9e-25 }, index=[0]) exp = exp[[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]] pdt.assert_frame_equal(df, exp)
def test_read_hmm_cov2(self): """ Test that the hmm hits are well read, it returns only the hits with coverage > given threshold """ rep_name = "acba.007.p01.13" replicon_id = 'ACBA.007.P01_13' replicon_path = self.find_data( os.path.join('Replicons', rep_name + '.fst')) prot_file = self.find_data( os.path.join('Proteins', replicon_id + '.prt')) args = argparse.Namespace() args.gembase = False args.replicon = replicon_path cfg = Config(args) sequences_db = read_multi_prot_fasta(replicon_path) replicon = next(sequences_db) prot_db = ProdigalDB(replicon, cfg, prot_file=prot_file) infile = self.find_data( os.path.join("fictive_results", "{}_intI.res".format(replicon_id))) df1 = read_hmm(rep_name, prot_db, infile, cfg, coverage=0.7) exp1 = pd.DataFrame(data={ "Accession_number": [rep_name] * 2, "query_name": ["intI_Cterm"] * 2, "ID_query": ["-", "-"], "ID_prot": ["ACBA.007.P01_13_1", "ACBA.007.P01_13_2"], "strand": [1, -1], "pos_beg": [55, 905], "pos_end": [1014, 1609], "evalue": [1.9e-25, 1e-3] }, index=[0, 1]) exp1 = exp1[[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]] pdt.assert_frame_equal(df1, exp1)
def test_find_gembase_file_basename(self): """ test if find_gembase_file_basename get the the right basename for files in gembase """ gembase_path = self.find_data('Gembase') file_names = ('ACBA.0917.00019.fna', 'ESCO001.C.00001.C001.fst') for file_name in file_names: replicon_path = self.find_data(os.path.join('Gembase', 'Replicons', file_name)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) self.assertTrue(db._find_gembase_file_basename(gembase_path, replicon_path), os.path.splitext(file_name)[0])
def test_get_description(self): # SeqDesc(id, strand, strat, stop) file_name = 'acba.007.p01.13' replicon_path = self.find_data( os.path.join('Replicons', file_name + '.fst')) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = ProdigalDB(replicon, cfg) descriptions = { 'ACBA.007.P01_13_23': SeqDesc('ACBA.007.P01_13_23', -1, 19721, 20254), 'ACBA.007.P01_13_1': SeqDesc('ACBA.007.P01_13_1', 1, 55, 1014) } for seq_id, desc in descriptions.items(): self.assertEqual(desc, db.get_description(seq_id))
def test_find_gembase_file_basename(self): """ test if find_gembase_file_basename get the the right basename for files in gembase """ gembase_path = self.find_data('Gembase') file_names = ('ACBA.0917.00019.fna', 'ESCO001.C.00001.C001.fst') for file_name in file_names: replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', file_name)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) with self.catch_log(): db = GembaseDB(replicon, cfg) self.assertTrue( db._find_gembase_file_basename(gembase_path, replicon_path), os.path.splitext(file_name)[0])
def test_read_empty(self): """ Test that when there are no hits in the hmm result file, it returns an empty dataframe, without error. """ rep_name = "acba.007.p01.13" replicon_id = 'ACBA.007.P01_13' replicon_path = self.find_data( os.path.join('Replicons', rep_name + '.fst')) prot_file = self.find_data( os.path.join('Proteins', replicon_id + '.prt')) args = argparse.Namespace() args.gembase = False args.replicon = replicon_path cfg = Config(args) sequences_db = read_multi_prot_fasta(replicon_path) replicon = next(sequences_db) prot_db = ProdigalDB(replicon, cfg, prot_file=prot_file) infile = self.find_data( os.path.join("fictive_results", "{}_intI-empty.res".format(replicon_id))) df = read_hmm(rep_name, prot_db, infile, cfg) exp = pd.DataFrame(columns=[ "Accession_number", "query_name", "ID_query", "ID_prot", "strand", "pos_beg", "pos_end", "evalue" ]) intcols = ["pos_beg", "pos_end", "strand"] floatcol = ["evalue"] exp[intcols] = exp[intcols].astype(int) exp[floatcol] = exp[floatcol].astype(float) pdt.assert_frame_equal(df, exp)
def test_get_description(self): # SeqDesc(id, strand, strat, stop) file_name = { ('ACBA.0917.00019', '.fna'): { 'ACBA.0917.00019.b0001_00001': SeqDesc('ACBA.0917.00019.b0001_00001', -1, 266, 1480), 'ACBA.0917.00019.i0001_03957': SeqDesc('ACBA.0917.00019.i0001_03957', -1, 4043755, 4044354) }, } for seq_name, ext in file_name: replicon_path = self.find_data( os.path.join('Gembase', 'Replicons', seq_name + ext)) self.args.replicon = replicon_path cfg = Config(self.args) seq_db = read_multi_prot_fasta(replicon_path) replicon = next(seq_db) replicon.path = replicon_path os.makedirs(cfg.tmp_dir(replicon.id)) db = GembaseDB(replicon, cfg) descriptions = file_name[(seq_name, ext)] for seq_id, desc in descriptions.items(): self.assertEqual(desc, db.get_description(seq_id)) with self.assertRaises(IntegronError) as ctx: db.get_description('nimport_naoik') self.assertEqual( str(ctx.exception), "'nimport_naoik' is not a valid Gembase protein identifier.") with self.assertRaises(KeyError) as ctx: db.get_description('FOO.BAR.00019.i0001_03924') self.assertEqual(str(ctx.exception), "'FOO.BAR.00019.i0001_03924'")