-
Notifications
You must be signed in to change notification settings - Fork 1
/
ambitus_pno_annotatetips.py
53 lines (46 loc) · 1.71 KB
/
ambitus_pno_annotatetips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import csv # To process tab-delimited files
import numpy # Used for random sampling
import sys # To process arguments
import subprocess
from ambitus_main import shell_call as shell_call
def AnnotateTips(process):
binlist = [] # List of bins created by phyloclim
weightlist = [] # List of probabilities matched to bins, used for weighted average
midpointlist = [] # List of medians from pno distribution
specieslabellist = {}
specieslist = []
counter = 1
try:
with open('specieslist.csv', 'r') as speciesfile:
reader=csv.reader(speciesfile, delimiter='\t')
for row in reader:
specieslist.append(row[0])
except:
print("Could not open species list.")
for species in specieslist:
counter = 1
while counter <= process:
with open('./pnos/pno{0}_{1}.csv'.format(counter, species), 'r') as datafile:
reader=csv.reader(datafile, delimiter=',')
next(reader)
for row in reader:
binlist.append(float(row[1]))
weightlist.append(float(row[2]))
midpointlist.append('variable{0}={1}'.format(counter,numpy.average(binlist, weights = weightlist))) # Weighted average to get center of distribution
binlist = []
weightlist = []
counter += 1
label = ""
for x in midpointlist:
label += (x + ",")
label = "[&" + label + "]"
specieslabellist[species] = label
midpointlist = []
shell_call('cp tree_labels_midpoints_clean.tre tree_labels_midpointsandtips.tre')
for species in specieslist:
specieslabel = specieslabellist[species]
command = 'sed "s/{0}:/{0}{1}:/g" tree_labels_midpointsandtips.tre > temp.txt'.format(species,specieslabel)
command = command.replace("&","\&")
shell_call(command)
shell_call("mv temp.txt tree_labels_midpointsandtips.tre")