Skip to content

Commit

Permalink
Merge pull request #8 from asrhou/master
Browse files Browse the repository at this point in the history
fixed many bugs
  • Loading branch information
asrhou authored Aug 19, 2020
2 parents ae7a593 + f8ecc9a commit 6c1275f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 16 additions & 2 deletions DiffEdges.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ def IdentifyPopulationChanges(refClusterMapDF, testClusterMapDF, resultFolder, r

sumClusterDF = pd.concat([refClusterSizeDF, testClusterSizeDF])
sumClusterDF.columns = ['Cluster', 'Population (cells)', 'Fraction (%)', 'Source']
newsumClusterDFDict = {'Cluster':[], 'Population (cells)':[], 'Fraction (%)':[], 'Source':[]}
for cct in set(sumClusterDF['Cluster']):
tsumClusterDF = sumClusterDF.loc[sumClusterDF['Cluster']==cct,]
if len(tsumClusterDF) == 1:
sourcetype = tsumClusterDF.loc[tsumClusterDF.index[0],'Source']
newsumClusterDFDict['Cluster'].append(cct)
newsumClusterDFDict['Population (cells)'].append(0)
newsumClusterDFDict['Fraction (%)'].append(0.0)
if sourcetype == 'reference dataset':
newsumClusterDFDict['Source'].append('target dataset')
else:
newsumClusterDFDict['Source'].append('reference dataset')
newsumClusterDF = pd.DataFrame(newsumClusterDFDict)
sumClusterDF = pd.concat([sumClusterDF, newsumClusterDF])
sumClusterDF = sumClusterDF.sort_values(by=['Cluster'])
sumClusterDF.set_index('Cluster').to_excel(os.path.join(resultFolder,'cluster_comparison.xlsx'), index=True, header=True)

Expand All @@ -73,11 +87,11 @@ def IdentifyPopulationChanges(refClusterMapDF, testClusterMapDF, resultFolder, r
# visualise population changes in clusters
sns.set_style("whitegrid")
fig = plt.figure(figsize=(plotWidth, plotHeight))
ax = sns.barplot(y="Cluster", x="Population (cells)", hue="Source", data=sumClusterDF, order=cltOrder, palette={'reference dataset':'r','target dataset':'lime'})
ax = sns.barplot(y="Cluster", x="Population (cells)", hue="Source", data=sumClusterDF, order=cltOrder, palette={'reference dataset':'red','target dataset':'blue'})
fig = ax.get_figure()
fig.savefig(os.path.join(resultFolder,'cluster_size_comparison.pdf'), bbox_inches='tight')
fig = plt.figure(figsize=(plotWidth, plotHeight))
ax = sns.barplot(y="Cluster", x="Fraction (%)", hue="Source", data=sumClusterDF, order=cltOrder, palette={'reference dataset':'r','target dataset':'lime'})
ax = sns.barplot(y="Cluster", x="Fraction (%)", hue="Source", data=sumClusterDF, order=cltOrder, palette={'reference dataset':'red','target dataset':'blue'})
fig = ax.get_figure()
fig.savefig(os.path.join(resultFolder,'cluster_fraction_comparison.pdf'), bbox_inches='tight')

Expand Down
12 changes: 9 additions & 3 deletions VisInteractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ def DrawDeltaGraphvizPlot(readmeStr, typeStr, numStr, nxgS1, adjSpecM1, nxgS2, a
cltSizeColorDict[clt] = (1,0,0)
else:
cltSizeColorDict[clt] = (1, 1, 0)

# draw delta networks

# convert to a graphviz graph
Expand Down Expand Up @@ -966,7 +967,9 @@ def DrawGraphvizPlot(readmeStr, typeStr, numStr, nxgS, adjSpecM, dataType, resul
def BuildDeltaInterClusterNetwork(origlabels, labels, cltSizes, ccolorList, edgeDF, specificityThreshold, weightThreshold, frequencyThreshold, keepTopEdge, interDB, weightType, layout, plotFormat, plotWidth, plotHeight, fontSize, edgeWidth, maxClusterSize, clusterDistance, resultDir, dataType=''):
readmeStr = '\n'

compT = pd.read_excel(os.path.join(resultDir,'..','cluster_comparison.xlsx'), index_col=0,header=0)
compT = pd.read_excel(os.path.join(resultDir,'..','cluster_comparison.xlsx'), index_col=None,header=0)
compT = compT.sort_values(by=['Cluster','Source'])
compT = compT.set_index('Cluster')
cltFDdict = {}
maxFC = 0
inflist = []
Expand All @@ -980,7 +983,7 @@ def BuildDeltaInterClusterNetwork(origlabels, labels, cltSizes, ccolorList, edge
cltFDdict[compT.index[idx]] = 1.0*tempFC
if maxFC < tempFC:
maxFC = tempFC
elif compT.iloc[idx,1] == 0:
elif compT.iloc[idx,1] * compT.iloc[idx+1,1] == 0:
inflist.append(idx)
#set inf as 2 x max fold change
maxFC = maxFC *2
Expand Down Expand Up @@ -1786,7 +1789,8 @@ def DrawTopLRCltPairs(readmeStr, edgeDF, plotFormat,plotWidth, plotHeight, fontS
topLRE = list(edgeDFE.loc[:,['lrpair']].drop_duplicates().head(10)['lrpair'])
topLRS = list(edgeDFS.loc[:,['lrpair']].drop_duplicates().head(10)['lrpair'])
topEdgeDFE = edgeDFE.loc[edgeDFE['lrpair'].isin(topLRE)]
topEdgeDFS = edgeDFE.loc[edgeDFS['lrpair'].isin(topLRS)]
topEdgeDFS = edgeDFS.loc[edgeDFS['lrpair'].isin(topLRS)]

lrCltMtxE = pd.DataFrame()
for idx in topEdgeDFE.index:
if dataType == '':
Expand All @@ -1799,6 +1803,7 @@ def DrawTopLRCltPairs(readmeStr, edgeDF, plotFormat,plotWidth, plotHeight, fontS
lrCltMtxS.loc[topEdgeDFS.loc[idx,'lrpair'],topEdgeDFS.loc[idx,'cellpair']] = topEdgeDFS.loc[idx,'product of specified']
else:
lrCltMtxS.loc[topEdgeDFS.loc[idx,'lrpair'],topEdgeDFS.loc[idx,'cellpair']] = topEdgeDFS.loc[idx,'delta specificity']

lrnum = lrCltMtxE.shape[0]
if lrCltMtxE.shape[0]*2 < lrCltMtxE.shape[1]:
ctnum = lrnum*2
Expand Down Expand Up @@ -1833,6 +1838,7 @@ def DrawTopLRCltPairs(readmeStr, edgeDF, plotFormat,plotWidth, plotHeight, fontS

#draw heatmap
cmap = 'afmhot_r'
#cmap = 'coolwarm'
import matplotlib.pyplot as plt
lrCltMtxE = lrCltMtxE.divide(lrCltMtxE.max(axis=1),axis=0)
f, ax = plt.subplots(figsize=(max(lrCltMtxS.shape),max(lrCltMtxS.shape)/scaleE))
Expand Down

0 comments on commit 6c1275f

Please sign in to comment.