Skip to content

Instantly share code, notes, and snippets.

@ShaiberAlon
Last active January 11, 2019 22:27
Show Gist options
  • Save ShaiberAlon/23fc13ed56e02854bee42773672832a5 to your computer and use it in GitHub Desktop.
Save ShaiberAlon/23fc13ed56e02854bee42773672832a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# To use this:
# python gen-collection-for-merged-fasta.py -f fasta.txt -o collection-file.txt
import sys
from anvio import fastalib
from anvio import utils
from anvio import filesnpaths
from anvio.errors import ConfigError, FilesNPathsError
def main(args):
fasta_files = utils.get_TAB_delimited_file_as_dictionary(args.fasta_txt)
with open(args.output_file, 'w') as f:
for name in fasta_files:
fasta_reader = fastalib.ReadFasta(fasta_files[name]['path'])
contigs = fasta_reader.ids
for c in contigs:
f.write('%s\t%s\n' % (c, name))
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description="Takes a fasta.txt file, and returns a collection file for contigs.")
parser.add_argument('-f', '--fasta-txt', metavar = 'FILE', help = 'fasta.txt file (the same as used in anvio contigs snakemake workflow)')
parser.add_argument('-o', '--output-file', metavar = 'FILE', help = 'Output file name.')
args = parser.parse_args()
try:
main(args)
except ConfigError as e:
print(e)
sys.exit(-1)
except FilesNPathsError as e:
print(e)
sys.exit(-2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment