-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from io import BufferedIOBase, BytesIO | ||
|
||
from tola.assembly.assembly import Assembly | ||
from tola.assembly.gap import Gap | ||
from tola.assembly.scaffold import Scaffold | ||
from tola.fasta.index import FastaIndex | ||
|
||
|
||
class FastaStream: | ||
def __init__( | ||
self, | ||
out: BufferedIOBase, | ||
index: FastaIndex, | ||
line_length=60, | ||
gap_character=b"N", | ||
): | ||
self.out = out | ||
self.index = index | ||
self.line_length = line_length | ||
self.gap_character = gap_character | ||
|
||
def write_assembly(self, assembly: Assembly): | ||
for scffld in assembly.scaffolds: | ||
self.write_scaffold(scffld) | ||
|
||
def write_scaffold(self, scaffold: Scaffold): | ||
out = self.out | ||
fai = self.index | ||
line_length = self.line_length | ||
want = line_length | ||
|
||
out.write(f">{scaffold.name}\n".encode().lower()) | ||
for row in scaffold.rows: | ||
itr = ( | ||
self.gap_seq(row) | ||
if isinstance(row, Gap) | ||
else fai.get_sequence(row) | ||
) | ||
for chunk in itr: | ||
chunk.seek(0) | ||
while True: | ||
if seq := chunk.read(want): | ||
out.write(seq) | ||
want -= len(seq) | ||
if want == 0: | ||
out.write(b"\n") | ||
want = line_length | ||
else: | ||
break | ||
|
||
if want != line_length: | ||
out.write(b"\n") | ||
|
||
def gap_seq(self, gap: Gap): | ||
yield BytesIO(self.gap_character * gap.length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters