annotate peak_calling_script.py @ 9:9f6798968481 draft default tip

Uploaded
author nitrozyna
date Thu, 29 Mar 2018 12:06:28 -0400
parents 15a3d5439e7b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
1
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
2 from __future__ import print_function
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
3 import sys
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
4 import numpy
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
5 import math
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
6 import random
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
7 import csv
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
8 import matplotlib.pyplot as plt
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
9 import pystache
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
10 import json
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
11 from sklearn import mixture
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
12
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
13
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
14 x = []
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
15 y = []
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
16
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
17 toolInput = sys.argv[1]
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
18 toolOutput = sys.argv[2]
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
19 toolWebsite = sys.argv[3]
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
20 inName = sys.argv[4]
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
21
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
22 with open(toolInput, 'rb') as csvfile:
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
23 spamreader = csv.reader(csvfile, delimiter='\t')
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
24 for i, row in enumerate(spamreader):
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
25 if i != 0:
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
26 x.append(int(row[0]))
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
27 y.append(int(row[1]))
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
28
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
29 csvfile.close()
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
30
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
31 # you have to set this manually to weed out all the noise. Every bit of noise should be below it.
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
32 threshold = 20
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
33 rightLimit = 200
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
34
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
35 # unravelling histogram into samples.
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
36 samples = []
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
37 for no, value in enumerate([int(round(i)) for i in y]):
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
38 if value > threshold and no < rightLimit:
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
39 for _ in range(value):
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
40 samples.append(no)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
41
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
42 # total number of reads
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
43 totalAmp = len(samples)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
44
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
45 # reshaping numpy arrays to indicate that we pass a lot of samples, not a lot of features.
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
46 xArray = numpy.array(x).reshape(1, -1)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
47 samplesArray = numpy.array(samples).reshape(-1, 1)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
48
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
49 # learning a gaussian mixture model.
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
50 gmm2 = mixture.BayesianGaussianMixture(n_components=2).fit(samplesArray)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
51
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
52 # getting the mean of each gaussian
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
53 means = [x[int(round(i[0]))] for i in gmm2.means_]
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
54
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
55 # rounding errors
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
56 roundErr = [i[0] - int(round(i[0])) for i in gmm2.means_]
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
57
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
58 # getting the coverage of each gaussian
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
59 weights = gmm2.weights_
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
60
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
61 sampleID = inName
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
62
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
63 #with open(toolOutput, "w") as f:
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
64 # print("sampleID", file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
65 # print("Al1", file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
66 # print("Al2", file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
67 # print("frac1", file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
68 # print("frac2", file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
69 # print(file=f)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
70 # print(sampleID, file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
71 # print(means[0], file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
72 # print(means[1], file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
73 # print(weights[0], file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
74 # print(weights[1], file=f, end="\t")
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
75
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
76 template_dir = {
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
77 "sampleID": sampleID,
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
78 "al1": means[0],
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
79 "al2": means[1],
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
80 "freq1": weights[0],
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
81 "freq2": weights[1],
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
82 "x": json.dumps(x),
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
83 "y": json.dumps(y)
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
84 }
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
85 with open(toolWebsite) as wt:
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
86 with open(toolOutput, "w") as wr:
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
87 wr.write(pystache.render(wt.read(), template_dir))
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
88
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
89 wt.close()
15a3d5439e7b Uploaded
nitrozyna
parents:
diff changeset
90 wr.close()