comparison cpo_galaxy_tree.py @ 7:4d2777aa99db draft

planemo upload
author jjjjia
date Thu, 23 Aug 2018 18:35:54 -0400
parents cabceaa239e4
children 93c25036d3b9
comparison
equal deleted inserted replaced
6:cabceaa239e4 7:4d2777aa99db
2 2
3 #$ -S /home/jjjjia/.conda/envs/py36/bin/python 3 #$ -S /home/jjjjia/.conda/envs/py36/bin/python
4 #$ -V # Pass environment variables to the job 4 #$ -V # Pass environment variables to the job
5 #$ -N CPO_pipeline # Replace with a more specific job name 5 #$ -N CPO_pipeline # Replace with a more specific job name
6 #$ -wd /home/jjjjia/testCases # Use the current working dir 6 #$ -wd /home/jjjjia/testCases # Use the current working dir
7 #$ -pe smp 8 # Parallel Environment (how many cores) 7 #$ -pe smp 1 # Parallel Environment (how many cores)
8 #$ -l h_vmem=11G # Memory (RAM) allocation *per core* 8 #$ -l h_vmem=11G # Memory (RAM) allocation *per core*
9 #$ -e ./logs/$JOB_ID.err 9 #$ -e ./logs/$JOB_ID.err
10 #$ -o ./logs/$JOB_ID.log 10 #$ -o ./logs/$JOB_ID.log
11 #$ -m ea 11 #$ -m ea
12 #$ -M bja20@sfu.ca 12 #$ -M bja20@sfu.ca
13 13
14 #~/scripts/pipeline.py -i BC11-Kpn005_S2 -f /data/jjjjia/R1/BC11-Kpn005_S2_L001_R1_001.fastq.gz -r /data/jjjjia/R2/BC11-Kpn005_S2_L001_R2_001.fastq.gz -o pipelineResultsQsub -e "Klebsiella pneumoniae" 14 # >python cpo_galaxy_tree.py -t /path/to/tree.ph -d /path/to/distance/matrix -m /path/to/metadata
15
16 # <requirements>
17 # <requirement type="package" version="0.23.4">pandas</requirement>
18 # <requirement type="package" version="3.6">python</requirement>
19 # <requirement type="package" version="3.1.1">ete3</requirement>
20 # <requirement type="package" version="5.9.3">pyqt</requirement>
21 # </requirements>
15 22
16 import subprocess 23 import subprocess
17 import pandas 24 import pandas #conda pandas
18 import optparse 25 import optparse
19 import os 26 import os
20 import datetime 27 import datetime
21 import sys 28 import sys
22 import time 29 import time
23 import urllib.request 30 import urllib.request
24 import gzip 31 import gzip
25 import collections 32 import collections
26 import json 33 import json
27 import numpy 34 import numpy #conda numpy
28 import ete3 as e 35 import ete3 as e #conda ete3 3.1.1**** >requires pyqt5
29 36
30 37
31 #parses some parameters 38 #parses some parameters
32 parser = optparse.OptionParser("Usage: %prog [options] arg1 arg2 ...") 39 parser = optparse.OptionParser("Usage: %prog [options] arg1 arg2 ...")
33 parser.add_option("-t", "--tree", dest="treePath", type="string", default="./pipelineTest/tree.txt", help="identifier of the isolate") 40 parser.add_option("-t", "--tree", dest="treePath", type="string", default="./pipelineTest/tree.txt", help="identifier of the isolate")
67 self.NearestReference = "" 74 self.NearestReference = ""
68 75
69 #endregion 76 #endregion
70 77
71 #region useful functions 78 #region useful functions
72 def read(path): 79 def read(path): #read in a text file to a list
73 return [line.rstrip('\n') for line in open(path)] 80 return [line.rstrip('\n') for line in open(path)]
74 def execute(command): 81 def execute(command): #subprocess.popen call bash command
75 process = subprocess.Popen(command, shell=False, cwd=curDir, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) 82 process = subprocess.Popen(command, shell=False, cwd=curDir, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
76 83
77 # Poll process for new output until finished 84 # Poll process for new output until finished
78 while True: 85 while True:
79 nextline = process.stdout.readline() 86 nextline = process.stdout.readline()
87 94
88 if (exitCode == 0): 95 if (exitCode == 0):
89 return output 96 return output
90 else: 97 else:
91 raise subprocess.CalledProcessError(exitCode, command) 98 raise subprocess.CalledProcessError(exitCode, command)
92 def httpGetFile(url, filepath=""): 99 def httpGetFile(url, filepath=""): #download a file from the web
93 if (filepath == ""): 100 if (filepath == ""):
94 return urllib.request.urlretrieve(url) 101 return urllib.request.urlretrieve(url)
95 else: 102 else:
96 urllib.request.urlretrieve(url, filepath) 103 urllib.request.urlretrieve(url, filepath)
97 return True 104 return True
98 def gunzip(inputpath="", outputpath=""): 105 def gunzip(inputpath="", outputpath=""): #gunzip
99 if (outputpath == ""): 106 if (outputpath == ""):
100 with gzip.open(inputpath, 'rb') as f: 107 with gzip.open(inputpath, 'rb') as f:
101 gzContent = f.read() 108 gzContent = f.read()
102 return gzContent 109 return gzContent
103 else: 110 else:
104 with gzip.open(inputpath, 'rb') as f: 111 with gzip.open(inputpath, 'rb') as f:
105 gzContent = f.read() 112 gzContent = f.read()
106 with open(outputpath, 'wb') as out: 113 with open(outputpath, 'wb') as out:
107 out.write(gzContent) 114 out.write(gzContent)
108 return True 115 return True
109 def addFace(name): 116 def addFace(name): #function to add a facet to a tree
110 #if its the reference branch, populate the faces with column headers 117 #if its the reference branch, populate the faces with column headers
111 face = e.faces.TextFace(name,fsize=10,tight_text=True) 118 face = e.faces.TextFace(name,fsize=10,tight_text=True)
112 face.border.margin = 5 119 face.border.margin = 5
113 face.margin_right = 5 120 face.margin_right = 5
114 face.margin_left = 5 121 face.margin_left = 5