annotate cpo_galaxy_tree.py @ 11:8aaa4001383b draft

planemo upload
author jjjjia
date Fri, 24 Aug 2018 16:54:25 -0400
parents b7e9a3be2b11
children 4b2738bc81ed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
1 #!/home/jjjjia/.conda/envs/py36/bin/python
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
2
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
3 #$ -S /home/jjjjia/.conda/envs/py36/bin/python
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
4 #$ -V # Pass environment variables to the job
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
5 #$ -N CPO_pipeline # Replace with a more specific job name
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
6 #$ -wd /home/jjjjia/testCases # Use the current working dir
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
7 #$ -pe smp 1 # Parallel Environment (how many cores)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
8 #$ -l h_vmem=11G # Memory (RAM) allocation *per core*
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
9 #$ -e ./logs/$JOB_ID.err
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
10 #$ -o ./logs/$JOB_ID.log
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
11 #$ -m ea
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
12 #$ -M bja20@sfu.ca
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
13
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
14 # >python cpo_galaxy_tree.py -t /path/to/tree.ph -d /path/to/distance/matrix -m /path/to/metadata
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
15
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
16 # <requirements>
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
17 # <requirement type="package" version="0.23.4">pandas</requirement>
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
18 # <requirement type="package" version="3.6">python</requirement>
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
19 # <requirement type="package" version="3.1.1">ete3</requirement>
8
93c25036d3b9 planemo upload
jjjjia
parents: 7
diff changeset
20 # <requirement type="package" version="5.6.0">pyqt</requirement>
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
21 # </requirements>
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
22
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
23 import subprocess
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
24 import pandas #conda pandas
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
25 import optparse
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
26 import os
11
8aaa4001383b planemo upload
jjjjia
parents: 10
diff changeset
27 os.environ['QT_QPA_PLATFORM']='offscreen'
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
28 import datetime
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
29 import sys
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
30 import time
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
31 import urllib.request
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
32 import gzip
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
33 import collections
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
34 import json
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
35 import numpy #conda numpy
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
36 import ete3 as e #conda ete3 3.1.1**** >requires pyqt5
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
37
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
38
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
39 #parses some parameters
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
40 parser = optparse.OptionParser("Usage: %prog [options] arg1 arg2 ...")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
41 parser.add_option("-t", "--tree", dest="treePath", type="string", default="./pipelineTest/tree.txt", help="identifier of the isolate")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
42 parser.add_option("-d", "--distance", dest="distancePath", type="string", default="./pipelineTest/distance.tab", help="absolute file path forward read (R1)")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
43 parser.add_option("-m", "--metadata", dest="metadataPath", type="string", default="./pipelineTest/metadata.tsv",help="absolute file path to reverse read (R2)")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
44 (options,args) = parser.parse_args()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
45 treePath = str(options.treePath).lstrip().rstrip()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
46 distancePath = str(options.distancePath).lstrip().rstrip()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
47 metadataPath = str(options.metadataPath).lstrip().rstrip()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
48
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
49
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
50 #region result objects
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
51 #define some objects to store values from results
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
52 #//TODO this is not the proper way of get/set private object variables. every value has manually assigned defaults intead of specified in init(). Also, use property(def getVar, def setVar).
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
53 class workflowResult(object):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
54 def __init__(self):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
55 self.new = False
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
56 self.ID = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
57 self.ExpectedSpecies = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
58 self.MLSTSpecies = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
59 self.SequenceType = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
60 self.MLSTScheme = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
61 self.CarbapenemResistanceGenes =""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
62 self.OtherAMRGenes=""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
63 self.TotalPlasmids = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
64 self.plasmids = []
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
65 self.DefinitelyPlasmidContigs =""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
66 self.LikelyPlasmidContigs=""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
67 self.row = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
68 class plasmidObj(object):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
69 def __init__(self):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
70 self.PlasmidsID = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
71 self.Num_Contigs = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
72 self.PlasmidLength = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
73 self.PlasmidRepType = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
74 self.PlasmidMobility = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
75 self.NearestReference = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
76
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
77 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
78
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
79 #region useful functions
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
80 def read(path): #read in a text file to a list
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
81 return [line.rstrip('\n') for line in open(path)]
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
82 def execute(command): #subprocess.popen call bash command
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
83 process = subprocess.Popen(command, shell=False, cwd=curDir, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
84
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
85 # Poll process for new output until finished
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
86 while True:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
87 nextline = process.stdout.readline()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
88 if nextline == '' and process.poll() is not None:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
89 break
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
90 sys.stdout.write(nextline)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
91 sys.stdout.flush()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
92
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
93 output = process.communicate()[0]
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
94 exitCode = process.returncode
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
95
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
96 if (exitCode == 0):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
97 return output
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
98 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
99 raise subprocess.CalledProcessError(exitCode, command)
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
100 def httpGetFile(url, filepath=""): #download a file from the web
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
101 if (filepath == ""):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
102 return urllib.request.urlretrieve(url)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
103 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
104 urllib.request.urlretrieve(url, filepath)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
105 return True
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
106 def gunzip(inputpath="", outputpath=""): #gunzip
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
107 if (outputpath == ""):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
108 with gzip.open(inputpath, 'rb') as f:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
109 gzContent = f.read()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
110 return gzContent
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
111 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
112 with gzip.open(inputpath, 'rb') as f:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
113 gzContent = f.read()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
114 with open(outputpath, 'wb') as out:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
115 out.write(gzContent)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
116 return True
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
117 def addFace(name): #function to add a facet to a tree
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
118 #if its the reference branch, populate the faces with column headers
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
119 face = e.faces.TextFace(name,fsize=10,tight_text=True)
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
120 face.border.margin = 5
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
121 face.margin_right = 5
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
122 face.margin_left = 5
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
123 return face
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
124 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
125
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
126 #region functions to parse result files
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
127 def ParseWorkflowResults(pathToResult):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
128 _worflowResult = {}
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
129 r = pandas.read_csv(pathToResult, delimiter='\t', header=0)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
130 r = r.replace(numpy.nan, '', regex=True)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
131 for i in range(len(r.index)):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
132 _results = workflowResult()
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
133 if(str(r.loc[r.index[i], 'new']).lower() == "new"):
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
134 _results.new = True
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
135 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
136 _results.new = False
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
137 _results.ID = str(r.loc[r.index[i], 'ID'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
138 _results.ExpectedSpecies = str(r.loc[r.index[i], 'Expected Species'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
139 _results.MLSTSpecies = str(r.loc[r.index[i], 'MLST Species'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
140 _results.SequenceType = str(r.loc[r.index[i], 'Sequence Type'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
141 _results.MLSTScheme = (str(r.loc[r.index[i], 'MLST Scheme']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
142 _results.CarbapenemResistanceGenes = (str(r.loc[r.index[i], 'Carbapenem Resistance Genes']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
143 _results.OtherAMRGenes = (str(r.loc[r.index[i], 'Other AMR Genes']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
144 _results.TotalPlasmids = int(r.loc[r.index[i], 'Total Plasmids'])
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
145 for j in range(0,_results.TotalPlasmids):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
146 _plasmid = plasmidObj()
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
147 _plasmid.PlasmidsID =(((str(r.loc[r.index[i], 'Plasmids ID'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
148 _plasmid.Num_Contigs = (((str(r.loc[r.index[i], 'Num_Contigs'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
149 _plasmid.PlasmidLength = (((str(r.loc[r.index[i], 'Plasmid Length'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
150 _plasmid.PlasmidRepType = (((str(r.loc[r.index[i], 'Plasmid RepType'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
151 _plasmid.PlasmidMobility = ((str(r.loc[r.index[i], 'Plasmid Mobility'])).split(";"))[j]
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
152 _plasmid.NearestReference = ((str(r.loc[r.index[i], 'Nearest Reference'])).split(";"))[j]
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
153 _results.plasmids.append(_plasmid)
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
154 _results.DefinitelyPlasmidContigs = (str(r.loc[r.index[i], 'Definitely Plasmid Contigs']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
155 _results.LikelyPlasmidContigs = (str(r.loc[r.index[i], 'Likely Plasmid Contigs']))
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
156 _results.row = "\t".join(str(x) for x in r.ix[i].tolist())
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
157 _worflowResult[_results.ID] = _results
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
158 return _worflowResult
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
159
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
160 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
161
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
162 def Main():
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
163 metadata = ParseWorkflowResults(metadataPath)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
164 distance = read(distancePath)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
165 treeFile = "".join(read(treePath))
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
166
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
167 distanceDict = {} #store the distance matrix as rowname:list<string>
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
168 for i in range(len(distance)):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
169 temp = distance[i].split("\t")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
170 distanceDict[temp[0]] = temp[1:]
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
171
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
172 #region create box tree
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
173 #region step5: tree construction
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
174 treeFile = "".join(read(treePath))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
175 t = e.Tree(treeFile)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
176 t.set_outgroup(t&"Reference")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
177
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
178 #set the tree style
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
179 ts = e.TreeStyle()
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
180 ts.show_leaf_name = False
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
181 ts.show_branch_length = True
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
182 ts.scale = 2000 #pixel per branch length unit
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
183 ts.branch_vertical_margin = 15 #pixel between branches
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
184 style2 = e.NodeStyle()
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
185 style2["fgcolor"] = "#000000"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
186 style2["shape"] = "circle"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
187 style2["vt_line_color"] = "#0000aa"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
188 style2["hz_line_color"] = "#0000aa"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
189 style2["vt_line_width"] = 2
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
190 style2["hz_line_width"] = 2
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
191 style2["vt_line_type"] = 0 # 0 solid, 1 dashed, 2 dotted
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
192 style2["hz_line_type"] = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
193 for n in t.traverse():
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
194 n.set_style(style2)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
195
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
196 #find the plasmid origins
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
197 plasmidIncs = {}
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
198 for key in metadata:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
199 for plasmid in metadata[key].plasmids:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
200 for inc in plasmid.PlasmidRepType.split(","):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
201 if (inc.lower().find("inc") > -1):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
202 if not (inc in plasmidIncs):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
203 plasmidIncs[inc] = [metadata[key].ID]
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
204 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
205 if metadata[key].ID not in plasmidIncs[inc]:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
206 plasmidIncs[inc].append(metadata[key].ID)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
207 #plasmidIncs = sorted(plasmidIncs)
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
208 for n in t.traverse(): #loop through the nodes of a tree
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
209 if (n.is_leaf() and n.name == "Reference"):
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
210 #if its the reference branch, populate the faces with column headers
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
211 index = 0
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
212 (t&"Reference").add_face(addFace("SampleID"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
213 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
214 (t&"Reference").add_face(addFace("New?"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
215 index = index + 1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
216 for i in range(len(plasmidIncs)): #this loop adds the columns (aka the incs) to the reference node
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
217 (t&"Reference").add_face(addFace(list(plasmidIncs.keys())[i]), i + index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
218 index = index + len(plasmidIncs)
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
219 (t&"Reference").add_face(addFace("MLSTScheme"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
220 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
221 (t&"Reference").add_face(addFace("Sequence Type"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
222 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
223 (t&"Reference").add_face(addFace("Carbapenamases"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
224 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
225 for i in range(len(distanceDict[list(distanceDict.keys())[0]])): #this loop adds the distance matrix
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
226 (t&"Reference").add_face(addFace(distanceDict[list(distanceDict.keys())[0]][i]), index + i, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
227 index = index + len(distanceDict[list(distanceDict.keys())[0]])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
228 elif (n.is_leaf() and not n.name == "Reference"):
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
229 #not reference branches, populate with metadata
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
230 index = 0
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
231 mData = metadata[n.name.replace(".fa","")]
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
232 n.add_face(addFace(mData.ID), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
233 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
234 if (metadata[n.name.replace(".fa","")].new == True): #new column
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
235 face = e.RectFace(30,30,"green","green") # TextFace("Y",fsize=10,tight_text=True)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
236 face.border.margin = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
237 face.margin_right = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
238 face.margin_left = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
239 face.vt_align = 1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
240 face.ht_align = 1
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
241 n.add_face(face, index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
242 index = index + 1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
243 for incs in plasmidIncs: #this loop adds presence/absence to the sample nodes
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
244 if (n.name.replace(".fa","") in plasmidIncs[incs]):
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
245 face = e.RectFace(30,30,"black","black") # TextFace("Y",fsize=10,tight_text=True)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
246 face.border.margin = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
247 face.margin_right = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
248 face.margin_left = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
249 face.vt_align = 1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
250 face.ht_align = 1
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
251 n.add_face(face, list(plasmidIncs.keys()).index(incs) + index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
252 index = index + len(plasmidIncs)
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
253 n.add_face(addFace(mData.MLSTSpecies), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
254 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
255 n.add_face(addFace(mData.SequenceType), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
256 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
257 n.add_face(addFace(mData.CarbapenemResistanceGenes), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
258 index = index + 1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
259 for i in range(len(distanceDict[list(distanceDict.keys())[0]])): #this loop adds distance matrix
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
260 n.add_face(addFace(list(distanceDict[n.name])[i]), index + i, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
261
8
93c25036d3b9 planemo upload
jjjjia
parents: 7
diff changeset
262 t.render("./tree.pdf", w=5000,units="mm", tree_style=ts) #save it as a png. or an phyloxml
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
263
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
264 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
265 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
266
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
267
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
268 start = time.time()#time the analysis
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
269
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
270 #analysis time
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
271 Main()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
272
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
273 end = time.time()
10
b7e9a3be2b11 planemo upload
jjjjia
parents: 8
diff changeset
274 print("Finished!\nThe analysis used: " + str(end-start) + " seconds")