| 1 |
#cython: language_level=3 |
| 2 |
|
| 3 |
''' |
| 4 |
Created on 30 mars 2016 |
| 5 |
|
| 6 |
@author: coissac |
| 7 |
''' |
| 8 |
|
| 9 |
import types |
| 10 |
|
| 11 |
from obitools3.dms.obiseq cimport Nuc_Seq |
| 12 |
|
| 13 |
|
| 14 |
# def fastaIterator(lineiterator, |
| 15 |
# int skip=0, |
| 16 |
# only=None, |
| 17 |
# firstline=None, |
| 18 |
# int buffersize=100000000 |
| 19 |
# ): |
| 20 |
# cdef str ident |
| 21 |
# cdef str definition |
| 22 |
# cdef dict tags |
| 23 |
# cdef list s |
| 24 |
# cdef bytes sequence |
| 25 |
# cdef int skipped, ionly, read |
| 26 |
# |
| 27 |
# if only is None: |
| 28 |
# ionly=-1 |
| 29 |
# else: |
| 30 |
# ionly=int(only) |
| 31 |
# |
| 32 |
# if isinstance(lineiterator, (str, bytes)): |
| 33 |
# lineiterator=uopen(lineiterator) |
| 34 |
# if isinstance(lineiterator, LineBuffer): |
| 35 |
# iterator = iter(lineiterator) |
| 36 |
# else: |
| 37 |
# if hasattr(lineiterator, "readlines"): |
| 38 |
# iterator = iter(LineBuffer(lineiterator, buffersize)) |
| 39 |
# elif hasattr(lineiterator, '__next__'): |
| 40 |
# iterator = lineiterator |
| 41 |
# else: |
| 42 |
# raise Exception("Invalid line iterator") |
| 43 |
# |
| 44 |
# skipped = 0 |
| 45 |
# i = iterator |
| 46 |
# |
| 47 |
# if firstline is None: |
| 48 |
# line = next(i) |
| 49 |
# else: |
| 50 |
# line = firstline |
| 51 |
# |
| 52 |
# while True: |
| 53 |
# |
| 54 |
# if ionly >= 0 and read >= ionly: |
| 55 |
# break |
| 56 |
# |
| 57 |
# while skipped < skip : |
| 58 |
# line = next(i) |
| 59 |
# try: |
| 60 |
# while line[0]!='>': |
| 61 |
# line = next(i) |
| 62 |
# except StopIteration: |
| 63 |
# pass |
| 64 |
# skipped += 1 |
| 65 |
# |
| 66 |
# ident,tags,definition = parseHeader(line) |
| 67 |
# s = [] |
| 68 |
# line = next(i) |
| 69 |
# |
| 70 |
# try: |
| 71 |
# while line[0]!='>': |
| 72 |
# s.append(str2bytes(line)[0:-1]) |
| 73 |
# line = next(i) |
| 74 |
# |
| 75 |
# except StopIteration: |
| 76 |
# pass |
| 77 |
# |
| 78 |
# sequence = b"".join(s) |
| 79 |
# |
| 80 |
# yield { "id" : ident, |
| 81 |
# "definition" : definition, |
| 82 |
# "sequence" : sequence, |
| 83 |
# "quality" : None, |
| 84 |
# "offset" : None, |
| 85 |
# "tags" : tags, |
| 86 |
# "annotation" : {} |
| 87 |
# } |
| 88 |
# |
| 89 |
# read+=1 |
| 90 |
|
| 91 |
|
| 92 |
def fastaNucIterator(lineiterator, |
| 93 |
int skip=0, |
| 94 |
only=None, |
| 95 |
firstline=None, |
| 96 |
int buffersize=100000000, |
| 97 |
bytes nastring=b"NA" |
| 98 |
): |
| 99 |
|
| 100 |
cdef bytes ident |
| 101 |
cdef bytes definition |
| 102 |
cdef dict tags |
| 103 |
cdef list s |
| 104 |
cdef bytes sequence |
| 105 |
cdef int skipped, ionly, read |
| 106 |
cdef Nuc_Seq seq |
| 107 |
cdef bint stop |
| 108 |
|
| 109 |
if only is None: |
| 110 |
ionly = -1 |
| 111 |
else: |
| 112 |
ionly = int(only) |
| 113 |
|
| 114 |
if isinstance(lineiterator, (str, bytes)): |
| 115 |
lineiterator=uopen(lineiterator) |
| 116 |
if isinstance(lineiterator, LineBuffer): |
| 117 |
iterator = iter(lineiterator) |
| 118 |
else: |
| 119 |
if hasattr(lineiterator, "readlines"): |
| 120 |
iterator = iter(LineBuffer(lineiterator, buffersize)) |
| 121 |
elif hasattr(lineiterator, '__next__'): |
| 122 |
iterator = lineiterator |
| 123 |
else: |
| 124 |
raise Exception("Invalid line iterator") |
| 125 |
|
| 126 |
skipped = 0 |
| 127 |
read = 0 |
| 128 |
|
| 129 |
if firstline is None: |
| 130 |
line = next(iterator) |
| 131 |
else: |
| 132 |
line = firstline |
| 133 |
|
| 134 |
stop=False |
| 135 |
while not stop: |
| 136 |
|
| 137 |
if ionly >= 0 and read >= ionly: |
| 138 |
break |
| 139 |
|
| 140 |
while skipped < skip : |
| 141 |
line = next(iterator) |
| 142 |
try: |
| 143 |
while line[:1]!=b'>': |
| 144 |
line = next(iterator) |
| 145 |
except StopIteration: |
| 146 |
pass |
| 147 |
skipped += 1 |
| 148 |
|
| 149 |
ident,tags,definition = parseHeader(line, nastring=nastring) |
| 150 |
s = [] |
| 151 |
line = next(iterator) |
| 152 |
|
| 153 |
try: |
| 154 |
while line[:1]!=b'>': |
| 155 |
s.append(line[0:-1]) |
| 156 |
line = next(iterator) |
| 157 |
except StopIteration: |
| 158 |
stop=True |
| 159 |
|
| 160 |
sequence = b"".join(s) |
| 161 |
|
| 162 |
seq = Nuc_Seq(ident, |
| 163 |
sequence, |
| 164 |
definition=definition, |
| 165 |
quality=None, |
| 166 |
offset=-1, |
| 167 |
tags=tags) |
| 168 |
|
| 169 |
yield seq |
| 170 |
|
| 171 |
read+=1 |
| 172 |
|
| 173 |
|