annotate maaslin_format_input_selector.py @ 8:e9677425c6c3 default tip

Updated the structure of the libraries
author george.weingart@gmail.com
date Mon, 09 Feb 2015 12:17:40 -0500
parents e0b5980139d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
1 #!/usr/bin/env python
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
2
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
3 """
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
4 Author: George Weingart
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
5 Description: Dynamically read columns from input file for UI
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
6 """
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
7
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
8 #####################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
9 #Copyright (C) <2012>
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
10 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
11 #Permission is hereby granted, free of charge, to any person obtaining a copy of
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
12 #this software and associated documentation files (the "Software"), to deal in the
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
13 #Software without restriction, including without limitation the rights to use, copy,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
14 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
15 #and to permit persons to whom the Software is furnished to do so, subject to
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
16 #the following conditions:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
17 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
18 #The above copyright notice and this permission notice shall be included in all copies
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
19 #or substantial portions of the Software.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
20 #
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
21 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
22 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
23 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
24 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
25 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
26 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
27 #####################################################################################
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
28
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
29 __author__ = "George Weingart"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
30 __copyright__ = "Copyright 2012"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
31 __credits__ = ["George Weingart"]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
32 __license__ = "MIT"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
33 __maintainer__ = "George Weingart"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
34 __email__ = "george.weingart@gmail.com"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
35 __status__ = "Development"
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
36
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
37 import sys,string,time
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
38 from pprint import pprint
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
39
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
40 def red(st,l):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
41 if len(st) <= l: return st
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
42 l1,l2 = l/2,l/2
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
43 return st[:l1]+".."+st[len(st)-l2:]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
44
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
45 def get_cols(data,full_names):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
46 if data == "": return []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
47 max_len =32
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
48 fname = data.dataset.file_name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
49 input_file = open(fname)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
50 input_lines = input_file.readlines()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
51 input_file.close()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
52 table_lines = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
53 for x in input_lines:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
54 first_column = x.split('\t')[0]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
55 table_lines.append(first_column)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
56
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
57 opt = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
58 rc = ''
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
59 lines = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
60 try:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
61 lines = [(red((rc+v.split()[0]),max_len),'%d' % (i+1),False) for i,v in enumerate(table_lines) if v]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
62
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
63 except:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
64 l1 = '*ALL*'
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
65 l2 = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
66 l3 = False
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
67 MyList = [l1,l2,l3]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
68 lines.append(MyList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
69 return opt+lines
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
70
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
71 def get_cols_add_line(data,full_names,lastmeta):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
72 if data == "": return []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
73 display_to = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
74 try:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
75 display_to = int(lastmeta)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
76 except:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
77 pass
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
78
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
79 max_len = 32
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
80 fname = data.dataset.file_name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
81 input_file = open(fname)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
82 input_lines = input_file.readlines()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
83 input_file.close()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
84 table_lines = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
85 for x in input_lines:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
86 first_column = x.split('\t')[0]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
87 table_lines.append(first_column)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
88 table_lines.insert(0,'-')
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
89 if not display_to == 1:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
90 del table_lines[display_to + 1:]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
91
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
92
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
93 opt = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
94 rc = ''
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
95 lines = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
96 try:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
97 lines = [(red((rc+v.split()[0]),max_len),'%d' % (i+1),False) for i,v in enumerate(table_lines) if v]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
98
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
99 except:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
100 l1 = '*ALL*'
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
101 l2 = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
102 l3 = False
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
103 MyList = [l1,l2,l3]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
104 lines.append(MyList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
105 return opt+lines
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
106
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
107 def get_cols_features(data,full_names,lastmeta):
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
108 if data == "": return []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
109 display_from = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
110 try:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
111 display_from = int(lastmeta)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
112 except:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
113 pass
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
114 max_len = 32
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
115 fname = data.dataset.file_name
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
116 input_file = open(fname)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
117 input_lines = input_file.readlines()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
118 input_file.close()
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
119 table_lines = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
120 for x in input_lines:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
121 first_column = x.split('\t')[0]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
122 table_lines.append(first_column)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
123
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
124 opt = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
125 rc = ''
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
126 del table_lines[:display_from]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
127 lines = []
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
128 try:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
129 lines = [(red((rc+v.split()[0]),max_len),'%d' % (i+1),False) for i,v in enumerate(table_lines) if v]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
130
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
131 except:
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
132 l1 = '*ALL*'
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
133 l2 = 1
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
134 l3 = False
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
135 MyList = [l1,l2,l3]
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
136 lines.append(MyList)
e0b5980139d9 maaslin
george-weingart
parents:
diff changeset
137 return opt+lines