Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New script: extract query from fasta file.

  • Loading branch information...
commit 205a0857ed6104272ba5675c7a3dee5ec7cddc8d 1 parent 41fa38c
@bl0b authored
Showing with 48 additions and 0 deletions.
  1. +37 −0 extract_for_exonerate.py
  2. +11 −0 fasta.py
View
37 extract_for_exonerate.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+import fasta
+import os
+import sys
+
+fcount = 0
+ftotal = 0
+
+
+def extract(filename):
+ global fcount, ftotal
+ fcount += 1
+ print "\r%i/%i" % (fcount, ftotal),
+ exti = filename.rfind('.')
+ ext = filename[exti:]
+ path, query = os.path.split(filename[:exti])
+ fhits = fasta.read_from(filename)
+ if query in fhits:
+ fquery = fasta.Fasta()
+ fquery.add_seq(fhits[query])
+ fhits.remove(query)
+ fhits.save_to(os.path.join(path, query + '-hits' + ext))
+ fquery.save_to(os.path.join(path, query + '-query' + ext))
+
+
+if __name__ == '__main__':
+ args = sys.argv[1:]
+ if args[0] in ('-h', '--help'):
+ print "Usage:"
+ print sys.argv[0], "[-h,--help] [-w fasta_width] fasta_file..."
+ sys.exit(0)
+ if args[0] == '-w':
+ fasta.WIDTH = int(args[1])
+ args = args[2:]
+ print "Extracting queries from", len(args), "files..."
+ ftotal = len(args)
+ map(extract, args)
View
11 fasta.py
@@ -29,6 +29,11 @@ def add_seq(self, seq):
def __len__(self):
return len(self.seq_list)
+ def __contains__(self, x):
+ return (type(x) is int and x < len(self.seq_list)
+ or
+ type(x) is str and x in self.seq_dic)
+
def __iter__(self):
return iter(self.seq_list)
@@ -66,3 +71,9 @@ def save_to(self, filename):
f = open(filename, 'w')
for s in self.seq_list:
print >> f, s
+
+
+def read_from(filename):
+ ret = Fasta()
+ ret.read_from(filename)
+ return ret
Please sign in to comment.
Something went wrong with that request. Please try again.