annotate TEisotools-1.1.a/TEiso/Bedtools_closest.py @ 13:feef9a0db09d draft

Uploaded
author urgi-team
date Wed, 20 Jul 2016 09:04:42 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
1 #!/usr/bin/env python
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
2
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
3 # Copyright INRA (Institut National de la Recherche Agronomique)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
4 # http://www.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
5 # http://urgi.versailles.inra.fr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
6 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
7 # This software is governed by the CeCILL license under French law and
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
8 # abiding by the rules of distribution of free software. You can use,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
9 # modify and/ or redistribute the software under the terms of the CeCILL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
10 # license as circulated by CEA, CNRS and INRIA at the following URL
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
11 # "http://www.cecill.info".
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
12 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
13 # As a counterpart to the access to the source code and rights to copy,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
14 # modify and redistribute granted by the license, users are provided only
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
15 # with a limited warranty and the software's author, the holder of the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
16 # economic rights, and the successive licensors have only limited
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
17 # liability.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
18 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
19 # In this respect, the user's attention is drawn to the risks associated
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
20 # with loading, using, modifying and/or developing or reproducing the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
21 # software by the user in light of its specific status of free software,
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
22 # that may mean that it is complicated to manipulate, and that also
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
23 # therefore means that it is reserved for developers and experienced
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
24 # professionals having in-depth computer knowledge. Users are therefore
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
25 # encouraged to load and test the software's suitability as regards their
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
26 # requirements in conditions enabling the security of their systems and/or
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
27 # data to be ensured and, more generally, to use and operate it in the
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
28 # same conditions as regards security.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
29 #
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
30 # The fact that you are presently reading this means that you have had
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
31 # knowledge of the CeCILL license and that you accept its terms.
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
32
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
33 import os
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
34 from commons.core.checker.CheckerUtils import CheckerUtils
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
35 from commons.core.utils.RepetOptionParser import RepetOptionParser
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
36 from commons.core.utils.FileUtils import FileUtils
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
37 import subprocess
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
38
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
39 class Bedtools_closest(object):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
40
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
41 def __init__(self, input_file_A = "", input_file_B = "", output_file = "", verbosity = 3):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
42 self._input_file_A = input_file_A
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
43 self._input_file_B = input_file_B
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
44 self._output_file = output_file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
45 self._verbosity = verbosity
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
46
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
47 def setAttributesFromCmdLine(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
48 description = "For each feature in A, finds the closest feature (upstream or downstream) in B.\n"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
49 usage = " Bedtools_closest [OPTIONS] -a <bed/gff/vcf> -b <bed/gff/vcf> -o <output_dir> \n"
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
50 parser = RepetOptionParser(description = description, usage = usage)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
51 parser.add_option( '-a', '--input_file_A', dest='input_file_A', help='bed/gff/vcf' )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
52 parser.add_option( '-b', '--input_file_B', dest='input_file_B', help='bed/gff/vcf' )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
53 parser.add_option( '-o', '--output_file', dest='output_file', help='write all output in this file/bed/gff/vcf', default = "")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
54 options, args = parser.parse_args()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
55 self.setAttributesFromOptions(options)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
56
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
57 def setAttributesFromOptions(self, options):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
58 self._input_file_A = options.input_file_A
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
59 self._input_file_B = options.input_file_B
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
60 self._output_file = options.output_file
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
61
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
62 def checkExecutables(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
63 if not CheckerUtils.isExecutableInUserPath("bedtools"):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
64 raise Exception("ERROR: bedtools must be in your path")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
65
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
66 def checkOptions(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
67 if self._input_file_A != "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
68 if not FileUtils.isRessourceExists(self._input_file_A ):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
69 raise Exception("ERROR: reference file %s does not exist!" % self._input_file_A )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
70 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
71 raise Exception("ERROR: No specified -a option!")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
72
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
73 if self._input_file_B != "":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
74 if not FileUtils.isRessourceExists(self._input_file_B):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
75 raise Exception("ERROR: reference file %s does not exist!" % self._input_file_B )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
76 else:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
77 raise Exception("ERROR: No specified -b option!")
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
78
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
79 def getbedtoolsclosestCmd(self, file_A, file_B, output_file):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
80 cmd = 'bedtools closest -a %s -b %s -d -D a > %s' % (file_A,file_B, output_file)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
81 ##print cmd
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
82 return cmd
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
83
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
84 def run(self):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
85 self.checkExecutables()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
86 self.checkOptions()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
87 sortfileA = "%s.sorted" % self._input_file_A
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
88 sortfileB = "%s.sorted" % self._input_file_B
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
89 os.system("bedtools sort -i %s > %s " % (self._input_file_A, sortfileA))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
90 os.system("bedtools sort -i %s > %s " % (self._input_file_B, sortfileB))
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
91
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
92 try:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
93 if os.path.exists(self._output_file):
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
94 raise Exception("ERROR: %s already exists." % self._output_file)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
95
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
96 cmd_bedtoolsclosest = self.getbedtoolsclosestCmd(sortfileA, sortfileB, self._output_file)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
97 ## hide output of subprocess: stdout = index_dir_stderr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
98 fstdout = open( "bedtools_closest.log" , 'w' )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
99 process = subprocess.Popen(cmd_bedtoolsclosest, shell = True, stdout = fstdout, stderr=subprocess.STDOUT)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
100 returncode = process.wait()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
101 fstdout.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
102 # get stderr, allowing for case where it's very large
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
103 fstdout = open("bedtools_closest.log", 'rb' )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
104 stderr = ''
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
105 buffsize = 1048576
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
106 try:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
107 while True:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
108 stderr += fstdout.read( buffsize )
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
109 if not stderr or len( stderr ) % buffsize != 0:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
110 break
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
111 except OverflowError:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
112 pass
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
113 fstdout.close()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
114 if returncode != 0:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
115 raise Exception, stderr
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
116 #os.system("mv cufflinks.log %s/cufflinks.log " % workingDir)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
117 except Exception:
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
118 raise Exception("ERROR in %s " % cmd_bedtoolsclosest)
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
119
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
120
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
121 if __name__ == "__main__":
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
122 iLaunch = Bedtools_closest()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
123 iLaunch.setAttributesFromCmdLine()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
124 iLaunch.run()
feef9a0db09d Uploaded
urgi-team
parents:
diff changeset
125