Here's a Python-based way to do this, that does not use sorting:
#!/usr/bin/env python
import sys
if len(sys.argv) != 3:
raise SystemError("Usage: ./filter_fasta.py target.fa query.fa")
target = sys.argv[1]
query = sys.argv[2]
m = {}
k = None
v = ''
with open(query, "r") as qfh:
for line in qfh:
line = line.strip()
if line.startswith('>'):
if k:
m[k] = v
v = ''
k = line
else:
v += line
m[k] = v
k = None
v = ''
with open(target, "r") as rfh:
for line in rfh:
line = line.strip()
if line.startswith('>'):
if k and (k not in m):
sys.stdout.write("%s\n%s\n" % (k, v))
k = line
v = ''
else:
v += line
if k and (k not in m):
sys.stdout.write("%s\n%s\n" % (k, v))
Output:
$ ./filter_fasta.py f1.fa f2.fa
>9-10946
UGAAGCUGCCAGCAUGAUCU
>17-8260
UUCCACAGCUUUCUUGAACUU
You could probably use the other approaches if your inputs are small or don't need much preprocessing.
Here are a couple advantages of my approach:
Other approaches require sorting. For large datasets, sorting can get expensive in time. My approach reads through each file once to make hash tables ("dictionaries" in Python-speak), and as O(n+m) < O(nlogn + mlogm) you'll end up spending a lot less time making hash tables than on sorting, if your inputs are very large.
You don't need to linearize the FASTA file inputs. This script takes in multiline FASTA.