def __init_file_streams(self, in_arg=0, out_arg=1): if in_arg is not None: if len(self.args) - 1 < in_arg or self.args[in_arg] == '-': self.infile = sys.stdin self.from_stdin = True else: self.infile = self.args[in_arg] if not os.path.isfile(self.infile): self.error("Cannot read input file: " + self.infile) self.infile = io.xopen(self.infile, 'r') self.from_stdin = False if out_arg is not None: if len(self.args) - 1 < out_arg or self.args[out_arg] == '-': self.outfile = sys.stdout self.to_stdout = True else: self.outfile = self.args[out_arg] if os.path.isfile(self.outfile) and not self.options.wild: self.error("Outfile already exists, " \ "use -w/--wild to overwite") self.outfile = io.xopen(self.outfile, 'w') self.to_stdout = False
def parse(infile, *args, **kwargs): if isinstance(infile, file) or isinstance(infile, GzipFile): fh = infile do_close = False else: fh = xopen(infile, 'r') do_close = True record = None for idx, line in enumerate(fh): line = line.strip() step = idx % 4 if step == 0: if record is not None: yield record record = FastqRead(id=line[1:]) if step == 1: record.sequence = line elif step == 2: record.optional_id = line[1:] else: record.quality = line if do_close: fh.close()
def seek_to_start(fh, max_iter=100): """Moves a file handle to the first ">" in a FASTA file. All lines that come before that are ignored. """ if isinstance(fh, str): fh = xopen(fh, 'r') fh.seek(0) found = False for idx,line in enumerate(fh): if idx > max_iter: raise Exception("Seeking passed max_iter") line = line.strip() if line.startswith(">"): found = True break if not found: raise Exception("Seeking past end of file") fh.seek(0) while idx > 0: fh.next() idx = idx - 1 return fh
def seek_to_start(fh, max_iter=100): """Moves a file handle to the first ">" in a FASTA file. All lines that come before that are ignored. """ if isinstance(fh, str): fh = xopen(fh, 'r') fh.seek(0) found = False for idx, line in enumerate(fh): if idx > max_iter: raise Exception("Seeking passed max_iter") line = line.strip() if line.startswith(">"): found = True break if not found: raise Exception("Seeking past end of file") fh.seek(0) while idx > 0: fh.next() idx = idx - 1 return fh
def parse(infile, *args, **kwargs): if isinstance(infile, file) or isinstance(infile, GzipFile): fh = infile do_close = False else: fh = xopen(infile, 'r') do_close = True record = None for idx,line in enumerate(fh): line = line.strip() step = idx % 4 if step == 0: if record is not None: yield record record = FastqRead(id=line[1:]) if step == 1: record.sequence = line elif step == 2: record.optional_id = line[1:] else: record.quality = line if do_close: fh.close()
def parse(infile, *args, **kwargs): fh = xopen(infile, 'r') record = None for idx,line in enumerate(fh): yield BowtieRead.from_record(line) fh.close()
def parse(infile, *args, **kwargs): fh = xopen(infile, 'r') record = None for idx, line in enumerate(fh): yield BowtieRead.from_record(line) fh.close()
def parse(qseq, *args, **kwargs): fh = xopen(qseq, 'r') record = None for idx,line in enumerate(fh): yield QseqRead.from_record(line) fh.close()