Mercurial > repos > rmarenco > hubarchivecreator
comparison util/subtools.py @ 1:fb5e60d4d18a draft
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 64cfc08088d11f6818c1b4e5514ef9e67969eaff-dirty
author | rmarenco |
---|---|
date | Wed, 13 Jul 2016 13:36:37 -0400 |
parents | |
children | acc233161f50 |
comparison
equal
deleted
inserted
replaced
0:0f3bc17e5ede | 1:fb5e60d4d18a |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf8 -*- | |
3 | |
4 """ | |
5 This class handles the subprocess calls of the different tools used | |
6 in HubArchiveCreator | |
7 """ | |
8 | |
9 import os | |
10 import subprocess | |
11 | |
12 | |
13 def _handleExceptionAndCheckCall(array_call, **kwargs): | |
14 """ | |
15 This class handle exceptions and call the tool. | |
16 It maps the signature of subprocess.check_call: | |
17 See https://docs.python.org/2/library/subprocess.html#subprocess.check_call | |
18 """ | |
19 stdin = kwargs.get('stdin') | |
20 stdout = kwargs.get('stdout') | |
21 stderr = kwargs.get('stderr') | |
22 shell = kwargs.get('shell') | |
23 try: | |
24 p = subprocess.check_call(array_call, stdin=stdin, stdout=stdout, stderr=stderr, shell=shell) | |
25 except subprocess.CalledProcessError: | |
26 raise | |
27 return p | |
28 | |
29 | |
30 def twoBitInfo(two_bit_file_name, two_bit_info_file): | |
31 """ | |
32 Call twoBitInfo and write the result into twoBit_info_file | |
33 :param two_bit_file_name: | |
34 :param two_bit_info_file: | |
35 :return the subprocess.check_call return object: | |
36 """ | |
37 array_call = ['twoBitInfo', two_bit_file_name, two_bit_info_file] | |
38 p = _handleExceptionAndCheckCall(array_call) | |
39 return p | |
40 | |
41 | |
42 def faToTwoBit(fasta_file_name, mySpecieFolder): | |
43 """ | |
44 This function call faToTwoBit UCSC tool, and return the twoBitFile | |
45 :param fasta_file_name: | |
46 :param mySpecieFolder: | |
47 :return: | |
48 """ | |
49 baseNameFasta = os.path.basename(fasta_file_name) | |
50 suffixTwoBit, extensionTwoBit = os.path.splitext(baseNameFasta) | |
51 nameTwoBit = suffixTwoBit + '.2bit' | |
52 | |
53 with open(os.path.join(mySpecieFolder, nameTwoBit), 'w') as twoBitFile: | |
54 array_call = ['faToTwoBit', fasta_file_name, twoBitFile.name] | |
55 _handleExceptionAndCheckCall(array_call) | |
56 | |
57 return twoBitFile | |
58 | |
59 | |
60 def gtfToGenePred(input_gtf_file_name, gene_pred_file_name): | |
61 """ | |
62 Call gtfToGenePred and write the result into gene_pred_file_name | |
63 :param input_gtf_file_name: | |
64 :param gene_pred_file_name: | |
65 :return: | |
66 """ | |
67 array_call = ['gtfToGenePred', input_gtf_file_name, gene_pred_file_name] | |
68 p = _handleExceptionAndCheckCall(array_call) | |
69 return p | |
70 | |
71 | |
72 def gff3ToGenePred(input_gff3_file_name, gene_pred_file_name): | |
73 """ | |
74 Call gff3ToGenePred and write the result into gene_pred_file_name | |
75 :param input_gff3_file_name: | |
76 :param gene_pred_file_name: | |
77 :return: | |
78 """ | |
79 array_call = ['gff3ToGenePred', input_gff3_file_name, gene_pred_file_name] | |
80 p = _handleExceptionAndCheckCall(array_call) | |
81 return p | |
82 | |
83 | |
84 def genePredToBed(gene_pred_file_name, unsorted_bed_file_name): | |
85 """ | |
86 Call genePredToBed and write the result into unsorted_bed_file_name | |
87 :param gene_pred_file_name: | |
88 :param unsorted_bed_file_name: | |
89 :return: | |
90 """ | |
91 array_call = ['genePredToBed', gene_pred_file_name, unsorted_bed_file_name] | |
92 p = _handleExceptionAndCheckCall(array_call) | |
93 return p | |
94 | |
95 | |
96 def sort(unsorted_bed_file_name, sorted_bed_file_name): | |
97 """ | |
98 Call sort with -k1,1 -k2,2n on unsorted_bed_file_name and write the result into sorted_bed_file_name | |
99 :param unsorted_bed_file_name: | |
100 :param sorted_bed_file_name: | |
101 :return: | |
102 """ | |
103 array_call = ['sort', '-k', '1,1', '-k', '2,2n', unsorted_bed_file_name, '-o', sorted_bed_file_name] | |
104 p = _handleExceptionAndCheckCall(array_call) | |
105 return p | |
106 | |
107 | |
108 def sortChromSizes(two_bit_info_file_name, chrom_sizes_file_name): | |
109 """ | |
110 Call sort with -k2rn on two_bit_info_file_name and write the result into chrom_sizes_file_name | |
111 :param two_bit_info_file_name: | |
112 :param chrom_sizes_file_name: | |
113 :return: | |
114 """ | |
115 array_call = ['sort', '-k2rn', two_bit_info_file_name, '-o', chrom_sizes_file_name] | |
116 p = _handleExceptionAndCheckCall(array_call) | |
117 return p | |
118 | |
119 | |
120 def bedToBigBed(sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name, typeOption=None, autoSql=None): | |
121 """ | |
122 Call bedToBigBed on sorted_bed_file_name, using chrom_sizes_file_name and write the result into big_bed_file_name | |
123 :param sorted_bed_file_name: | |
124 :param chrom_sizes_file_name: | |
125 :param big_bed_file_name: | |
126 :return: | |
127 """ | |
128 array_call = ['bedToBigBed', sorted_bed_file_name, chrom_sizes_file_name, big_bed_file_name] | |
129 if typeOption: | |
130 array_call.append(typeOption) | |
131 if autoSql: | |
132 array_call.append(autoSql) | |
133 | |
134 p = _handleExceptionAndCheckCall(array_call) | |
135 return p | |
136 | |
137 | |
138 def sortBam(input_bam_file_name, output_sorted_bam_name): | |
139 """ | |
140 Call samtools on input_bam_file_name and output the result in output_sorted_bam_name | |
141 :param input_bam_file_name: | |
142 :param output_sorted_bam_name: | |
143 :return: | |
144 """ | |
145 array_call = ['samtools', 'sort', input_bam_file_name, '-o', output_sorted_bam_name] | |
146 p = _handleExceptionAndCheckCall(array_call) | |
147 return p | |
148 | |
149 | |
150 def createBamIndex(input_sorted_bam_file_name, output_name_index_name): | |
151 """ | |
152 Call `samtools index` on imput_sorted_bam_file_name and output the result in output_name_index_name | |
153 :param input_sorted_bam_file_name: | |
154 :param output_name_index_name: | |
155 :return: | |
156 """ | |
157 array_call = ['samtools', 'index', input_sorted_bam_file_name, output_name_index_name] | |
158 p = _handleExceptionAndCheckCall(array_call) | |
159 return p |