-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta-to-fromfile.py
executable file
·109 lines (84 loc) · 3.15 KB
/
fasta-to-fromfile.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /usr/bin/env python3
"""
"""
import sys
import argparse
import screed
import csv
import os
import shutil
from kiln import InputFile, OutputRecords, check_dna, remove_extension
from sourmash.tax.tax_utils import MultiLineageDB
from sourmash.logging import notify, error
def main():
p = argparse.ArgumentParser()
p.add_argument('filenames', nargs='*', help="names of files to process")
p.add_argument('-F', '--file-list', action='append',
help='text files with filenames to add to command line',)
p.add_argument('-o', '--output-csv', required=True)
p.add_argument('--ident-from-filename', action='store_true',
help="determine identifer from filename prefixes")
args = p.parse_args()
if not args.filenames:
args.filenames = []
# load --file-list
if args.file_list:
for ff in args.file_list:
with open(ff, 'rt') as fp:
filelist = [ x.strip() for x in fp ]
notify(f"Loaded {len(filelist)} entries from '{ff}'")
args.filenames.extend(filelist)
if not args.filenames:
error("** ERROR: no input filenames and no --file-list provided.")
sys.exit(-1)
output = OutputRecords(args.output_csv)
output.open()
fileinfo_d = {}
n = 0
for filename in args.filenames:
print(f"processing file '{filename}'")
fileinfo = InputFile()
record = None
for record in screed.open(filename):
record_name = record.name
break
if record is None:
assert 0, f"no sequences in {filename}"
is_dna = check_dna(record.sequence)
# figure out identifiers from first record
if not args.ident_from_filename:
full_ident, *remainder = record_name.split(' ', 1)
ident = full_ident
if '.' in ident:
ident = ident.split('.', 1)[0]
fileinfo.ident = ident
fileinfo.full_ident = full_ident
fileinfo.name = record_name
else:
# should use the same approach as genome grist...
basename = os.path.basename(filename)
name = remove_extension(basename)
fileinfo.ident = name
fileinfo.full_ident = name
fileinfo.name = name
if is_dna:
fileinfo.genome_filename = filename
else:
fileinfo.protein_filename = filename
moltype = "DNA" if is_dna else "protein"
previous = fileinfo_d.get(fileinfo.ident)
if previous is not None:
print(f"(merging into existing record '{fileinfo.ident}' moltype={moltype})")
fileinfo = previous.merge(fileinfo)
else:
print(f"(new record for identifier '{fileinfo.ident}' moltype={moltype})")
assert not fileinfo.is_empty(), fileinfo.__dict__
fileinfo_d[fileinfo.ident] = fileinfo
for n, (ident, fileinfo) in enumerate(fileinfo_d.items()):
output.write_record(fileinfo)
output.close()
print('---')
print(f"wrote {len(fileinfo_d)} entries to '{args.output_csv}'")
return 0
if __name__ == '__main__':
sys.exit(main())