Mercurial > repos > matces > carpet_toolsuite
annotate carpet-src-1/tools/CARPET/TSS_distance.py @ 0:cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author | matces |
---|---|
date | Tue, 07 Jun 2011 16:50:41 -0400 |
parents | |
children |
rev | line source |
---|---|
0
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
1 #!/usr/bin/env python |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
2 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
3 # Copyright 2009 Matteo Cesaroni, Lucilla Luzi |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
4 # |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
5 # This program is free software; ; you can redistribute it and/or modify |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
6 # it under the terms of the GNU Lesser General Public License as published by |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
7 # the Free Software Foundation; either version 3 of the License, or (at your |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
8 # option) any later version. |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
9 # |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
10 # This program is distributed in the hope that it will be useful, |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
13 # GNU General Public License for more details. |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
14 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
15 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
16 import sys |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
17 from rpy import * |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
18 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
19 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
20 def stop_err(msg): |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
21 sys.stderr.write(msg) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
22 sys.exit() |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
23 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
24 def main(): |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
25 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
26 # Handle input params |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
27 in_fname = sys.argv[1] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
28 out_fname = sys.argv[2] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
29 try: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
30 column = int( sys.argv[3] ) - 1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
31 except: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
32 stop_err( "..Column not specified, your query does not contain a column of numerical data." ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
33 title = sys.argv[4] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
34 xlab = sys.argv[5] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
35 breaks = int( sys.argv[6] ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
36 if breaks == 0: breaks = "Sturges" |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
37 if sys.argv[7] == "true": density = True |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
38 else: density = False |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
39 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
40 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
41 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
42 matrix = [] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
43 skipped_lines = 0 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
44 first_invalid_line = 0 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
45 invalid_value = '' |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
46 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
47 for i, line in enumerate( file( in_fname ) ): |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
48 valid = True |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
49 line = line.rstrip('\r\n') |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
50 # Skip comments |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
51 if line and not line.startswith( '#' ): |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
52 # Extract values and convert to floats |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
53 row = [] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
54 try: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
55 fields = line.split( "\t" ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
56 val = fields[column] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
57 if val.lower() == "na": |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
58 row.append( float( "nan" ) ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
59 if float(val) > float(xlab): |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
60 val = (float(xlab)+2000) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
61 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
62 row.append( float( val ) ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
63 except: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
64 valid = False |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
65 skipped_lines += 1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
66 if not first_invalid_line: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
67 first_invalid_line = i+1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
68 else: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
69 try: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
70 row.append( float( val ) ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
71 except ValueError: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
72 valid = False |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
73 skipped_lines += 1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
74 if not first_invalid_line: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
75 first_invalid_line = i+1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
76 invalid_value = fields[column] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
77 else: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
78 valid = False |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
79 skipped_lines += 1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
80 if not first_invalid_line: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
81 first_invalid_line = i+1 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
82 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
83 if valid: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
84 matrix.append( row ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
85 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
86 if skipped_lines < i: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
87 print "..on columnn %s" %sys.argv[3] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
88 try: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
89 a = array( matrix ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
90 r.pdf( out_fname, 8, 8 ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
91 r.hist( a, probability=True, main=title, xlab="TSS distance", breaks=breaks ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
92 if density: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
93 r.lines( r.density( a ) ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
94 r.dev_off() |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
95 except exc: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
96 stop_err("Building histogram resulted in error: %s." %str( exc )) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
97 else: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
98 print "..all values in column %s are non-numeric." %sys.argv[3] |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
99 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
100 if skipped_lines > 0: |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
101 print "..skipped %d invalid lines starting with line #%d. Value '%s' is not numeric." % ( skipped_lines, first_invalid_line, invalid_value ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
102 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
103 r.quit( save="no" ) |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
104 |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
105 if __name__ == "__main__": |
cdd489d98766
Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
matces
parents:
diff
changeset
|
106 main() |