<s> import os , csv <EOL> def parse_blast ( resultfile ) : <EOL> from Bio . Blast import NCBIXML <EOL> result_handle = open ( resultfile ) <EOL> blast_records = NCBIXML . parse ( result_handle ) <EOL> csv_list = [ ] <EOL> header = [ '<STR_LIT>' , <EOL> '<STR_LIT:Name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> csv_list . append ( header ) <EOL> count = <NUM_LIT:0> <EOL> for blast_record in blast_records : <EOL> '''<STR_LIT>''' <EOL> '''<STR_LIT>''' <EOL> count += <NUM_LIT:1> <EOL> query = blast_record . query <EOL> for alignment in blast_record . alignments : <EOL> name = alignment . title <EOL> length = alignment . length <EOL> hsp = alignment . hsps [ <NUM_LIT:0> ] <EOL> score = hsp . score <EOL> expect = hsp . expect <EOL> querystart = hsp . query_start <EOL> queryend = hsp . query_end <EOL> subjectstart = hsp . sbjct_start <EOL> subjectend = hsp . sbjct_end <EOL> row = [ query , name , length , score , expect , querystart , queryend , subjectstart , subjectend ] <EOL> csv_list . append ( row ) <EOL> result_handle . close ( ) <EOL> return csv_list <EOL> def write_csv ( dest , csv_cont ) : <EOL> with open ( dest , '<STR_LIT:w>' ) as out_file : <EOL> writer = csv . writer ( out_file , delimiter = '<STR_LIT:U+002C>' ) <EOL> for row in csv_cont : <EOL> writer . writerow ( row ) <EOL> indir = "<STR_LIT>" <EOL> outdir = "<STR_LIT>" <EOL> for fn in os . listdir ( "<STR_LIT>" % indir ) : <EOL> ID = fn [ : fn . index ( '<STR_LIT:.>' ) ] <EOL> if fn == '<STR_LIT>' : <EOL> continue <EOL> csv_list = parse_blast ( fn ) <EOL> write_csv ( "<STR_LIT>" % ( outdir , ID ) , csv_list ) </s>
