Mercurial > repos > miller-lab > genome_diversity
comparison reorder.py @ 24:248b06e86022
Added gd_genotype datatype. Modified tools to support new datatype.
author | Richard Burhans <burhans@bx.psu.edu> |
---|---|
date | Tue, 28 May 2013 16:24:19 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
23:66a183c44dd5 | 24:248b06e86022 |
---|---|
1 #!/usr/bin/env python | |
2 | |
3 import sys | |
4 | |
5 def parse_rangelist(string): | |
6 rv = [] | |
7 | |
8 tokens = strip_split(string, ',') | |
9 for token in tokens: | |
10 int_list = parse_token(token) | |
11 for int_val in int_list: | |
12 int_val -= 1 | |
13 if int_val not in rv: | |
14 rv.append(int_val) | |
15 | |
16 return rv | |
17 | |
18 def parse_token(token): | |
19 values = strip_split(token, '-') | |
20 num_values = len(values) | |
21 | |
22 if num_values not in [1, 2]: | |
23 print >> sys.stderr, 'Error: "%s" is not a valid range' % token | |
24 sys.exit(1) | |
25 | |
26 int_list = [] | |
27 for value in values: | |
28 if value: | |
29 int_val = as_int(value) | |
30 | |
31 if int_val < 1: | |
32 print >> sys.stderr, 'Error: "%s" is not >= 1' % value | |
33 sys.exit(1) | |
34 | |
35 int_list.append(int_val) | |
36 else: | |
37 print >> sys.stderr, 'Error: "%s" is not a valid range' % token | |
38 sys.exit(1) | |
39 | |
40 if num_values == 1: | |
41 return int_list | |
42 | |
43 a, b = int_list | |
44 | |
45 if a <= b: | |
46 return range(a, b+1) | |
47 else: | |
48 return range(a, b-1, -1) | |
49 | |
50 def strip_split(string, delim): | |
51 return [elem.strip() for elem in string.split(delim)] | |
52 | |
53 def as_int(string): | |
54 try: | |
55 val = int(string) | |
56 except: | |
57 print >> sys.stderr, 'Error: "%s" does not appear to be an integer' % string | |
58 sys.exit(1) | |
59 return val | |
60 | |
61 def get_lines(filename): | |
62 rv = [] | |
63 | |
64 fh = open(filename) | |
65 for line in fh: | |
66 line = line.rstrip('\r\n') | |
67 rv.append(line) | |
68 fh.close() | |
69 | |
70 return rv | |
71 | |
72 def reorder(old_lines, new_order, filename): | |
73 max_index = len(old_lines) - 1 | |
74 | |
75 fh = open(filename, 'w') | |
76 | |
77 for index in new_order: | |
78 if index <= max_index: | |
79 print >> fh, old_lines[index] | |
80 old_lines[index] = None | |
81 | |
82 for line in old_lines: | |
83 if line is not None: | |
84 print >> fh, line | |
85 | |
86 fh.close() | |
87 | |
88 if len(sys.argv) != 4: | |
89 print >> sys.stderr, "Usage" | |
90 sys.exit(1) | |
91 | |
92 input, output, order_string = sys.argv[1:] | |
93 | |
94 new_order = parse_rangelist(order_string) | |
95 old_lines = get_lines(input) | |
96 reorder(old_lines, new_order, output) | |
97 | |
98 sys.exit(0) |