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