Last active
August 29, 2015 14:20
-
-
Save moonwatcher/fa4f37d3700ac27d864d to your computer and use it in GitHub Desktop.
Search for a sequence in a FASTA file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Search for a sequence in a FASTA file | |
# Author: Lior Galanti < [email protected] > | |
# NYU Center for Genetics and System Biology 2015 | |
# | |
# search 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. | |
# | |
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
# See the GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License along with this program. | |
# If not, see <http://www.gnu.org/licenses/>. | |
import io | |
import sys | |
from io import StringIO | |
def usage(): | |
print('usage: path query') | |
def search(path, query): | |
reference = StringIO() | |
with io.open(path, 'r') as file: | |
try: | |
for line in file: | |
line = line.strip() | |
if line[0] != '>': | |
reference.write(line) | |
reference.seek(0) | |
reference = reference.getvalue() | |
start = reference.find(query) | |
while start >= 0: | |
end = start + len(query) | |
print('{}:{}'.format(start, end)) | |
start = reference.find(query, start + 1) | |
except OSError as e: | |
print('{} {}'.format(e.strerror, path)) | |
if len(sys.argv) > 1: | |
path = sys.argv[1] | |
query = sys.argv[2] | |
search(path, query) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment