source: trunk/brs2010p35/src/jag/file_fetch_and_write.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: 6.2 KB
Line 
1"""
2@author: maartenk, esther lips
3
4file handling script
5"""
6
7import jag.common as common
8import os
9import shutil
10import re
11import glob
12import time
13import logging as log
14import sys
15import tempfile
16
17
18class InAndOut(object):
19    """
20    class to handle reading and writing of files
21    """
22    def __init__(self):
23        self.out = os.path.abspath(os.path.curdir) + "/"
24        self.time_extention = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
25        self.run_rproject = True
26       
27   
28    def set_outfile(self, out_str):
29        """
30        set a path prefix in the same way plink --out functions
31        """
32        if (not out_str == None):
33            full_path = os.path.abspath(out_str.strip())                #get full pathname
34            full_dir = os.path.dirname(full_path)
35            #check dir exists and create if not exists and check if dir is writable
36            if (_create_output_dir(full_dir) and _check_writability(full_dir)):
37                #if only a directory is set, do not include a zero in out path
38                if (os.path.basename(full_path) == ""):
39                    self.out = full_path
40                else:   
41                    self.out = full_path + "."
42            else:
43                print("Jag stopped")
44                sys.exit(1)
45   
46        return True
47
48   
49    def save_sumlog(self, assoc_file, clusterresults):
50        """
51        save sumlog file to disk.
52        """
53
54        output_number = get_number_of_assoc_file(assoc_file)
55        filename = self.create_assoc_analysis_filename(output_number)
56        results_as_text = clusterresults.format_results()
57               
58        if os.path.exists(filename):
59            _remove_file([filename2 for filename2 in self.get_sumlog_filenames()])
60
61        common.save_text_file(filename, results_as_text)
62        log.info("Saved sumlog file as " + filename)
63
64
65    def copy_assoc_file(self, assoc_file):
66        """
67            copy assoc file to directory to where program is ran from
68   
69        """
70        #get base extension e.g. qassoc,assoc assoc.linear
71        base_extention = common.plink_results_extention(assoc_file)
72        output_number = get_number_of_assoc_file(assoc_file)
73        assoc_files = []
74        extentions = ["", ".adjusted"]
75       
76        for ext in extentions:
77            if (os.path.exists(assoc_file + ext)):
78                dest_assoc_file = self.out + "results" + output_number + "." + base_extention + ext
79               
80                if os.path.exists(dest_assoc_file):
81                    _remove_file(dest_assoc_file)
82                shutil.copy(assoc_file, dest_assoc_file)
83                os.path.abspath(os.path.curdir)
84                log.info("\nResults of PLINK saved as " + dest_assoc_file)
85                assoc_files.append(dest_assoc_file)
86           
87            else:
88                pass
89           
90        return(assoc_files)
91
92
93    def copy_prune_in_file(self, temp_file):
94        """
95            copy prun.in file to directory to where program is ran from
96   
97        """
98        prune_file_name = temp_file + ".prune.in"
99       
100        if (os.path.exists(prune_file_name)):
101            dest_file = self.out + "prune.in"
102           
103            if os.path.exists(dest_file):
104                _remove_file(dest_file)
105            shutil.copy(prune_file_name, dest_file)
106            os.path.abspath(os.path.curdir)
107           
108        else:
109            log.info(prune_file_name + " does not exists")
110           
111        return(dest_file)
112
113
114    def create_assoc_analysis_filename(self, number):
115        """
116        create a full path for a assoc analysis file based on number
117        """
118        return self.out + "sumlog" + str(number) + ".out"
119
120
121    def get_sumlog_filenames(self):
122        """
123        get association analysis filename from the current directory
124        with a format like sumlog*.out
125        """
126
127        files = glob.glob(self.out + "sumlog*.out")
128        test = re.compile("sumlog(\.[Pp]\d{0,3}){0,1}\.out$")
129        files_clean = [x for x in files if test.search(x) != None]
130
131        return files_clean
132
133
134    def save_text_to_filename(self, filename, text):
135        """save random draws """
136        filename = self.out + filename
137       
138        if os.path.exists(filename):
139            _remove_file(filename)
140           
141        common.save_text_file(filename, text)
142       
143        return(filename)
144
145
146def get_assoc_file_number(resultfile, assoc_file):
147    """
148    get the number of the assoc file. If not found return 1
149    result file is the base temp file
150    assoc it the whole path of the files
151    """
152   
153    resultfile = re.escape(resultfile)
154    match = re.search(resultfile + "(\.P(?P<number>\d{0,3})){0,1}\.(q){0,1}assoc", assoc_file)
155
156    number = match.group("number")
157
158    if (number == None):
159        number = 1
160       
161    return number
162
163
164def get_number_of_assoc_file(assoc_file):
165    """
166    get file number from assoc file
167    """
168   
169    match = re.search("(?P<number>\.P\d{0,3}){0,1}\.(q){0,1}assoc*", assoc_file)
170    output_number = match.group("number")
171   
172    if(output_number == None):
173        output_number = ".P1"
174       
175    return output_number
176
177
178def _remove_file(path):
179    """
180    removes path and gives message
181    input can be a string with path or a list with paths
182    """
183    if type(path).__name__ == "list":
184        for path_item in path:
185            os.remove(path_item)
186
187    else:
188        os.remove(path)
189
190
191def _check_writability(dir_path):
192    """
193    check if a directory is writable by writing a temp file to a file.
194    This file is  removed
195    returns true is is writable
196    """
197    try:
198        temp_file = tempfile.TemporaryFile(dir=dir_path)
199        temp_file.write('This file can be deleted.This is used by JAG to check\
200            writability of a directory')
201        temp_file.close()
202    except OSError:
203        print ("directory not writable")
204        return False
205    else:
206        return True
207   
208   
209def _create_output_dir(full_dir):
210    """
211    create dir if not already exists
212    """
213   
214    if not os.path.isdir(full_dir):
215        try:
216            print("Creating the following directory: " + full_dir)
217            os.makedirs(full_dir)
218           
219        except OSError:
220            print("Could not create directory: " + full_dir + " ")
221            return False
222       
223    return True
224
Note: See TracBrowser for help on using the repository browser.