annotate commons/tools/PrepareBatches.py @ 19:9bcfa7936eec

Deleted selected files
author m-zytnicki
date Mon, 29 Apr 2013 03:23:29 -0400
parents 94ab73e8a190
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
1 #!/usr/bin/env python
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
2
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
3 # Copyright INRA (Institut National de la Recherche Agronomique)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
4 # http://www.inra.fr
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
5 # http://urgi.versailles.inra.fr
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
6 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
7 # This software is governed by the CeCILL license under French law and
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
8 # abiding by the rules of distribution of free software. You can use,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
9 # modify and/ or redistribute the software under the terms of the CeCILL
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
10 # license as circulated by CEA, CNRS and INRIA at the following URL
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
11 # "http://www.cecill.info".
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
12 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
13 # As a counterpart to the access to the source code and rights to copy,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
14 # modify and redistribute granted by the license, users are provided only
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
15 # with a limited warranty and the software's author, the holder of the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
16 # economic rights, and the successive licensors have only limited
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
17 # liability.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
18 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
19 # In this respect, the user's attention is drawn to the risks associated
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
20 # with loading, using, modifying and/or developing or reproducing the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
21 # software by the user in light of its specific status of free software,
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
22 # that may mean that it is complicated to manipulate, and that also
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
23 # therefore means that it is reserved for developers and experienced
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
24 # professionals having in-depth computer knowledge. Users are therefore
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
25 # encouraged to load and test the software's suitability as regards their
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
26 # requirements in conditions enabling the security of their systems and/or
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
27 # data to be ensured and, more generally, to use and operate it in the
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
28 # same conditions as regards security.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
29 #
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
30 # The fact that you are presently reading this means that you have had
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
31 # knowledge of the CeCILL license and that you accept its terms.
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
32
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
33 import os
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
34 import sys
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
35 from ConfigParser import NoSectionError, NoOptionError
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
36 from commons.core.checker.CheckerUtils import CheckerUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
37 from commons.core.checker.CheckerException import CheckerException
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
38 from commons.core.utils.FileUtils import FileUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
39 from commons.core.seq.FastaUtils import FastaUtils
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
40
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
41 class PrepareBatches(object):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
42
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
43 def __init__(self, pipelineName, projectDir, projectName, iConfig, verbose):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
44 self._pipelineName = pipelineName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
45 self._projectDir = projectDir
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
46 self._projectName = projectName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
47 self._iConfig = iConfig
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
48 self._verbose = verbose
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
49
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
50 def run(self):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
51 if self._verbose > 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
52 print "beginning of step 1"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
53 sys.stdout.flush()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
54 if FileUtils.isRessourceExists("%s_db" % self._projectName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
55 print "ERROR: directory '%s_db' already exists" % self._projectName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
56 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
57
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
58 os.mkdir("%s_db" % self._projectName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
59 os.chdir("%s_db" % self._projectName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
60 genomeFastaFileName = "%s.fa" % self._projectName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
61 os.symlink("../%s" % genomeFastaFileName, genomeFastaFileName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
62 sectionName = "prepare_batches"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
63 self._checkConfig(sectionName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
64
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
65 separator = "\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
66 inGenomeFileHandler = open(genomeFastaFileName, "r")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
67 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
68 CheckerUtils.checkHeaders(inGenomeFileHandler)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
69 except CheckerException, e:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
70 print "Error in file %s. Wrong headers are :" % genomeFastaFileName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
71 print separator.join(e.messages)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
72 print "Authorized characters are : a-z A-Z 0-9 - . : _\n"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
73 inGenomeFileHandler.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
74 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
75 inGenomeFileHandler.close()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
76
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
77 doClean = False
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
78 if self._iConfig.get(sectionName, "clean") == "yes":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
79 doClean = True
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
80 chunkFilePrefix = "%s_chunks" % self._projectName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
81 chunkLength = int(self._iConfig.get(sectionName, "chunk_length"))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
82 chunkOverlap = int(self._iConfig.get(sectionName, "chunk_overlap"))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
83 FastaUtils.dbChunks(genomeFastaFileName, chunkLength, chunkOverlap, 0, chunkFilePrefix, doClean, self._verbose)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
84
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
85 nbSeq = int(self._iConfig.get(sectionName, "nb_seq_per_batch"))
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
86 FastaUtils.splitFastaFileInBatches("%s.fa" % chunkFilePrefix, nbSeq * chunkLength)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
87
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
88 if self._iConfig.get(sectionName, "clean") == "yes":
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
89 FileUtils.removeFilesByPattern("%s.fa*" % self._projectName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
90
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
91 os.chdir( ".." )
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
92 if self._verbose > 0:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
93 print "step 1 finished successfully"
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
94 sys.stdout.flush()
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
95
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
96 def _checkConfig(self, sectionName):
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
97 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
98 CheckerUtils.checkSectionInConfigFile(self._iConfig, sectionName)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
99 except NoSectionError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
100 print "ERROR: the section %s must be in your configuration file" % sectionName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
101 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
102 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
103 CheckerUtils.checkOptionInSectionInConfigFile(self._iConfig, sectionName, "chunk_length")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
104 except NoOptionError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
105 print "ERROR: the option 'chunk_length' must be defined in %s in your configuration file" % sectionName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
106 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
107 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
108 CheckerUtils.checkOptionInSectionInConfigFile(self._iConfig, sectionName, "chunk_overlap")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
109 except NoOptionError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
110 print "ERROR: the option 'chunk_overlap' must be defined in %s in your configuration file" % sectionName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
111 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
112 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
113 CheckerUtils.checkOptionInSectionInConfigFile(self._iConfig, sectionName, "nb_seq_per_batch")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
114 except NoOptionError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
115 print "ERROR: the option 'nb_seq_per_batch' must be defined in %s in your configuration file" % sectionName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
116 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
117 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
118 CheckerUtils.checkOptionInSectionInConfigFile(self._iConfig, sectionName, "resources")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
119 except NoOptionError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
120 print "ERROR: the option 'resources' must be defined in %s in your configuration file" % sectionName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
121 sys.exit(1)
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
122 try:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
123 CheckerUtils.checkOptionInSectionInConfigFile(self._iConfig, sectionName, "tmpDir")
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
124 except NoOptionError:
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
125 print "ERROR: the option 'tmpDir' must be defined in %s in your configuration file" % sectionName
94ab73e8a190 Uploaded
m-zytnicki
parents:
diff changeset
126 sys.exit(1)