annotate util/subtools.py @ 0:bc00f5c4c59e draft

planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
author yating-l
date Tue, 17 Oct 2017 17:28:05 -0400
parents
children 2ae1e96a8380
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
1 #!/usr/bin/env python
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
2
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
3 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
4 This file include common used functions for converting file format to gff3
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
5 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
6 from collections import OrderedDict
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
7 import json
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
8 import subprocess
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
9 import os
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
10 import sys
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
11 import tempfile
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
12 import string
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
13 import logging
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
14
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
15 class PopenError(Exception):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
16 def __init__(self, cmd, error, return_code):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
17 self.cmd = cmd
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
18 self.error = error
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
19 self.return_code = return_code
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
20
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
21 def __str__(self):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
22 message = "The subprocess {0} has returned the error: {1}.".format(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
23 self.cmd, self.return_code)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
24 message = ','.join(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
25 (message, "Its error message is: {0}".format(self.error)))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
26 return repr(message)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
27
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
28
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
29 def _handleExceptionAndCheckCall(array_call, **kwargs):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
30 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
31 This class handle exceptions and call the tool.
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
32 It maps the signature of subprocess.check_call:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
33 See https://docs.python.org/2/library/subprocess.html#subprocess.check_call
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
34 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
35 stdout = kwargs.get('stdout', subprocess.PIPE)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
36 stderr = kwargs.get('stderr', subprocess.PIPE)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
37 shell = kwargs.get('shell', False)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
38 stdin = kwargs.get('stdin', None)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
39
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
40 cmd = array_call[0]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
41
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
42 output = None
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
43 error = None
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
44
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
45 # TODO: Check the value of array_call and <=[0]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
46 logging.debug("Calling {0}:".format(cmd))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
47 logging.debug("%s", array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
48 logging.debug("---------")
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
49
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
50 # TODO: Use universal_newlines option from Popen?
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
51 try:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
52 p = subprocess.Popen(array_call, stdout=stdout,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
53 stderr=stderr, shell=shell, stdin=stdin)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
54
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
55 # TODO: Change this because of possible memory issues => https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
56
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
57 output, error = p.communicate()
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
58
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
59 if stdout == subprocess.PIPE:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
60 logging.debug("\t{0}".format(output))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
61 else:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
62 logging.debug("\tOutput in file {0}".format(stdout.name))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
63 # If we detect an error from the subprocess, then we raise an exception
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
64 # TODO: Manage if we raise an exception for everything, or use CRITICAL etc... but not stop process
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
65 # TODO: The responsability of returning a sys.exit() should not be there, but up in the app.
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
66 if p.returncode:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
67 if stderr == subprocess.PIPE:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
68 raise PopenError(cmd, error, p.returncode)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
69 else:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
70 # TODO: To Handle properly with a design behind, if we received a option as a file for the error
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
71 raise Exception("Error when calling {0}. Error as been logged in your file {1}. Error code: {2}"
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
72 .format(cmd, stderr.name, p.returncode))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
73
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
74 except OSError as e:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
75 message = "The subprocess {0} has encountered an OSError: {1}".format(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
76 cmd, e.strerror)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
77 if e.filename:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
78 message = '\n'.join(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
79 (message, ", against this file: {0}".format(e.filename)))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
80 logging.error(message)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
81 sys.exit(-1)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
82 except PopenError as p:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
83 message = "The subprocess {0} has returned the error: {1}.".format(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
84 p.cmd, p.return_code)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
85 message = '\n'.join(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
86 (message, "Its error message is: {0}".format(p.error)))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
87
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
88 logging.exception(message)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
89
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
90 sys.exit(p.return_code)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
91 except Exception as e:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
92 message = "The subprocess {0} has encountered an unknown error: {1}".format(
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
93 cmd, e)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
94 logging.exception(message)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
95
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
96 sys.exit(-1)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
97 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
98
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
99
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
100 def write_features(field, attribute, gff3):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
101 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
102 The function write the features to gff3 format (defined in https://github.com/The-Sequence-Ontology/Specifications/blob/master/gff3.md)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
103 field, attribute are ordered dictionary
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
104 gff3 is the file handler
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
105 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
106 attr = []
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
107 for v in field.values():
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
108 gff3.write(str(v) + '\t')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
109 for k, v in attribute.items():
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
110 s = str(k) + '=' + str(v)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
111 attr.append(s)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
112 gff3.write(';'.join(attr))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
113 gff3.write('\n')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
114
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
115 def twoBitInfo(two_bit_file_name, two_bit_info_file):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
116 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
117 Call twoBitInfo and write the result into twoBit_info_file
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
118 :param two_bit_file_name:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
119 :param two_bit_info_file:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
120 :return the subprocess.check_call return object:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
121 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
122 array_call = ['twoBitInfo', two_bit_file_name, two_bit_info_file]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
123 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
124 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
125
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
126
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
127 def faToTwoBit(fasta_file_name, twoBitFile):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
128 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
129 This function call faToTwoBit UCSC tool, and return the twoBitFile
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
130 :param fasta_file_name:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
131 :param mySpecieFolder:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
132 :return:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
133 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
134
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
135 array_call = ['faToTwoBit', fasta_file_name, twoBitFile]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
136 _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
137
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
138 return twoBitFile
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
139
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
140 def sortChromSizes(two_bit_info_file_name, chrom_sizes_file_name):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
141 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
142 Call sort with -k2rn on two_bit_info_file_name and write the result into chrom_sizes_file_name
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
143 :param two_bit_info_file_name:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
144 :param chrom_sizes_file_name:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
145 :return:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
146 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
147 array_call = ['sort', '-k2rn', two_bit_info_file_name,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
148 '-o', chrom_sizes_file_name]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
149 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
150 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
151
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
152 def getChromSizes(reference, tool_dir):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
153 #TODO: find a better way instead of shipping the two exec files with the tool
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
154 faToTwoBit = os.path.join(tool_dir, 'faToTwoBit')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
155 twoBitInfo = os.path.join(tool_dir, 'twoBitInfo')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
156 try:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
157 twoBitFile = tempfile.NamedTemporaryFile(bufsize=0)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
158 chrom_sizes = tempfile.NamedTemporaryFile(bufsize=0, suffix='.chrom.sizes', delete=False)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
159 except IOError as err:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
160 print "Cannot create tempfile err({0}): {1}".format(err.errno, err.strerror)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
161 try:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
162 subprocess.call(['faToTwoBit', reference, twoBitFile.name])
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
163 except OSError as err:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
164 print "Cannot generate twoBitFile from faToTwoBit err({0}): {1}".format(err.errno, err.strerror)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
165 try:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
166 subprocess.call(['twoBitInfo', twoBitFile.name, chrom_sizes.name])
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
167 except OSError as err:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
168 print "Cannot generate chrom_sizes from twoBitInfo err({0}): {1}".format(err.errno, err.strerror)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
169 return chrom_sizes
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
170
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
171 def sequence_region(chrom_sizes):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
172 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
173 This function read from a chromatin size file generated by twoBitInfo and write the information to dict
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
174 return a dict
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
175 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
176 f = open(chrom_sizes, 'r')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
177 sizes = f.readlines()
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
178 sizes_dict = {}
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
179 for line in sizes:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
180 chrom_info = line.rstrip().split('\t')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
181 sizes_dict[chrom_info[0]] = chrom_info[1]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
182 return sizes_dict
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
183
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
184 def child_blocks(parent_field, parent_attr, gff3, child_type):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
185 num = 0
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
186 blockcount = int(parent_attr['blockcount'])
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
187 chromstart = parent_attr['chromstarts'].split(',')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
188 blocksize = parent_attr['blocksizes'].split(',')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
189 parent_start = parent_field['start']
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
190 while num < blockcount:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
191 child_attr = OrderedDict()
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
192 child_field = parent_field
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
193 child_field['type'] = child_type
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
194 child_field['start'] = int(chromstart[num]) + int(parent_start)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
195 child_field['end'] = int(child_field['start']) + int(blocksize[num]) - 1
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
196 child_attr['ID'] = parent_attr['ID'] + '_part_' + str(num+1)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
197 child_attr['Parent'] = parent_attr['ID']
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
198 write_features(child_field, child_attr, gff3)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
199 num = num + 1
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
200
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
201 def add_tracks_to_json(trackList_json, new_tracks, modify_type):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
202 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
203 Add to track configuration (trackList.json)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
204 # modify_type = 'add_tracks': add a new track like bam or bigwig, new_track = dict()
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
205 # modify_type = 'add_attr': add configuration to the existing track, new_track = dict(track_name: dict())
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
206 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
207 with open(trackList_json, 'r+') as f:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
208 data = json.load(f)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
209 if modify_type == 'add_tracks':
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
210 data['tracks'].append(new_tracks)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
211 elif modify_type == 'add_attr':
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
212 for k in new_tracks:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
213 for track in data['tracks']:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
214 if k.lower() in track['urlTemplate'].lower():
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
215 attr = new_tracks[k]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
216 for k, v in attr.items():
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
217 track[k] = v
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
218 f.seek(0, 0)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
219 f.write(json.dumps(data, separators=(',' , ':'), indent=4))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
220 f.truncate()
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
221 f.close()
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
222
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
223
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
224 def createBamIndex(bamfile):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
225 subprocess.call(['samtools', 'index', bamfile])
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
226 filename = bamfile + '.bai'
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
227 if os.path.exists(filename):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
228 return filename
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
229 else:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
230 raise ValueError('Did not find bai file')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
231
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
232 def flatfile_to_json(inputFile, dataType, trackType, trackLabel, outputFolder, options=None, compress=False):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
233 if "bed" in dataType:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
234 fileType = "--bed"
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
235 elif "gff" in dataType:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
236 fileType = "--gff"
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
237 else:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
238 raise ValueError("%s is not a valid filetype for flatfile_to_json" % dataType)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
239
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
240
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
241 array_call = ['flatfile-to-json.pl',
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
242 fileType, inputFile,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
243 '--trackType', trackType,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
244 '--trackLabel', trackLabel,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
245 '--out', outputFolder]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
246 if compress:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
247 array_call.append('--compress')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
248 if options:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
249 config = options.get("config")
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
250 clientConfig = options.get("clientConfig")
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
251 renderClassName = options.get('renderClassName')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
252 subfeatureClasses = options.get('subfeatureClasses')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
253 load_type = options.get("type")
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
254 if clientConfig:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
255 array_call.append('--clientConfig')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
256 array_call.append(clientConfig)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
257 if config:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
258 array_call.append('--config')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
259 array_call.append(config)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
260 if load_type:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
261 array_call.append('--type')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
262 array_call.append(load_type)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
263 if renderClassName:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
264 array_call.append('--renderClassName')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
265 array_call.append(renderClassName)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
266 if subfeatureClasses:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
267 array_call.append('--subfeatureClasses')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
268 array_call.append(json.dumps(subfeatureClasses))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
269
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
270 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
271 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
272
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
273 def bam_to_json(inputFile, trackLabel, outputFolder, options=None, compress=False):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
274
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
275 array_call = ['bam-to-json.pl',
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
276 '--bam', inputFile,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
277 '--trackLabel', trackLabel,
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
278 '--out', outputFolder]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
279 if compress:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
280 array_call.append('--compress')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
281 if options:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
282 config = options.get('config')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
283 clientConfig = options.get('clientConfig')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
284 if clientConfig:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
285 array_call.append('--clientConfig')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
286 array_call.append(clientConfig)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
287 if config:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
288 array_call.append('--config')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
289 array_call.append(config)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
290
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
291 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
292 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
293
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
294 def add_track_json(trackList, track_json):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
295 track_json = json.dumps(track_json)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
296 new_track = subprocess.Popen(['echo', track_json], stdout=subprocess.PIPE)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
297 p = subprocess.call(['add-track-json.pl', trackList], stdin=new_track.stdout)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
298 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
299
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
300 def prepare_refseqs(fasta_file_name, outputFolder):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
301 array_call = ['prepare-refseqs.pl', '--fasta', fasta_file_name, '--out', outputFolder]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
302 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
303 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
304
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
305 def generate_names(outputFolder):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
306 array_call = ['generate-names.pl', '-v', '--out', outputFolder]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
307 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
308 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
309
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
310 def validateFiles(input_file, chrom_sizes_file_name, file_type, options=None):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
311 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
312 Call validateFiles on input_file, using chrom_sizes_file_name and file_type
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
313 :param input_file:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
314 :param chrom_sizes_file_name:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
315 :param file_type:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
316 :return:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
317 """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
318
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
319 array_call = ['validateFiles', '-chromInfo=' + chrom_sizes_file_name, '-type='+ file_type, input_file]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
320 if options:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
321 tab = options.get("tab")
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
322 autoSql = options.get("autoSql")
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
323 logging.debug("tab: {0}".format(tab))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
324 logging.debug("autoSql: {0}".format(autoSql))
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
325 if autoSql:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
326 autoSql = ''.join(['-as=', autoSql])
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
327 array_call.append(autoSql)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
328 if tab:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
329 array_call.append('-tab')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
330 p = _handleExceptionAndCheckCall(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
331 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
332
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
333 def arrow_add_organism(organism_name, organism_dir, public=False):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
334 array_call = ['arrow', 'organisms', 'add_organism', organism_name, organism_dir]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
335 if public:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
336 array_call.append('--public')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
337 print array_call
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
338 p = subprocess.check_output(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
339 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
340
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
341 def arrow_create_user(user_email, firstname, lastname, password, admin=False):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
342 """ Create a new user of Apollo, the default user_role is "user" """
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
343 array_call = ['arrow', 'users', 'create_user', user_email, firstname, lastname, password]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
344 if admin:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
345 array_call += ['--role', 'admin']
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
346 logging.debug("%s", array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
347 print array_call
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
348 p = subprocess.check_output(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
349 print ("p = %s", p)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
350 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
351
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
352 def arrow_update_organism_permissions(user_id, organism, **user_permissions):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
353 array_call = ['arrow', 'users', 'update_organism_permissions', str(user_id), str(organism)]
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
354 admin = user_permissions.get("admin", False)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
355 write = user_permissions.get("write", False)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
356 read = user_permissions.get("read", False)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
357 export = user_permissions.get("export", False)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
358 if admin:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
359 array_call.append('--administrate')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
360 if write:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
361 array_call.append('--write')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
362 if read:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
363 array_call.append('--read')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
364 if export:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
365 array_call.append('--export')
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
366 p = subprocess.check_output(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
367 return p
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
368
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
369 def arrow_get_users(user_email):
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
370 array_call = ['arrow', 'users', 'get_users']
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
371 logging.debug("%s", array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
372 print array_call
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
373 p = subprocess.check_output(array_call)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
374 all_users = json.loads(p)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
375 for d in all_users:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
376 if d['username'] == user_email:
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
377 return d['userId']
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
378 logging.error("Cannot find user %s", user_email)
bc00f5c4c59e planemo upload for repository https://github.com/Yating-L/jbrowse-archive-creator.git
yating-l
parents:
diff changeset
379