Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
lastline = None
for i, line in enumerate(fastafile):
line_blen = len(line)
line = line.decode()
line_clen = len(line.rstrip('\n\r'))
lastline = i
# write an index line
if line[0] == '>':
valid_entry = check_bad_lines(
rname, bad_lines, i - 1)
if valid_entry and i > 0:
indexfile.write(
"{0}\t{1:d}\t{2:d}\t{3:d}\t{4:d}\n".format(
rname, rlen, thisoffset, clen, blen))
elif not valid_entry:
raise FastaIndexingError(
"Line length of fasta"
" file is not "
"consistent! "
"Inconsistent line found in >{0} at "
"line {1:n}.".format(
rname, bad_lines[0][0] + 1))
blen = 0
rlen = 0
clen = 0
bad_lines = []
try: # must catch empty deflines (actually these might be okay: https://github.com/samtools/htslib/pull/258)
rname = line.rstrip('\n\r')[1:].split()[
0] # duplicates are detected with read_fai
except IndexError:
raise FastaIndexingError(
"Bad sequence name %s at line %s." %
else: # check line and advance offset
if not blen:
blen = line_blen
if not clen:
clen = line_clen
# only one short line should be allowed
# before we hit the next header, and it
# should be the last line in the entry
if line_blen != blen or line_blen == 1:
bad_lines.append((i, line_blen))
offset += line_blen
rlen += line_clen
# check that we find at least 1 valid FASTA record
if not valid_entry:
raise FastaIndexingError(
"The FASTA file %s does not contain a valid sequence. "
"Check that sequence definition lines start with '>'." % self.filename)
# write the final index line, if there is one.
if lastline is not None:
valid_entry = check_bad_lines(
rname, bad_lines, lastline
) # advance index since we're at the end of the file
if valid_entry:
indexfile.write(
"{0:s}\t{1:d}\t{2:d}\t{3:d}\t{4:d}\n".format(
rname, rlen, thisoffset, clen, blen))
else:
raise FastaIndexingError(
"Line length of fasta"
" file is not "
"{0:s}\t{1:d}\t{2:d}\t{3:d}\t{4:d}\n".format(
rname, rlen, thisoffset, clen, blen))
else:
raise FastaIndexingError(
"Line length of fasta"
" file is not "
"consistent! "
"Inconsistent line found in >{0} at "
"line {1:n}.".format(rname,
bad_lines[0][0] + 1))
except (IOError, FastaIndexingError) as e:
if isinstance(e, IOError):
raise IOError(
"%s may not be writable. Please use Fasta(rebuild=False), Faidx(rebuild=False) or faidx --no-rebuild."
% self.indexname)
elif isinstance(e, FastaIndexingError):
raise e