Example #1
0
 def testOpenFile(self):
     """
     When an open file pointer is passed to asHandle, that same file
     pointer must be returned.
     """
     with patch.object(builtins, 'open', mock_open()):
         fp = open('file')
         with asHandle(fp) as newfp:
             self.assertIs(fp, newfp)
Example #2
0
 def testStr(self):
     """
     When a string filename is passed to asHandle, it must be possible to
     read the correct data from the fp that is returned.
     """
     mockOpener = mock_open(read_data='xxx')
     with patch.object(builtins, 'open', mockOpener):
         with asHandle('file') as fp:
             self.assertEqual('xxx', fp.read())
Example #3
0
 def testStr(self):
     """
     When a string filename is passed to asHandle, it must be possible to
     read the correct data from the fp that is returned.
     """
     mockOpener = mockOpen(read_data='xxx')
     with patch.object(builtins, 'open', mockOpener):
         with asHandle('file') as fp:
             self.assertEqual('xxx', fp.read())
Example #4
0
    def save(self, filename):
        """
        Write SAM output to a file.

        @param filename: A C{str} filename or an already-open file handle.
        """
        with asHandle(filename) as fp:
            self._writer.save(fp, tmpfp=self._tf)
        self._tf.close()
        self._tf = self._writer = None
Example #5
0
 def testOpenFile(self):
     """
     When an open file pointer is passed to asHandle, that same file
     pointer must be returned.
     """
     mockOpener = mockOpen()
     with patch.object(builtins, 'open', mockOpener):
         fp = open('file')
         with asHandle(fp) as newfp:
             self.assertIs(fp, newfp)
Example #6
0
    def testGzip(self):
        """
        When a string '*.gz' filename is passed to asHandle, it must be
        possible to read the correct data from the fp that is returned.
        """
        result = BytesIO(b'xxx')

        with patch.object(gzip, 'GzipFile') as mockMethod:
            mockMethod.return_value = result
            with asHandle('file.gz') as fp:
                self.assertEqual('xxx', fp.read())
Example #7
0
 def iter(self):
     """
     Iterate over the sequences in the files in self.files_, yielding each
     as an instance of the desired read class.
     """
     for _file in self._files:
         with asHandle(_file) as fp:
             # Use FastqGeneralIterator because it provides access to
             # the unconverted quality string (i.e., it doesn't try to
             # figure out the numeric quality values, which we don't
             # care about at this point).
             for sequenceId, sequence, quality in FastqGeneralIterator(fp):
                 yield self.readClass(sequenceId, sequence, quality)
Example #8
0
    def iter(self):
        """
        Iterate over the sequences in self.file_, yielding each as an
        instance of the desired read class.

        @raise ValueError: If the input file has an odd number of records or
            if any sequence has a different length than its predicted
            secondary structure.
        """
        checkAlphabet = self._checkAlphabet
        upperCase = self._upperCase
        count = 0
        for _file in self._files:
            with asHandle(_file) as fp:
                records = SeqIO.parse(fp, 'fasta')
                while True:
                    try:
                        record = next(records)
                    except StopIteration:
                        break

                    count += 1

                    try:
                        structureRecord = next(records)
                    except StopIteration:
                        raise ValueError('Structure file %r has an odd number '
                                         'of records.' % _file)

                    if len(structureRecord) != len(record):
                        raise ValueError(
                            'Sequence %r length (%d) is not equal to '
                            'structure %r length (%d) in input file %r.' % (
                                record.description, len(record),
                                structureRecord.description,
                                len(structureRecord), _file))

                    if upperCase:
                        read = self._readClass(
                            record.description,
                            str(record.seq.upper()),
                            str(structureRecord.seq.upper()))
                    else:
                        read = self._readClass(record.description,
                                               str(record.seq),
                                               str(structureRecord.seq))

                    if checkAlphabet is None or count < checkAlphabet:
                        read.checkAlphabet(count=None)

                    yield read
Example #9
0
    def testGzip(self):
        """
        When a string '*.gz' filename is passed to asHandle, it must be
        possible to read the correct data from the fp that is returned.
        """
        if six.PY3:
            self.skipTest('Mocking gzip.GzipFile disabled under Python 3')

        # This test should be better. It should actually create some gzip
        # compressed data and make sure that it's decompressed
        # properly. But Python mocking makes me so confused...
        result = File('xxx')

        with patch.object(gzip, 'GzipFile') as mockMethod:
            mockMethod.return_value = result
            with asHandle('file.gz') as fp:
                self.assertEqual('xxx', fp.read())
Example #10
0
    def testGzip(self):
        """
        When a string '*.gz' filename is passed to asHandle, it must be
        possible to read the correct data from the fp that is returned.
        """
        if six.PY3:
            self.skipTest('Mocking gzip.GzipFile disabled under Python 3')

        # This test should be better. It should actually create some gzip
        # compressed data and make sure that it's decompressed
        # properly. But Python mocking makes me so confused...
        result = StringIO('xxx')

        with patch.object(gzip, 'GzipFile') as mockMethod:
            mockMethod.return_value = result
            with asHandle('file.gz') as fp:
                self.assertEqual('xxx', fp.read())
Example #11
0
    def iter(self):
        """
        Iterate over the sequences in self.file_, yielding each as an
        instance of the desired read class.

        @raise ValueError: If the input file has an odd number of records or
            if any sequence has a different length than its predicted
            secondary structure.
        """
        upperCase = self._upperCase
        for _file in self._files:
            with asHandle(_file) as fp:
                records = SeqIO.parse(fp, 'fasta')
                while True:
                    try:
                        record = next(records)
                    except StopIteration:
                        break

                    try:
                        structureRecord = next(records)
                    except StopIteration:
                        raise ValueError('Structure file %r has an odd number '
                                         'of records.' % _file)

                    if len(structureRecord) != len(record):
                        raise ValueError(
                            'Sequence %r length (%d) is not equal to '
                            'structure %r length (%d) in input file %r.' % (
                                record.description, len(record),
                                structureRecord.description,
                                len(structureRecord), _file))

                    if upperCase:
                        read = self._readClass(
                            record.description,
                            str(record.seq.upper()),
                            str(structureRecord.seq.upper()))
                    else:
                        read = self._readClass(record.description,
                                               str(record.seq),
                                               str(structureRecord.seq))

                    yield read
Example #12
0
 def iter(self):
     """
     Iterate over the sequences in the files in self.files_, yielding each
     as an instance of the desired read class.
     """
     checkAlphabet = self._checkAlphabet
     count = 0
     for _file in self._files:
         with asHandle(_file) as fp:
             for seq in SeqIO.parse(fp, 'fasta'):
                 if self._upperCase:
                     read = self._readClass(seq.description,
                                            str(seq.seq.upper()))
                 else:
                     read = self._readClass(seq.description, str(seq.seq))
                 if checkAlphabet is None or count < checkAlphabet:
                     read.checkAlphabet(count=None)
                 yield read
                 count += 1
Example #13
0
 def iter(self):
     """
     Iterate over the sequences in the files in self.files_, yielding each
     as an instance of the desired read class.
     """
     checkAlphabet = self._checkAlphabet
     count = 0
     for _file in self._files:
         with asHandle(_file) as fp:
             for seq in SeqIO.parse(fp, 'fasta'):
                 if self._upperCase:
                     read = self._readClass(seq.description,
                                            str(seq.seq.upper()))
                 else:
                     read = self._readClass(seq.description, str(seq.seq))
                 if checkAlphabet is None or count < checkAlphabet:
                     read.checkAlphabet(count=None)
                 yield read
                 count += 1
Example #14
0
 def iter(self):
     """
     Iterate over the sequences in the files in self.files_, yielding each
     as an instance of the desired read class.
     """
     count = 0
     for _file in self._files:
         with asHandle(_file) as fp:
             # Duplicate some code here so as not to test
             # self._upperCase in the loop.
             if self._upperCase:
                 for seq in SeqIO.parse(fp, 'fasta'):
                     read = self._readClass(seq.description,
                                            str(seq.seq.upper()))
                     yield read
                     count += 1
             else:
                 for seq in SeqIO.parse(fp, 'fasta'):
                     read = self._readClass(seq.description, str(seq.seq))
                     yield read
                     count += 1
Example #15
0
 def iter(self):
     """
     Iterate over the sequences in the files in self.files_, yielding each
     as an instance of the desired read class.
     """
     count = 0
     for _file in self._files:
         with asHandle(_file) as fp:
             # Duplicate some code here so as not to test
             # self._upperCase in the loop.
             if self._upperCase:
                 for seq in SeqIO.parse(fp, 'fasta'):
                     read = self._readClass(seq.description,
                                            str(seq.seq.upper()))
                     yield read
                     count += 1
             else:
                 for seq in SeqIO.parse(fp, 'fasta'):
                     read = self._readClass(seq.description, str(seq.seq))
                     yield read
                     count += 1