Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lucventurini committed Jul 26, 2019
1 parent cdc3590 commit c7bad77
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
18 changes: 8 additions & 10 deletions Mikado/subprograms/util/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,15 @@ def grep_gtf(args, gene_ids, mrna_ids):
if not record:
continue
if args.genes is False:
if record.transcript in mrna_ids:
if not args.reverse:
print(record, file=args.out)
elif args.reverse:
print(record, file=args.out)
if record.transcript in mrna_ids and not args.reverse:
print(record, file=args.out)
elif record.transcript not in mrna_ids and args.reverse:
print(record, file=args.out)
else:
if record.gene in gene_ids:
if not args.reverse:
print(record, file=args.out)
elif args.reverse:
print(record, file=args.out)
if record.gene in gene_ids and not args.reverse:
print(record, file=args.out)
elif record.gene not in gene_ids and args.reverse:
print(record, file=args.out)


def launch(args):
Expand Down
61 changes: 61 additions & 0 deletions Mikado/tests/test_system_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import threading
from time import sleep
import pysam
import io


class ConvertCheck(unittest.TestCase):
Expand Down Expand Up @@ -1642,5 +1643,65 @@ def test_stat(self):
dir.cleanup()


class GrepTest(unittest.TestCase):

@mark.slow
def test_grep(self):
files = [pkg_resources.resource_filename("Mikado.tests", fname) for fname in ["trinity.gtf", "trinity.gff3"]]
with io.TextIOWrapper(pkg_resources.resource_stream("Mikado.tests", "trinity.ids")) as id_file:
ids = [tuple(line.rstrip().split("\t")) for line in id_file]

id_temp_file = tempfile.NamedTemporaryFile("wt", suffix=".txt")
to_write = list(random.sample(ids, 10))
[print(*idline, sep="\t", file=id_temp_file) for idline in to_write]
id_temp_file.flush()

for fname in files:
with self.subTest(fname=fname):
form = os.path.splitext(fname)[1]
outfile = tempfile.NamedTemporaryFile("wt", suffix=form)
outfile.close()
self.assertFalse(os.path.exists(outfile.name))
sys.argv = ["mikado", "util", "grep", id_temp_file.name, fname, outfile.name]
pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
self.assertTrue(os.path.exists(outfile.name))
found = set()
with to_gff(outfile.name, input_format=form[1:]) as stream:
for record in stream:
if record.is_transcript:
found.add(record.transcript)
self.assertEqual(len(found), 10)
self.assertEqual(found, set(_[0] for _ in to_write))

@mark.slow
def test_v_grep(self):
files = [pkg_resources.resource_filename("Mikado.tests", fname) for fname in ["trinity.gtf", "trinity.gff3"]]
with io.TextIOWrapper(pkg_resources.resource_stream("Mikado.tests", "trinity.ids")) as id_file:
ids = [tuple(line.rstrip().split("\t")) for line in id_file]

id_temp_file = tempfile.NamedTemporaryFile("wt", suffix=".txt")
to_write = list(random.sample(ids, 10))
others = [_ for _ in ids if _ not in to_write]
[print(*idline, sep="\t", file=id_temp_file) for idline in to_write]
id_temp_file.flush()

for fname in files:
with self.subTest(fname=fname):
form = os.path.splitext(fname)[1]
outfile = tempfile.NamedTemporaryFile("wt", suffix=form)
outfile.close()
self.assertFalse(os.path.exists(outfile.name))
sys.argv = ["mikado", "util", "grep", "-v", id_temp_file.name, fname, outfile.name]
pkg_resources.load_entry_point("Mikado", "console_scripts", "mikado")()
self.assertTrue(os.path.exists(outfile.name))
found = set()
with to_gff(outfile.name, input_format=form[1:]) as stream:
for record in stream:
if record.is_transcript:
found.add(record.transcript)
self.assertEqual(len(found), len(others))
self.assertEqual(found, set(_[0] for _ in others))


if __name__ == "__main__":
unittest.main()
38 changes: 38 additions & 0 deletions Mikado/tests/trinity.ids
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
c73_g1_i1.mrna1.160 c73_g1_i1.path1.160
c11_g1_i2.mrna1.111 c11_g1_i2.path1.111
c11_g1_i1.mrna1.350 c11_g1_i1.path1.350
c3_g1_i2.mrna1.181 c3_g1_i2.path1.181
c3_g1_i1.mrna1.716 c3_g1_i1.path1.716
c58_g1_i3.mrna1.19 c58_g1_i3.path1.19
c58_g1_i2.mrna1.35 c58_g1_i2.path1.35
c58_g1_i1.mrna1.190 c58_g1_i1.path1.190
c58_g1_i7.mrna1.1 c58_g1_i7.path1.1
c108_g1_i1.mrna1.104 c108_g1_i1.path1.104
c58_g1_i11.mrna1 c58_g1_i11.path1
c58_g1_i5.mrna1.3 c58_g1_i5.path1.3
c58_g1_i10.mrna1 c58_g1_i10.path1
c68_g1_i1.mrna1.173 c68_g1_i1.path1.173
c58_g1_i11.mrna2 c58_g1_i11.path2
c58_g1_i8.mrna2 c58_g1_i8.path2
c58_g1_i10.mrna2 c58_g1_i10.path2
c58_g1_i12.mrna1 c58_g1_i12.path1
c58_g1_i9.mrna1 c58_g1_i9.path1
c58_g1_i4.mrna1.6 c58_g1_i4.path1.6
c58_g1_i6.mrna1.3 c58_g1_i6.path1.3
c58_g1_i8.mrna1 c58_g1_i8.path1
c113_g1_i1.mrna1.94 c113_g1_i1.path1.94
c77_g1_i1.mrna1.153 c77_g1_i1.path1.153
c109_g1_i1.mrna1.102 c109_g1_i1.path1.102
c60_g1_i1.mrna1.189 c60_g1_i1.path1.189
c115_g1_i1.mrna1.88 c115_g1_i1.path1.88
c152_g1_i1.mrna1.66 c152_g1_i1.path1.66
c21_g1_i1.mrna1.302 c21_g1_i1.path1.302
c41_g1_i1.mrna1.224 c41_g1_i1.path1.224
c6_g1_i1.mrna1.412 c6_g1_i1.path1.412
c74_g1_i1.mrna1.154 c74_g1_i1.path1.154
c71_g1_i1.mrna1.167 c71_g1_i1.path1.167
c114_g1_i1.mrna1.89 c114_g1_i1.path1.89
c137_g1_i1.mrna1.77 c137_g1_i1.path1.77
c37_g1_i2.mrna1.66 c37_g1_i2.path1.66
c37_g1_i1.mrna1.234 c37_g1_i1.path1.234
c120_g1_i1.mrna1.89 c120_g1_i1.path1.89

0 comments on commit c7bad77

Please sign in to comment.