Skip to content

Instantly share code, notes, and snippets.

@meren
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meren/f4266ede7b085441c39a to your computer and use it in GitHub Desktop.
Save meren/f4266ede7b085441c39a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2010 - 2012, A. Murat Eren
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
import sys
import Oligotyping.lib.fastalib as u
import Oligotyping.utils.utils as utils
progress = utils.Progress()
run = utils.Run()
# get read length
fasta = u.SequenceSource(sys.argv[1], lazy_init = False)
fasta.next()
len_fasta_entry = len(fasta.seq)
# reset fasta.
fasta.reset()
# make sure all reads have equal length
while fasta.next():
if len(fasta.seq) != len_fasta_entry:
sys.stderr.write('All reads must have equal number of characters, but it is not the case. Sorry and bye.\n')
sys.exit()
fasta.reset()
# -*-
nucleotide_positions = set(range(0, len_fasta_entry))
invalid_columns = set(range(0, len_fasta_entry))
progress.new('Step 1')
while fasta.next():
if fasta.pos % 100 == 1:
progress.update('%.2d%% -- pos: %d' % (fasta.pos * 100 / fasta.total_seq, fasta.pos))
for i in nucleotide_positions:
if fasta.seq[i] != '-' and i in invalid_columns:
invalid_columns.remove(i)
progress.end()
fasta.reset()
# -*-
columns_to_keep = [x for x in range(0, len_fasta_entry) if x not in invalid_columns]
f = open(sys.argv[1] + '-TRIMMED', 'w')
progress.new('Step 2')
while fasta.next():
if fasta.pos % 100 == 1:
progress.update('%.2d%% -- pos: %d' % (fasta.pos * 100 / fasta.total_seq, fasta.pos))
new_seq = ''.join(fasta.seq[i] for i in columns_to_keep)
f.write('>' + fasta.id + '\n')
f.write(new_seq + '\n')
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment