Exemple #1
0
def test_append():
    for ext in ["", ".gz"]:  # BZ2 does NOT support append
        text = "AB"
        reference = text + text
        filename = 'truncated.fastq' + ext
        mode = 'a'
        if ext != "":
            mode = 'ab'
            text = text.encode()
            reference = text + text
            text = get_compressor(filename).compress(
                text)  # On Py3, need to send BYTES, not unicode
        print("Trying ext=%s" % ext)
        with temporary_path(filename) as path:
            try:
                os.unlink(path)
            except OSError:
                pass
            with open_output(path, mode) as f:
                f.write(text)
            print(path)
            with open_output(path, mode) as f:
                f.write(text)
            with xopen(path, 'r') as f:
                try:
                    reference = reference.decode("utf-8")
                except AttributeError:
                    pass
                for appended in f:
                    assert appended == reference
Exemple #2
0
 def test_truncated_gz_iter():
     with raises(EOFError), temporary_path('truncated.gz') as path:
         create_truncated_file(path)
         f = xopen(path, 'r', use_system=False)  # work around bug in py3.4
         for line in f:
             pass
         f.close()
Exemple #3
0
    def get_writer(self, file_desc, compressed=False):
        """Create the writer for a file descriptor if it does not already
        exist.
        
        Args:
            file_desc: File descriptor. If `compressed==True`, this is a tuple
                (path, mode), otherwise it's only a path.
            compressed: Whether data has already been compressed.
        
        Returns:
            The writer.
        """
        if compressed:
            path, mode = file_desc
        else:
            path = file_desc

        if path not in self.writers:
            if self.suffix:
                real_path = add_suffix_to_path(path, self.suffix)
            else:
                real_path = path
            # TODO: test whether O_NONBLOCK allows non-blocking write to NFS
            if compressed:
                self.writers[path] = open_output(real_path, mode)
            else:
                self.writers[path] = xopen(real_path, "w")

        return self.writers[path]
Exemple #4
0
 def get_writer(self, file_desc, compressed=False):
     """Create the writer for a file descriptor if it does not already
     exist.
     
     Args:
         file_desc: File descriptor. If `compressed==True`, this is a tuple
             (path, mode), otherwise it's only a path.
         compressed: Whether data has already been compressed.
     
     Returns:
         The writer.
     """
     if compressed:
         path, mode = file_desc
     else:
         path = file_desc
     
     if path not in self.writers:
         if self.suffix:
             real_path = add_suffix_to_path(path, self.suffix)
         else:
             real_path = path
         # TODO: test whether O_NONBLOCK allows non-blocking write to NFS
         if compressed:
             self.writers[path] = open_output(real_path, mode)
         else:
             self.writers[path] = xopen(real_path, "w")
     
     return self.writers[path]
Exemple #5
0
def test_append():
    for ext in ["", ".gz"]:  # BZ2 does NOT support append
        text = "AB"
        reference = text + text
        filename = 'truncated.fastq' + ext
        mode = 'a'
        if ext != "":
            mode = 'ab'
            text = text.encode()
            reference = text + text
            text = get_compressor(filename).compress(text)  # On Py3, need to send BYTES, not unicode
        print("Trying ext=%s" % ext)
        with temporary_path(filename) as path:
            try:
                os.unlink(path)
            except OSError:
                pass
            with open_output(path, mode) as f:
                f.write(text)
            print(path)
            with open_output(path, mode) as f:
                f.write(text)
            with xopen(path, 'r') as f:
                try:
                    reference = reference.decode("utf-8")
                except AttributeError:
                    pass
                for appended in f:
                    assert appended == reference
Exemple #6
0
 def test_truncated_gz_iter():
     with raises(EOFError), temporary_path('truncated.gz') as path:
         create_truncated_file(path)
         f = xopen(path, 'r', use_system=False) # work around bug in py3.4
         for line in f:
             pass
         f.close()
Exemple #7
0
def test_xopen_binary():
    for name in files:
        f = xopen(name, 'rb')
        try:
            lines = list(f)
            assert len(lines) == 12
            assert lines[5] == b'AGCCGCTANGACGGGTTGGCCCTTAGACGTATCT\n', name
        finally:
            f.close()
Exemple #8
0
def test_context_manager():
    major, minor = sys.version_info[0:2]
    for name in files:
        if major == 2 and minor == 6:
            continue  # Py26 compression libraries do not support context manager protocol.
        with xopen(name, 'rt') as f:
            lines = list(f)
            assert len(lines) == 12
            assert lines[5] == 'AGCCGCTANGACGGGTTGGCCCTTAGACGTATCT\n', name
Exemple #9
0
def test_xopen_binary():
    for name in files:
        f = xopen(name, 'rb')
        try:
            lines = list(f)
            assert len(lines) == 12
            assert lines[5] == b'AGCCGCTANGACGGGTTGGCCCTTAGACGTATCT\n', name
        finally:
            f.close()
Exemple #10
0
def test_context_manager():
    major, minor = sys.version_info[0:2]
    for name in files:
        if major == 2 and minor == 6:
            continue  # Py26 compression libraries do not support context manager protocol.
        with xopen(name, 'rt') as f:
            lines = list(f)
            assert len(lines) == 12
            assert lines[5] == 'AGCCGCTANGACGGGTTGGCCCTTAGACGTATCT\n', name
Exemple #11
0
def create_truncated_file(path):
    # Random text
    text = ''.join(random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in range(200))
    f = xopen(path, 'w')
    f.write(text)
    f.close()
    f = open(path, 'a')
    f.truncate(os.stat(path).st_size - 10)
    f.close()
Exemple #12
0
def create_truncated_file(path):
    # Random text
    text = ''.join(
        random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ') for _ in range(200))
    f = xopen(path, 'w')
    f.write(text)
    f.close()
    f = open(path, 'a')
    f.truncate(os.stat(path).st_size - 10)
    f.close()
Exemple #13
0
 def __init__(self, path, mode='r', quality_base=None, alphabet=None):
     self.quality_base = quality_base
     self.alphabet = alphabet
     if isinstance(path, str):
         self.name = path
         self._file = xopen(path, mode)
         self._close_on_exit = True
     else:
         if hasattr(path, 'name'):
             self.name = path.name
         else:
             # TODO: generate random unique name?
             self.name = path.__class__
         self._file = path
Exemple #14
0
 def __init__(self, path, mode='r', quality_base=None, alphabet=None):
     self.quality_base = quality_base
     self.alphabet = alphabet
     if isinstance(path, str):
         self.name = path
         self._file = xopen(path, mode)
         self._close_on_exit = True
     else:
         if hasattr(path, 'name'):
             self.name = path.name
         else:
             # TODO: generate random unique name?
             self.name = path.__class__
         self._file = path
Exemple #15
0
 def __init__(
         self, path, quality_base=33, sequence_class=Sequence,
         alphabet=None):
     self._close_on_exit = False
     if isinstance(path, str):
         path = xopen(path, 'rb')
         self._close_on_exit = True
     if isinstance(path, str):
         self.name = path
     else:
         self.name = path.name
     self._file = path
     self.quality_base = quality_base
     self.sequence_class = sequence_class
     self.alphabet = alphabet
Exemple #16
0
 def __init__(self,
              path,
              quality_base=33,
              sequence_class=Sequence,
              alphabet=None):
     self._close_on_exit = False
     if isinstance(path, str):
         path = xopen(path, 'rb')
         self._close_on_exit = True
     if isinstance(path, str):
         self.name = path
     else:
         self.name = path.name
     self._file = path
     self.quality_base = quality_base
     self.sequence_class = sequence_class
     self.alphabet = alphabet
Exemple #17
0
 def test_truncated_gz():
     with raises(EOFError), temporary_path('truncated.gz') as path:
         create_truncated_file(path)
         f = xopen(path, 'r')
         f.read()
         f.close()
Exemple #18
0
 def test_truncated_gz():
     with raises(EOFError), temporary_path('truncated.gz') as path:
         create_truncated_file(path)
         f = xopen(path, 'r')
         f.read()
         f.close()