annotate metaMS/match_library.py @ 7:a1e3324dc244

added match_library.py to each project
author pieter.lukasse@wur.nl
date Thu, 19 Mar 2015 12:32:51 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
1 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
2 Containing functions are called from Galaxy to populate lists/checkboxes with selectable items
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
3 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
4 import csv
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
5 import glob
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
6 import os
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
7
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
8
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
9 __author__ = "Marcel Kempenaar"
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
10 __contact__ = "brs@nbic.nl"
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
11 __copyright__ = "Copyright, 2012, Netherlands Bioinformatics Centre"
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
12 __license__ = "MIT"
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
13
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
14 def get_column_type(library_file):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
15 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
16 Returns a Galaxy formatted list of tuples containing all possibilities for the
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
17 GC-column types. Used by the library_lookup.xml tool
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
18 @param library_file: given library file from which the list of GC-column types is extracted
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
19 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
20 if library_file == "":
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
21 galaxy_output = [("", "", False)]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
22 else:
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
23 (data, header) = read_library(library_file)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
24
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
25 if 'columntype' not in header:
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
26 raise IOError('Missing columns in ', library_file)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
27
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
28 # Filter data on column type
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
29 column_type = header.index("columntype")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
30 amounts_in_list_dict = count_occurrence([row[column_type] for row in data])
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
31 galaxy_output = [(str(a) + "(" + str(b) + ")", a, False) for a, b in amounts_in_list_dict.items()]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
32
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
33 return(galaxy_output)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
34
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
35
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
36 def filter_column(library_file, column_type_name):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
37 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
38 Filters the Retention Index database on column type
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
39 @param library_file: file containing the database
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
40 @param column_type_name: column type to filter on
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
41 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
42 if library_file == "":
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
43 galaxy_output = [("", "", False)]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
44 else:
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
45 (data, header) = read_library(library_file)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
46
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
47 if ('columntype' not in header or
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
48 'columnphasetype' not in header):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
49 raise IOError('Missing columns in ', library_file)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
50
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
51 column_type = header.index("columntype")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
52 statphase = header.index("columnphasetype")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
53
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
54 # Filter data on colunn type name
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
55 statphase_list = [line[statphase] for line in data if line[column_type] == column_type_name]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
56 amounts_in_list_dict = count_occurrence(statphase_list)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
57 galaxy_output = [(str(a) + "(" + str(b) + ")", a, False)for a, b in amounts_in_list_dict.items()]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
58
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
59 return(sorted(galaxy_output))
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
60
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
61
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
62 def filter_column2(library_file, column_type_name, statphase):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
63 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
64 Filters the Retention Index database on column type
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
65 @param library_file: file containing the database
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
66 @param column_type_name: column type to filter on
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
67 @param statphase: stationary phase of the column to filter on
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
68 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
69 if library_file == "":
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
70 galaxy_output = [("", "", False)]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
71 else:
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
72 (data, header) = read_library(library_file)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
73
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
74 if ('columntype' not in header or
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
75 'columnphasetype' not in header or
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
76 'columnname' not in header):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
77 raise IOError('Missing columns in ', library_file)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
78
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
79 column_type_column = header.index("columntype")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
80 statphase_column = header.index("columnphasetype")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
81 column_name_column = header.index("columnname")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
82
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
83 # Filter data on given column type name and stationary phase
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
84 statphase_list = [line[column_name_column] for line in data if line[column_type_column] == column_type_name and
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
85 line[statphase_column] == statphase]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
86 amounts_in_list_dict = count_occurrence(statphase_list)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
87 galaxy_output = [(str(a) + "(" + str(b) + ")", a, False)for a, b in amounts_in_list_dict.items()]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
88
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
89 return(sorted(galaxy_output))
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
90
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
91
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
92 def read_library(filename):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
93 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
94 Reads a CSV file and returns its contents and a normalized header
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
95 @param filename: file to read
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
96 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
97 data = list(csv.reader(open(filename, 'rU'), delimiter='\t'))
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
98 header_clean = [i.lower().strip().replace(".", "").replace("%", "") for i in data.pop(0)]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
99 return(data, header_clean)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
100
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
101
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
102
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
103 def get_directory_files(dir_name):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
104 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
105 Reads the directory and
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
106 returns the list of .txt files found as a dictionary
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
107 with file name and full path so that it can
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
108 fill a Galaxy drop-down combo box.
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
109
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
110 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
111 files = glob.glob(dir_name + "/*.*")
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
112 if len(files) == 0:
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
113 # Configuration error: no library files found in <galaxy-home-dir>/" + dir_name :
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
114 galaxy_output = [("Configuration error: expected file not found in <galaxy-home-dir>/" + dir_name, "", False)]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
115 else:
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
116 galaxy_output = [(str(get_file_name_no_ext(file_name)), str(os.path.abspath(file_name)), False) for file_name in files]
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
117 return(galaxy_output)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
118
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
119 def get_file_name_no_ext(full_name):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
120 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
121 returns just the last part of the name
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
122 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
123 simple_name = os.path.basename(full_name)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
124 base, ext = os.path.splitext(simple_name)
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
125 return base
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
126
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
127
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
128 def count_occurrence(data_list):
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
129 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
130 Counts occurrences in a list and returns a dict with item:occurrence
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
131 @param data_list: list to count items from
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
132 '''
a1e3324dc244 added match_library.py to each project
pieter.lukasse@wur.nl
parents:
diff changeset
133 return dict((key, data_list.count(key)) for key in set(data_list))