-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfaSize.py
More file actions
executable file
·56 lines (44 loc) · 1.69 KB
/
faSize.py
File metadata and controls
executable file
·56 lines (44 loc) · 1.69 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 12/3/2020 3:42 PM
# @Author : Runsheng
# @File : faSize.py
"""
mimic the faSize -detailed result using UCSC kentutils
"""
import argparse
from Bio import SeqIO
def fa_size(fastafile, filetype="fastq"):
"""
Give a fasta file name, return a dict contains the name and seq
Require Biopython SeqIO medule to parse the sequence into dict, a large genome may take a long time to parser
"""
len_list=[]
handle=open(fastafile, "r")
for contig in SeqIO.parse(handle,filetype):
name=contig.name
len_list.append( (name, len(contig)) )
handle.close()
return len_list
def write_len(len_list, filename="out.sizes"):
fw=open(filename, "w")
for i in len_list:
name, length=i
fw.write(name+"\t"+str(length)+"\n")
fw.close()
if __name__=="__main__":
import sys
example_text = '''example:
### example to run the faSize
faSize.py --file file.fa --filetype fasta --output file.fa.sizes
'''
parser = argparse.ArgumentParser(prog='faSize',
epilog=example_text,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-f", "--file", help="input file in fasta/fastq format")
parser.add_argument("-o", "--output", help="the two column text file for name:size")
parser.add_argument("-t", "--filetype", help="the file is fastq or fasta", default="fasta")
args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])
#main
len_l=fa_size(fastafile=args.file, filetype=args.filetype)
write_len(len_list=len_l, filename=args.output)