-
Notifications
You must be signed in to change notification settings - Fork 3
/
seq-len.py
executable file
·71 lines (53 loc) · 1.65 KB
/
seq-len.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# !/bin/sh
# Author: Arkadiy Garber
from collections import defaultdict
import re
import os
import textwrap
import argparse
import sys
parser = argparse.ArgumentParser(
prog="seq-len.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent('''
************************************************************************
************************************************************************
Script for printing the length of each sequence in a provided FASTA file
Developed by Arkadiy Garber: agarber4@asu.edu
************************************************************************
************************************************************************
'''))
parser.add_argument('-fasta', help='fasta file')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(0)
args = parser.parse_known_args()[0]
def fasta(fasta_file):
seq = ''
header = ''
Dict = defaultdict(lambda: defaultdict(lambda: 'EMPTY'))
for i in fasta_file:
i = i.rstrip()
if re.match(r'^>', i):
if len(seq) > 0:
Dict[header] = seq
header = i[1:]
seq = ''
else:
header = i[1:]
seq = ''
else:
seq += i
Dict[header] = seq
return Dict
file = open(args.fasta, "r")
file = fasta(file)
ls = []
Dict = defaultdict(list)
for i in file.keys():
# print(i + "\t" + str(len(file[i])))
Dict[len(file[i])].append(i.split(" ")[0])
for i in sorted(Dict.keys(), reverse=True):
for j in Dict[i]:
print(str(j) + "\t" + str(i))