Pārlūkot izejas kodu

correction of transformed sfs plot

tforest 1 gadu atpakaļ
vecāks
revīzija
f3fc152df3
1 mainītis faili ar 17 papildinājumiem un 11 dzēšanām
  1. 17 11
      sfs_tools.py

+ 17 - 11
sfs_tools.py Parādīt failu

@@ -192,10 +192,10 @@ def sfs_from_parsed_vcf(n, vcf_dict, folded = True, diploid = True, phased = Fal
192 192
     return SFS_values, count_pluriall
193 193
 
194 194
 
195
-def barplot_sfs(sfs,  xlab, ylab, folded=True, title = "Barplot", transformed = False):
195
+def barplot_sfs(sfs,  xlab, ylab, folded=True, title = "Barplot", transformed = False, normalized = False):
196 196
     sfs_val = []
197 197
     n = len(sfs.values())
198
-    print("n =", n)
198
+    sum_sites = sum(list(sfs.values()))
199 199
     for k, ksi in sfs.items():
200 200
         #ksi = list(sfs.values())[k-1]
201 201
         # k+1 because k starts from 0
@@ -209,24 +209,30 @@ def barplot_sfs(sfs,  xlab, ylab, folded=True, title = "Barplot", transformed =
209 209
         #         sfs_val.append(ksi)
210 210
         if transformed:
211 211
             if folded:
212
-                #sfs_val.append(ksi * k * (2*n - k))
213
-                sfs_val.append(((k*(2*n - k)) / (2*n))*ksi) 
212
+                val = ((k*(2*n - k)) / (2*n))*(ksi)
214 213
             else:
215
-                sfs_val.append(ksi * k)
214
+                val = ksi * k
216 215
         else:
217
-             sfs_val.append(ksi)
216
+            val = ksi
217
+        sfs_val.append(val)
218 218
             
219 219
     #terminal case, same for folded or unfolded
220 220
     if transformed:
221
-        sfs_val[-1] = list(sfs.values())[n-1] * n
221
+        last_bin = list(sfs.values())[n-1] * n/2
222 222
     else:
223
-        sfs_val[-1] = list(sfs.values())[n-1]
223
+        last_bin = list(sfs.values())[n-1]
224
+    sfs_val[-1] = last_bin
225
+    if normalized:
226
+        ylab = "Fraction of SNPs"
227
+        sum_val = sum(sfs_val)
228
+        for k, sfs_bin in enumerate(sfs_val):
229
+            sfs_val[k] = sfs_bin / sum_val
224 230
     #build the plot
225
-    title = title+" [folded="+str(folded)+"]"+" [transformed="+str(transformed)+"]"
231
+    title = title+" (n="+str(len(sfs_val)+1)+") [folded="+str(folded)+"]"+" [transformed="+str(transformed)+"]"
226 232
     print("SFS =", sfs)
227 233
     if transformed:
228
-        print("Transformed SFS ( n =",len(sfs_val), ") :", sfs_val)
229
-    customgraphics.barplot(x = sfs.keys(), y= sfs_val, xlab = xlab, ylab = ylab, title = title)
234
+        print("Transformed SFS ( n =",len(sfs_val)+1, ") :", sfs_val)
235
+    customgraphics.barplot(x = [x for x in list(sfs.keys())], y= sfs_val, xlab = xlab, ylab = ylab, title = title)
230 236
     plt.show()
231 237
 
232 238
 if __name__ == "__main__":