>temp_0|source:-*- coding: utf-8 -*-|start:5624|stop:6124 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean (sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequ ence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:des t = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_ file_path, lazy_init >temp_1|source:-*- coding: utf-8 -*-|start:5244|stop:5744 on) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickste p)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, yma x = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0 .5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False) plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: % .2f / min: %s / max: >temp_2|source:-*- coding: utf-8 -*-|start:4017|stop:4517 matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next( ):if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Readi ng: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))sel f.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_len gths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * ( max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt. figure(figsize = (16 >temp_3|source:-*- coding: utf-8 -*-|start:6776|stop:7276 plit()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.fil e_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + lin e.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.q uals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()de f reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nones elf.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = SequenceS ource(sys.argv[1])fa >temp_4|source:-*- coding: utf-8 -*-|start:4142|stop:4642 ys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequen ce_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq _len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) o r 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq _len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update ({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth= 0.1)gs = gridspec.Gr >temp_5|source:-*- coding: utf-8 -*-|start:6202|stop:6702 = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself .ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0) if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self): self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_in t = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores):sel >temp_6|source:-*- coding: utf-8 -*-|start:2084|stop:2584 for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if sel f.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_regul ar():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_has h_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[h ash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [sel f.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([ (self.unique_hash_di >temp_7|source:-*- coding: utf-8 -*-|start:6069|stop:6569 ualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_ path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self .id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.tota l_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readline s() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer .readline()[1:].stri >temp_8|source:-*- coding: utf-8 -*-|start:974|stop:1474 (sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:t icks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file _obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequenc es = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta. pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FA STA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.f asta.id)self.sequenc >temp_9|source:-*- coding: utf-8 -*-|start:3960|stop:4460 ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 = = 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sy s.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.wri te('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequen ce_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l i n sequence_lengths:s >temp_10|source:-*- coding: utf-8 -*-|start:6709|stop:7209 = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.p os += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.see k(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self .quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()] self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(self) :self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []s elf.file_pointer.see >temp_11|source:-*- coding: utf-8 -*-|start:6187|stop:6687 self.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals _int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_ pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset() def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = No neself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline( )if not line:if len( >temp_12|source:-*- coding: utf-8 -*-|start:2857|stop:3357 nique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[se lf.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self. allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.i ds = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regu lar(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()seque nce = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence): self.seq = sequences >temp_13|source:-*- coding: utf-8 -*-|start:536|stop:1036 = True, store_frequencies = True):if entry.unique and store_frequencies:self.wr ite_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id( entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file _obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = se lf.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, p iece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence) ]return '\n'.join([s >temp_14|source:-*- coding: utf-8 -*-|start:5061|stop:5561 == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickste p, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_dist ribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len )plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution ) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-la rge', ha = 'center') >temp_15|source:-*- coding: utf-8 -*-|start:2309|stop:2809 self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.uni que_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self .id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] fo r i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.uni que_hash_dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)se lf.reset()def next(self):if self.unique:return self.next_unique()else:return sel f.next_regular()def >temp_16|source:-*- coding: utf-8 -*-|start:1770|stop:2270 _case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []s elf.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.uniq ue_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer. seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.uniq ue:self.init_unique_hash()def init_unique_hash(self):while self.next_regular():h ash = hashlib.sha1(s >temp_17|source:-*- coding: utf-8 -*-|start:5189|stop:5689 e='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y fo r y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.x lim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribut ion) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt. rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks( [])plt.text(0.02, 0. >temp_18|source:-*- coding: utf-8 -*-|start:2427|stop:2927 self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,' count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[ hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.t otal_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.uniqu e:return self.next_unique()else:return self.next_regular()def next_unique(self): if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_en try = self.unique_ha >temp_19|source:-*- coding: utf-8 -*-|start:3664|stop:4164 t():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_p ointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(self, t itle, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):impor t matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys .stderr.write('\r[fa >temp_20|source:-*- coding: utf-8 -*-|start:57|stop:557 ram is free software; you can redistribute it and/or modify it under# the terms of the GNU General Public License as published by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Plea se read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOut put:def __init__(self, output_file_path):self.output_file_path = output_file_pat hself.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = True, store_freque >temp_21|source:-*- coding: utf-8 -*-|start:4538|stop:5038 date({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewi dth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt .subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq _len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_se q_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylab el('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtick step = (max_seq_len >temp_22|source:-*- coding: utf-8 -*-|start:1074|stop:1574 n range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class Re adFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or s elf.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequenc es.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close( )class SequenceSourc >temp_23|source:-*- coding: utf-8 -*-|start:44|stop:544 n## This program is free software; you can redistribute it and/or modify it unde r# the terms of the GNU General Public License as published by the Free# Softwar e Foundation; either version 2 of the License, or (at your option)# any later ve rsion.## Please read the docs/COPYING file.import sysimport numpyimport hashlibc lass FastaOutput:def __init__(self, output_file_path):self.output_file_path = ou tput_file_pathself.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = True, >temp_24|source:-*- coding: utf-8 -*-|start:5830|stop:6330 ),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, si ze = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass Qu alSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_p ath = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self. id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_fil >temp_25|source:-*- coding: utf-8 -*-|start:2806|stop:3306 ef next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self .total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos] ]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_en try['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself .id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.f ile_pointer.readline >temp_26|source:-*- coding: utf-8 -*-|start:5759|stop:6259 uence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(se quence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-l arge')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')e xcept:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource: def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = qua ls_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Non eself.quals = Nonese >temp_27|source:-*- coding: utf-8 -*-|start:4441|stop:4941 sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12 ))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', line style='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8]) plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_betwee n(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alph a = 0.15)plt.ylabel( >temp_28|source:-*- coding: utf-8 -*-|start:6353|stop:6853 r.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def ne xt(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself .quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if no t line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q ) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.s tartswith('>'):self. >temp_29|source:-*- coding: utf-8 -*-|start:4447|stop:4947 nce_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt. rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle= '-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.gr id(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)p lt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(rang e(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0. 15)plt.ylabel('numbe >temp_30|source:-*- coding: utf-8 -*-|start:5498|stop:5998 % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot (gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([]) plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequ ence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alp ha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.sa vefig(dest + '.pdf') >temp_31|source:-*- coding: utf-8 -*-|start:1875|stop:2375 nique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file _pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init: self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.rea dlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_hash( )def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.s eq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[ha sh]['ids'].append(se >temp_32|source:-*- coding: utf-8 -*-|start:6083|stop:6583 __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_f ile_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Nonesel f.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.qu als_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = None else:self.total_quals = len([l for l in self.file_pointer.readlines() if l.start swith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1: ].strip()self.quals >temp_33|source:-*- coding: utf-8 -*-|start:4790|stop:5290 ution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number o f sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_ seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 2 0 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size ='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(s >temp_34|source:-*- coding: utf-8 -*-|start:5675|stop:6175 .text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len (sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\mi n(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pd f')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSou rce:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathself >temp_35|source:-*- coding: utf-8 -*-|start:6187|stop:6687 self.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals _int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_ pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset() def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = No neself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline( )if not line:if len( >temp_36|source:-*- coding: utf-8 -*-|start:2189|stop:2689 _hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1( self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_d ict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:se lf.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'co unt': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[ha sh]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.tot al_unique = len(self >temp_37|source:-*- coding: utf-8 -*-|start:5092|stop:5592 _distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size ='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max( seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96 , '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.s ubplot(gs[9])plt.rcP >temp_38|source:-*- coding: utf-8 -*-|start:90|stop:590 istribute it and/or modify it under# the terms of the GNU General Public License as published by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.imp ort sysimport numpyimport hashlibclass FastaOutput:def __init__(self, output_fil e_path):self.output_file_path = output_file_pathself.output_file_obj = open(outp ut_file_path, 'w')def store(self, entry, split = True, store_frequencies = True) :if entry.unique and >temp_39|source:-*- coding: utf-8 -*-|start:3991|stop:4491 otlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []se lf.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr .write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_length s.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_ seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_ len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_dist ribution[l] += 1fig >temp_40|source:-*- coding: utf-8 -*-|start:1281|stop:1781 f self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastali b] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids. append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n') def close(self):self.fasta.close()class SequenceSource():def __init__(self, fast a_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fa sta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.al low_mixed_case = all >temp_41|source:-*- coding: utf-8 -*-|start:782|stop:1282 rite('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.spl it(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_l ength = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]retur n '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)]) def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f _name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if >temp_42|source:-*- coding: utf-8 -*-|start:4270|stop:4770 stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int( max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsiz e = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color=' 0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subpl ot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.9 5, right = 0.98)plt. >temp_43|source:-*- coding: utf-8 -*-|start:5234|stop:5734 distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1 , ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymi n = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt .figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'c enter')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.g rid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2 f / std: %.2f / min: >temp_44|source:-*- coding: utf-8 -*-|start:840|stop:1340 f split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(s elf, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in rang e(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFast a:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = Sequen ceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fa sta.pos == 1:sys.std >temp_45|source:-*- coding: utf-8 -*-|start:4229|stop:4729 ths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:ma x_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)se q_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_di stribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes .linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_a djust(left=0.05, bot >temp_46|source:-*- coding: utf-8 -*-|start:2920|stop:3420 ique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entr y['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_ entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return F alsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline() [1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line: if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>') >temp_47|source:-*- coding: utf-8 -*-|start:5146|stop:5646 x_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max( seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribut ion) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt. ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 2 0.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 2 0})plt.grid(False)pl >temp_48|source:-*- coding: utf-8 -*-|start:3055|stop:3555 ].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse: return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = sel f.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_point er.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_p ointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if se lf.allow_mixed_case >temp_49|source:-*- coding: utf-8 -*-|start:1812|stop:2312 d = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(s elf.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.sta rtswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_ hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdi gest()if hash in sel >temp_50|source:-*- coding: utf-8 -*-|start:3562|stop:4062 quence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self .reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef c lose(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Nones elf.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_lengt h_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, y tickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gr idspecsequence_lengt >temp_51|source:-*- coding: utf-8 -*-|start:4223|stop:4723 e_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_ len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_ len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update( {'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0 .1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subp lots_adjust(left=0.0 >temp_52|source:-*- coding: utf-8 -*-|start:627|stop:1127 %s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)se lf.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write( '>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(se q)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n '.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def c lose(self):self.outp >temp_53|source:-*- coding: utf-8 -*-|start:4408|stop:4908 = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1f ig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9}) plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec (10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bo ttom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'bl ack', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distributi on, y2 = 0, color = >temp_54|source:-*- coding: utf-8 -*-|start:96|stop:596 ute it and/or modify it under# the terms of the GNU General Public License as pu blished by the Free# Software Foundation; either version 2 of the License, or (a t your option)# any later version.## Please read the docs/COPYING file.import sy simport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path ):self.output_file_path = output_file_pathself.output_file_obj = open(output_fil e_path, 'w')def store(self, entry, split = True, store_frequencies = True):if en try.unique and store >temp_55|source:-*- coding: utf-8 -*-|start:891|stop:1391 rite('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x ]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_f ile_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequ ences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fas ta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: >temp_56|source:-*- coding: utf-8 -*-|start:2012|stop:2512 ek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l i n self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique :self.init_unique_hash()def init_unique_hash(self):while self.next_regular():has h = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:s elf.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['co unt'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'s eq': self.seq,'count >temp_57|source:-*- coding: utf-8 -*-|start:3312|stop:3812 ot line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - l en(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_ca se else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, rea d_id):self.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.i d = Noneself.seq = >temp_58|source:-*- coding: utf-8 -*-|start:1762|stop:2262 ow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.i ds = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []s elf.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_ pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len( [l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if s elf.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_reg ular():hash = hashli >temp_59|source:-*- coding: utf-8 -*-|start:2874|stop:3374 f.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_li st[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry[' ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self.se q = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:l ine = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequen ceself.pos += 1retur >temp_60|source:-*- coding: utf-8 -*-|start:5888|stop:6388 nter', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_path try:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()exc ept:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = T rue):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy _initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if sel f.lazy_init:self.tot >temp_61|source:-*- coding: utf-8 -*-|start:1387|stop:1887 ry: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self. sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta .close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = T rue, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file _pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mix ed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniq ueself.unique_hash_d >temp_62|source:-*- coding: utf-8 -*-|start:4120|stop:4620 = 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sy s.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.wri te('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequen ce_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l i n sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 1 2))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', lin estyle='-', linewidt >temp_63|source:-*- coding: utf-8 -*-|start:1752|stop:2252 itself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_ list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path) self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_ seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.r eset()if self.unique:self.init_unique_hash()def init_unique_hash(self):while sel f.next_regular():has >temp_64|source:-*- coding: utf-8 -*-|start:2489|stop:2989 ,'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self .unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reve rse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(s elf):if self.unique:return self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.to tal_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]se lf.pos += 1self.seq >temp_65|source:-*- coding: utf-8 -*-|start:1417|stop:1917 .stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.se q)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSource() :def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mix ed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy _init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}se lf.unique_hash_list >temp_66|source:-*- coding: utf-8 -*-|start:2591|stop:3091 ]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total _unique = len(self.unique_hash_dict)self.reset()def next(self):if self.unique:re turn self.next_unique()else:return self.next_regular()def next_unique(self):if s elf.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id'] >temp_67|source:-*- coding: utf-8 -*-|start:3100|stop:3600 = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular (self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):sel f.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>') :self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += li ne.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self .pos += 1return True >temp_68|source:-*- coding: utf-8 -*-|start:5429|stop:5929 ion) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt. rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks( [])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\ % (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_length s),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, s ize = 'x-large')if d >temp_69|source:-*- coding: utf-8 -*-|start:2262|stop:2762 b.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique _hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1 else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self. seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_ dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]s elf.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if self. unique:return self.n >temp_70|source:-*- coding: utf-8 -*-|start:1446|stop:1946 d(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def c lose(self):self.fasta.close()class SequenceSource():def __init__(self, fasta_fil e_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_f ile_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_m ixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self. unique_next_hash = 0 >temp_71|source:-*- coding: utf-8 -*-|start:3762|stop:4262 reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_poi nter.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplo t as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()w hile self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[ fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(le n(self.seq))self.res >temp_72|source:-*- coding: utf-8 -*-|start:334|stop:834 sysimport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_pa th):self.output_file_path = output_file_pathself.output_file_obj = open(output_f ile_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency :%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, spl it)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq( self, seq, split = T >temp_73|source:-*- coding: utf-8 -*-|start:2024|stop:2524 .lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_ pointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_u nique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib. sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_h ash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1el se:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.se q,'count': 1}self.un >temp_74|source:-*- coding: utf-8 -*-|start:289|stop:789 .## Please read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path):self.output_file_path = output_ file_pathself.output_file_obj = open(output_file_path, 'w')def store(self, entry , split = True, store_frequencies = True):if entry.unique and store_frequencies: self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.wr ite_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.outp ut_file_obj.write('> >temp_75|source:-*- coding: utf-8 -*-|start:3852|stop:4352 0)def visualize_sequence_length_distribution(self, title, dest = None, max_seq_l en = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltim port matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self. next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq ))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequenc e_lengths) + (int(ma >temp_76|source:-*- coding: utf-8 -*-|start:1074|stop:1574 n range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class Re adFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or s elf.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequenc es.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close( )class SequenceSourc >temp_77|source:-*- coding: utf-8 -*-|start:5772|stop:6272 ),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_length s),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.sav efig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__( self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_paths elf.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int >temp_78|source:-*- coding: utf-8 -*-|start:4220|stop:4720 ence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_s eq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:s eq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.upda te({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidt h=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.s ubplots_adjust(left= >temp_79|source:-*- coding: utf-8 -*-|start:3507|stop:4007 p()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next() :if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_poin ter.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(self, titl e, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import m atplotlib.pyplot as >temp_80|source:-*- coding: utf-8 -*-|start:6535|stop:7035 f.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequals cores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscor es):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals. split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.fi le_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + li ne.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self. quals.split()]self.p >temp_81|source:-*- coding: utf-8 -*-|start:3474|stop:3974 (line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_ id):self.reset()while self.next():if self.id == read_id:return self.seqreturn Fa lsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequen ce_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = N >temp_82|source:-*- coding: utf-8 -*-|start:576|stop:1076 try.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:% d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, split )def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(se lf, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write( '%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(s equence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:tic ks[x + 1]] for x in >temp_83|source:-*- coding: utf-8 -*-|start:653|stop:1153 y:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, sp lit)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq (self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.wri te('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, le n(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]: ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_fil e_obj.close()class R >temp_84|source:-*- coding: utf-8 -*-|start:1488|stop:1988 .fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class Sequenc eSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = None self.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0se lf.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_di ct = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = op en(self.fasta_file_p >temp_85|source:-*- coding: utf-8 -*-|start:3572|stop:4072 er()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()wh ile self.next():if self.id == read_id:return self.seqreturn Falsedef close(self) :self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribu tion(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequ ence_lengths = []sel >temp_86|source:-*- coding: utf-8 -*-|start:2470|stop:2970 id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.uniq ue_hash_dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)sel f.reset()def next(self):if self.unique:return self.next_unique()else:return self .next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_has h_list[self.pos]]sel >temp_87|source:-*- coding: utf-8 -*-|start:1816|stop:2316 Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}s elf.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self. fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = None else:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startsw ith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash (self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest ()if hash in self.un >temp_88|source:-*- coding: utf-8 -*-|start:2179|stop:2679 nit_unique_hash()def init_unique_hash(self):while self.next_regular():hash = has hlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.uni que_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] + = 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': se lf.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_ha sh_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True )]self.total_unique >temp_89|source:-*- coding: utf-8 -*-|start:994|stop:1494 ngth) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x i n range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class Re adFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or s elf.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequenc es.append(self.fasta >temp_90|source:-*- coding: utf-8 -*-|start:6189|stop:6689 lf.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_i nt = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_po inter.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = le n([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()de f next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = None self.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()i f not line:if len(qu >temp_91|source:-*- coding: utf-8 -*-|start:3887|stop:4387 tribution(self, title, dest = None, max_seq_len = None, xtickstep = None, yticks tep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspe csequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stder r.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n' )if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_leng ths) / 100.0) or 10) >temp_92|source:-*- coding: utf-8 -*-|start:652|stop:1152 cy:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, s plit)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_se q(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.wr ite('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, l en(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x] :ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_fi le_obj.close()class >temp_93|source:-*- coding: utf-8 -*-|start:6086|stop:6586 nit__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file _pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.q uals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals _file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneels e:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswi th('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].s trip()self.quals = N >temp_94|source:-*- coding: utf-8 -*-|start:77|stop:577 ; you can redistribute it and/or modify it under# the terms of the GNU General P ublic License as published by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COP YING file.import sysimport numpyimport hashlibclass FastaOutput:def __init__(sel f, output_file_path):self.output_file_path = output_file_pathself.output_file_ob j = open(output_file_path, 'w')def store(self, entry, split = True, store_freque ncies = True):if ent >temp_95|source:-*- coding: utf-8 -*-|start:4799|stop:5299 lor = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_d istribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequenc es')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt .xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-smal l')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in ran ge(0, max(seq_len_di >temp_96|source:-*- coding: utf-8 -*-|start:4534|stop:5034 s.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', li newidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True )plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot (seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, ma x_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt. ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:x tickstep = (max_seq_ >temp_97|source:-*- coding: utf-8 -*-|start:2400|stop:2900 ct[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sort ed([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_di ct], reverse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()d ef next(self):if self.unique:return self.next_unique()else:return self.next_regu lar()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique: >temp_98|source:-*- coding: utf-8 -*-|start:4075|stop:4575 eset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.wri te('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.ap pend(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_ len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_ distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribu tion[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linew idth' : 0.9})plt.rc( >temp_99|source:-*- coding: utf-8 -*-|start:4082|stop:4582 hile self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[ fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(le n(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = m ax(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distrib ution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', >temp_100|source:-*- coding: utf-8 -*-|start:5738|stop:6238 / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequ ence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alp ha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.sa vefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passre turnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self. quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself. pos = 0self.id = No >temp_101|source:-*- coding: utf-8 -*-|start:3633|stop:4133 _id):self.reset()while self.next():if self.id == read_id:return self.seqreturn F alsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_seque nce_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.grids pec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.p >temp_102|source:-*- coding: utf-8 -*-|start:5203|stop:5703 lt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0 , max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(se q_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'bla ck', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.updat e({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0. 02, 0.5, 'total: %s >temp_103|source:-*- coding: utf-8 -*-|start:1177|stop:1677 lf, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)w hile self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys. stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys .stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.se q)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSource() :def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mix ed_case = False):sel >temp_104|source:-*- coding: utf-8 -*-|start:4937|stop:5437 bel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtic kstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_dist ribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rota tion=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytic kstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx- small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_l en_distribution) + ( >temp_105|source:-*- coding: utf-8 -*-|start:6413|stop:6913 total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>') ])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()s elf.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_poi nter.readline()if not line:if len(qualscores):self.quals = qualscores.strip()sel f.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:r eturn Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell () - len(line))break >temp_106|source:-*- coding: utf-8 -*-|start:2939|stop:3439 .unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.al low_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regula r(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequenc e = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):se lf.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>' ):self.file_pointer. >temp_107|source:-*- coding: utf-8 -*-|start:1069|stop:1569 r x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()cla ss ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fas ta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory : %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.se quences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.c lose()class Sequence >temp_108|source:-*- coding: utf-8 -*-|start:5357|stop:5857 = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + ( max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams .update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.t ext(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(s equence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min( sequence_lengths),\m >temp_109|source:-*- coding: utf-8 -*-|start:60|stop:560 is free software; you can redistribute it and/or modify it under# the terms of the GNU General Public License as published by the Free# Software Foundation; ei ther version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput :def __init__(self, output_file_path):self.output_file_path = output_file_pathse lf.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = T rue, store_frequenci >temp_110|source:-*- coding: utf-8 -*-|start:2570|stop:3070 unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], rever se = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(se lf):if self.unique:return self.next_unique()else:return self.next_regular()def n ext_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.tot al_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]sel f.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry[ 'seq'].upper()self.i >temp_111|source:-*- coding: utf-8 -*-|start:404|stop:904 ut_file_path):self.output_file_path = output_file_pathself.output_file_obj = ope n(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entr y.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_fil e_obj.write('%s\n' % >temp_112|source:-*- coding: utf-8 -*-|start:6211|stop:6711 itself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = [] self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.l azy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.fil e_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Noneq ualscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qual scores):self.quals = >temp_113|source:-*- coding: utf-8 -*-|start:2037|stop:2537 lf.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readl ines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_hash()d ef init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq .upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash ]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.uniqu e_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1} self.unique_hash_lis >temp_114|source:-*- coding: utf-8 -*-|start:1716|stop:2216 .name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_casese lf.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.un ique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_ pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:s elf.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.read lines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_hash() def init_unique_hash >temp_115|source:-*- coding: utf-8 -*-|start:178|stop:678 ished by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysi mport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path): self.output_file_path = output_file_pathself.output_file_obj = open(output_file_ path, 'w')def store(self, entry, split = True, store_frequencies = True):if entr y.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))e >temp_116|source:-*- coding: utf-8 -*-|start:2939|stop:3439 .unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.al low_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regula r(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequenc e = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):se lf.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>' ):self.file_pointer. >temp_117|source:-*- coding: utf-8 -*-|start:6292|stop:6792 elf.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.la zy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file _pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequ alscores = ''while 1:line = self.file_pointer.readline()if not line:if len(quals cores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.qua ls.split()]self.pos >temp_118|source:-*- coding: utf-8 -*-|start:1211|stop:1711 ences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fas ta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(sel f.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close( self):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_pat h, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_p ath = fasta_file_pat >temp_119|source:-*- coding: utf-8 -*-|start:4751|stop:5251 , right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.f ill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'bl ack', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length' )if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:yt ickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq _len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_l en_distribution) + 1 >temp_120|source:-*- coding: utf-8 -*-|start:2611|stop:3111 r hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.u nique_hash_dict)self.reset()def next(self):if self.unique:return self.next_uniqu e()else:return self.next_regular()def next_unique(self):if self.unique:if self.t otal_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_d ict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id'] self.ids = hash_entr >temp_121|source:-*- coding: utf-8 -*-|start:6523|stop:7023 elf.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith( '>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) fo r q in self.quals.sp >temp_122|source:-*- coding: utf-8 -*-|start:492|stop:992 _file_path, 'w')def store(self, entry, split = True, store_frequencies = True):i f entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequen cy:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, s plit)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_se q(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.wr ite('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, l en(sequence), piece_ >temp_123|source:-*- coding: utf-8 -*-|start:2287|stop:2787 hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].a ppend(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dic t[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.uniqu e_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash )\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(se lf.unique_hash_dict)self.reset()def next(self):if self.unique:return self.next_u nique()else:return s >temp_124|source:-*- coding: utf-8 -*-|start:3540|stop:4040 low_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_ id(self, read_id):self.reset()while self.next():if self.id == read_id:return sel f.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self.p os = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def vi sualize_sequence_length_distribution(self, title, dest = None, max_seq_len = Non e, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport mat plotlib.gridspec as >temp_125|source:-*- coding: utf-8 -*-|start:5977|stop:6477 avefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passr eturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self .quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself .pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.f ile_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_in it:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_poin ter.readlines() if l >temp_126|source:-*- coding: utf-8 -*-|start:4044|stop:4544 specsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.st derr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write(' \n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_l engths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in se quence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))p lt.rcParams.update({ >temp_127|source:-*- coding: utf-8 -*-|start:3849|stop:4349 ek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_se q_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pl timport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while se lf.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastali b] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self. seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequ ence_lengths) + (int >temp_128|source:-*- coding: utf-8 -*-|start:5560|stop:6060 )ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(Fa lse)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / st d: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_length s), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\ va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_f ile_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.s how()except:passretu >temp_129|source:-*- coding: utf-8 -*-|start:5185|stop:5685 size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[ y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')p lt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distr ibution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (tit le), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9]) plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xti cks([])plt.text(0.02 >temp_130|source:-*- coding: utf-8 -*-|start:896|stop:1396 '%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(s equence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:tic ks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_o bj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.po s % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FAST A into memory: %s' % >temp_131|source:-*- coding: utf-8 -*-|start:1102|stop:1602 def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f _name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stder r.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stde rr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys .stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self, fasta >temp_132|source:-*- coding: utf-8 -*-|start:6026|stop:6526 png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_fi le_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Non eself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.qual s_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file _pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset( )def next(self):self >temp_133|source:-*- coding: utf-8 -*-|start:179|stop:679 shed by the Free# Software Foundation; either version 2 of the License, or (at y our option)# any later version.## Please read the docs/COPYING file.import sysim port numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path):s elf.output_file_path = output_file_pathself.output_file_obj = open(output_file_p ath, 'w')def store(self, entry, split = True, store_frequencies = True):if entry .unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))el >temp_134|source:-*- coding: utf-8 -*-|start:1091|stop:1591 cks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __ini t__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_ name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.po s))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fa sta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSo urce():def __init__( >temp_135|source:-*- coding: utf-8 -*-|start:1048|stop:1548 s[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.outpu t_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.s equences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self. fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Read ing FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append( self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def clo se(self):self.fasta. >temp_136|source:-*- coding: utf-8 -*-|start:5376|stop:5876 q_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distrib ution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = ' xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edge color' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'tot al: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),nu mpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\ max(sequence_lengths >temp_137|source:-*- coding: utf-8 -*-|start:6658|stop:7158 readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.qua ls_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()se lf.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef c lose(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Nones elf.quals = Noneself >temp_138|source:-*- coding: utf-8 -*-|start:720|stop:1220 entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id) def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output _file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([seque nce[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):se lf.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = [ >temp_139|source:-*- coding: utf-8 -*-|start:3913|stop:4413 t = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplot lib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self .reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.w rite('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths. append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_se q_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_le n_distribution = [0] >temp_140|source:-*- coding: utf-8 -*-|start:5714|stop:6214 f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_ lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengt hs)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.f asta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try :plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.la zy_init = lazy_inits >temp_141|source:-*- coding: utf-8 -*-|start:3043|stop:3543 _entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']retu rn Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = Nones elf.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = sel f.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.po s += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek (self.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = se quence if self.allow >temp_142|source:-*- coding: utf-8 -*-|start:5672|stop:6172 plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% ( len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths), \min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + ' .pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass Qual Source:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_pat h = quals_file_paths >temp_143|source:-*- coding: utf-8 -*-|start:4999|stop:5499 kstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1 , xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distr ibution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, yti ckstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0 , ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figt ext(0.5, 0.96, '%s' >temp_144|source:-*- coding: utf-8 -*-|start:550|stop:1050 _frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self. write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>% s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)s elf.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.j oin([sequence[ticks[ >temp_145|source:-*- coding: utf-8 -*-|start:6519|stop:7019 f):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals _int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line :if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startsw ith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualsc ores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q ) for q in self.qual >temp_146|source:-*- coding: utf-8 -*-|start:6156|stop:6656 quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open( self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readli ne()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:lin e = self.file_pointe >temp_147|source:-*- coding: utf-8 -*-|start:6387|stop:6887 tal_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readli nes() if l.startswith('>')])self.reset()def next(self):self.id = self.file_point er.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''wh ile 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.qua ls = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self .pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.s eek(self.file_pointe >temp_148|source:-*- coding: utf-8 -*-|start:6350|stop:6850 nter.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len ([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Nones elf.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [in t(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif lin e.startswith('>'):se >temp_149|source:-*- coding: utf-8 -*-|start:6099|stop:6599 uals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.nam e = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Nonese lf.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)se lf.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_ quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self .reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.qu als = Noneself.quals >temp_150|source:-*- coding: utf-8 -*-|start:4070|stop:4570 elf.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stder r.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengt hs.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max _seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq _len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_dis tribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes. linewidth' : 0.9})pl >temp_151|source:-*- coding: utf-8 -*-|start:544|stop:1044 store_frequencies = True):if entry.unique and store_frequencies:self.write_id(' %s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id )self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.wri te('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split (seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_len gth = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ >temp_152|source:-*- coding: utf-8 -*-|start:773|stop:1273 ile_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence , piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequen ce)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(tick s) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init_ _(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_na me)while self.fasta. >temp_153|source:-*- coding: utf-8 -*-|start:4184|stop:4684 % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset ()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq _len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure( figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', c olor='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt .subplot(gs[0:8])plt >temp_154|source:-*- coding: utf-8 -*-|start:189|stop:689 e Free# Software Foundation; either version 2 of the License, or (at your option )# any later version.## Please read the docs/COPYING file.import sysimport numpy import hashlibclass FastaOutput:def __init__(self, output_file_path):self.output _file_path = output_file_pathself.output_file_obj = open(output_file_path, 'w')d ef store(self, entry, split = True, store_frequencies = True):if entry.unique an d store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entr y.ids)))else:self.wr >temp_155|source:-*- coding: utf-8 -*-|start:6586|stop:7086 oneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline ()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals _int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(sel f):self.file_pointer >temp_156|source:-*- coding: utf-8 -*-|start:4386|stop:4886 )seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len _distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'a xes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1) gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplot s_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_dis tribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribu >temp_157|source:-*- coding: utf-8 -*-|start:3523|stop:4023 quence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == r ead_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def r eset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_poin ter.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplo >temp_158|source:-*- coding: utf-8 -*-|start:1826|stop:2326 seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique _hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file _path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self. total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')]) self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):whi le self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_ >temp_159|source:-*- coding: utf-8 -*-|start:5618|stop:6118 lor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total : %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),nump y.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\ma x(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == No ne:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(d est + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, laz >temp_160|source:-*- coding: utf-8 -*-|start:5436|stop:5936 (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParam s.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt. text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len( sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min (sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = ' x-large')if dest == >temp_161|source:-*- coding: utf-8 -*-|start:3343|stop:3843 eq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):se lf.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line. strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.po s += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.ne xt():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_ pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.id s = []self.file_poin >temp_162|source:-*- coding: utf-8 -*-|start:554|stop:1054 quencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % ( entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.writ e_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self. output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80): ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join( [sequence[ticks[x]:t >temp_163|source:-*- coding: utf-8 -*-|start:970|stop:1470 len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[ x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_ file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.seq uences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fa sta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Readin g FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(se lf.fasta.id)self.seq >temp_164|source:-*- coding: utf-8 -*-|start:2|stop:502 v.140216# Copyright (C) 2014, A. Murat Eren## This program is free software; you can redistribute it and/or modify it under# the terms of the GNU General Public License as published by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput:def __init__(self, ou tput_file_path):self.output_file_path = output_file_pathself.output_file_obj = o pen(output_file_path >temp_165|source:-*- coding: utf-8 -*-|start:2772|stop:3272 ()else:return self.next_regular()def next_unique(self):if self.unique:if self.to tal_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_di ct[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']s elf.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next _regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip() sequence = ''while 1 >temp_166|source:-*- coding: utf-8 -*-|start:2811|stop:3311 xt_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.tota l_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self .pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry[' seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Truee lse:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_p ointer.readline()if >temp_167|source:-*- coding: utf-8 -*-|start:1058|stop:1558 [x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj .close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta .id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):s elf.fasta.close()cla >temp_168|source:-*- coding: utf-8 -*-|start:6840|stop:7340 th('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualsco res += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self):self.file_p ointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.q uals_int = Noneself.ids = []self.file_pointer.seek(0)if __name__ == '__main__':f asta = SequenceSource(sys.argv[1])fasta.visualize_sequence_length_distribution(t itle = sys.argv[2] i >temp_169|source:-*- coding: utf-8 -*-|start:5118|stop:5618 t.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-sma ll')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in ra nge(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (m ax(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams. update({'axes.edgeco >temp_170|source:-*- coding: utf-8 -*-|start:1303|stop:1803 0 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)s elf.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.f asta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_ file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow _mixed_caseself.pos >temp_171|source:-*- coding: utf-8 -*-|start:3153|stop:3653 eturn Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.rea dline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if no t line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return F alseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - le n(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_cas e else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read _id):self.reset()whi >temp_172|source:-*- coding: utf-8 -*-|start:1984|stop:2484 le_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:sel f.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>') ])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):w hile self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if has h in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)sel f.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [sel >temp_173|source:-*- coding: utf-8 -*-|start:6666|stop:7166 ()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals _int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(sel f):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.qual s = Noneself.quals_i >temp_174|source:-*- coding: utf-8 -*-|start:6071|stop:6571 lSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_pa th = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.i d = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_ quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines( ) if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.r eadline()[1:].strip( >temp_175|source:-*- coding: utf-8 -*-|start:1794|stop:2294 self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself. unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.fil e_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init :self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.re adlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_hash ()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self. seq.upper()).hexdige >temp_176|source:-*- coding: utf-8 -*-|start:2933|stop:3433 t[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if s elf.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']se lf.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_ regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()s equence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequen ce):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswi th('>'):self.file_po >temp_177|source:-*- coding: utf-8 -*-|start:1515|stop:2015 ('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self , fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):s elf.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_inits elf.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Non eself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_lis t = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)sel f.file_pointer.seek( >temp_178|source:-*- coding: utf-8 -*-|start:3029|stop:3529 case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_en try['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):se lf.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''whil e 1:line = self.file_pointer.readline()if not line:if len(sequence):self.seq = s equenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.fil e_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip( )self.seq = sequence >temp_179|source:-*- coding: utf-8 -*-|start:1300|stop:1800 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA in to memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.i d)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):sel f.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_i nit = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fas ta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = al low_mixed_caseself.p >temp_180|source:-*- coding: utf-8 -*-|start:3666|stop:4166 ):if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_poi nter.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(self, tit le, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.s tderr.write('\r[fast >temp_181|source:-*- coding: utf-8 -*-|start:6549|stop:7049 .readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''whil e 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.p os += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.see k(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self .quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()] self.pos += 1return >temp_182|source:-*- coding: utf-8 -*-|start:1852|stop:2352 f.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique _next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.se ek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l i n self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique :self.init_unique_hash()def init_unique_hash(self):while self.next_regular():has h = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:s elf.unique_hash_dict >temp_183|source:-*- coding: utf-8 -*-|start:6292|stop:6792 elf.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.la zy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file _pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequ alscores = ''while 1:line = self.file_pointer.readline()if not line:if len(quals cores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.qua ls.split()]self.pos >temp_184|source:-*- coding: utf-8 -*-|start:5401|stop:5901 ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtex t(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center') ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(Fal se)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std : %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths ), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\v a = 'center', alpha >temp_185|source:-*- coding: utf-8 -*-|start:3252|stop:3752 sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(seque nce):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startsw ith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequen ce += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upp er()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()wh ile self.next():if self.id == read_id:return self.seqreturn Falsedef close(self) :self.file_pointer.c >temp_186|source:-*- coding: utf-8 -*-|start:2212|stop:2712 hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdi gest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append (self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[has h] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_has h_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.un ique_hash_dict)self. >temp_187|source:-*- coding: utf-8 -*-|start:1084|stop:1584 len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:de f __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSo urce(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta. pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.f asta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append( self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class Seq uenceSource():def __ >temp_188|source:-*- coding: utf-8 -*-|start:5564|stop:6064 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False) plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: % .2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_ pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show( )except:passreturncl >temp_189|source:-*- coding: utf-8 -*-|start:5863|stop:6363 uence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:de st = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals _file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.q uals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.f ile_pointer.seek(0)i >temp_190|source:-*- coding: utf-8 -*-|start:1051|stop:1551 ]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_f ile_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequ ences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fas ta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(sel f.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close( self):self.fasta.clo >temp_191|source:-*- coding: utf-8 -*-|start:40|stop:540 Eren## This program is free software; you can redistribute it and/or modify it under# the terms of the GNU General Public License as published by the Free# Sof tware Foundation; either version 2 of the License, or (at your option)# any late r version.## Please read the docs/COPYING file.import sysimport numpyimport hash libclass FastaOutput:def __init__(self, output_file_path):self.output_file_path = output_file_pathself.output_file_obj = open(output_file_path, 'w')def store(se lf, entry, split = T >temp_192|source:-*- coding: utf-8 -*-|start:1373|stop:1873 ASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self. fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(se lf):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_pat h = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_ca se = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self .unique = uniqueself >temp_193|source:-*- coding: utf-8 -*-|start:4516|stop:5016 6, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[ 0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, rig ht = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_b etween(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if x tickstep == None:xti >temp_194|source:-*- coding: utf-8 -*-|start:402|stop:902 tput_file_path):self.output_file_path = output_file_pathself.output_file_obj = o pen(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id , 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(en try.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)de f write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_f ile_obj.write('%s\n' >temp_195|source:-*- coding: utf-8 -*-|start:5825|stop:6325 ngths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0. 8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(d est + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturncla ss QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_f ile_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0 self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_poin ter = open(self.qual >temp_196|source:-*- coding: utf-8 -*-|start:2080|stop:2580 n([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_r egular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique _hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_di ct[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sort ed([(self.unique_has >temp_197|source:-*- coding: utf-8 -*-|start:2509|stop:3009 unt': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[ha sh]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.tot al_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.unique: return self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entr y = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] >temp_198|source:-*- coding: utf-8 -*-|start:281|stop:781 version.## Please read the docs/COPYING file.import sysimport numpyimport hashl ibclass FastaOutput:def __init__(self, output_file_path):self.output_file_path = output_file_pathself.output_file_obj = open(output_file_path, 'w')def store(sel f, entry, split = True, store_frequencies = True):if entry.unique and store_freq uencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else :self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):s elf.output_file_obj. >temp_199|source:-*- coding: utf-8 -*-|start:3945|stop:4445 ickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotl ib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():if sel f.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset( )sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_ len + 1)for l in seq >temp_200|source:-*- coding: utf-8 -*-|start:1602|stop:2102 _file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fas ta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.all ow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.i ds = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []s elf.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_ pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len( [l for l in self.fil >temp_201|source:-*- coding: utf-8 -*-|start:5863|stop:6363 uence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:de st = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals _file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.q uals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.f ile_pointer.seek(0)i >temp_202|source:-*- coding: utf-8 -*-|start:3319|stop:3819 :if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):s elf.reset()while self.next():if self.id == read_id:return self.seqreturn Falsede f close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = No neself.seq = Nonesel >temp_203|source:-*- coding: utf-8 -*-|start:2200|stop:2700 init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.up per()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash][' ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_h ash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}sel f.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count' ], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.unique_has >temp_204|source:-*- coding: utf-8 -*-|start:2082|stop:2582 [l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if s elf.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_reg ular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_h ash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict [hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [s elf.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted ([(self.unique_hash_ >temp_205|source:-*- coding: utf-8 -*-|start:4289|stop:4789 f not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_length s) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequenc e_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rc Params.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='- ', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid (True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt .plot(seq_len_distri >temp_206|source:-*- coding: utf-8 -*-|start:6498|stop:6998 f.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.q uals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer. readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.qua ls_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()se lf.quals_int = [int( >temp_207|source:-*- coding: utf-8 -*-|start:5633|stop:6133 grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %. 2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence _lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_leng ths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self. fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')tr y:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path , lazy_init = True): >temp_208|source:-*- coding: utf-8 -*-|start:1380|stop:1880 to memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.i d)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):sel f.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_i nit = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fas ta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = al low_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique >temp_209|source:-*- coding: utf-8 -*-|start:3981|stop:4481 port matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_leng ths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1: sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()seque nce_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_se q_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:se q_len_distribution[l >temp_210|source:-*- coding: utf-8 -*-|start:1887|stop:2387 ict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = o pen(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_s eq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_un ique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()). hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].a ppend(self.id)self.u >temp_211|source:-*- coding: utf-8 -*-|start:6506|stop:7006 )def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = N oneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline ()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals _int = [int(q) for q >temp_212|source:-*- coding: utf-8 -*-|start:2409|stop:2909 'count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id] ,'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self .unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reve rse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(s elf):if self.unique:return self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.to tal_unique:hash_entr >temp_213|source:-*- coding: utf-8 -*-|start:4671|stop:5171 t(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95 , right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.f ill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'bl ack', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length' )if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:yt ickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq _len + 1, xtickstep) >temp_214|source:-*- coding: utf-8 -*-|start:4726|stop:5226 bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = ' black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribu tion, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt .xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks (range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt. yticks(range(0, max( >temp_215|source:-*- coding: utf-8 -*-|start:4674|stop:5174 s[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, r ight = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill _between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black ', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytick step = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_le n + 1, xtickstep), r >temp_216|source:-*- coding: utf-8 -*-|start:1262|stop:1762 self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stder r.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stde rr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys .stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_ca se = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.all >temp_217|source:-*- coding: utf-8 -*-|start:5015|stop:5515 ickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_di stribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), ro tation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, yt ickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='x x-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq _len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, ' %s' % (title), weigh >temp_218|source:-*- coding: utf-8 -*-|start:5845|stop:6345 _lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large') if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except: plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __ init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_fil e_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself. quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.qual s_file_path)self.fil >temp_219|source:-*- coding: utf-8 -*-|start:2788|stop:3288 lf.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 a nd self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_h ash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed _case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_e ntry['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):s elf.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''whi le 1:line = self.fil >temp_220|source:-*- coding: utf-8 -*-|start:917|stop:1417 t(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_lengt h) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in r ange(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadF asta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = Seq uenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self .fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys >temp_221|source:-*- coding: utf-8 -*-|start:684|stop:1184 lf.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self .output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if sp lit:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [l en(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:de f __init__(self, f_n >temp_222|source:-*- coding: utf-8 -*-|start:5763|stop:6263 e_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequen ce_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large ')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')excep t:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_f ile_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Nonesel f.quals = Noneself.q >temp_223|source:-*- coding: utf-8 -*-|start:1873|stop:2373 .unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.fi le_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_ini t:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.r eadlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_has h()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self .seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[ hash]['ids'].append( >temp_224|source:-*- coding: utf-8 -*-|start:1866|stop:2366 queself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0 self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.l azy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_po inter.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_uni que_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sh a1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_has h_dict[hash]['ids']. >temp_225|source:-*- coding: utf-8 -*-|start:6079|stop:6579 def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = qua ls_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Non eself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(sel f.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.s tartswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline( )[1:].strip()self.qu >temp_226|source:-*- coding: utf-8 -*-|start:4709|stop:5209 djust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distri bution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max _seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, siz e='xx-small')plt.yti >temp_227|source:-*- coding: utf-8 -*-|start:1461|stop:1961 )self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self .fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_in it = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fast a_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = all ow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_has h = 0self.file_point >temp_228|source:-*- coding: utf-8 -*-|start:5973|stop:6473 lt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:p assreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True): self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_init self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []se lf.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.laz y_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_ pointer.readlines() >temp_229|source:-*- coding: utf-8 -*-|start:4253|stop:4753 )self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence _lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.r c('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, >temp_230|source:-*- coding: utf-8 -*-|start:3650|stop:4150 while self.next():if self.id == read_id:return self.seqreturn Falsedef close(sel f):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distri bution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecse quence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or sel f.pos == 1:sys.stder >temp_231|source:-*- coding: utf-8 -*-|start:2429|stop:2929 lf.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'co unt': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[ha sh]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.tot al_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.unique: return self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entr y = self.unique_hash >temp_232|source:-*- coding: utf-8 -*-|start:4934|stop:5434 ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:x tickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_d istribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), r otation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, y tickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size=' xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(se q_len_distribution) >temp_233|source:-*- coding: utf-8 -*-|start:3195|stop:3695 = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:li ne = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequenc eself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_poin ter.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip()self. seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next():if self .id == read_id:retur >temp_234|source:-*- coding: utf-8 -*-|start:1242|stop:1742 Source(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fast a.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self .fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.appen d(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class S equenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = F alse, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init >temp_235|source:-*- coding: utf-8 -*-|start:6612|stop:7112 alscores = ''while 1:line = self.file_pointer.readline()if not line:if len(quals cores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.qua ls.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self .file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in se lf.quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close ()def reset(self):se >temp_236|source:-*- coding: utf-8 -*-|start:1229|stop:1729 ta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory : %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.se quences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.c lose()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = Tru e, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_p athself.name = Nones >temp_237|source:-*- coding: utf-8 -*-|start:3739|stop:4239 ile_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Nonesel f.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(se lf, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None): import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_le ngths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()seq uence_lengths.append >temp_238|source:-*- coding: utf-8 -*-|start:4410|stop:4910 [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})pl t.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(1 0, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bott om = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'blac k', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution , y2 = 0, color = 'b >temp_239|source:-*- coding: utf-8 -*-|start:6355|stop:6855 seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l f or l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next (self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.q uals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.sta rtswith('>'):self.fi >temp_240|source:-*- coding: utf-8 -*-|start:1507|stop:2007 rr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __ini t__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = la zy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.s eq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_ hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_ path)self.file_point >temp_241|source:-*- coding: utf-8 -*-|start:2285|stop:2785 ).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'] .append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_d ict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.uni que_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], ha sh)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len( self.unique_hash_dict)self.reset()def next(self):if self.unique:return self.next _unique()else:return >temp_242|source:-*- coding: utf-8 -*-|start:2331|stop:2831 self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['c ount'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],' seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.u nique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], revers e = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(sel f):if self.unique:return self.next_unique()else:return self.next_regular()def ne xt_unique(self):if s >temp_243|source:-*- coding: utf-8 -*-|start:1940|stop:2440 sh = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.f ile_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.in it_unique_hash()def init_unique_hash(self):while self.next_regular():hash = hash lib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.uniq ue_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_h >temp_244|source:-*- coding: utf-8 -*-|start:1708|stop:2208 pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixe d_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqu eself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0se lf.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.laz y_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_poin ter.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_uniqu e_hash()def init_uni >temp_245|source:-*- coding: utf-8 -*-|start:4062|stop:4562 hs = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:s ys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequen ce_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq _len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) o r 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq _len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update ({'axes.linewidth' : >temp_246|source:-*- coding: utf-8 -*-|start:3068|stop:3568 .id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falsee lse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointe r.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline() if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:ret urn Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixe d_case else sequence >temp_247|source:-*- coding: utf-8 -*-|start:868|stop:1368 )self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n' .join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def cl ose(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name) :self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.f asta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.writ e('\r[fastalib] Read >temp_248|source:-*- coding: utf-8 -*-|start:1400|stop:1900 lf.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.app end(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.nam e = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.p os = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique _hash_dict = {}self. >temp_249|source:-*- coding: utf-8 -*-|start:6457|stop:6957 ter.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self. file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalsco res = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores ):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.sp lit()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file _pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line .strip()self.quals = >temp_250|source:-*- coding: utf-8 -*-|start:4640|stop:5140 GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left= 0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, col or = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_di stribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequence s')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt. xticks(range(xtickst >temp_251|source:-*- coding: utf-8 -*-|start:6787|stop:7287 .pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.s eek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()se lf.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split( )]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(sel f):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = [ ]self.file_pointer.seek(0)if __name__ == '__main__':fasta = SequenceSource(sys.a rgv[1])fasta.visuali >temp_252|source:-*- coding: utf-8 -*-|start:1923|stop:2423 lf.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_p ointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([ l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if se lf.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_regu lar():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_ha sh_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[ hash]['count'] += 1e >temp_253|source:-*- coding: utf-8 -*-|start:1221|stop:1721 self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1 000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA int o memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id )self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self .fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_in it = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fast a_file_pathself.name >temp_254|source:-*- coding: utf-8 -*-|start:3386|stop:3886 eturn Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell () - len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mi xed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(sel f, read_id):self.reset()while self.next():if self.id == read_id:return self.seqr eturn Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0 self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualiz e_sequence_length_di >temp_255|source:-*- coding: utf-8 -*-|start:3774|stop:4274 self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0) def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimpo rt matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.ne xt():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Re ading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq)) self.reset()sys.stde >temp_256|source:-*- coding: utf-8 -*-|start:1183|stop:1683 name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while s elf.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr .write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stder r.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys. stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def _ _init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_cas e = False):self.fast >temp_257|source:-*- coding: utf-8 -*-|start:4459|stop:4959 seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.upd ate({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewid th=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt. subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_ len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq _len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabe l('number of sequenc >temp_258|source:-*- coding: utf-8 -*-|start:4202|stop:4702 stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write ('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence _lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12) )plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', lines tyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])p lt.grid(True)plt.sub >temp_259|source:-*- coding: utf-8 -*-|start:4278|stop:4778 rite('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequ ence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', l inestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0: 8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq >temp_260|source:-*- coding: utf-8 -*-|start:2375|stop:2875 lf.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_l ist = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for ha sh in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.uniqu e_hash_dict)self.reset()def next(self):if self.unique:return self.next_unique()e lse:return self.next_regular()def next_unique(self):if self.unique:if self.total _unique > 0 and self >temp_261|source:-*- coding: utf-8 -*-|start:1709|stop:2209 athself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed _caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = unique self.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0sel f.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy _init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_point er.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique _hash()def init_uniq >temp_262|source:-*- coding: utf-8 -*-|start:3141|stop:3641 Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file _pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.rea dline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Truee lse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer .tell() - len(line))breaksequence += line.strip()self.seq = sequence if self.all ow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_i d(self, read_id):sel >temp_263|source:-*- coding: utf-8 -*-|start:1016|stop:1516 ]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(s elf, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name) while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys .stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sy s.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.s eq)sys.stderr.write( >temp_264|source:-*- coding: utf-8 -*-|start:3129|stop:3629 eelse:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file _pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1 return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self. file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_s eq_by_read_id(self, >temp_265|source:-*- coding: utf-8 -*-|start:2536|stop:3036 st = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for has h in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.unique _hash_dict)self.reset()def next(self):if self.unique:return self.next_unique()el se:return self.next_regular()def next_unique(self):if self.unique:if self.total_ unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[s elf.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self .allow_mixed_case el >temp_266|source:-*- coding: utf-8 -*-|start:5141|stop:5641 p, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_dist ribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len )plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution ) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-la rge', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor ' : 20})plt.grid(Fal >temp_267|source:-*- coding: utf-8 -*-|start:324|stop:824 le.import sysimport numpyimport hashlibclass FastaOutput:def __init__(self, outp ut_file_path):self.output_file_path = output_file_pathself.output_file_obj = ope n(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entr y.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, >temp_268|source:-*- coding: utf-8 -*-|start:2098|stop:2598 .file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:self. init_unique_hash()def init_unique_hash(self):while self.next_regular():hash = ha shlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.un ique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': s elf.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_h ash_dict[hash]['coun >temp_269|source:-*- coding: utf-8 -*-|start:3780|stop:4280 os = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def vi sualize_sequence_length_distribution(self, title, dest = None, max_seq_len = Non e, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport mat plotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():i f self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.r eset()sys.stderr.wri >temp_270|source:-*- coding: utf-8 -*-|start:6744|stop:7244 = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return False if line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(li ne))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.qua ls_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(s elf):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.qu als = Noneself.quals_int = Noneself.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta >temp_271|source:-*- coding: utf-8 -*-|start:1951|stop:2451 file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_i nit:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer .readlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_h ash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(se lf.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dic t[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self .unique_hash_dict[ha >temp_272|source:-*- coding: utf-8 -*-|start:2712|stop:3212 reset()def next(self):if self.unique:return self.next_unique()else:return self.n ext_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and s elf.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_ list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_cas e else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry ['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self. seq = Noneself.id = >temp_273|source:-*- coding: utf-8 -*-|start:2713|stop:3213 eset()def next(self):if self.unique:return self.next_unique()else:return self.ne xt_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and se lf.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_l ist[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry[ 'ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self.s eq = Noneself.id = s >temp_274|source:-*- coding: utf-8 -*-|start:6370|stop:6870 lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.fi le_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = None qualscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qua lscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.q uals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):se lf.file_pointer.seek >temp_275|source:-*- coding: utf-8 -*-|start:5250|stop:5750 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],si ze='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = ma x(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0. 96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt .subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yt icks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% >temp_276|source:-*- coding: utf-8 -*-|start:532|stop:1032 plit = True, store_frequencies = True):if entry.unique and store_frequencies:sel f.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write _id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_ file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequenc e, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(seque nce)]return '\n'.joi >temp_277|source:-*- coding: utf-8 -*-|start:2391|stop:2891 e_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.i d,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.uniqu e_hash_dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)self .reset()def next(self):if self.unique:return self.next_unique()else:return self. next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.tota >temp_278|source:-*- coding: utf-8 -*-|start:5577|stop:6077 t(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([] )plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(seq uence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', al pha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.s avefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passr eturnclass QualSourc >temp_279|source:-*- coding: utf-8 -*-|start:6215|stop:6715 lf.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self .file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_ init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_po inter.readlines() if l.startswith('>')])self.reset()def next(self):self.id = sel f.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequals cores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscor es):self.quals = qua >temp_280|source:-*- coding: utf-8 -*-|start:905|stop:1405 seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1] ] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close( )class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self .fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into me mory: %s' % (self.fa >temp_281|source:-*- coding: utf-8 -*-|start:3510|stop:4010 self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1r eturn Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointer .close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []s elf.file_pointer.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matp lotlib.pyplot as plt >temp_282|source:-*- coding: utf-8 -*-|start:4997|stop:5497 ickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickste p = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_dis tribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, y tickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.fi gtext(0.5, 0.96, '%s >temp_283|source:-*- coding: utf-8 -*-|start:5949|stop:6449 elf.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png ')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_ path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Nonese lf.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_i nt = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_po inter.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = le n([l for l in self.f >temp_284|source:-*- coding: utf-8 -*-|start:6819|stop:7319 Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - l en(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()sel f.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef cl ose(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Nonese lf.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer.seek(0)if __n ame__ == '__main__':fasta = SequenceSource(sys.argv[1])fasta.visualize_sequence_ length_distribution( >temp_285|source:-*- coding: utf-8 -*-|start:6145|stop:6645 ile_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0 self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_poin ter = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self. total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.read lines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_poi nter.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = '' while 1:line = self. >temp_286|source:-*- coding: utf-8 -*-|start:5314|stop:5814 , ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymi n = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt .figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'c enter')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.g rid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2 f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_ lengths), numpy.std( >temp_287|source:-*- coding: utf-8 -*-|start:4065|stop:4565 = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys. stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_ lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_le n:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 1 0)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_le n_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({' axes.linewidth' : 0. >temp_288|source:-*- coding: utf-8 -*-|start:2704|stop:3204 ct)self.reset()def next(self):if self.unique:return self.next_unique()else:retur n self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.uniq ue_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_m ixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = ha sh_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular(sel f):self.seq = Nonese >temp_289|source:-*- coding: utf-8 -*-|start:3202|stop:3702 self.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = se lf.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.p os += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.see k(self.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = s equence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truede f get_seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == read_id:return self. >temp_290|source:-*- coding: utf-8 -*-|start:1327|stop:1827 == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fast a.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(sel f.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class Sequen ceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Non eself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0s elf.id = Noneself.s >temp_291|source:-*- coding: utf-8 -*-|start:5461|stop:5961 ) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-la rge', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor ' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: % s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.m ean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(s equence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None: dest = self.fasta_fi >temp_292|source:-*- coding: utf-8 -*-|start:517|stop:1017 (self, entry, split = True, store_frequencies = True):if entry.unique and store_ frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids))) else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, i d):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True ):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def spli t(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_lengt h) + [len(sequence)] >temp_293|source:-*- coding: utf-8 -*-|start:516|stop:1016 e(self, entry, split = True, store_frequencies = True):if entry.unique and store _frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)) )else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = Tru e):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def spl it(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_leng th) + [len(sequence) >temp_294|source:-*- coding: utf-8 -*-|start:690|stop:1190 te_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.outpu t_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:se q = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, seque nce, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(seq uence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(t icks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __in it__(self, f_name):s >temp_295|source:-*- coding: utf-8 -*-|start:2326|stop:2826 dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[has h]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self. id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(s elf.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], r everse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def nex t(self):if self.unique:return self.next_unique()else:return self.next_regular()d ef next_unique(self) >temp_296|source:-*- coding: utf-8 -*-|start:1047|stop:1547 ks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.outp ut_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self. sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self .fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Rea ding FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append (self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def cl ose(self):self.fasta >temp_297|source:-*- coding: utf-8 -*-|start:6232|stop:6732 = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = o pen(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_q uals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.re adline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1 :line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()se >temp_298|source:-*- coding: utf-8 -*-|start:5035|stop:5535 en / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx- small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xm in = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weigh t = 'black', size = >temp_299|source:-*- coding: utf-8 -*-|start:6768|stop:7268 .quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'): self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q i n self.quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.c lose()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = S equenceSource(sys.ar >temp_300|source:-*- coding: utf-8 -*-|start:5063|stop:5563 None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, m ax(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distri bution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)p lt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-larg e', ha = 'center')ax >temp_301|source:-*- coding: utf-8 -*-|start:2670|stop:3170 l_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.unique:r eturn self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.i d = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseels e:return Falsedef ne >temp_302|source:-*- coding: utf-8 -*-|start:6176|stop:6676 name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Non eself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path )self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.tot al_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])s elf.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self .quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointe r.readline()if not l >temp_303|source:-*- coding: utf-8 -*-|start:5219|stop:5719 0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_di stribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_l en)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distributi on) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx- large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecol or' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / s >temp_304|source:-*- coding: utf-8 -*-|start:3939|stop:4439 ne, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport ma tplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next(): if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading : %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self. reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengt hs) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (ma x_seq_len + 1)for l >temp_305|source:-*- coding: utf-8 -*-|start:4551|stop:5051 inewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adj ust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribu tion, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), s eq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_s eq_len / 50) or 1if >temp_306|source:-*- coding: utf-8 -*-|start:4839|stop:5339 een(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', al pha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtic kstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1 , xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distr ibution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, yti ckstep)],size='xx-sm >temp_307|source:-*- coding: utf-8 -*-|start:6457|stop:6957 ter.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self. file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalsco res = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores ):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.sp lit()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file _pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line .strip()self.quals = >temp_308|source:-*- coding: utf-8 -*-|start:6575|stop:7075 f.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_point er.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self. quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:ret urn Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip( )self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truede f close(self):self.f >temp_309|source:-*- coding: utf-8 -*-|start:258|stop:758 your option)# any later version.## Please read the docs/COPYING file.import sysi mport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path): self.output_file_path = output_file_pathself.output_file_obj = open(output_file_ path, 'w')def store(self, entry, split = True, store_frequencies = True):if entr y.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, split)d ef write_id(self, id >temp_310|source:-*- coding: utf-8 -*-|start:1668|stop:2168 alse):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = laz y_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.se q = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_h ash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_p ath)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.to tal_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])se lf.reset()if self.un >temp_311|source:-*- coding: utf-8 -*-|start:4263|stop:4763 t()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_se q_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure (figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = pl t.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, to p = 0.95, right = 0. >temp_312|source:-*- coding: utf-8 -*-|start:1098|stop:1598 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(sel f, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)wh ile self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.s tderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys. stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq )sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSource(): def __init__(self, f >temp_313|source:-*- coding: utf-8 -*-|start:2790|stop:3290 .next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_has h_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_c ase else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_ent ry['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):sel f.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_ >temp_314|source:-*- coding: utf-8 -*-|start:5702|stop:6202 / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.me an(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(se quence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:d est = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, qual s_file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init >temp_315|source:-*- coding: utf-8 -*-|start:4387|stop:4887 seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_ distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'ax es.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)g s = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots _adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_dist ribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1 ), seq_len_distribut >temp_316|source:-*- coding: utf-8 -*-|start:5927|stop:6427 dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:pl t.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __in it__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_ pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.qu als = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_ file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse :self.total_quals = >temp_317|source:-*- coding: utf-8 -*-|start:2128|stop:2628 .startswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_uni que_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).h exdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].ap pend(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict [hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique _hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash) \for hash in self.un >temp_318|source:-*- coding: utf-8 -*-|start:2873|stop:3373 lf.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_l ist[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry[ 'ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self.s eq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1: line = self.file_pointer.readline()if not line:if len(sequence):self.seq = seque nceself.pos += 1retu >temp_319|source:-*- coding: utf-8 -*-|start:5750|stop:6250 (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths ),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, si ze = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass Qu alSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_p ath = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self. id = Noneself.quals >temp_320|source:-*- coding: utf-8 -*-|start:6123|stop:6623 t = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself. ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)i f self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self):s elf.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = >temp_321|source:-*- coding: utf-8 -*-|start:460|stop:960 lf.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = T rue, store_frequencies = True):if entry.unique and store_frequencies:self.write_ id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entr y.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj .write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.s plit(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece _length = 80):ticks >temp_322|source:-*- coding: utf-8 -*-|start:5890|stop:6390 er', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtr y:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()excep t:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = Tru e):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_i nitself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = [ ]self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self. lazy_init:self.total >temp_323|source:-*- coding: utf-8 -*-|start:3447|stop:3947 f.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequen ce if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get _seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == read_ id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset (self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer. seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_ seq_len = None, xtic >temp_324|source:-*- coding: utf-8 -*-|start:4203|stop:4703 tderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write( '\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_ lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in s equence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12)) plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linest yle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])pl t.grid(True)plt.subp >temp_325|source:-*- coding: utf-8 -*-|start:372|stop:872 taOutput:def __init__(self, output_file_path):self.output_file_path = output_fil e_pathself.output_file_obj = open(output_file_path, 'w')def store(self, entry, s plit = True, store_frequencies = True):if entry.unique and store_frequencies:sel f.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write _id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_ file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)sel >temp_326|source:-*- coding: utf-8 -*-|start:5983|stop:6483 (dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnc lass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals _file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_po inter = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:sel f.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.re adlines() if l.start >temp_327|source:-*- coding: utf-8 -*-|start:5146|stop:5646 x_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max( seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribut ion) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt. ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 2 0.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 2 0})plt.grid(False)pl >temp_328|source:-*- coding: utf-8 -*-|start:5267|stop:5767 for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt .xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distrib ution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title ), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])pl t.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xtick s([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s '\% (len(sequence_le >temp_329|source:-*- coding: utf-8 -*-|start:2517|stop:3017 self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['cou nt'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_uniqu e = len(self.unique_hash_dict)self.reset()def next(self):if self.unique:return s elf.next_unique()else:return self.next_regular()def next_unique(self):if self.un ique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self .unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_e ntry['seq'] if self. >temp_330|source:-*- coding: utf-8 -*-|start:675|stop:1175 ))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = Tr ue):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def sp lit(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_len gth) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class Rea dFasta:def __init__( >temp_331|source:-*- coding: utf-8 -*-|start:2344|stop:2844 ash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1el se:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.se q,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_di ct[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]sel f.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.un ique:return self.next_unique()else:return self.next_regular()def next_unique(sel f):if self.unique:if >temp_332|source:-*- coding: utf-8 -*-|start:6746|stop:7246 [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals _int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(sel f):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.qual s = Noneself.quals_int = Noneself.ids = []self.file_pointer.seek(0)if __name__ = = '__main__':fasta = >temp_333|source:-*- coding: utf-8 -*-|start:2078|stop:2578 len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset() if self.unique:self.init_unique_hash()def init_unique_hash(self):while self.next _regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.uniq ue_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_ dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids' : [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in so rted([(self.unique_h >temp_334|source:-*- coding: utf-8 -*-|start:726|stop:1226 seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def wr ite_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_ obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = rang e(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ti cks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.out put_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self .sequences = []self. >temp_335|source:-*- coding: utf-8 -*-|start:3082|stop:3582 try['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Fal sedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1 :].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif li ne.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))b reaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else se quence.upper()self.p >temp_336|source:-*- coding: utf-8 -*-|start:5373|stop:5873 _seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_dist ribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.e dgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, ' total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths) ,numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths ),\max(sequence_leng >temp_337|source:-*- coding: utf-8 -*-|start:3494|stop:3994 += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper ()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()whil e self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):s elf.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = No neself.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distributi on(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = N one):import matplotl >temp_338|source:-*- coding: utf-8 -*-|start:2720|stop:3220 ef next(self):if self.unique:return self.next_unique()else:return self.next_regu lar()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[sel f.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else h ash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']r eturn Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = No neself.id = self.fil >temp_339|source:-*- coding: utf-8 -*-|start:4292|stop:4792 ot max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_l engths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcPar ams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(Tr ue)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.pl ot(seq_len_distribut >temp_340|source:-*- coding: utf-8 -*-|start:1355|stop:1855 astalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()sel f.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write ('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self , fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):s elf.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_inits elf.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Non eself.ids = []self.u >temp_341|source:-*- coding: utf-8 -*-|start:1232|stop:1732 = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: % s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.seque nces.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.clos e()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_path self.name = Noneself >temp_342|source:-*- coding: utf-8 -*-|start:5654|stop:6154 ([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std( sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:pl t.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:pa ssreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):s elf.quals_file_path >temp_343|source:-*- coding: utf-8 -*-|start:4973|stop:5473 ('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if yt ickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range( xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks( range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_ len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max _seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_dist ribution) / 20.0))pl >temp_344|source:-*- coding: utf-8 -*-|start:2207|stop:2707 ique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()). hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].a ppend(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dic t[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.uniqu e_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash )\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(se lf.unique_hash_dict) >temp_345|source:-*- coding: utf-8 -*-|start:6777|stop:7277 lit()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file _pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line .strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.qu als.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nonese lf.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = SequenceSo urce(sys.argv[1])fas >temp_346|source:-*- coding: utf-8 -*-|start:4756|stop:5256 ht = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_b etween(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if x tickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickst ep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_di stribution) + 1, yti >temp_347|source:-*- coding: utf-8 -*-|start:1936|stop:2436 t_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0 )if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in se lf.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:sel f.init_unique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self. unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count' ] += 1else:self.uniq >temp_348|source:-*- coding: utf-8 -*-|start:3159|stop:3659 Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline( )[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line :if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):s elf.reset()while sel >temp_349|source:-*- coding: utf-8 -*-|start:1325|stop:1825 os == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fa sta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(s elf.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class Sequ enceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = Fals e, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = N oneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself >temp_350|source:-*- coding: utf-8 -*-|start:2701|stop:3201 _dict)self.reset()def next(self):if self.unique:return self.next_unique()else:re turn self.next_regular()def next_unique(self):if self.unique:if self.total_uniqu e > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.u nique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allo w_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular( self):self.seq = Non >temp_351|source:-*- coding: utf-8 -*-|start:4656|stop:5156 x1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0 .03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', al pha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('s equence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytick step == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xti ckstep, max_seq_len >temp_352|source:-*- coding: utf-8 -*-|start:880|stop:1380 _file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([seque nce[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):se lf.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next(): if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastal ib] Reading FASTA in >temp_353|source:-*- coding: utf-8 -*-|start:3321|stop:3821 f len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif l ine.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line)) breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else s equence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):sel f.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = None self.seq = Noneself. >temp_354|source:-*- coding: utf-8 -*-|start:3864|stop:4364 ize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, x tickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplot lib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():if se lf.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset ()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_l >temp_355|source:-*- coding: utf-8 -*-|start:3124|stop:3624 n Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = Nonese lf.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self .file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek( self.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = seq uence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(s >temp_356|source:-*- coding: utf-8 -*-|start:5720|stop:6220 d: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_length s), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\ va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_f ile_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.s how()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_ init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_ini t = lazy_initself.po >temp_357|source:-*- coding: utf-8 -*-|start:3790|stop:4290 .id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_se quence_length_distribution(self, title, dest = None, max_seq_len = None, xtickst ep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gr idspec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (se lf.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys. stderr.write('\n')if >temp_358|source:-*- coding: utf-8 -*-|start:3723|stop:4223 ose(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Nonese lf.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_length _distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, yt ickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gri dspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.s tderr.flush()sequenc >temp_359|source:-*- coding: utf-8 -*-|start:6080|stop:6580 ef __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = qual s_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = None self.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self .quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = N oneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.st artswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline() [1:].strip()self.qua >temp_360|source:-*- coding: utf-8 -*-|start:1871|stop:2371 lf.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self. file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_i nit:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer .readlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique_h ash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(se lf.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dic t[hash]['ids'].appen >temp_361|source:-*- coding: utf-8 -*-|start:6720|stop:7220 es.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1retu rn Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file _pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qu alscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_po inter.seek(0)if __na >temp_362|source:-*- coding: utf-8 -*-|start:6678|stop:7178 e:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.starts with('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breakquals cores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int( q) for q in self.quals.split()]self.pos += 1return Truedef close(self):self.file _pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself .quals_int = Nonesel >temp_363|source:-*- coding: utf-8 -*-|start:1865|stop:2365 iqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self. lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_p ointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_un ique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.s ha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_ha sh_dict[hash]['ids'] >temp_364|source:-*- coding: utf-8 -*-|start:6672|stop:7172 ot line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int( q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line. startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))brea kqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self):sel f.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = No neself.quals_int = N >temp_365|source:-*- coding: utf-8 -*-|start:3588|stop:4088 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next(): if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_point er.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = [ ]self.file_pointer.seek(0)def visualize_sequence_length_distribution(self, title , dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import ma tplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = [ ]self.reset()while s >temp_366|source:-*- coding: utf-8 -*-|start:3454|stop:3954 pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if s elf.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by _read_id(self, read_id):self.reset()while self.next():if self.id == read_id:retu rn self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self): self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0) def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = >temp_367|source:-*- coding: utf-8 -*-|start:1662|stop:2162 se = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Nones elf.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.un ique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_ file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:s elf.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('> ')])self.reset()if s >temp_368|source:-*- coding: utf-8 -*-|start:3952|stop:4452 = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.grid spec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self .pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.st derr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(ma x(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1 )for l in sequence_l >temp_369|source:-*- coding: utf-8 -*-|start:529|stop:1029 , split = True, store_frequencies = True):if entry.unique and store_frequencies: self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.wr ite_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.outp ut_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:s eq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequ ence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(se quence)]return '\n'. >temp_370|source:-*- coding: utf-8 -*-|start:1934|stop:2434 ext_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek (0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:s elf.init_unique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:sel f.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['coun t'] += 1else:self.un >temp_371|source:-*- coding: utf-8 -*-|start:1430|stop:1930 ()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr. write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__ (self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = Fal se):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_ initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_has h_list = []self.uniq >temp_372|source:-*- coding: utf-8 -*-|start:4275|stop:4775 r.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(s equence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)fo r l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = ( 16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50' , linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs [0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, ri ght = 0.98)plt.plot( >temp_373|source:-*- coding: utf-8 -*-|start:6108|stop:6608 _path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Nones elf.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_ int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_p ointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = l en([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()d ef next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Non eself.quals_int = No >temp_374|source:-*- coding: utf-8 -*-|start:1551|stop:2051 se()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pat hself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_c aseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniquese lf.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self. file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_i nit:self.total_seq = >temp_375|source:-*- coding: utf-8 -*-|start:4888|stop:5388 on, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.x label('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1 if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(r ange(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yt icks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max (seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.yl >temp_376|source:-*- coding: utf-8 -*-|start:465|stop:965 tput_file_obj = open(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.write_id('% s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id) self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.writ e('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split( seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_leng th = 80):ticks = ran >temp_377|source:-*- coding: utf-8 -*-|start:1131|stop:1631 ile_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequ ences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fas ta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(sel f.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close( self):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_pat h, lazy_init = True, >temp_378|source:-*- coding: utf-8 -*-|start:2749|stop:3249 return self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entr y = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self. id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseel se:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer .readline()[1:].stri >temp_379|source:-*- coding: utf-8 -*-|start:5536|stop:6036 xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edge color' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'tot al: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),nu mpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\ max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig (dest + '.png')try:p >temp_380|source:-*- coding: utf-8 -*-|start:2822|stop:3322 elf):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:ha sh_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1se lf.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper ()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_ pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.read line()if not line:if >temp_381|source:-*- coding: utf-8 -*-|start:3663|stop:4163 xt():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_ pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.id s = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):impo rt matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_length s = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sy s.stderr.write('\r[f >temp_382|source:-*- coding: utf-8 -*-|start:2075|stop:2575 = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.rese t()if self.unique:self.init_unique_hash()def init_unique_hash(self):while self.n ext_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.u nique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_ha sh_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'i ds': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.uniqu >temp_383|source:-*- coding: utf-8 -*-|start:3524|stop:4024 uence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == re ad_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def re set(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_point er.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, m ax_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplot >temp_384|source:-*- coding: utf-8 -*-|start:4832|stop:5332 ll_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'bla ck', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length') if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:yti ckstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_ len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_le n_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size >temp_385|source:-*- coding: utf-8 -*-|start:6551|stop:7051 eadline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek( self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.q uals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]se lf.pos += 1return Tr >temp_386|source:-*- coding: utf-8 -*-|start:1868|stop:2368 eself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0se lf.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.laz y_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_poin ter.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_uniqu e_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1 (self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_ dict[hash]['ids'].ap >temp_387|source:-*- coding: utf-8 -*-|start:6475|stop:6975 l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readl ine()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:li ne = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qua lscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self .file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip( >temp_388|source:-*- coding: utf-8 -*-|start:5894|stop:6394 alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:pl t.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:pa ssreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):s elf.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_inits elf.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []sel f.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy _init:self.total_qua >temp_389|source:-*- coding: utf-8 -*-|start:1423|stop:1923 r.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys. stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def _ _init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_cas e = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Nonese lf.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.uni que_hash_list = []se >temp_390|source:-*- coding: utf-8 -*-|start:3661|stop:4161 next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.fil e_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself. ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(self , title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):im port matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_leng ths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1: sys.stderr.write('\r >temp_391|source:-*- coding: utf-8 -*-|start:593|stop:1093 ore_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.id s)))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(sel f, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_l ength) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(tick >temp_392|source:-*- coding: utf-8 -*-|start:296|stop:796 ase read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOu tput:def __init__(self, output_file_path):self.output_file_path = output_file_pa thself.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.wr ite_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id( entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file _obj.write('>%s\n' % >temp_393|source:-*- coding: utf-8 -*-|start:751|stop:1251 elf, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)de f split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece _length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_ >temp_394|source:-*- coding: utf-8 -*-|start:1477|stop:1977 append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()cl ass SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, uniqu e = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself. name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_casesel f.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.uni que_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_p ointer = open(self.f >temp_395|source:-*- coding: utf-8 -*-|start:4437|stop:4937 l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16 , 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0 :8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, righ t = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_be tween(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.yla >temp_396|source:-*- coding: utf-8 -*-|start:140|stop:640 the GNU General Public License as published by the Free# Software Foundation; ei ther version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput :def __init__(self, output_file_path):self.output_file_path = output_file_pathse lf.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = T rue, store_frequencies = True):if entry.unique and store_frequencies:self.write_ id('%s|%s' % (entry. >temp_397|source:-*- coding: utf-8 -*-|start:4270|stop:4770 stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int( max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsiz e = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color=' 0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subpl ot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.9 5, right = 0.98)plt. >temp_398|source:-*- coding: utf-8 -*-|start:356|stop:856 hashlibclass FastaOutput:def __init__(self, output_file_path):self.output_file_p ath = output_file_pathself.output_file_obj = open(output_file_path, 'w')def stor e(self, entry, split = True, store_frequencies = True):if entry.unique and store _frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)) )else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = Tru e):if split:seq = se >temp_399|source:-*- coding: utf-8 -*-|start:1185|stop:1685 me):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while sel f.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.w rite('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr. flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.st derr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __i nit__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_ >temp_400|source:-*- coding: utf-8 -*-|start:5783|stop:6283 n(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(seq uence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:de st = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals _file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.q uals_int = Noneself. >temp_401|source:-*- coding: utf-8 -*-|start:2344|stop:2844 ash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1el se:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.se q,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_di ct[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]sel f.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.un ique:return self.next_unique()else:return self.next_regular()def next_unique(sel f):if self.unique:if >temp_402|source:-*- coding: utf-8 -*-|start:3528|stop:4028 e if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_ seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == read_i d:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset( self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.s eek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_s eq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as p ltimport matplotlib. >temp_403|source:-*- coding: utf-8 -*-|start:2754|stop:3254 n self.next_unique()else:return self.next_regular()def next_unique(self):if self .unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = s elf.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = has h_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:re turn Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.read line()[1:].strip()se >temp_404|source:-*- coding: utf-8 -*-|start:3932|stop:4432 en = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltim port matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self. next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq ))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequenc e_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0 ] * (max_seq_len + 1 >temp_405|source:-*- coding: utf-8 -*-|start:1758|stop:2258 .allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Nonese lf.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.f ile_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset() if self.unique:self.init_unique_hash()def init_unique_hash(self):while self.next _regular():hash = ha >temp_406|source:-*- coding: utf-8 -*-|start:1974|stop:2474 f.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = No neelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.start swith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_ha sh(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdige st()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(s elf.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,' >temp_407|source:-*- coding: utf-8 -*-|start:5259|stop:5759 tep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-sm all')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len _distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot( gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])p lt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(seq >temp_408|source:-*- coding: utf-8 -*-|start:5487|stop:5987 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt .yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), num py.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'c enter', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pat htry:plt.savefig(des >temp_409|source:-*- coding: utf-8 -*-|start:674|stop:1174 )))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self , id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = T rue):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def s plit(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_le ngth) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x i n range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class Re adFasta:def __init__ >temp_410|source:-*- coding: utf-8 -*-|start:5827|stop:6327 ths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(des t + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_fil e_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0se lf.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointe r = open(self.quals_ >temp_411|source:-*- coding: utf-8 -*-|start:560|stop:1060 es = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry. id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq( entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id) def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output _file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([seque nce[ticks[x]:ticks[x >temp_412|source:-*- coding: utf-8 -*-|start:1370|stop:1870 g FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(se lf.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close (self):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_pa th, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_ path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed _case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []s elf.unique = uniques >temp_413|source:-*- coding: utf-8 -*-|start:6780|stop:7280 ()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_po inter.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.st rip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals .split()]self.pos += 1return Truedef close(self):self.file_pointer.close()def re set(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself. ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = SequenceSourc e(sys.argv[1])fasta. >temp_414|source:-*- coding: utf-8 -*-|start:1924|stop:2424 f.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_po inter.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if sel f.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_regul ar():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_has h_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[h ash]['count'] += 1el >temp_415|source:-*- coding: utf-8 -*-|start:4302|stop:4802 _len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) o r 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq _len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update ({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth= 0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.sub plots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len _distribution, color >temp_416|source:-*- coding: utf-8 -*-|start:832|stop:1332 True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_ length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1 >temp_417|source:-*- coding: utf-8 -*-|start:2791|stop:3291 next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash _list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_ca se else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entr y['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self .seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_p >temp_418|source:-*- coding: utf-8 -*-|start:4728|stop:5228 ttom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'bl ack', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distributi on, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.x label('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1 if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(r ange(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yt icks(range(0, max(se >temp_419|source:-*- coding: utf-8 -*-|start:2071|stop:2571 _seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self. reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):while se lf.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in se lf.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.uniqu e_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.i d,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.u >temp_420|source:-*- coding: utf-8 -*-|start:5342|stop:5842 ')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_di stribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % ( title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[ 9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt. xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / ma x: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence _lengths),\min(seque >temp_421|source:-*- coding: utf-8 -*-|start:3472|stop:3972 en(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_ca se else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, rea d_id):self.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.i d = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequ ence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = >temp_422|source:-*- coding: utf-8 -*-|start:638|stop:1138 y.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_se q(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % i d)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.outp ut_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):tick s = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([seq uence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self): self.output_file_obj >temp_423|source:-*- coding: utf-8 -*-|start:2660|stop:3160 ]self.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if sel f.unique:return self.next_unique()else:return self.next_regular()def next_unique (self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique: hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1 self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upp er()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:retur n Falseelse:return F >temp_424|source:-*- coding: utf-8 -*-|start:309|stop:809 docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput:def __in it__(self, output_file_path):self.output_file_path = output_file_pathself.output _file_obj = open(output_file_path, 'w')def store(self, entry, split = True, stor e_frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s ' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self .write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('> %s\n' % id)def write >temp_425|source:-*- coding: utf-8 -*-|start:221|stop:721 her version 2 of the License, or (at your option)# any later version.## Please r ead the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput: def __init__(self, output_file_path):self.output_file_path = output_file_pathsel f.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = Tr ue, store_frequencies = True):if entry.unique and store_frequencies:self.write_i d('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry .id)self.write_seq(e >temp_426|source:-*- coding: utf-8 -*-|start:3682|stop:4182 read_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_poi nter.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplo t as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()w hile self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[ fastalib] Reading: % >temp_427|source:-*- coding: utf-8 -*-|start:4128|stop:4628 elf.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr .flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n') if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengt hs) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequen ce_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.r cParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle=' -', linewidth=0.1)gs >temp_428|source:-*- coding: utf-8 -*-|start:6201|stop:6701 = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nonesel f.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0 )if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l i n self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self) :self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_i nt = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:i f len(qualscores):se >temp_429|source:-*- coding: utf-8 -*-|start:12|stop:512 Copyright (C) 2014, A. Murat Eren## This program is free software; you can redis tribute it and/or modify it under# the terms of the GNU General Public License a s published by the Free# Software Foundation; either version 2 of the License, o r (at your option)# any later version.## Please read the docs/COPYING file.impor t sysimport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_ path):self.output_file_path = output_file_pathself.output_file_obj = open(output _file_path, 'w')def >temp_430|source:-*- coding: utf-8 -*-|start:4683|stop:5183 t.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0. 98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between( range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickste p == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = ma x(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xt ickstep), rotation=9 >temp_431|source:-*- coding: utf-8 -*-|start:345|stop:845 umpyimport hashlibclass FastaOutput:def __init__(self, output_file_path):self.ou tput_file_path = output_file_pathself.output_file_obj = open(output_file_path, ' w')def store(self, entry, split = True, store_frequencies = True):if entry.uniqu e and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len( entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def writ e_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if spl >temp_432|source:-*- coding: utf-8 -*-|start:5874|stop:6374 hs)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.f asta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try :plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.la zy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer .seek(0)if self.lazy >temp_433|source:-*- coding: utf-8 -*-|start:2274|stop:2774 seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[h ash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.un ique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash][' count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_un ique = len(self.unique_hash_dict)self.reset()def next(self):if self.unique:retur n self.next_unique() >temp_434|source:-*- coding: utf-8 -*-|start:3290|stop:3790 pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1r eturn Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.f ile_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_se q_by_read_id(self, read_id):self.reset()while self.next():if self.id == read_id: return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(se lf):self.pos = 0self >temp_435|source:-*- coding: utf-8 -*-|start:1507|stop:2007 rr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __ini t__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = la zy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.s eq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_ hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_ path)self.file_point >temp_436|source:-*- coding: utf-8 -*-|start:4565|stop:5065 9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridS pec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distrib ution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')pl t.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) o r 1if ytickstep == N >temp_437|source:-*- coding: utf-8 -*-|start:5704|stop:6204 mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean (sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequ ence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:des t = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_ file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = N oneself.lazy_init = >temp_438|source:-*- coding: utf-8 -*-|start:1104|stop:1604 f close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_n ame):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while se lf.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr. write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr .flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.s tderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __ init__(self, fasta_f >temp_439|source:-*- coding: utf-8 -*-|start:1234|stop:1734 SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or s elf.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequenc es.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close( )class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, un ique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathse lf.name = Noneself.l >temp_440|source:-*- coding: utf-8 -*-|start:5521|stop:6021 lack', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.upd ate({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text( 0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(seque nce_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequ ence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-lar ge')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')exc ept:plt.savefig(dest >temp_441|source:-*- coding: utf-8 -*-|start:1539|stop:2039 lf.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_ init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fa sta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = a llow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.uniqu e = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_h ash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self >temp_442|source:-*- coding: utf-8 -*-|start:5050|stop:5550 ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(ran ge(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.ytic ks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(s eq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_d istribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', si ze = 'xx-large', ha >temp_443|source:-*- coding: utf-8 -*-|start:1638|stop:2138 = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.n ame = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself .pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.uniq ue_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_po inter = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:sel f.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readli nes() if l.startswit >temp_444|source:-*- coding: utf-8 -*-|start:568|stop:1068 e):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'fre quency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.se q, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def writ e_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_ob j.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range( 0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[tick s[x]:ticks[x + 1]] f >temp_445|source:-*- coding: utf-8 -*-|start:2951|stop:3451 _list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_ca se else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entr y['ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self .seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):self.seq = seq uenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_ pointer.seek(self.fi >temp_446|source:-*- coding: utf-8 -*-|start:4873|stop:5373 _len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of s equences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq _len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 o r 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='x x-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim( xmin = 0, xmax = max >temp_447|source:-*- coding: utf-8 -*-|start:974|stop:1474 (sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:t icks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file _obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequenc es = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta. pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FA STA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.f asta.id)self.sequenc >temp_448|source:-*- coding: utf-8 -*-|start:4624|stop:5124 1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subpl ots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_d istribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('nu mber of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distributio n) / 20 or 1plt.xtic >temp_449|source:-*- coding: utf-8 -*-|start:2753|stop:3253 rn self.next_unique()else:return self.next_regular()def next_unique(self):if sel f.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = ha sh_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:r eturn Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.rea dline()[1:].strip()s >temp_450|source:-*- coding: utf-8 -*-|start:463|stop:963 output_file_obj = open(output_file_path, 'w')def store(self, entry, split = True , store_frequencies = True):if entry.unique and store_frequencies:self.write_id( '%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.i d)self.write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.wr ite('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.spli t(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_le ngth = 80):ticks = r >temp_451|source:-*- coding: utf-8 -*-|start:874|stop:1374 output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80): ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join( [sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(se lf):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self. ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.n ext():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[ fastalib] Reading FA >temp_452|source:-*- coding: utf-8 -*-|start:1744|stop:2244 lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Nonesel f.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.uniq ue_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_fi le_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:sel f.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>') ])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):w hile self.next_regul >temp_453|source:-*- coding: utf-8 -*-|start:1849|stop:2349 self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.uni que_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer .seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.uni que:self.init_unique_hash()def init_unique_hash(self):while self.next_regular(): hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dic t:self.unique_hash_d >temp_454|source:-*- coding: utf-8 -*-|start:4815|stop:5315 lpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel(' sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytic kstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xt ickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(ra nge(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_le n_distribution) + 1, >temp_455|source:-*- coding: utf-8 -*-|start:3243|stop:3743 ].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif lin e.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))br eaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else seq uence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self. reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef cl ose(self):self.file_ >temp_456|source:-*- coding: utf-8 -*-|start:2104|stop:2604 pointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_u nique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib. sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_h ash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1el se:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.se q,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_di ct[hash]['count'], h >temp_457|source:-*- coding: utf-8 -*-|start:6770|stop:7270 uals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):se lf.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.clo se()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = Seq uenceSource(sys.argv >temp_458|source:-*- coding: utf-8 -*-|start:4808|stop:5308 ack', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distributi on, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.x label('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1 if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(r ange(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yt icks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max (seq_len_distributio >temp_459|source:-*- coding: utf-8 -*-|start:4356|stop:4856 quence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (1 6, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[ 0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, rig ht = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_b etween(range(0, max_ >temp_460|source:-*- coding: utf-8 -*-|start:5293|stop:5793 len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max _seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_dist ribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.e dgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, ' total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths) ,numpy.mean(sequence >temp_461|source:-*- coding: utf-8 -*-|start:3543|stop:4043 _mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id( self, read_id):self.reset()while self.next():if self.id == read_id:return self.s eqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visua lize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplo tlib.gridspec as gri >temp_462|source:-*- coding: utf-8 -*-|start:6636|stop:7136 e = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qual scores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1 return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self. file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.po s += 1return Truedef close(self):self.file_pointer.close()def reset(self):self.p os = 0self.id = Non >temp_463|source:-*- coding: utf-8 -*-|start:1472|stop:1972 nces.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.clos e()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_path self.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_ca seself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniquesel f.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.f ile_pointer = open(s >temp_464|source:-*- coding: utf-8 -*-|start:6093|stop:6593 elf, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathse lf.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_p ath)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self. total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>') ])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()s elf.quals = Noneself >temp_465|source:-*- coding: utf-8 -*-|start:1979|stop:2479 ta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneels e:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith ('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(se lf):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()i f hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.i d)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {' id' : self.id,'ids': >temp_466|source:-*- coding: utf-8 -*-|start:1996|stop:2496 .file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset ()if self.unique:self.init_unique_hash()def init_unique_hash(self):while self.ne xt_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.un ique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_has h_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'id s': [self.id],'seq': >temp_467|source:-*- coding: utf-8 -*-|start:5992|stop:6492 .pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass Qual Source:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_pat h = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = o pen(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_q uals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>' >temp_468|source:-*- coding: utf-8 -*-|start:1642|stop:2142 alse, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_h ash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointe r = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.to tal_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines( ) if l.startswith('> >temp_469|source:-*- coding: utf-8 -*-|start:2029|stop:2529 _init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_point er.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_unique _hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1( self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_d ict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:se lf.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'co unt': 1}self.unique_ >temp_470|source:-*- coding: utf-8 -*-|start:5895|stop:6395 alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt .savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:pas sreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):se lf.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initse lf.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self .file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_ init:self.total_qual >temp_471|source:-*- coding: utf-8 -*-|start:1374|stop:1874 STA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.f asta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(sel f):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_cas e = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self. unique = uniqueself. >temp_472|source:-*- coding: utf-8 -*-|start:4264|stop:4764 ()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq _len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure( figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', c olor='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt .subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.9 >temp_473|source:-*- coding: utf-8 -*-|start:3588|stop:4088 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next(): if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_point er.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = [ ]self.file_pointer.seek(0)def visualize_sequence_length_distribution(self, title , dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import ma tplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = [ ]self.reset()while s >temp_474|source:-*- coding: utf-8 -*-|start:5668|stop:6168 ([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s' \% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengt hs),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file _path = quals_file_p >temp_475|source:-*- coding: utf-8 -*-|start:4856|stop:5356 seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.yl abel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xti ckstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_dis tribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rot ation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, yti ckstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx -small')plt.xlim(xmi >temp_476|source:-*- coding: utf-8 -*-|start:5974|stop:6474 t.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:pa ssreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):s elf.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_inits elf.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []sel f.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy _init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_p ointer.readlines() i >temp_477|source:-*- coding: utf-8 -*-|start:396|stop:896 lf, output_file_path):self.output_file_path = output_file_pathself.output_file_o bj = open(output_file_path, 'w')def store(self, entry, split = True, store_frequ encies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (en try.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_ seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.ou tput_file_obj.write( >temp_478|source:-*- coding: utf-8 -*-|start:2247|stop:2747 ):hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_d ict:self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash ]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.i d],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(se lf.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], re verse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next (self):if self.uniqu >temp_479|source:-*- coding: utf-8 -*-|start:6801|stop:7301 n Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_ pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qua lscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_poi nter.seek(0)if __name__ == '__main__':fasta = SequenceSource(sys.argv[1])fasta.v isualize_sequence_le >temp_480|source:-*- coding: utf-8 -*-|start:4050|stop:4550 quence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or sel f.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.f lush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths ) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence _lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcP arams.update({'axes. >temp_481|source:-*- coding: utf-8 -*-|start:4048|stop:4548 sequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or s elf.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr .flush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n') if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengt hs) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequen ce_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.r cParams.update({'axe >temp_482|source:-*- coding: utf-8 -*-|start:4892|stop:5392 y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabe l('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if y tickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range (xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks (range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq _len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = ma x_seq_len)plt.ylim(y >temp_483|source:-*- coding: utf-8 -*-|start:1226|stop:1726 fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta.pos % 1000 = = 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into mem ory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self .sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fast a.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_fil e_pathself.name = No >temp_484|source:-*- coding: utf-8 -*-|start:2579|stop:3079 sh_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True )]self.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if se lf.unique:return self.next_unique()else:return self.next_regular()def next_uniqu e(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique :hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].up per()self.id = hash >temp_485|source:-*- coding: utf-8 -*-|start:1568|stop:2068 eSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = None self.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0se lf.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_di ct = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = op en(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_se q = Noneelse:self.to >temp_486|source:-*- coding: utf-8 -*-|start:423|stop:923 output_file_path = output_file_pathself.output_file_obj = open(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.uni que and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % le n(entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def wr ite_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq , split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self >temp_487|source:-*- coding: utf-8 -*-|start:5693|stop:6193 total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths) ,numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths ),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.save fig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(s elf, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathse lf.name = Noneself.l >temp_488|source:-*- coding: utf-8 -*-|start:3048|stop:3548 y['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Tr ueelse:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.i d = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.fil e_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self .file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequenc e if self.allow_mixe >temp_489|source:-*- coding: utf-8 -*-|start:4219|stop:4719 uence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_ seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0 ) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths: seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.upd ate({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewid th=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt. subplots_adjust(left >temp_490|source:-*- coding: utf-8 -*-|start:4630|stop:5130 gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_ad just(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distrib ution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number o f sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_ seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 2 0 or 1plt.xticks(ran >temp_491|source:-*- coding: utf-8 -*-|start:2907|stop:3407 try = self.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.se q = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()sel f.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return False else:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_point er.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline( )if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:re turn Falseif line.st >temp_492|source:-*- coding: utf-8 -*-|start:2478|stop:2978 : [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in so rted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_ dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)self.reset( )def next(self):if self.unique:return self.next_unique()else:return self.next_re gular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.po s < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[s elf.pos]]self.pos += >temp_493|source:-*- coding: utf-8 -*-|start:3616|stop:4116 ead_id(self, read_id):self.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):se lf.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)de f visualize_sequence_length_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next ():if self.pos % 100 >temp_494|source:-*- coding: utf-8 -*-|start:5837|stop:6337 sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x -large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf' )except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSourc e:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = q uals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = N oneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(s elf.quals_file_path) >temp_495|source:-*- coding: utf-8 -*-|start:4387|stop:4887 seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_ distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'ax es.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)g s = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots _adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_dist ribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1 ), seq_len_distribut >temp_496|source:-*- coding: utf-8 -*-|start:207|stop:707 oundation; either version 2 of the License, or (at your option)# any later versi on.## Please read the docs/COPYING file.import sysimport numpyimport hashlibclas s FastaOutput:def __init__(self, output_file_path):self.output_file_path = outpu t_file_pathself.output_file_obj = open(output_file_path, 'w')def store(self, ent ry, split = True, store_frequencies = True):if entry.unique and store_frequencie s:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self. write_id(entry.id)se >temp_497|source:-*- coding: utf-8 -*-|start:1242|stop:1742 Source(f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fast a.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self .fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.appen d(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class S equenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = F alse, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init >temp_498|source:-*- coding: utf-8 -*-|start:4213|stop:4713 h()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if no t max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_le ngths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcPara ms.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', l inewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(Tru e)plt.subplots_adjus >temp_499|source:-*- coding: utf-8 -*-|start:4913|stop:5413 k', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')i f xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytic kstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_l en + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len _distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ym in = 0, ymax = max(s >temp_500|source:-*- coding: utf-8 -*-|start:2127|stop:2627 l.startswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_un ique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()). hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].a ppend(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dic t[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.uniqu e_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash )\for hash in self.u >temp_501|source:-*- coding: utf-8 -*-|start:2255|stop:2755 hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self .unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count '] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq' : self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.uniqu e_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(self):i f self.unique:return >temp_502|source:-*- coding: utf-8 -*-|start:5305|stop:5805 tion) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt .ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large' , ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean( sequence_lengths), n >temp_503|source:-*- coding: utf-8 -*-|start:4662|stop:5162 lt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, t op = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, co lor = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequenc e length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep = = None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep , max_seq_len + 1, x >temp_504|source:-*- coding: utf-8 -*-|start:3017|stop:3517 allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.i ds = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regu lar(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()seque nce = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence): self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith(' >'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip()self.se >temp_505|source:-*- coding: utf-8 -*-|start:4914|stop:5414 ', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytick step = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_le n + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_ distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1 , ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymi n = 0, ymax = max(se >temp_506|source:-*- coding: utf-8 -*-|start:3146|stop:3646 eelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_poin ter.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline ()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:r eturn Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell () - len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mi xed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(sel f, read_id):self.res >temp_507|source:-*- coding: utf-8 -*-|start:614|stop:1114 write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_i d(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_fi le_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequenc e)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks ) - 1)])def close(se >temp_508|source:-*- coding: utf-8 -*-|start:2836|stop:3336 nique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = sel f.unique_hash_dict[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_ entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = h ash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:retu rn Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readli ne()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not l ine:if len(sequence) >temp_509|source:-*- coding: utf-8 -*-|start:4432|stop:4932 )for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0. 50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot (gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fi ll_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'bla ck', alpha = 0.15)pl >temp_510|source:-*- coding: utf-8 -*-|start:6856|stop:7356 e_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + lin e.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.q uals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()de f reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nones elf.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = SequenceS ource(sys.argv[1])fasta.visualize_sequence_length_distribution(title = sys.argv[ 2] if len(sys.argv) >temp_511|source:-*- coding: utf-8 -*-|start:637|stop:1137 ry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_s eq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.out put_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):tic ks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([se quence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self) :self.output_file_ob >temp_512|source:-*- coding: utf-8 -*-|start:6854|stop:7354 ile_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + l ine.strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self .quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close() def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Non eself.ids = []self.file_pointer.seek(0)if __name__ == '__main__':fasta = Sequenc eSource(sys.argv[1])fasta.visualize_sequence_length_distribution(title = sys.arg v[2] if len(sys.argv >temp_513|source:-*- coding: utf-8 -*-|start:4983|stop:5483 length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, m ax(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distri bution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)p lt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext( >temp_514|source:-*- coding: utf-8 -*-|start:5721|stop:6221 : %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths ), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\v a = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_fi le_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.sh ow()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_i nit = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos >temp_515|source:-*- coding: utf-8 -*-|start:4176|stop:4676 ing: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))se lf.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_le ngths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt .figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc(' grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)a x1 = plt.subplot(gs[ >temp_516|source:-*- coding: utf-8 -*-|start:5962|stop:6462 e_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.sho w()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_in it = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself .ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0) if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.r >temp_517|source:-*- coding: utf-8 -*-|start:231|stop:731 n 2 of the License, or (at your option)# any later version.## Please read the do cs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput:def __init __(self, output_file_path):self.output_file_path = output_file_pathself.output_f ile_obj = open(output_file_path, 'w')def store(self, entry, split = True, store_ frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.w rite_seq(entry.seq, >temp_518|source:-*- coding: utf-8 -*-|start:4608|stop:5108 -', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.gri d(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)pl t.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range (0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.1 5)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq _len_distribution) / >temp_519|source:-*- coding: utf-8 -*-|start:4565|stop:5065 9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridS pec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distrib ution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')pl t.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) o r 1if ytickstep == N >temp_520|source:-*- coding: utf-8 -*-|start:1984|stop:2484 le_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:sel f.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>') ])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):w hile self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if has h in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)sel f.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [sel >temp_521|source:-*- coding: utf-8 -*-|start:6200|stop:6700 t = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nonese lf.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek( 0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(self ):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_ int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line: if len(qualscores):s >temp_522|source:-*- coding: utf-8 -*-|start:1398|stop:1898 self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.a ppend(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()cla ss SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.n ame = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself .pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.uniq ue_hash_dict = {}sel >temp_523|source:-*- coding: utf-8 -*-|start:4615|stop:5115 ewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True) plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot( seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max _seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.y label('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xt ickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_di stribution) / 20 or >temp_524|source:-*- coding: utf-8 -*-|start:2253|stop:2753 = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:se lf.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['cou nt'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'se q': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.uni que_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(self) :if self.unique:retu >temp_525|source:-*- coding: utf-8 -*-|start:2700|stop:3200 h_dict)self.reset()def next(self):if self.unique:return self.next_unique()else:r eturn self.next_regular()def next_unique(self):if self.unique:if self.total_uniq ue > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self. unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.all ow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular (self):self.seq = No >temp_526|source:-*- coding: utf-8 -*-|start:2147|stop:2647 elf.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):whil e self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash i n self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].append(self.id)self.u nique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : se lf.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.u nique_hash_dict], re >temp_527|source:-*- coding: utf-8 -*-|start:1817|stop:2317 Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}se lf.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.f asta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Nonee lse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswi th('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash( self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest( )if hash in self.uni >temp_528|source:-*- coding: utf-8 -*-|start:2942|stop:3442 ique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow _mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_regular(s elf):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):self. seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):s elf.file_pointer.see >temp_529|source:-*- coding: utf-8 -*-|start:4152|stop:4652 write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths .append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_s eq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_l en_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distr ibution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.li newidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = g ridspec.GridSpec(10, >temp_530|source:-*- coding: utf-8 -*-|start:5734|stop:6234 %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std( sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:pl t.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:pa ssreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):s elf.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_inits elf.pos = 0self.id >temp_531|source:-*- coding: utf-8 -*-|start:3738|stop:4238 file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Nonese lf.ids = []self.file_pointer.seek(0)def visualize_sequence_length_distribution(s elf, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None) :import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_l engths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()se quence_lengths.appen >temp_532|source:-*- coding: utf-8 -*-|start:1591|stop:2091 self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = Fals e):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_i nitself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash _list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path )self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total _seq = len([l for l >temp_533|source:-*- coding: utf-8 -*-|start:5361|stop:5861 , xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max( seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'b lack', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.upd ate({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text( 0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(seque nce_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequ ence_lengths),\max(s >temp_534|source:-*- coding: utf-8 -*-|start:4033|stop:4533 pec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self. pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.std err.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max (sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1) for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcPara >temp_535|source:-*- coding: utf-8 -*-|start:2051|stop:2551 Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.st artswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique _hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexd igest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].appen d(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[ha sh] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique_ha sh_list = [i[1] for >temp_536|source:-*- coding: utf-8 -*-|start:122|stop:622 der# the terms of the GNU General Public License as published by the Free# Softw are Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysimport numpyimport hashli bclass FastaOutput:def __init__(self, output_file_path):self.output_file_path = output_file_pathself.output_file_obj = open(output_file_path, 'w')def store(self , entry, split = True, store_frequencies = True):if entry.unique and store_frequ encies:self.write_id >temp_537|source:-*- coding: utf-8 -*-|start:717|stop:1217 eq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.out put_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):tic ks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([se quence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self) :self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences >temp_538|source:-*- coding: utf-8 -*-|start:4595|stop:5095 , linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs [0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, ri ght = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_ between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black' , alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:yticks tep = max(seq_len_di >temp_539|source:-*- coding: utf-8 -*-|start:3722|stop:4222 lose(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Nones elf.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_lengt h_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, y tickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gr idspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys. stderr.flush()sequen >temp_540|source:-*- coding: utf-8 -*-|start:939|stop:1439 e_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]re turn '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1 )])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self , f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)whi le self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.st derr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.s tderr.flush()self.id >temp_541|source:-*- coding: utf-8 -*-|start:1641|stop:2141 False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.po s = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_ hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_point er = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.t otal_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines () if l.startswith(' >temp_542|source:-*- coding: utf-8 -*-|start:1600|stop:2100 ta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.f asta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.a llow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself .ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = [ ]self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.fil e_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = le n([l for l in self.f >temp_543|source:-*- coding: utf-8 -*-|start:1168|stop:1668 init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource (f_name)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta .pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self .fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class Sequenc eSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = F >temp_544|source:-*- coding: utf-8 -*-|start:864|stop:1364 (seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_len gth = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])de f close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_n ame):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while se lf.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr. write('\r[fastalib] >temp_545|source:-*- coding: utf-8 -*-|start:4163|stop:4663 astalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len (self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = ma x(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribu tion = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.Gri dSpec(10, 1)ax1 = pl >temp_546|source:-*- coding: utf-8 -*-|start:2730|stop:3230 lf):if self.unique:return self.next_unique()else:return self.next_regular()def n ext_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.tot al_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]sel f.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry[ 'seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return True else:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer. >temp_547|source:-*- coding: utf-8 -*-|start:6669|stop:7169 f not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [i nt(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif li ne.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))b reakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.quals_in t = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self): self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int >temp_548|source:-*- coding: utf-8 -*-|start:6263|stop:6763 uals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.f ile_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_qual s = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.res et()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readl ine()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_in t = [int(q) for q in >temp_549|source:-*- coding: utf-8 -*-|start:1583|stop:2083 _init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_cas e = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Nonese lf.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.uni que_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_f ile_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:se lf.total_seq = len([ >temp_550|source:-*- coding: utf-8 -*-|start:3319|stop:3819 :if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line ))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):s elf.reset()while self.next():if self.id == read_id:return self.seqreturn Falsede f close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = No neself.seq = Nonesel >temp_551|source:-*- coding: utf-8 -*-|start:4673|stop:5173 gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fil l_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'blac k', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')i f xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytic kstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_l en + 1, xtickstep), >temp_552|source:-*- coding: utf-8 -*-|start:4983|stop:5483 length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, m ax(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distri bution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)p lt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext( >temp_553|source:-*- coding: utf-8 -*-|start:1748|stop:2248 y_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.se q = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_h ash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_p ath)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.to tal_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])se lf.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):while self.next_regular() >temp_554|source:-*- coding: utf-8 -*-|start:3081|stop:3581 ntry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Fa lsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[ 1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:i f len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif l ine.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line)) breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else s equence.upper()self. >temp_555|source:-*- coding: utf-8 -*-|start:5973|stop:6473 lt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:p assreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True): self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_init self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []se lf.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.laz y_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_ pointer.readlines() >temp_556|source:-*- coding: utf-8 -*-|start:4886|stop:5386 tion, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt .xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks (range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt. yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, m ax(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xma x = max_seq_len)plt. >temp_557|source:-*- coding: utf-8 -*-|start:2170|stop:2670 ue:self.init_unique_hash()def init_unique_hash(self):while self.next_regular():h ash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict :self.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash][' count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id], 'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self. unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], rever se = True)]self.tota >temp_558|source:-*- coding: utf-8 -*-|start:3081|stop:3581 ntry['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Fa lsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[ 1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:i f len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif l ine.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line)) breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else s equence.upper()self. >temp_559|source:-*- coding: utf-8 -*-|start:2404|stop:2904 ash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [sel f.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([ (self.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def n ext(self):if self.unique:return self.next_unique()else:return self.next_regular( )def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < se lf.total_unique:hash >temp_560|source:-*- coding: utf-8 -*-|start:5765|stop:6265 lengths),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence _lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large') if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except: plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __ init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_fil e_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself. quals = Noneself.qua >temp_561|source:-*- coding: utf-8 -*-|start:5770|stop:6270 hs),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_leng ths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if de st == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.s avefig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init_ _(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pat hself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_in >temp_562|source:-*- coding: utf-8 -*-|start:1365|stop:1865 eading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.appe nd(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self, fasta_fi le_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_ file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_ mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = un >temp_563|source:-*- coding: utf-8 -*-|start:5910|stop:6410 ze = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass Qu alSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_p ath = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self. id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total _quals = Noneelse:se >temp_564|source:-*- coding: utf-8 -*-|start:873|stop:1373 .output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80) :ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join ([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(s elf):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self .ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta. next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r [fastalib] Reading F >temp_565|source:-*- coding: utf-8 -*-|start:3040|stop:3540 ash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']r eturn Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = No neself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself .pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.s eek(self.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if self.al >temp_566|source:-*- coding: utf-8 -*-|start:3067|stop:3567 f.id = hash_entry['id']self.ids = hash_entry['ids']return Trueelse:return False else:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_point er.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer.readline( )if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:re turn Falseif line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell( ) - len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mix ed_case else sequenc >temp_567|source:-*- coding: utf-8 -*-|start:2045|stop:2545 _seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() i f l.startswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_ unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper() ).hexdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'] .append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_d ict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.uni que_hash_list = [i[1 >temp_568|source:-*- coding: utf-8 -*-|start:5998|stop:6498 except:plt.savefig(dest + '.png')try:plt.show()except:passreturnclass QualSource :def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = qu als_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = No neself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(se lf.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l. startswith('>')])sel >temp_569|source:-*- coding: utf-8 -*-|start:3470|stop:3970 len(line))breaksequence += line.strip()self.seq = sequence if self.allow_mixed_ case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, r ead_id):self.reset()while self.next():if self.id == read_id:return self.seqretur n Falsedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self .id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_se quence_length_distribution(self, title, dest = None, max_seq_len = None, xtickst ep = None, ytickstep >temp_570|source:-*- coding: utf-8 -*-|start:4489|stop:4989 g = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})p lt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec( 10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bot tom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'bla ck', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distributio n, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xl abel('sequence lengt >temp_571|source:-*- coding: utf-8 -*-|start:242|stop:742 License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysimport numpyimport hashlibclass FastaOutput:def __init__(self, ou tput_file_path):self.output_file_path = output_file_pathself.output_file_obj = o pen(output_file_path, 'w')def store(self, entry, split = True, store_frequencies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id , 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_seq(en try.seq, split)def w >temp_572|source:-*- coding: utf-8 -*-|start:1865|stop:2365 iqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.seek(0)if self. lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_p ointer.readlines() if l.startswith('>')])self.reset()if self.unique:self.init_un ique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.s ha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.unique_ha sh_dict[hash]['ids'] >temp_573|source:-*- coding: utf-8 -*-|start:6119|stop:6619 _init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_in it = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nones elf.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek (0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(sel f):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals _int = Nonequalscore >temp_574|source:-*- coding: utf-8 -*-|start:5329|stop:5829 ize='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = m ax(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0 .96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = pl t.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.y ticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy .std(sequence_length >temp_575|source:-*- coding: utf-8 -*-|start:1192|stop:1692 f.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta .next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\ r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush() self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.wr ite('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(s elf, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False ):self.fasta_file_pa >temp_576|source:-*- coding: utf-8 -*-|start:4849|stop:5349 0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15 )plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == N one:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_ len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickste p), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],s ize='xx-small')plt.x >temp_577|source:-*- coding: utf-8 -*-|start:28|stop:528 14, A. Murat Eren## This program is free software; you can redistribute it and/o r modify it under# the terms of the GNU General Public License as published by t he Free# Software Foundation; either version 2 of the License, or (at your optio n)# any later version.## Please read the docs/COPYING file.import sysimport nump yimport hashlibclass FastaOutput:def __init__(self, output_file_path):self.outpu t_file_path = output_file_pathself.output_file_obj = open(output_file_path, 'w') def store(self, entr >temp_578|source:-*- coding: utf-8 -*-|start:1744|stop:2244 lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Nonesel f.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.uniq ue_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_fi le_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:sel f.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>') ])self.reset()if self.unique:self.init_unique_hash()def init_unique_hash(self):w hile self.next_regul >temp_579|source:-*- coding: utf-8 -*-|start:5345|stop:5845 lt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distr ibution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (tit le), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9]) plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xti cks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(sequence_le ngths),\min(sequence >temp_580|source:-*- coding: utf-8 -*-|start:679|stop:1179 se:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id) :self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True): if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split( self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in ran ge(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFas ta:def __init__(self >temp_581|source:-*- coding: utf-8 -*-|start:973|stop:1473 n(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]: ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_fil e_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequen ces = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fasta .pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading F ASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self. fasta.id)self.sequen >temp_582|source:-*- coding: utf-8 -*-|start:5902|stop:6402 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.savefi g(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passreturn class QualSource:def __init__(self, quals_file_path, lazy_init = True):self.qual s_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_p ointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:se lf.total_quals = Non >temp_583|source:-*- coding: utf-8 -*-|start:5058|stop:5558 ep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtick step, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range (0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_d istribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_ len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribut ion) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx -large', ha = 'cente >temp_584|source:-*- coding: utf-8 -*-|start:22|stop:522 (C) 2014, A. Murat Eren## This program is free software; you can redistribute it and/or modify it under# the terms of the GNU General Public License as publishe d by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.import sysimpor t numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path):self .output_file_path = output_file_pathself.output_file_obj = open(output_file_path , 'w')def store(self >temp_585|source:-*- coding: utf-8 -*-|start:6242|stop:6742 lf.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.q uals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Non eelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.star tswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1 :].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = se lf.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores .strip()self.quals_i >temp_586|source:-*- coding: utf-8 -*-|start:171|stop:671 as published by the Free# Software Foundation; either version 2 of the License, or (at your option)# any later version.## Please read the docs/COPYING file.impo rt sysimport numpyimport hashlibclass FastaOutput:def __init__(self, output_file _path):self.output_file_path = output_file_pathself.output_file_obj = open(outpu t_file_path, 'w')def store(self, entry, split = True, store_frequencies = True): if entry.unique and store_frequencies:self.write_id('%s|%s' % (entry.id, 'freque ncy:%d' % len(entry. >temp_587|source:-*- coding: utf-8 -*-|start:3765|stop:4265 et(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointe r.seek(0)def visualize_sequence_length_distribution(self, title, dest = None, ma x_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot a s pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()whil e self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fas talib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(s elf.seq))self.reset( >temp_588|source:-*- coding: utf-8 -*-|start:3241|stop:3741 1:].strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:i f len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif l ine.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line)) breaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else s equence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):sel f.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.fil >temp_589|source:-*- coding: utf-8 -*-|start:4246|stop:4746 lf.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(s equence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distributio n = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9 })plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSp ec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = >temp_590|source:-*- coding: utf-8 -*-|start:1330|stop:1830 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.p os))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.f asta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceS ource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, al low_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Nonese lf.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self .id = Noneself.seq >temp_591|source:-*- coding: utf-8 -*-|start:3847|stop:4347 seek(0)def visualize_sequence_length_distribution(self, title, dest = None, max_ seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fasta lib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.append(len(sel f.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(se quence_lengths) + (i >temp_592|source:-*- coding: utf-8 -*-|start:799|stop:1299 )def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.outpu t_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequ ence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):s elf.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta.next() :if self.fasta.pos % >temp_593|source:-*- coding: utf-8 -*-|start:1732|stop:2232 .lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.i d = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(s elf.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.sta rtswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_ hash(self):while sel >temp_594|source:-*- coding: utf-8 -*-|start:4583|stop:5083 color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = pl t.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, to p = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0 .3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, col or = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = ma >temp_595|source:-*- coding: utf-8 -*-|start:5380|stop:5880 n)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distributio n) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-l arge', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolo r' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy. mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max( sequence_lengths)),\ >temp_596|source:-*- coding: utf-8 -*-|start:5659|stop:6159 lt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std(seque nce_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alph a = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:plt.sav efig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:passret urnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.q uals_file_path = qua >temp_597|source:-*- coding: utf-8 -*-|start:2261|stop:2761 ib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:self.uniqu e_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self .seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash _dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)] self.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if self .unique:return self. >temp_598|source:-*- coding: utf-8 -*-|start:3268|stop:3768 le 1:line = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.fi le_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip ()self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next(): if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_point er.close()def reset( >temp_599|source:-*- coding: utf-8 -*-|start:4034|stop:4534 ec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 1 0000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.p os))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.stde rr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max( sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)f or l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParam >temp_600|source:-*- coding: utf-8 -*-|start:3350|stop:3850 quenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file _pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip() self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1r eturn Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointer .close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []s elf.file_pointer.see >temp_601|source:-*- coding: utf-8 -*-|start:92|stop:592 tribute it and/or modify it under# the terms of the GNU General Public License a s published by the Free# Software Foundation; either version 2 of the License, o r (at your option)# any later version.## Please read the docs/COPYING file.impor t sysimport numpyimport hashlibclass FastaOutput:def __init__(self, output_file_ path):self.output_file_path = output_file_pathself.output_file_obj = open(output _file_path, 'w')def store(self, entry, split = True, store_frequencies = True):i f entry.unique and s >temp_602|source:-*- coding: utf-8 -*-|start:1851|stop:2351 lf.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.uniqu e_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.s eek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.uniqu e:self.init_unique_hash()def init_unique_hash(self):while self.next_regular():ha sh = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict: self.unique_hash_dic >temp_603|source:-*- coding: utf-8 -*-|start:3332|stop:3832 nce):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.startsw ith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequen ce += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.upp er()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset()wh ile self.next():if self.id == read_id:return self.seqreturn Falsedef close(self) :self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = []sel >temp_604|source:-*- coding: utf-8 -*-|start:452|stop:952 e_pathself.output_file_obj = open(output_file_path, 'w')def store(self, entry, s plit = True, store_frequencies = True):if entry.unique and store_frequencies:sel f.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.write _id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.output_ file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequenc e, piece_length = 80 >temp_605|source:-*- coding: utf-8 -*-|start:1132|stop:1632 le_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.seque nces = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.fast a.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self .fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(s elf):self.fasta.close()class SequenceSource():def __init__(self, fasta_file_path , lazy_init = True, >temp_606|source:-*- coding: utf-8 -*-|start:3619|stop:4119 _id(self, read_id):self.reset()while self.next():if self.id == read_id:return se lf.seqreturn Falsedef close(self):self.file_pointer.close()def reset(self):self. pos = 0self.id = Noneself.seq = Noneself.ids = []self.file_pointer.seek(0)def v isualize_sequence_length_distribution(self, title, dest = None, max_seq_len = No ne, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport ma tplotlib.gridspec as gridspecsequence_lengths = []self.reset()while self.next(): if self.pos % 10000 >temp_607|source:-*- coding: utf-8 -*-|start:478|stop:978 = open(output_file_path, 'w')def store(self, entry, split = True, store_frequen cies = True):if entry.unique and store_frequencies:self.write_id('%s|%s' % (entr y.id, 'frequency:%d' % len(entry.ids)))else:self.write_id(entry.id)self.write_se q(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>%s\n' % i d)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.outp ut_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):tick s = range(0, len(seq >temp_608|source:-*- coding: utf-8 -*-|start:439|stop:939 = output_file_pathself.output_file_obj = open(output_file_path, 'w')def store(s elf, entry, split = True, store_frequencies = True):if entry.unique and store_fr equencies:self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))el se:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id) :self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True): if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split( self, sequence, piec >temp_609|source:-*- coding: utf-8 -*-|start:5953|stop:6453 fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')tr y:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path , lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.l azy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointe r.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_ >temp_610|source:-*- coding: utf-8 -*-|start:4535|stop:5035 .update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', lin ewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True) plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot( seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(range(0, max _seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.y label('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xt ickstep = (max_seq_l >temp_611|source:-*- coding: utf-8 -*-|start:6038|stop:6538 .show()except:passreturnclass QualSource:def __init__(self, quals_file_path, laz y_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_i nit = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = None self.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.see k(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(se lf):self.id = self.f >temp_612|source:-*- coding: utf-8 -*-|start:5205|stop:5705 .yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xm ax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_ len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black ', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update( {'axes.edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02 , 0.5, 'total: %s / >temp_613|source:-*- coding: utf-8 -*-|start:6287|stop:6787 = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if se lf.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self .file_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self. id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = N onequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len( qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in sel f.quals.split()]self >temp_614|source:-*- coding: utf-8 -*-|start:5098|stop:5598 ibution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotat ion=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytick step),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-s mall')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_le n_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot (gs[9])plt.rcParams. >temp_615|source:-*- coding: utf-8 -*-|start:6321|stop:6821 quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = No neelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.sta rtswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[ 1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = s elf.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscore s.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1retur n Trueelse:return Fa >temp_616|source:-*- coding: utf-8 -*-|start:2874|stop:3374 f.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_li st[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry[' ids']return Trueelse:return Falseelse:return Falsedef next_regular(self):self.se q = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:l ine = self.file_pointer.readline()if not line:if len(sequence):self.seq = sequen ceself.pos += 1retur >temp_617|source:-*- coding: utf-8 -*-|start:1430|stop:1930 ()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr. write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__ (self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = Fal se):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_ initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_has h_list = []self.uniq >temp_618|source:-*- coding: utf-8 -*-|start:3041|stop:3541 sh_entry['seq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']re turn Trueelse:return Falseelse:return Falsedef next_regular(self):self.seq = Non eself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = s elf.file_pointer.readline()if not line:if len(sequence):self.seq = sequenceself. pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.se ek(self.file_pointer.tell() - len(line))breaksequence += line.strip()self.seq = sequence if self.all >temp_619|source:-*- coding: utf-8 -*-|start:1302|stop:1802 00 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id) self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self. fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_ini t = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta _file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allo w_mixed_caseself.pos >temp_620|source:-*- coding: utf-8 -*-|start:1457|stop:1957 a.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self): self.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, laz y_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.uni que = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next _hash = 0self.file_p >temp_621|source:-*- coding: utf-8 -*-|start:6014|stop:6514 ig(dest + '.png')try:plt.show()except:passreturnclass QualSource:def __init__(se lf, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathsel f.name = Noneself.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = N oneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_pa th)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.t otal_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')] )self.reset()def nex >temp_622|source:-*- coding: utf-8 -*-|start:2094|stop:2594 self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique:s elf.init_unique_hash()def init_unique_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dict:sel f.unique_hash_dict[hash]['ids'].append(self.id)self.unique_hash_dict[hash]['coun t'] += 1else:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq ': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.uniq ue_hash_dict[hash][' >temp_623|source:-*- coding: utf-8 -*-|start:1692|stop:2192 th = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_c ase = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []sel f.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique _next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.se ek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l i n self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique :self.init_unique_ha >temp_624|source:-*- coding: utf-8 -*-|start:4879|stop:5379 istribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequenc es')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt .xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-smal l')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in ran ge(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_l >temp_625|source:-*- coding: utf-8 -*-|start:4069|stop:4569 self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stde rr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_leng ths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:ma x_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)se q_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_di stribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes .linewidth' : 0.9})p >temp_626|source:-*- coding: utf-8 -*-|start:866|stop:1366 eq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_lengt h = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\ n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_nam e):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self .fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.wr ite('\r[fastalib] Re >temp_627|source:-*- coding: utf-8 -*-|start:1668|stop:2168 alse):self.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = laz y_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.se q = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_h ash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_p ath)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.to tal_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])se lf.reset()if self.un >temp_628|source:-*- coding: utf-8 -*-|start:2128|stop:2628 .startswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_uni que_hash(self):while self.next_regular():hash = hashlib.sha1(self.seq.upper()).h exdigest()if hash in self.unique_hash_dict:self.unique_hash_dict[hash]['ids'].ap pend(self.id)self.unique_hash_dict[hash]['count'] += 1else:self.unique_hash_dict [hash] = {'id' : self.id,'ids': [self.id],'seq': self.seq,'count': 1}self.unique _hash_list = [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash) \for hash in self.un >temp_629|source:-*- coding: utf-8 -*-|start:1326|stop:1826 s == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fas ta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(se lf.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class Seque nceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False , allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.name = No neself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0 self.id = Noneself. >temp_630|source:-*- coding: utf-8 -*-|start:4571|stop:5071 .rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10 , 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, botto m = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distribution, color = 'black ', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlab el('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:yt >temp_631|source:-*- coding: utf-8 -*-|start:1440|stop:1940 .append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n' )def close(self):self.fasta.close()class SequenceSource():def __init__(self, fas ta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.f asta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.a llow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself .ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = [ ]self.unique_next_ha >temp_632|source:-*- coding: utf-8 -*-|start:6447|stop:6947 .file_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self. id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = N onequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len( qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in sel f.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>') :self.file_pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()se >temp_633|source:-*- coding: utf-8 -*-|start:5954|stop:6454 asta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try :plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.la zy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer .seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_p >temp_634|source:-*- coding: utf-8 -*-|start:1306|stop:1806 = 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into mem ory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta.id)self .sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fast a.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_fil e_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mi xed_caseself.pos = 0 >temp_635|source:-*- coding: utf-8 -*-|start:4764|stop:5264 8)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.fill_between(r ange(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max (seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xti ckstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distributi on) + 1, ytickstep), >temp_636|source:-*- coding: utf-8 -*-|start:4431|stop:4931 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0 .50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplo t(gs[0:8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95 , right = 0.98)plt.plot(seq_len_distribution, color = 'black', alpha = 0.3)plt.f ill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, color = 'bl ack', alpha = 0.15)p >temp_637|source:-*- coding: utf-8 -*-|start:2773|stop:3273 )else:return self.next_regular()def next_unique(self):if self.unique:if self.tot al_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dic t[self.unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if s elf.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entry['id']se lf.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef next_ regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()s equence = ''while 1: >temp_638|source:-*- coding: utf-8 -*-|start:4278|stop:4778 rite('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequ ence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', l inestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0: 8])plt.grid(True)plt.subplots_adjust(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq >temp_639|source:-*- coding: utf-8 -*-|start:1732|stop:2232 .lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.i d = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(s elf.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.sta rtswith('>')])self.reset()if self.unique:self.init_unique_hash()def init_unique_ hash(self):while sel >temp_640|source:-*- coding: utf-8 -*-|start:2424|stop:2924 se:self.unique_hash_dict[hash] = {'id' : self.id,'ids': [self.id],'seq': self.se q,'count': 1}self.unique_hash_list = [i[1] for i in sorted([(self.unique_hash_di ct[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]sel f.total_unique = len(self.unique_hash_dict)self.reset()def next(self):if self.un ique:return self.next_unique()else:return self.next_regular()def next_unique(sel f):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash _entry = self.unique >temp_641|source:-*- coding: utf-8 -*-|start:3890|stop:4390 bution(self, title, dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspecse quence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or sel f.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.f lush()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths ) / 100.0) or 10)seq >temp_642|source:-*- coding: utf-8 -*-|start:1378|stop:1878 into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(self.fasta .id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):s elf.fasta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy _init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = f asta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.uniq ue = uniqueself.uniq >temp_643|source:-*- coding: utf-8 -*-|start:670|stop:1170 .ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def write_id( self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)d ef split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piec e_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()clas s ReadFasta:def __in >temp_644|source:-*- coding: utf-8 -*-|start:5144|stop:5644 max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, ma x(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distrib ution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)pl t.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large ', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False) >temp_645|source:-*- coding: utf-8 -*-|start:2892|stop:3392 _unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]self. pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry['s eq'].upper()self.id = hash_entry['id']self.ids = hash_entry['ids']return Trueel se:return Falseelse:return Falsedef next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_po inter.readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1ret urn Trueelse:return >temp_646|source:-*- coding: utf-8 -*-|start:1463|stop:1963 elf.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.f asta.close()class SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_ file_pathself.name = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow _mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer >temp_647|source:-*- coding: utf-8 -*-|start:6801|stop:7301 n Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_ pointer.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qua lscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_poi nter.seek(0)if __name__ == '__main__':fasta = SequenceSource(sys.argv[1])fasta.v isualize_sequence_le >temp_648|source:-*- coding: utf-8 -*-|start:5081|stop:5581 max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distrib ution) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytick step)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtex t(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center') ax1 = plt.subplot(gs >temp_649|source:-*- coding: utf-8 -*-|start:4310|stop:4810 _seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq _len_distribution = [0] * (max_seq_len + 1)for l in sequence_lengths:seq_len_dis tribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcParams.update({'axes. linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = gridspec.GridSpec(10, 1)ax1 = plt.subplot(gs[0:8])plt.grid(True)plt.subplots_ad just(left=0.05, bottom = 0.03, top = 0.95, right = 0.98)plt.plot(seq_len_distrib ution, color = 'blac >temp_650|source:-*- coding: utf-8 -*-|start:1596|stop:2096 fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):se lf.fasta_file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initse lf.allow_mixed_case = allow_mixed_caseself.pos = 0self.id = Noneself.seq = None self.ids = []self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_pointer = open(self.fasta_file_path)self .file_pointer.seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in se >temp_651|source:-*- coding: utf-8 -*-|start:6065|stop:6565 ss QualSource:def __init__(self, quals_file_path, lazy_init = True):self.quals_f ile_path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0 self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_poin ter = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self. total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.read lines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_poi nter.readline()[1:]. >temp_652|source:-*- coding: utf-8 -*-|start:2540|stop:3040 [i[1] for i in sorted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reverse = True)]self.total_unique = len(self.unique_has h_dict)self.reset()def next(self):if self.unique:return self.next_unique()else:r eturn self.next_regular()def next_unique(self):if self.unique:if self.total_uniq ue > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self. unique_hash_list[self.pos]]self.pos += 1self.seq = hash_entry['seq'] if self.all ow_mixed_case else h >temp_653|source:-*- coding: utf-8 -*-|start:6697|stop:7197 ):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.sp lit()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file _pointer.seek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line .strip()self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.qu als.split()]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Noneself.quals = Noneself.quals_int = Nonese lf.ids = []self.file >temp_654|source:-*- coding: utf-8 -*-|start:3004|stop:3504 eq'] if self.allow_mixed_case else hash_entry['seq'].upper()self.id = hash_entr y['id']self.ids = hash_entry['ids']return Trueelse:return Falseelse:return False def next_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:] .strip()sequence = ''while 1:line = self.file_pointer.readline()if not line:if l en(sequence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line .startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))bre aksequence += line.s >temp_655|source:-*- coding: utf-8 -*-|start:2569|stop:3069 .unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_dict], reve rse = True)]self.total_unique = len(self.unique_hash_dict)self.reset()def next(s elf):if self.unique:return self.next_unique()else:return self.next_regular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.to tal_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[self.pos]]se lf.pos += 1self.seq = hash_entry['seq'] if self.allow_mixed_case else hash_entry ['seq'].upper()self. >temp_656|source:-*- coding: utf-8 -*-|start:5372|stop:5872 x_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max(seq_len_dis tribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes. edgecolor' : 20})plt.grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths ),numpy.mean(sequence_lengths), numpy.std(sequence_lengths),\min(sequence_length s),\max(sequence_len >temp_657|source:-*- coding: utf-8 -*-|start:1015|stop:1515 )]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__( self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name )while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sy s.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))s ys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta. seq)sys.stderr.write >temp_658|source:-*- coding: utf-8 -*-|start:969|stop:1469 , len(sequence), piece_length) + [len(sequence)]return '\n'.join([sequence[ticks [x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(self):self.output _file_obj.close()class ReadFasta:def __init__(self, f_name):self.ids = []self.se quences = []self.fasta = SequenceSource(f_name)while self.fasta.next():if self.f asta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Readi ng FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.append(s elf.fasta.id)self.se >temp_659|source:-*- coding: utf-8 -*-|start:6627|stop:7127 ile 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.qua ls = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self .pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.s eek(self.file_pointer.tell() - len(line))breakqualscores += ' ' + line.strip()se lf.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split( )]self.pos += 1return Truedef close(self):self.file_pointer.close()def reset(sel f):self.pos = 0self. >temp_660|source:-*- coding: utf-8 -*-|start:6149|stop:6649 path = quals_file_pathself.name = Noneself.lazy_init = lazy_initself.pos = 0self .id = Noneself.quals = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.lazy_init:self.tota l_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readline s() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer .readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''whil e 1:line = self.file >temp_661|source:-*- coding: utf-8 -*-|start:4032|stop:4532 spec as gridspecsequence_lengths = []self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self .pos))sys.stderr.flush()sequence_lengths.append(len(self.seq))self.reset()sys.st derr.write('\n')if not max_seq_len:max_seq_len = max(sequence_lengths) + (int(ma x(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1 )for l in sequence_lengths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcPar >temp_662|source:-*- coding: utf-8 -*-|start:4862|stop:5362 n + 1), seq_len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel(' number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribut ion) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation= 90, size='xx-small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep ),[y for y in range(0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small ')plt.xlim(xmin = 0, >temp_663|source:-*- coding: utf-8 -*-|start:4824|stop:5324 3)plt.fill_between(range(0, max_seq_len + 1), seq_len_distribution, y2 = 0, colo r = 'black', alpha = 0.15)plt.ylabel('number of sequences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small')plt.yticks(range(0, ma x(seq_len_distribution) + 1, ytickstep),[y for y in range(0, max(seq_len_distrib ution) + 1, ytickste >temp_664|source:-*- coding: utf-8 -*-|start:3217|stop:3717 file_pointer.readline()[1:].strip()sequence = ''while 1:line = self.file_pointer .readline()if not line:if len(sequence):self.seq = sequenceself.pos += 1return T rueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_poi nter.tell() - len(line))breaksequence += line.strip()self.seq = sequence if self .allow_mixed_case else sequence.upper()self.pos += 1return Truedef get_seq_by_re ad_id(self, read_id):self.reset()while self.next():if self.id == read_id:return self.seqreturn False >temp_665|source:-*- coding: utf-8 -*-|start:1094|stop:1594 ) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__ (self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_nam e)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:s ys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos)) sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta .seq)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSourc e():def __init__(sel >temp_666|source:-*- coding: utf-8 -*-|start:3349|stop:3849 equenceself.pos += 1return Trueelse:return Falseif line.startswith('>'):self.fil e_pointer.seek(self.file_pointer.tell() - len(line))breaksequence += line.strip( )self.seq = sequence if self.allow_mixed_case else sequence.upper()self.pos += 1 return Truedef get_seq_by_read_id(self, read_id):self.reset()while self.next():i f self.id == read_id:return self.seqreturn Falsedef close(self):self.file_pointe r.close()def reset(self):self.pos = 0self.id = Noneself.seq = Noneself.ids = [] self.file_pointer.se >temp_667|source:-*- coding: utf-8 -*-|start:5003|stop:5503 p == None:xtickstep = (max_seq_len / 50) or 1if ytickstep == None:ytickstep = ma x(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xt ickstep), rotation=90, size='xx-small')plt.yticks(range(0, max(seq_len_distribut ion) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickst ep)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, ym ax = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext( 0.5, 0.96, '%s' % (t >temp_668|source:-*- coding: utf-8 -*-|start:3908|stop:4408 , dest = None, max_seq_len = None, xtickstep = None, ytickstep = None):import ma tplotlib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = [ ]self.reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.std err.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_len gths.append(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:m ax_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)s eq_len_distribution >temp_669|source:-*- coding: utf-8 -*-|start:1029|stop:1529 join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def clo se(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name): self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fa sta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write ('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flus h()self.ids.append(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr .write('\n')def clos >temp_670|source:-*- coding: utf-8 -*-|start:3914|stop:4414 = None, max_seq_len = None, xtickstep = None, ytickstep = None):import matplotl ib.pyplot as pltimport matplotlib.gridspec as gridspecsequence_lengths = []self. reset()while self.next():if self.pos % 10000 == 0 or self.pos == 1:sys.stderr.wr ite('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flush()sequence_lengths.a ppend(len(self.seq))self.reset()sys.stderr.write('\n')if not max_seq_len:max_seq _len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len _distribution = [0] >temp_671|source:-*- coding: utf-8 -*-|start:529|stop:1029 , split = True, store_frequencies = True):if entry.unique and store_frequencies: self.write_id('%s|%s' % (entry.id, 'frequency:%d' % len(entry.ids)))else:self.wr ite_id(entry.id)self.write_seq(entry.seq, split)def write_id(self, id):self.outp ut_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:s eq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequ ence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(se quence)]return '\n'. >temp_672|source:-*- coding: utf-8 -*-|start:5244|stop:5744 on) + 1, ytickstep),[y for y in range(0, max(seq_len_distribution) + 1, ytickste p)],size='xx-small')plt.xlim(xmin = 0, xmax = max_seq_len)plt.ylim(ymin = 0, yma x = max(seq_len_distribution) + (max(seq_len_distribution) / 20.0))plt.figtext(0 .5, 0.96, '%s' % (title), weight = 'black', size = 'xx-large', ha = 'center')ax1 = plt.subplot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False) plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: % .2f / min: %s / max: >temp_673|source:-*- coding: utf-8 -*-|start:953|stop:1453 :ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.join ([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def close(s elf):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):self .ids = []self.sequences = []self.fasta = SequenceSource(f_name)while self.fasta. next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r [fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()s elf.ids.append(self. >temp_674|source:-*- coding: utf-8 -*-|start:5041|stop:5541 0) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.x ticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx-small' )plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y in range (0, max(seq_len_distribution) + 1, ytickstep)],size='xx-small')plt.xlim(xmin = 0 , xmax = max_seq_len)plt.ylim(ymin = 0, ymax = max(seq_len_distribution) + (max( seq_len_distribution) / 20.0))plt.figtext(0.5, 0.96, '%s' % (title), weight = 'b lack', size = 'xx-la >temp_675|source:-*- coding: utf-8 -*-|start:710|stop:1210 write_seq(entry.seq, split)def write_id(self, id):self.output_file_obj.write('>% s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)s elf.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]return '\n'.j oin([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1)])def clos e(self):self.output_file_obj.close()class ReadFasta:def __init__(self, f_name):s elf.ids = []self.seq >temp_676|source:-*- coding: utf-8 -*-|start:1772|stop:2272 ase = allow_mixed_caseself.pos = 0self.id = Noneself.seq = Noneself.ids = []sel f.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.unique _next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer.se ek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l i n self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.unique :self.init_unique_hash()def init_unique_hash(self):while self.next_regular():has h = hashlib.sha1(sel >temp_677|source:-*- coding: utf-8 -*-|start:4132|stop:4632 pos == 1:sys.stderr.write('\r[fastalib] Reading: %s' % (self.pos))sys.stderr.flu sh()sequence_lengths.append(len(self.seq))self.reset()sys.stderr.write('\n')if n ot max_seq_len:max_seq_len = max(sequence_lengths) + (int(max(sequence_lengths) / 100.0) or 10)seq_len_distribution = [0] * (max_seq_len + 1)for l in sequence_l engths:seq_len_distribution[l] += 1fig = plt.figure(figsize = (16, 12))plt.rcPar ams.update({'axes.linewidth' : 0.9})plt.rc('grid', color='0.50', linestyle='-', linewidth=0.1)gs = g >temp_678|source:-*- coding: utf-8 -*-|start:6249|stop:6749 s = Noneself.quals_int = Noneself.ids = []self.file_pointer = open(self.quals_fi le_path)self.file_pointer.seek(0)if self.lazy_init:self.total_quals = Noneelse:s elf.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith( '>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].stri p()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file _pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip( )self.quals_int = [i >temp_679|source:-*- coding: utf-8 -*-|start:19|stop:519 ht (C) 2014, A. Murat Eren## This program is free software; you can redistribute it and/or modify it under# the terms of the GNU General Public License as publi shed by the Free# Software Foundation; either version 2 of the License, or (at y our option)# any later version.## Please read the docs/COPYING file.import sysim port numpyimport hashlibclass FastaOutput:def __init__(self, output_file_path):s elf.output_file_path = output_file_pathself.output_file_obj = open(output_file_p ath, 'w')def store(s >temp_680|source:-*- coding: utf-8 -*-|start:1849|stop:2349 self.unique = uniqueself.unique_hash_dict = {}self.unique_hash_list = []self.uni que_next_hash = 0self.file_pointer = open(self.fasta_file_path)self.file_pointer .seek(0)if self.lazy_init:self.total_seq = Noneelse:self.total_seq = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()if self.uni que:self.init_unique_hash()def init_unique_hash(self):while self.next_regular(): hash = hashlib.sha1(self.seq.upper()).hexdigest()if hash in self.unique_hash_dic t:self.unique_hash_d >temp_681|source:-*- coding: utf-8 -*-|start:6391|stop:6891 quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines( ) if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.r eadline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.startswith('>'):self.file_pointer.seek( self.file_pointer.te >temp_682|source:-*- coding: utf-8 -*-|start:4794|stop:5294 n, color = 'black', alpha = 0.3)plt.fill_between(range(0, max_seq_len + 1), seq_ len_distribution, y2 = 0, color = 'black', alpha = 0.15)plt.ylabel('number of se quences')plt.xlabel('sequence length')if xtickstep == None:xtickstep = (max_seq_ len / 50) or 1if ytickstep == None:ytickstep = max(seq_len_distribution) / 20 or 1plt.xticks(range(xtickstep, max_seq_len + 1, xtickstep), rotation=90, size='xx -small')plt.yticks(range(0, max(seq_len_distribution) + 1, ytickstep),[y for y i n range(0, max(seq_l >temp_683|source:-*- coding: utf-8 -*-|start:3250|stop:3750 ()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(seq uence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.start swith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequ ence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.u pper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self.reset() while self.next():if self.id == read_id:return self.seqreturn Falsedef close(sel f):self.file_pointer >temp_684|source:-*- coding: utf-8 -*-|start:2479|stop:2979 [self.id],'seq': self.seq,'count': 1}self.unique_hash_list = [i[1] for i in sor ted([(self.unique_hash_dict[hash]['count'], hash)\for hash in self.unique_hash_d ict], reverse = True)]self.total_unique = len(self.unique_hash_dict)self.reset() def next(self):if self.unique:return self.next_unique()else:return self.next_reg ular()def next_unique(self):if self.unique:if self.total_unique > 0 and self.pos < self.total_unique:hash_entry = self.unique_hash_dict[self.unique_hash_list[se lf.pos]]self.pos += >temp_685|source:-*- coding: utf-8 -*-|start:3090|stop:3590 ]self.ids = hash_entry['ids']return Trueelse:return Falseelse:return Falsedef ne xt_regular(self):self.seq = Noneself.id = self.file_pointer.readline()[1:].strip ()sequence = ''while 1:line = self.file_pointer.readline()if not line:if len(seq uence):self.seq = sequenceself.pos += 1return Trueelse:return Falseif line.start swith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(line))breaksequ ence += line.strip()self.seq = sequence if self.allow_mixed_case else sequence.u pper()self.pos += 1r >temp_686|source:-*- coding: utf-8 -*-|start:6644|stop:7144 .file_pointer.readline()if not line:if len(qualscores):self.quals = qualscores.s trip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return T rueelse:return Falseif line.startswith('>'):self.file_pointer.seek(self.file_poi nter.tell() - len(line))breakqualscores += ' ' + line.strip()self.quals = qualsc ores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1re turn Truedef close(self):self.file_pointer.close()def reset(self):self.pos = 0se lf.id = Noneself.qu >temp_687|source:-*- coding: utf-8 -*-|start:6353|stop:6853 r.seek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def ne xt(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself .quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readline()if no t line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q ) for q in self.quals.split()]self.pos += 1return Trueelse:return Falseif line.s tartswith('>'):self. >temp_688|source:-*- coding: utf-8 -*-|start:664|stop:1164 (entry.ids)))else:self.write_id(entry.id)self.write_seq(entry.seq, split)def wri te_id(self, id):self.output_file_obj.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self.split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piece_length = 80):ticks = range(0, len(sequence) , piece_length) + [len(sequence)]return '\n'.join([sequence[ticks[x]:ticks[x + 1 ]] for x in range(0, len(ticks) - 1)])def close(self):self.output_file_obj.close ()class ReadFasta:de >temp_689|source:-*- coding: utf-8 -*-|start:6037|stop:6537 t.show()except:passreturnclass QualSource:def __init__(self, quals_file_path, la zy_init = True):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_ init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Non eself.ids = []self.file_pointer = open(self.quals_file_path)self.file_pointer.se ek(0)if self.lazy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.file_pointer.readlines() if l.startswith('>')])self.reset()def next(s elf):self.id = self. >temp_690|source:-*- coding: utf-8 -*-|start:6505|stop:7005 ()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Nonequalscores = ''while 1:line = self.file_pointer.readlin e()if not line:if len(qualscores):self.quals = qualscores.strip()self.quals_int = [int(q) for q in self.quals.split()]self.pos += 1return Trueelse:return Falsei f line.startswith('>'):self.file_pointer.seek(self.file_pointer.tell() - len(lin e))breakqualscores += ' ' + line.strip()self.quals = qualscores.strip()self.qual s_int = [int(q) for >temp_691|source:-*- coding: utf-8 -*-|start:3482|stop:3982 reaksequence += line.strip()self.seq = sequence if self.allow_mixed_case else se quence.upper()self.pos += 1return Truedef get_seq_by_read_id(self, read_id):self .reset()while self.next():if self.id == read_id:return self.seqreturn Falsedef c lose(self):self.file_pointer.close()def reset(self):self.pos = 0self.id = Nones elf.seq = Noneself.ids = []self.file_pointer.seek(0)def visualize_sequence_lengt h_distribution(self, title, dest = None, max_seq_len = None, xtickstep = None, y tickstep = None):imp >temp_692|source:-*- coding: utf-8 -*-|start:5633|stop:6133 grid(False)plt.yticks([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %. 2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence _lengths), numpy.std(sequence_lengths),\min(sequence_lengths),\max(sequence_leng ths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self. fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')tr y:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_path , lazy_init = True): >temp_693|source:-*- coding: utf-8 -*-|start:6131|stop:6631 ):self.quals_file_path = quals_file_pathself.name = Noneself.lazy_init = lazy_in itself.pos = 0self.id = Noneself.quals = Noneself.quals_int = Noneself.ids = [] self.file_pointer = open(self.quals_file_path)self.file_pointer.seek(0)if self.l azy_init:self.total_quals = Noneelse:self.total_quals = len([l for l in self.fil e_pointer.readlines() if l.startswith('>')])self.reset()def next(self):self.id = self.file_pointer.readline()[1:].strip()self.quals = Noneself.quals_int = Noneq ualscores = ''while >temp_694|source:-*- coding: utf-8 -*-|start:779|stop:1279 j.write('>%s\n' % id)def write_seq(self, seq, split = True):if split:seq = self. split(seq)self.output_file_obj.write('%s\n' % seq)def split(self, sequence, piec e_length = 80):ticks = range(0, len(sequence), piece_length) + [len(sequence)]re turn '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks) - 1 )])def close(self):self.output_file_obj.close()class ReadFasta:def __init__(self , f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_name)whi le self.fasta.next() >temp_695|source:-*- coding: utf-8 -*-|start:5870|stop:6370 engths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = se lf.fasta_file_pathtry:plt.savefig(dest + '.pdf')except:plt.savefig(dest + '.png' )try:plt.show()except:passreturnclass QualSource:def __init__(self, quals_file_p ath, lazy_init = True):self.quals_file_path = quals_file_pathself.name = Nonesel f.lazy_init = lazy_initself.pos = 0self.id = Noneself.quals = Noneself.quals_in t = Noneself.ids = []self.file_pointer = open(self.quals_file_path)self.file_poi nter.seek(0)if self. >temp_696|source:-*- coding: utf-8 -*-|start:1284|stop:1784 elf.fasta.pos % 1000 == 0 or self.fasta.pos == 1:sys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos))sys.stderr.flush()self.ids.app end(self.fasta.id)self.sequences.append(self.fasta.seq)sys.stderr.write('\n')def close(self):self.fasta.close()class SequenceSource():def __init__(self, fasta_f ile_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta _file_path = fasta_file_pathself.name = Noneself.lazy_init = lazy_initself.allow _mixed_case = allow_ >temp_697|source:-*- coding: utf-8 -*-|start:5574|stop:6074 plot(gs[9])plt.rcParams.update({'axes.edgecolor' : 20})plt.grid(False)plt.yticks ([])plt.xticks([])plt.text(0.02, 0.5, 'total: %s / mean: %.2f / std: %.2f / min: %s / max: %s'\% (len(sequence_lengths),numpy.mean(sequence_lengths), numpy.std( sequence_lengths),\min(sequence_lengths),\max(sequence_lengths)),\va = 'center', alpha = 0.8, size = 'x-large')if dest == None:dest = self.fasta_file_pathtry:pl t.savefig(dest + '.pdf')except:plt.savefig(dest + '.png')try:plt.show()except:pa ssreturnclass QualSo >temp_698|source:-*- coding: utf-8 -*-|start:1560|stop:2060 SequenceSource():def __init__(self, fasta_file_path, lazy_init = True, unique = False, allow_mixed_case = False):self.fasta_file_path = fasta_file_pathself.nam e = Noneself.lazy_init = lazy_initself.allow_mixed_case = allow_mixed_caseself.p os = 0self.id = Noneself.seq = Noneself.ids = []self.unique = uniqueself.unique _hash_dict = {}self.unique_hash_list = []self.unique_next_hash = 0self.file_poin ter = open(self.fasta_file_path)self.file_pointer.seek(0)if self.lazy_init:self. total_seq = Noneelse >temp_699|source:-*- coding: utf-8 -*-|start:1014|stop:1514 e)]return '\n'.join([sequence[ticks[x]:ticks[x + 1]] for x in range(0, len(ticks ) - 1)])def close(self):self.output_file_obj.close()class ReadFasta:def __init__ (self, f_name):self.ids = []self.sequences = []self.fasta = SequenceSource(f_nam e)while self.fasta.next():if self.fasta.pos % 1000 == 0 or self.fasta.pos == 1:s ys.stderr.write('\r[fastalib] Reading FASTA into memory: %s' % (self.fasta.pos)) sys.stderr.flush()self.ids.append(self.fasta.id)self.sequences.append(self.fasta .seq)sys.stderr.writ