File size: 7,182 Bytes
a03d44f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
#!/usr/bin/env python
# encoding: utf-8
import csv, sys
import io, textwrap, itertools
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
csv.field_size_limit(sys.maxsize)
common_nucleotide_set = {'A', 'T', 'C', 'G', 'U', 'N'}
# not {'O', 'U', 'Z', 'J', 'B'}
# Common amino acids
common_amino_acid_set = {'R', 'X', 'S', 'G', 'W', 'I', 'Q', 'A', 'T', 'V', 'K', 'Y', 'C', 'N', 'L', 'F', 'D', 'M', 'P', 'H', 'E'}
def clean_seq(protein_id, seq):
seq = seq.upper()
new_seq = ""
has_invalid_char = False
invalid_char_set = set()
for ch in seq:
if 'A' <= ch <= 'Z' and ch not in ['J']:
new_seq += ch
else:
invalid_char_set.add(ch)
has_invalid_char = True
if has_invalid_char:
print("id: %s. Seq: %s" % (protein_id, seq))
print("invalid char set:", invalid_char_set)
return new_seq
def file_reader(filename, header=True, header_filter=True):
if filename.endswith(".fa") or filename.endswith(".fas") or filename.endswith(".fasta"):
return fasta_reader(filename)
elif filename.endswith(".csv"):
return csv_reader(filename, header=True, header_filter=True)
elif filename.endswith(".tsv"):
return tsv_reader(filename, header=True, header_filter=True)
else:
return txt_reader(filename, header=header, header_filter=header_filter)
def txt_reader(handle, header=True, header_filter=True):
'''
csv 读取器,适合大文件
:param handle:
:param header:
:param header_filter: 返回结果是否去掉头
:return:
'''
handle = handle if isinstance(handle, io.TextIOWrapper) else open(handle, 'r')
try:
cnt = 0
for line in handle:
cnt += 1
if header and header_filter and cnt == 1:
continue
yield line.strip()
except Exception as e:
raise StopIteration
finally:
if not handle.closed:
handle.close()
def tsv_reader(handle, header=True, header_filter=True):
'''
csv 读取器,适合大文件
:param handle:
:param header:
:param header_filter: 返回结果是否去掉头
:return:
'''
handle = handle if isinstance(handle, io.TextIOWrapper) else open(handle, 'r')
try:
reader = csv.reader(handle, delimiter="\t")
cnt = 0
for row in reader:
cnt += 1
if header and header_filter and cnt == 1:
continue
yield row
except Exception as e:
raise StopIteration
finally:
if not handle.closed:
handle.close()
def csv_reader(handle, header=True, header_filter=True):
'''
csv 读取器,适合大文件
:param handle:
:param header:
:param header_filter: 返回结果是否去掉头
:return:
'''
handle = handle if isinstance(handle, io.TextIOWrapper) else open(handle, 'r')
try:
# data = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",")
# reader = csv.reader(handle)
reader = csv.reader((line.replace('\0', '') for line in handle))
cnt = 0
for row in reader:
cnt += 1
if header and header_filter and cnt == 1:
continue
yield row
except Exception as e:
raise StopIteration
finally:
if not handle.closed:
handle.close()
def txt_writer(dataset, handle, header=None):
'''
txt 写
:param dataset: 数据
:param handle: 文件
:param header: 头
:return:
'''
'''
handle = handle if isinstance(handle, io.TextIOWrapper) else open(handle, 'w')
try:
if header:
if isinstance(header, list):
handle.write(",".join(header) + "\n")
else:
handle.write(header + "\n")
print("header: %s" %header)
for row in dataset:
handle.write(str(row) + "\n")
except Exception as e:
raise e
finally:
if not handle.closed:
handle.close()
'''
with open(handle, "w") as wfp:
if header:
if isinstance(header, list):
wfp.write(",".join(header) + "\n")
else:
wfp.write(header + "\n")
for row in dataset:
wfp.write(str(row) + "\n")
def csv_writer(dataset, handle, header):
'''
csv 写,适合大文件
:param dataset: 数据
:param handle: 文件
:param header: 头
:return:
'''
handle = handle if isinstance(handle, io.TextIOWrapper) else open(handle, 'w')
try:
writer = csv.writer(handle)
if header:
writer.writerow(header)
for row in dataset:
writer.writerow(row)
except Exception as e:
raise e
finally:
if not handle.closed:
handle.close()
def fasta_reader(handle, width=None):
"""
Reads a FASTA file, yielding header, sequence pairs for each sequence recovered 适合大文件
args:
:handle (str, pathliob.Path, or file pointer) - fasta to read from
:width (int or None) - formats the sequence to have max `width` character per line.
If <= 0, processed as None. If None, there is no max width.
yields:
:(header, sequence) tuples
returns:
:None
"""
FASTA_STOP_CODON = "*"
handle = handle if isinstance(handle, io.TextIOWrapper) else open(handle, 'r')
width = width if isinstance(width, int) and width > 0 else None
try:
header = None
for is_header, group in itertools.groupby(handle, lambda line: line.startswith(">")):
if is_header:
header = group.__next__().strip()
else:
seq = ''.join(line.strip() for line in group).strip().rstrip(FASTA_STOP_CODON)
if width is not None:
seq = textwrap.fill(seq, width)
yield header, seq
except Exception as e:
raise StopIteration
finally:
if not handle.closed:
handle.close()
def write_fasta(filepath, sequences):
'''
write fasta file
:param filepath: savepath
:param sequences: fasta sequence(each item: [id, seq])
:return:
'''
if sequences:
with open(filepath, "w") as output_handle:
if len(sequences[0]) > 1 and isinstance(sequences[0][0], str):
for row in sequences:
protein_id = row[0]
seq = row[1]
sequence = SeqRecord(Seq(seq, None), id=protein_id[1:] if protein_id and protein_id[0] == ">" else protein_id, description="")
SeqIO.write(sequence, output_handle, "fasta")
else:
for sequence in sequences:
SeqIO.write(sequence, output_handle, "fasta")
|