source: trunk/brs2010p35/src/jag/mergepermutations.py @ 2

Last change on this file since 2 was 2, checked in by tim.te.beek@…, 8 years ago

Add revision 338 snapshot of previous SVN instance, omitting PLINK binaries

File size: 4.3 KB
Line 
1'''
2Created on Feb 3, 2011
3
4@author: maartenk, esther lips
5'''
6
7from jag.clusterresults import Clusterresults
8from jag.permutationresults import PermutatedResults
9import jag.common as common
10import glob
11import os
12import re
13import sys
14import logging as log
15
16
17class MergePermutation:
18    """
19    Handles the results of all permutations
20    """
21    def __init__(self, inout):
22        self.inout = inout
23        self.current_path = os.path.abspath(os.path.curdir) + "/"
24        self.files = {}
25
26
27    def _get_ordered_files(self):
28        """
29        get all permuted files and return it in a dict with as key phenotype and a list of filenames
30        """
31        files = glob.glob(self.inout.out + "perm.*.out")   #get perm * files
32
33        if (len(files) < 1):
34            sys.exit("Only " + str(len(files)) + " " + self.inout.out + \
35                     "perm.*.out files found: did you run some permutations?")
36        ordered_results = _get_ordered_files_from_list(files)
37        return (ordered_results)
38
39
40    def _get_ordered_results(self):
41        """
42        returns dictionary of results by phenotype
43        """
44        ordered_results = self._get_ordered_files()
45       
46        for pheno_name  in ordered_results.keys():  #read the files
47            permutated = PermutatedResults()
48            permutated.read_permout(ordered_results[pheno_name])
49            ordered_results[pheno_name] = permutated
50
51        return ordered_results
52
53
54    def _load_sumlog_files(self):
55        """
56        read sumlog files
57        """
58       
59        sumlogfiles = glob.glob(self.inout.out + "sumlog.*.out")
60        test = re.compile("sumlog\.(?P<pheno>.*)\.out$")
61        files_clean = [x for x in sumlogfiles if test.search(x) != None]
62        aa_result = {}
63       
64        for file_clean in files_clean:
65            aa_result[test.search(file_clean).group("pheno")] = file_clean
66
67        return aa_result
68
69
70    def mergeresults(self):
71        """
72        function to merge multiple permutation files into one file
73        """
74        ordered_results = self._get_ordered_results()
75        #merge each phenotype
76
77        #load sumlog files to create sumlog files
78        aa_result = self._load_sumlog_files()
79
80        for key in ordered_results.keys():
81
82            #save permutions scores
83            perm_out_string = ordered_results[key].format_permout()
84            perm_filename = "perm.merged." + key + ".out"
85            perm_out_filename = self.inout.save_text_to_filename(perm_filename, perm_out_string)
86            log.info("Saved merged permutations as " + perm_out_filename)
87            #save empical P file is sumlog files is found
88            if (aa_result.has_key(key)):
89
90                aa_object = Clusterresults()
91                aa_object.read_formated_results(common.getfile_handle(aa_result[key]))
92
93                empp_out_as_text = ordered_results[key].format_permutated_results(aa_object)
94                emp_filename = "empp.merged." + key + ".out"
95                empp_filename = self.inout.save_text_to_filename(emp_filename, empp_out_as_text)
96                log.info("Saved empirical pvalues as " + empp_filename)
97                #call R for distribution plot
98                self.files[key] = {"perm":perm_out_filename, "empp":empp_filename}
99
100            else:
101                sys.exit("Could not find right sumlog file(" + self.inout.out + "sumlog." + key + ".out)")
102               
103               
104def _get_ordered_files_from_list(files):
105    """
106    files:a list of filenames
107    return:  a dict with as key phenotype and a list of filenames
108    """
109    # Skip 'merged' files, but accept manually entered seeds of any length
110    test_merged = re.compile("perm\.merged\.(?P<pheno>.*)\.out$")
111    files_clean = [x for x in files if test_merged.search(x) is  None]
112
113    #filter other none correct files
114    test = re.compile("perm\..{1,8}\.(?P<pheno>.*)\.out$")
115    files_clean = [x for x in files_clean if test.search(x) != None]
116    log.info("Found " + str(len(files_clean)) + " permutation files")
117
118    phenotypes = set([test.search(x).group("pheno") for x in files_clean])
119    ordered_results = {}
120   
121    for phenotype in phenotypes:
122        test_pheno = re.compile("perm\..{1,8}\." + str(phenotype) + "\.out$")
123        pheno_type_files = [x for x in files_clean if test_pheno.search(x) != None]
124        ordered_results[phenotype] = pheno_type_files
125       
126    return ordered_results
127
Note: See TracBrowser for help on using the repository browser.