annotate find_subsequences.py @ 1:d882a0a75759 draft default tip

Uploaded
author bgruening
date Fri, 10 Apr 2015 06:49:30 -0400
parents 7f39014f9404
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
1 #!/usr/bin/env python
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
2
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
3 import re
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
4 import sys
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
5 import argparse
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
6 from Bio import SeqIO
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
7 from Bio.Seq import Seq
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
8 from Bio.SeqUtils import nt_search
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
9 from Bio.Alphabet import generic_dna
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
10
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
11 choices = ['embl', 'fasta', 'fastq-sanger', 'fastq', 'fastq-solexa', 'fastq-illumina', 'genbank', 'gb']
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
12
1
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
13 def find_pattern(seqs, pattern, outfile_path, strand):
0
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
14 """
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
15 Finds all occurrences of a pattern in the a given sequence.
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
16 Outputs sequence ID, start and end postion of the pattern.
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
17 """
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
18 pattern = pattern.upper()
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
19 rev_compl = Seq(pattern, generic_dna).complement()
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
20 search_func = simple_pattern_search
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
21 if set(pattern).difference(set('ATCG')):
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
22 search_func = complex_pattern_search
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
23
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
24 with open(outfile_path, 'w+') as outfile:
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
25 for seq in seqs:
1
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
26 if strand in ['both', 'forward']:
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
27 search_func(seq, pattern, outfile)
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
28 if strand in ['both', 'reverse']:
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
29 search_func(seq, rev_compl, outfile, '-')
0
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
30
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
31
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
32 def simple_pattern_search(sequence, pattern, outfile, strand='+'):
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
33 """
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
34 Simple regular expression search. This is way faster than the complex search.
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
35 """
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
36 bed_template = '%s\t%s\t%s\t%s\t%s\t%s\n'
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
37 for match in re.finditer( str(pattern), str(sequence.seq) ):
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
38 outfile.write(bed_template % (sequence.id, match.start(), match.end(), sequence.description, '', strand))
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
39
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
40
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
41 def complex_pattern_search(sequence, pattern, outfile, strand='+'):
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
42 """
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
43 Searching for pattern with biopyhon's nt_search().
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
44 This allows for ambiguous values, like N = A or T or C or G, R = A or G ...
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
45 """
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
46 l = len(pattern)
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
47 matches = nt_search(str(sequence.seq), pattern)
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
48 bed_template = '%s\t%s\t%s\t%s\t%s\t%s\n'
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
49 for match in matches[1:]:
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
50 outfile.write(bed_template % (sequence.id, match, match+l, sequence.description, '', strand) )
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
51
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
52
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
53 if __name__ == "__main__":
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
54 parser = argparse.ArgumentParser()
1
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
55 parser.add_argument('-i', '--input', required=True)
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
56 parser.add_argument('-o', '--output', required=True)
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
57 parser.add_argument('-p', '--pattern', required=True)
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
58 parser.add_argument('--strand', choices=['both', 'forward', 'reverse'], default='both')
0
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
59 parser.add_argument('-f', '--format', default="fasta", choices=choices)
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
60 args = parser.parse_args()
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
61
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
62 with open(args.input) as handle:
1
d882a0a75759 Uploaded
bgruening
parents: 0
diff changeset
63 find_pattern( SeqIO.parse(handle, args.format), args.pattern, args.output, args.strand )
0
7f39014f9404 Imported from capsule None
bgruening
parents:
diff changeset
64