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