annotate extract_min_max_lines.py @ 0:90fc00b34716 draft default tip

planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
author bebatut
date Fri, 15 Apr 2016 07:59:28 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
1 #!/usr/bin/env python
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
2 # -*- coding: utf-8 -*-
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
3
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
4 import sys
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
5 import os
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
6 import argparse
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
7 import re
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
8 import time
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
9
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
10 def extract_lines(input_content, column_id, extraction_type, extraction_nb):
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
11 conserved_lines = []
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
12 for line in input_content:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
13 split_line = line[:-1].split('\t')
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
14 value = float(split_line[column_id])
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
15
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
16 if len(conserved_lines) < extraction_nb:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
17 conserved_lines.append(split_line)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
18 else:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
19 best_pos = None
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
20 #print value
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
21 #print conserved_lines
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
22 for i in range(len(conserved_lines)-1,-1,-1):
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
23 compared_value = float(conserved_lines[i][column_id])
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
24 if extraction_type(value, compared_value) == value:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
25 print value, compared_value, extraction_type(value, compared_value)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
26 best_pos = i
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
27 else:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
28 break
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
29 if best_pos != None:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
30 print best_pos
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
31 tmp_conserved_lines = conserved_lines
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
32 conserved_lines = tmp_conserved_lines[:best_pos]
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
33 conserved_lines += [split_line]
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
34 conserved_lines += tmp_conserved_lines[best_pos:-1]
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
35 print conserved_lines
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
36 print
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
37 return conserved_lines
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
38
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
39 def extract_min_max_lines(args):
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
40 if args.extraction_type == 'max':
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
41 extraction_type = max
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
42 elif args.extraction_type == 'min':
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
43 extraction_type = min
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
44
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
45 with open(args.input_file, 'r') as input_file:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
46 input_content = input_file.readlines()
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
47 conserved_lines = extract_lines(input_content, args.column_id - 1,
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
48 extraction_type, args.extraction_nb)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
49
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
50 with open(args.output_file, 'w') as output_file:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
51 for line in conserved_lines:
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
52 output_file.write('\t'.join(line) + "\n")
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
53
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
54 if __name__ == '__main__':
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
55 parser = argparse.ArgumentParser()
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
56 parser.add_argument('--input_file', required=True)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
57 parser.add_argument('--output_file', required=True)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
58 parser.add_argument('--column_id', required=True, type=int)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
59 parser.add_argument('--extraction_type', required=True, choices = ['min','max'])
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
60 parser.add_argument('--extraction_nb', required=True, type=int)
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
61 args = parser.parse_args()
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
62
90fc00b34716 planemo upload for repository https://github.com/asaim/galaxytools/tree/master/tools/extract_min_max_lines commit 718c006213709b631862b8d6d655fafc92e79ef7-dirty
bebatut
parents:
diff changeset
63 extract_min_max_lines(args)