from operator import itemgetter

def parse_blast_output(input_file, output_file):
   with open(input_file, 'r') as infile:
      table = []
      for line in infile:
         print i 
         col = line.split('\t')
         try:
            col[2] = float(col[2])
         except ValueError as err:
            raise RuntimeError("error in parsing {} : {}".format(input_file, err))
         col[-1] = col[-1][:-1]
         table.append(col)
   #from this point the input_file is closed
   table_sorted = sorted(table, key = itemgetter(2), reverse = True)
   # alternative
   # table_sorted = sorted(table, key = lambda x : x[2], reversed = True)
   with open(output_file, 'w') as output:
      for row in table_sorted:
         row = [str(x) for x in row]
         output.write("\t".join(row) + "\n")
         

@@ -29,58 +29,426 @@
 ter%0A
-%0Adef parse_blast_output(input_file, output_file):%0A
+from collections import namedtuple%0A%0AHit = namedtuple(%22Hit%22 ,%22id percent identity align_len mis_num, open_gap_num%5C%0A                  query_start, query_end, subject_start, subject_end, E_value, HSP_bit_score%22)%0A%0Adef parse_blast_output(input_file):%0A    %22%22%22%0A    :param input_file: the path of the blast report (in m8 format) to parse%0A    :type input_file: string%0A    :return: list of hits%0A    :rtype: list of Hit%0A    %22%22%22%0A 
    w
@@ -486,24 +486,26 @@
 file:%0A      
+ 
+ 
 table = %5B%5D%0A 
@@ -503,16 +503,18 @@
 le = %5B%5D%0A
+  
       fo
@@ -538,23 +538,8 @@
 %0A   
-      print i %0A
     
@@ -566,16 +566,19 @@
 t('%5Ct')%0A
+   
         
@@ -591,24 +591,28 @@
             
+    
 col%5B2%5D = flo
@@ -622,16 +622,19 @@
 col%5B2%5D)%0A
+   
         
@@ -672,16 +672,20 @@
         
+  
+  
 raise Ru
@@ -752,24 +752,27 @@
 ))%0A         
+   
 col%5B-1%5D = co
@@ -782,16 +782,19 @@
 1%5D%5B:-1%5D%0A
+   
         
@@ -819,210 +819,308 @@
 %0A   
-#from this point the input_file is closed%0A   table_sorted = sorted(table, key = itemgetter(2), reverse = True)%0A   # alternative%0A   # table_sorted = sorted(table, key = lambda x : x%5B2%5D, reversed = True)%0A
+ return table%0A%0A%0Adef write_blast_output(hits, output_file):%0A    %22%22%22%0A    Write hits in file in format text%0A    one hit per line%0A    %0A    :param hits: hit to wite in file%0A    :type hits: namedtuple Hit%0A    :param output_file: the path of the file to write hits in%0A    :type output_file: string%0A    %22%22%22%0A 
    w
@@ -1159,24 +1159,26 @@
 tput:%0A      
+ 
+ 
 for row in t
@@ -1190,16 +1190,19 @@
 sorted:%0A
+   
         
@@ -1239,16 +1239,19 @@
         
+   
 output.w
@@ -1277,16 +1277,377 @@
  + %22%5Cn%22)
+%0A%0A   %0Aif __name__ == '__main__':%0A    table_hits = parse_blast_output('blast2.txt')%0A    #table_hits = parse_blast_output('blast.txt')%0A    table_sorted = sorted(table_hits, key = itemgetter(2), reverse = True)%0A    # alternative%0A    # table_sorted = sorted(table, key = lambda x : x%5B2%5D, reversed = True)%0A    write_blast_output(table_hits, 'blast_sorted.txt')  %0A   
 %0A       

split monlithique func in several func