annotate adams_tool.py @ 0:83458fec2479 draft

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