annotate cpo_galaxy_tree.py @ 24:e5a7da2239af draft

planemo upload
author jjjjia
date Wed, 29 Aug 2018 02:31:49 -0400
parents 596bf8a792de
children 573136f142b6
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>
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
21 # <requirement type="package" version="5.6.2">qt</requirement>
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
22 # </requirements>
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
23
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
24 import subprocess
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
25 import pandas #conda pandas
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
26 import optparse
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
27 import os
11
8aaa4001383b planemo upload
jjjjia
parents: 10
diff changeset
28 os.environ['QT_QPA_PLATFORM']='offscreen'
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
29 import datetime
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
30 import sys
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
31 import time
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
32 import urllib.request
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
33 import gzip
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
34 import collections
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
35 import json
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
36 import numpy #conda numpy
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
37 import ete3 as e #conda ete3 3.1.1**** >requires pyqt5
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
38
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
39
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
40 #parses some parameters
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
41 parser = optparse.OptionParser("Usage: %prog [options] arg1 arg2 ...")
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
42 parser.add_option("-t", "--tree", dest="treePath", type="string", default="./pipelineTest/tree.txt", help="absolute file path to phylip tree")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
43 parser.add_option("-d", "--distance", dest="distancePath", type="string", default="./pipelineTest/distance.tab", help="absolute file path to distance matrix")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
44 parser.add_option("-m", "--metadata", dest="metadataPath", type="string", default="./pipelineTest/metadata.tsv",help="absolute file path to metadata file")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
45 parser.add_option("-o", "--output_file", dest="outputFile", type="string", default="tree.png", help="Output graphics file. Use ending 'png', 'pdf' or 'svg' to specify file format.")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
46
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
47 # sensitive data adder
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
48 parser.add_option("-p", "--sensitive_data", dest="sensitivePath", type="string", default="", help="Spreadsheet (CSV) with sensitive metadata")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
49 parser.add_option("-c", "--sensitive_cols", dest="sensitiveCols", type="string", default="", help="CSV list of column names from sensitive metadata spreadsheet to use as labels on dendrogram")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
50 parser.add_option("-b", "--bcid_column", dest="bcidCol", type="string", default="BCID", help="Column name of BCID in sensitive metadata file")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
51 parser.add_option("-n", "--missing_value", dest="naValue", type="string", default="NA", help="Value to write for missing data.")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
52
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
53 (options,args) = parser.parse_args()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
54 treePath = str(options.treePath).lstrip().rstrip()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
55 distancePath = str(options.distancePath).lstrip().rstrip()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
56 metadataPath = str(options.metadataPath).lstrip().rstrip()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
57
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
58 sensitivePath = str(options.sensitivePath).lstrip().rstrip()
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
59 sensitiveCols = str(options.sensitiveCols).lstrip().rstrip()
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
60 outputFile = str(options.outputFile).lstrip().rstrip()
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
61 bcidCol = str( str(options.bcidCol).lstrip().rstrip() )
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
62 naValue = str( str(options.naValue).lstrip().rstrip() )
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
63
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
64
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
65 #region result objects
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
66 #define some objects to store values from results
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
67 #//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).
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
68
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
69 class SensitiveMetadata(object):
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
70 def __init__(self):
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
71 x = pandas.read_csv( sensitivePath )
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
72 col_names = [ s for s in sensitiveCols.split(',')] # convert to 0 offset
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
73 if not bcidCol in col_names:
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
74 col_names.append( bcidCol )
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
75 all_cols = [ str(col) for col in x.columns ]
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
76 col_idxs = [ all_cols.index(col) for col in col_names ]
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
77 self.sensitive_data = x.iloc[:, col_idxs]
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
78 def get_columns(self):
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
79 cols = [ str(x) for x in self.sensitive_data.columns ]
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
80 return cols
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
81 def get_value( self, bcid, column_name ): # might be nice to get them all in single call via an input list of bcids ... for later
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
82 bcids= list( self.sensitive_data.loc[:, bcidCol ] ) # get the list of all BCIDs in sensitive metadata
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
83 if not bcid in bcids:
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
84 return naValue
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
85 else:
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
86 row_idx = bcids.index( bcid ) # lookup the row for this BCID
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
87 return self.sensitive_data.loc[ row_idx, column_name ] # return the one value based on the column (col_idx) and this row
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
88
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
89 class workflowResult(object):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
90 def __init__(self):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
91 self.new = False
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
92 self.ID = "?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
93 self.ExpectedSpecies = "?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
94 self.MLSTSpecies = "?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
95 self.SequenceType = "?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
96 self.MLSTScheme = "?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
97 self.CarbapenemResistanceGenes ="?"
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
98 self.plasmidBestMatch ="?"
24
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
99 self.plasmididentity =-1
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
100 self.plasmidsharedhashes ="?"
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
101 self.OtherAMRGenes="?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
102 self.TotalPlasmids = -1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
103 self.plasmids = []
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
104 self.DefinitelyPlasmidContigs ="?"
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
105 self.LikelyPlasmidContigs="?"
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
106 self.row = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
107 class plasmidObj(object):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
108 def __init__(self):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
109 self.PlasmidsID = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
110 self.Num_Contigs = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
111 self.PlasmidLength = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
112 self.PlasmidRepType = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
113 self.PlasmidMobility = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
114 self.NearestReference = ""
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
115
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
116 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
117
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
118 #region useful functions
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
119 def read(path): #read in a text file to a list
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
120 return [line.rstrip('\n') for line in open(path)]
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
121 def execute(command): #subprocess.popen call bash command
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
122 process = subprocess.Popen(command, shell=False, cwd=curDir, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
123
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
124 # Poll process for new output until finished
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
125 while True:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
126 nextline = process.stdout.readline()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
127 if nextline == '' and process.poll() is not None:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
128 break
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
129 sys.stdout.write(nextline)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
130 sys.stdout.flush()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
131
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
132 output = process.communicate()[0]
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
133 exitCode = process.returncode
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
134
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
135 if (exitCode == 0):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
136 return output
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
137 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
138 raise subprocess.CalledProcessError(exitCode, command)
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
139 def httpGetFile(url, filepath=""): #download a file from the web
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
140 if (filepath == ""):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
141 return urllib.request.urlretrieve(url)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
142 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
143 urllib.request.urlretrieve(url, filepath)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
144 return True
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
145 def gunzip(inputpath="", outputpath=""): #gunzip
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
146 if (outputpath == ""):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
147 with gzip.open(inputpath, 'rb') as f:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
148 gzContent = f.read()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
149 return gzContent
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
150 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
151 with gzip.open(inputpath, 'rb') as f:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
152 gzContent = f.read()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
153 with open(outputpath, 'wb') as out:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
154 out.write(gzContent)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
155 return True
7
4d2777aa99db planemo upload
jjjjia
parents: 6
diff changeset
156 def addFace(name): #function to add a facet to a tree
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
157 #if its the reference branch, populate the faces with column headers
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
158 face = e.faces.TextFace(name,fsize=10,tight_text=True)
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
159 face.border.margin = 5
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
160 face.margin_right = 5
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
161 face.margin_left = 5
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
162 return face
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
163 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
164
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
165 #region functions to parse result files
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
166 def ParseWorkflowResults(pathToResult):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
167 _worflowResult = {}
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
168 r = pandas.read_csv(pathToResult, delimiter='\t', header=0)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
169 r = r.replace(numpy.nan, '', regex=True)
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
170 _naResult = workflowResult()
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
171 _worflowResult["na"] = _naResult
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
172 for i in range(len(r.index)):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
173 _results = workflowResult()
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
174 if(str(r.loc[r.index[i], 'new']).lower() == "new"):
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
175 _results.new = True
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
176 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
177 _results.new = False
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
178 _results.ID = str(r.loc[r.index[i], 'ID']).replace(".fa","")
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
179 _results.ExpectedSpecies = str(r.loc[r.index[i], 'Expected Species'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
180 _results.MLSTSpecies = str(r.loc[r.index[i], 'MLST Species'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
181 _results.SequenceType = str(r.loc[r.index[i], 'Sequence Type'])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
182 _results.MLSTScheme = (str(r.loc[r.index[i], 'MLST Scheme']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
183 _results.CarbapenemResistanceGenes = (str(r.loc[r.index[i], 'Carbapenem Resistance Genes']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
184 _results.OtherAMRGenes = (str(r.loc[r.index[i], 'Other AMR Genes']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
185 _results.TotalPlasmids = int(r.loc[r.index[i], 'Total Plasmids'])
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
186 _results.plasmidBestMatch = str(r.loc[r.index[i], 'Plasmid Best Match'])
24
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
187 _results.plasmididentity = str(r.loc[r.index[i], 'Plasmid Identity'])
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
188 _results.plasmidsharedhashes = float(r.loc[r.index[i], 'Plasmid Shared Hash'])
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
189 for j in range(0,_results.TotalPlasmids):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
190 _plasmid = plasmidObj()
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
191 _plasmid.PlasmidsID =(((str(r.loc[r.index[i], 'Plasmids ID'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
192 _plasmid.Num_Contigs = (((str(r.loc[r.index[i], 'Num_Contigs'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
193 _plasmid.PlasmidLength = (((str(r.loc[r.index[i], 'Plasmid Length'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
194 _plasmid.PlasmidRepType = (((str(r.loc[r.index[i], 'Plasmid RepType'])).split(";"))[j])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
195 _plasmid.PlasmidMobility = ((str(r.loc[r.index[i], 'Plasmid Mobility'])).split(";"))[j]
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
196 _plasmid.NearestReference = ((str(r.loc[r.index[i], 'Nearest Reference'])).split(";"))[j]
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
197 _results.plasmids.append(_plasmid)
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
198 _results.DefinitelyPlasmidContigs = (str(r.loc[r.index[i], 'Definitely Plasmid Contigs']))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
199 _results.LikelyPlasmidContigs = (str(r.loc[r.index[i], 'Likely Plasmid Contigs']))
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
200 _results.row = "\t".join(str(x) for x in r.ix[i].tolist())
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
201 _worflowResult[_results.ID] = _results
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
202 return _worflowResult
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
203
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
204 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
205
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
206 def Main():
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
207 if len(sensitivePath)>0:
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
208 sensitive_meta_data = SensitiveMetadata()
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
209
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
210 metadata = ParseWorkflowResults(metadataPath)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
211 distance = read(distancePath)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
212 treeFile = "".join(read(treePath))
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
213
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
214 distanceDict = {} #store the distance matrix as rowname:list<string>
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
215 for i in range(len(distance)):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
216 temp = distance[i].split("\t")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
217 distanceDict[temp[0]] = temp[1:]
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
218
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
219 #region create box tree
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
220 #region step5: tree construction
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
221 treeFile = "".join(read(treePath))
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
222 t = e.Tree(treeFile)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
223 t.set_outgroup(t&"Reference")
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
224
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
225 #set the tree style
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
226 ts = e.TreeStyle()
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
227 ts.show_leaf_name = True
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
228 ts.show_branch_length = True
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
229 ts.scale = 2000 #pixel per branch length unit
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
230 ts.branch_vertical_margin = 15 #pixel between branches
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
231 style2 = e.NodeStyle()
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
232 style2["fgcolor"] = "#000000"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
233 style2["shape"] = "circle"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
234 style2["vt_line_color"] = "#0000aa"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
235 style2["hz_line_color"] = "#0000aa"
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
236 style2["vt_line_width"] = 2
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
237 style2["hz_line_width"] = 2
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
238 style2["vt_line_type"] = 0 # 0 solid, 1 dashed, 2 dotted
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
239 style2["hz_line_type"] = 0
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
240 for n in t.traverse():
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
241 n.set_style(style2)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
242
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
243 #find the plasmid origins
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
244 plasmidIncs = {}
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
245 for key in metadata:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
246 for plasmid in metadata[key].plasmids:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
247 for inc in plasmid.PlasmidRepType.split(","):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
248 if (inc.lower().find("inc") > -1):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
249 if not (inc in plasmidIncs):
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
250 plasmidIncs[inc] = [metadata[key].ID]
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
251 else:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
252 if metadata[key].ID not in plasmidIncs[inc]:
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
253 plasmidIncs[inc].append(metadata[key].ID)
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
254 #plasmidIncs = sorted(plasmidIncs)
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
255 for n in t.traverse(): #loop through the nodes of a tree
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
256 if (n.is_leaf() and n.name == "Reference"):
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
257 #if its the reference branch, populate the faces with column headers
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
258 index = 0
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
259
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
260 if len(sensitivePath)>0: #sensitive metadat @ chris
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
261 for sensitive_data_column in sensitive_meta_data.get_columns():
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
262 (t&"Reference").add_face(addFace(sensitive_data_column), index, "aligned")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
263 index = index + 1
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
264
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
265 (t&"Reference").add_face(addFace("SampleID"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
266 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
267 (t&"Reference").add_face(addFace("New?"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
268 index = index + 1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
269 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
270 (t&"Reference").add_face(addFace(list(plasmidIncs.keys())[i]), i + index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
271 index = index + len(plasmidIncs)
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
272 (t&"Reference").add_face(addFace("MLSTScheme"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
273 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
274 (t&"Reference").add_face(addFace("Sequence Type"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
275 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
276 (t&"Reference").add_face(addFace("Carbapenamases"), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
277 index = index + 1
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
278 (t&"Reference").add_face(addFace("Plasmid Best Match"), index, "aligned")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
279 index = index + 1
24
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
280 (t&"Reference").add_face(addFace("Best Match Identity"), index, "aligned")
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
281 index = index + 1
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
282 for i in range(len(distanceDict[list(distanceDict.keys())[0]])): #this loop adds the distance matrix
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
283 (t&"Reference").add_face(addFace(distanceDict[list(distanceDict.keys())[0]][i]), index + i, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
284 index = index + len(distanceDict[list(distanceDict.keys())[0]])
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
285 elif (n.is_leaf() and not n.name == "Reference"):
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
286 #not reference branches, populate with metadata
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
287 index = 0
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
288
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
289 if len(sensitivePath)>0: #sensitive metadata @ chris
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
290 # pushing in sensitive data
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
291 for sensitive_data_column in sensitive_meta_data.get_columns():
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
292 # tree uses bcids like BC18A021A_S12
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
293 # while sens meta-data uses BC18A021A
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
294 # trim the "_S.*" if present
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
295 bcid = str(mData.ID)
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
296 if bcid.find( "_S" ) != -1:
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
297 bcid = bcid[ 0:bcid.find( "_S" ) ]
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
298 sens_col_val = sensitive_meta_data.get_value(bcid=bcid, column_name=sensitive_data_column )
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
299 n.add_face(addFace(sens_col_val), index, "aligned")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
300 index = index + 1
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
301
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
302 if (n.name.replace(".fa","") in metadata.keys()):
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
303 mData = metadata[n.name.replace(".fa","")]
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
304 else:
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
305 mData = metadata["na"]
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
306 n.add_face(addFace(mData.ID), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
307 index = index + 1
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
308 if (mData.new == True): #new column
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
309 face = e.RectFace(30,30,"green","green") # TextFace("Y",fsize=10,tight_text=True)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
310 face.border.margin = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
311 face.margin_right = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
312 face.margin_left = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
313 face.vt_align = 1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
314 face.ht_align = 1
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
315 n.add_face(face, index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
316 index = index + 1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
317 for incs in plasmidIncs: #this loop adds presence/absence to the sample nodes
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
318 if (n.name.replace(".fa","") in plasmidIncs[incs]):
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
319 face = e.RectFace(30,30,"black","black") # TextFace("Y",fsize=10,tight_text=True)
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
320 face.border.margin = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
321 face.margin_right = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
322 face.margin_left = 5
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
323 face.vt_align = 1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
324 face.ht_align = 1
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
325 n.add_face(face, list(plasmidIncs.keys()).index(incs) + index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
326 index = index + len(plasmidIncs)
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
327 n.add_face(addFace(mData.MLSTSpecies), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
328 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
329 n.add_face(addFace(mData.SequenceType), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
330 index = index + 1
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
331 n.add_face(addFace(mData.CarbapenemResistanceGenes), index, "aligned")
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
332 index = index + 1
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
333 n.add_face(addFace(mData.plasmidBestMatch), index, "aligned")
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
334 index = index + 1
24
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
335 n.add_face(addFace(mData.plasmididentity), index, "aligned")
e5a7da2239af planemo upload
jjjjia
parents: 18
diff changeset
336 index = index + 1
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
337 for i in range(len(distanceDict[list(distanceDict.keys())[0]])): #this loop adds distance matrix
13
a14b12a71a53 planemo upload
jjjjia
parents: 12
diff changeset
338 if (n.name in distanceDict): #make sure the column is in the distance matrice
a14b12a71a53 planemo upload
jjjjia
parents: 12
diff changeset
339 n.add_face(addFace(list(distanceDict[n.name])[i]), index + i, "aligned")
6
cabceaa239e4 planemo upload
jjjjia
parents: 1
diff changeset
340
18
596bf8a792de planemo upload
jjjjia
parents: 13
diff changeset
341 t.render(outputFile, w=5000,units="mm", tree_style=ts) #save it as a png, pdf, svg or an phyloxml
1
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
342
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
343 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
344 #endregion
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
345
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
346
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
347 start = time.time()#time the analysis
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
348
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
349 #analysis time
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
350 Main()
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
351
fea89c4d5227 Uploaded
jjjjia
parents:
diff changeset
352 end = time.time()
12
4b2738bc81ed planemo upload
jjjjia
parents: 11
diff changeset
353 print("Finished!\nThe analysis used: " + str(end-start) + " seconds")