Example #1
0
    def assert_fasta_contains(self, base, args):
        for tag in args:
            valid = args[tag].split()
            fa = FASTA(base % tag)
            count = 0
            for read in fa.fetch():
                if read.name in valid:
                    count += 1
                else:
                    self.assertEqual('extra read in %s' % tag, read.name)

            self.assertEqual(count, len(valid))
Example #2
0
    def testError2(self):
        fa = StringIO.StringIO('''\
>foo_F3
ACTGACTG
''')
        qa = StringIO.StringIO('''\
>foo_F3
10 10 10 20 20 20 20 A
''')
        fasta = FASTA(fileobj=fa)
        qual = FASTA(fileobj=qa)
        out = StringIO.StringIO('')
        self.assertRaises(ValueError, ngsutils.fastq.fromfasta.merge_files, *(fasta, qual), **{'out': out})
Example #3
0
 def test_splitFastaOffset(self):
     path = os.path.dirname(__file__)
     ngsutils.fastq.barcode_split.fastx_barcode_split(FASTA(os.path.join(path, 'test_barcodes.fasta')), os.path.join(path, 'out.%s.fasta'), barcodes2, allow_revcomp=True, pos=1)
     self.assert_fasta_contains(os.path.join(path, 'out.%s.fasta'), {
         'missing': 'foo1 bar1 baz1 foo1-rc',
         'tag1': 'foo foo-rc foo2 foo2-rc',
         'tag2': 'bar bar2',
         'tag3': 'baz baz2'
         })
     self._unlink_fastx(os.path.join(path, 'out.%s.fasta'), 'missing tag1 tag2 tag3'.split())
Example #4
0
 def test_splitFasta(self):
     path = os.path.dirname(__file__)
     ngsutils.fastq.barcode_split.fastx_barcode_split(FASTA(os.path.join(path, 'test_barcodes.fasta')), os.path.join(path, 'out.%s.fasta'), barcodes2)
     self.assert_fasta_contains(os.path.join(path, 'out.%s.fasta'), {
         'missing': 'foo-rc foo1 bar1 baz1 foo2 bar2 baz2 foo1-rc foo2-rc',
         'tag1': 'foo',
         'tag2': 'bar',
         'tag3': 'baz'
         })
     self._unlink_fastx(os.path.join(path, 'out.%s.fasta'), 'missing tag1 tag2 tag3'.split())
Example #5
0
    def testQualFile(self):
        fa = StringIO.StringIO('''\
>foo_F3
ACTGACTG
''')
        qa = StringIO.StringIO('''\
>foo_F3
10 10 10 10 0 93 -5 99
''')
        fasta = FASTA(fileobj=fa)
        qual = FASTA(fileobj=qa, qual=True)
        out = StringIO.StringIO('')
        ngsutils.fastq.fromfasta.merge_files(fasta, qual, out=out)

        self.assertEqual(out.getvalue(), '''\
@foo
ACTGACTG
+
++++!~!~
''')
Example #6
0
    def testColor(self):
        fa = StringIO.StringIO('''\
>foo_F3
T01230123
''')
        qa = StringIO.StringIO('''\
>foo_F3
10 10 10 10 20 20 20 20
''')
        fasta = FASTA(fileobj=fa)
        qual = FASTA(fileobj=qa)
        out = StringIO.StringIO('')
        ngsutils.fastq.fromfasta.merge_files(fasta, qual, out=out)

        self.assertEqual(out.getvalue(), '''\
@foo
T01230123
+
++++5555
''')
Example #7
0
    def testSuffix(self):
        fa = StringIO.StringIO('''\
>foo_F3
ACTGACTG
''')
        fasta = FASTA(fileobj=fa)
        out = StringIO.StringIO('')
        ngsutils.fastq.fromfasta.merge_files(fasta, None, common_qual=';', suffix='_bar', out=out)

        self.assertEqual(out.getvalue(), '''\
@foo_bar
ACTGACTG
+
;;;;;;;;
''')
Example #8
0
    def testConstantColor(self):
        fa = StringIO.StringIO('''\
>foo_F3
T01230123
''')
        fasta = FASTA(fileobj=fa)
        out = StringIO.StringIO('')
        ngsutils.fastq.fromfasta.merge_files(fasta, None, common_qual=';', out=out)

        self.assertEqual(out.getvalue(), '''\
@foo
T01230123
+
;;;;;;;;
''')
Example #9
0
                    if first:
                        first = False
                        continue
                    cols = line.strip().split('\t')
                    if len(cols) < 4:
                        cols.append('y')
                    barcodes[cols[0]] = (cols[1], cols[2],
                                         True if cols[3] in 'TtyY' else False)
        elif not fname and os.path.exists(arg):
            fname = arg
        elif not templ:
            templ = arg

    if not barcodes or not fname or not templ:
        usage()

    if is_fasta_file(fname):
        outtempl = '%s_%%s.fasta' % templ
        reader = FASTA(fname)
    else:
        outtempl = '%s_%%s.fastq' % templ
        reader = FASTQ(fname)

    if stats:
        stats_fname = '%s.stats.txt' % templ
    else:
        stats_fname = None

    fastx_barcode_split(reader, outtempl, barcodes, edit, pos, allow_revcomp,
                        gz, stats_fname)
Example #10
0
            last = arg
        elif arg == '-h':
            usage()
        elif not fasta and os.path.exists(arg):
            fasta = arg
        elif not qual and os.path.exists(arg):
            qual = arg
        else:
            sys.stderr.write("ERROR: Unknown option: %s\n" % arg)
            usage()

    if not fasta:
        usage()

    if not qual and not common_qual:
        sys.stderr.write(
            'You must specify a quality input file or common qual value')
        usage()

    sys.stderr.write('Merging %s and %s\n' %
                     (os.path.basename(fasta),
                      os.path.basename(qual) if qual else common_qual))

    f = FASTA(fasta)
    q = FASTA(qual, qual=True) if qual else None

    merge_files(f, q, tag, common_qual)
    f.close()
    if q:
        q.close()
Example #11
0
                sys.exit(-1)
            last = None
        elif arg in ['-tag', '-q']:
            last = arg
        elif arg == '-h':
            usage()
        elif not fasta and os.path.exists(arg):
            fasta = arg
        elif not qual and os.path.exists(arg):
            qual = arg
        else:
            sys.stderr.write("ERROR: Unknown option: %s\n" % arg)
            usage()

    if not fasta:
        usage()

    if not qual and not common_qual:
        sys.stderr.write('You must specify a quality input file or common qual value')
        usage()

    sys.stderr.write('Merging %s and %s\n' % (os.path.basename(fasta), os.path.basename(qual) if qual else common_qual))

    f = FASTA(fasta)
    q = FASTA(qual, qual=True) if qual else None

    merge_files(f, q, tag, common_qual)
    f.close()
    if q:
        q.close()