defrevcom_dict(s): complement = { 'A' : 'T', 'G' : 'C', 'C' : 'G', 'T' : 'A', 'a' : 'T', 'g' : 'C', 'c' : 'G', 't' : 'A'} t = '' for base in s: t = complement[base] + t return t
defrevcom_transtable(sequence): # make a sequence complement # # replace function of string is too low! sequence = sequence.upper() transtable = str.maketrans('ATCG', 'TAGC') sequence = sequence.translate(transtable) return sequence[::-1]
defread_fasta(fp): name, seq = None, [] for line in fp: line = line.rstrip() if line.startswith(">"): if name: yield (name, ''.join(seq)) name, seq = line, [] else: seq.append(line) if name: yield (name, ''.join(seq))
defrun_time(info): print("[INFO]: {0} total run time: {1:.2f}".format(info, time.time() - t) + "s")
seqs = [] withopen('sequence.fasta') as fp: for name, seq in read_fasta(fp): seqs.append(seq)
# method1 t = time.time() for i in seqs: a = revcom_base(i) run_time("method1") t = time.time()
# method2 for i in seqs: b = revcom_dict(i) run_time("method2") t = time.time()
#method3 for i in seqs: c= revcom_transtable(i) run_time("method3")
运行结果:
1 2 3
[INFO]: method1 total run time: 0.01s [INFO]: method2 total run time: 24.47s [INFO]: method3 total run time: 0.00s