Mercurial > repos > artbio > blast_unmatched
comparison blast_unmatched.py @ 1:50c1fa95a076 draft
planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/blast_unmatched commit be082f72c8d8c1eebe3f5643da1a73ab0ac9e4b3
author | artbio |
---|---|
date | Tue, 03 Oct 2017 12:41:53 -0400 |
parents | f3b63b59a1ea |
children | dfcdac284538 |
comparison
equal
deleted
inserted
replaced
0:f3b63b59a1ea | 1:50c1fa95a076 |
---|---|
21 def get_matched(blast_file): | 21 def get_matched(blast_file): |
22 """ | 22 """ |
23 Get a dictionary of all the queries that got a match | 23 Get a dictionary of all the queries that got a match |
24 """ | 24 """ |
25 matched = dict() | 25 matched = dict() |
26 blast_file_handle = open(blast_file, 'r') | 26 with open(blast_file, 'r') as infile: |
27 for line in blast_file_handle.readlines(): | 27 for line in infile: |
28 fields = line.split("\t") | 28 fields = line.split("\t") |
29 query_id = fields[0] | 29 query_id = fields[0] |
30 matched[query_id] = 1 | 30 matched[query_id] = 1 |
31 blast_file_handle.close() | |
32 return matched | 31 return matched |
33 | 32 |
34 def get_unmatched(output_file, fasta_file, matched): | 33 def get_unmatched(output_file, fasta_file, matched): |
35 """ | 34 """ |
36 Compares matched queries to query fasta file and print unmatched to ouput | 35 Compares matched queries to query fasta file and print unmatched to ouput |
37 """ | 36 """ |
38 output_file_handle = open(output_file, 'w') | 37 output_file_handle = open(output_file, 'w') |
39 fasta_file_handle = open(fasta_file, 'r') | |
40 unmatched = False | 38 unmatched = False |
41 for line in fasta_file_handle.readlines(): | 39 with open(fasta_file, 'r') as infile: |
42 if line.startswith('>'): | 40 for line in infile: |
43 subline = line[1:100].rstrip() #qid are 100chars long in blast | 41 if line.startswith('>'): |
44 if subline not in matched: | 42 subline = line[1:100].rstrip() #qid are 100chars long in blast |
43 if subline not in matched: | |
44 output_file_handle.write(line) | |
45 unmatched = True | |
46 else: | |
47 unmatched = False | |
48 elif unmatched: | |
45 output_file_handle.write(line) | 49 output_file_handle.write(line) |
46 unmatched = True | |
47 else: | |
48 unmatched = False | |
49 elif unmatched: | |
50 output_file_handle.write(line) | |
51 fasta_file_handle.close() | |
52 output_file_handle.close() | 50 output_file_handle.close() |
53 | 51 |
54 def __main__(): | 52 def __main__(): |
55 opts = parse_options() | 53 opts = parse_options() |
56 matched = get_matched(opts.blast_file) | 54 matched = get_matched(opts.blast_file) |