forked from lab7arriam/Cells_2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mean_length.py
29 lines (23 loc) · 973 Bytes
/
mean_length.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
from Bio import SeqIO
import argparse
def read_fasta(file:str)->list:
with open(file, 'r') as handle:
seq_list = []
for entry in SeqIO.parse(handle, 'fasta'):
seq_list.append(entry)
return seq_list
def mean_length(seqs:list)->str:
"""
return the longest, the shortest and the mean lengths in the fasta file
"""
shortest = len(min(seqs, key = lambda x: len(x.seq)).seq)
longest = len(max(seqs, key = lambda x: len(x.seq)).seq)
mean = sum(list(map(lambda x: len(x.seq), seqs))) / len(seqs)
return f'The longest sequence is {longest} symbols long, the shortest sequence is {shortest} symbols long, mean sequence length across the file is {mean} symbols'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-i', help='input file path', type=str, required=True)
args = parser.parse_args()
i = args.i
print(mean_length(read_fasta(i)))