-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasym_table_ssasymp.py
91 lines (70 loc) · 2.38 KB
/
asym_table_ssasymp.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import csv
import os
import pandas as pd
import numpy as np
import tempfile
import subprocess
import time
def runr(huc = "huc"):
if "'" in huc:
huc = huc.replace("'","\\'")
print(huc)
w_table = "write.table(asym, 'asym_" + huc + ".txt', sep=',');"
theR = "suppressMessages(library(vegan));"
theR += "setwd('D:/Colton_Data/Species_Accumulation/ssasymp_tables');" #Make sure you change to directory w/ computer you're working on
theR += "sp1 <- read.csv('temp.csv');"
theR += "sp1$X <- NULL;"
theR += "sp2 <- specaccum(sp1, w=NULL);"
theR += "mod1 <-fitspecaccum(sp2, 'asymp');"
theR += "asym <- coef(mod1);"
theR += w_table
text_file = open('TemporaryR.txt' , 'w')
text_file.write(theR)
text_file.close
subprocess.Popen("Rscript --vanilla D:/Colton_Data/Species_Accumulation/TemporaryR.txt")
#Empty Lists
sighting_dic = {} #Appends taxon onto collecting events
col_keys = [] #Collecting of collecting events
taxon = [] # Taxon
huc8s = [] #HUC8 Key, with collecting event taxon plis
#Creates dataframe with 0 - MRB / 1 - HUC8Name / 2 = Taxon / 3 = Collecting Id
df = pd.read_csv('Data/spaccu.csv', header=None , sep=',')
#Generates array of unique HUC 8 names
for row in df.itertuples():
if row[2] not in huc8s:
huc8s.append(row[2])
#Run the R for each individual HUC8
for huc in huc8s:
#Clears previous list if filled
taxon.clear()
col_keys.clear()
sighting_dic.clear()
#Runs through each row in "spaccu.csv"
for row in df.itertuples():
if row[2] == huc: #checks if row is in in huc
if row[3] not in taxon: #Adds unique taxon to list
taxon.append(row[3])
if row[4] not in col_keys: #adds unique collecting ids to list
col_keys.append(row[4])
sighting_dic.setdefault(row[4],[]) #set key for dict
sighting_dic[row[4]].append(row[3]) #append tax
specAccT = {}
#For each unique species within HUC set value to 1 if it's the first time it has been seen
#in the HUC, else set it to zero
for x in taxon:
specAccT.setdefault(x,[])
for y in col_keys:
if x in sighting_dic[y]:
specAccT[x].append(1)
else:
specAccT[x].append(0)
#Create dataframe
dataFinal = pd.DataFrame(specAccT)
count_row = dataFinal.shape[0]
#
if count_row > 1:
dataFinal.to_csv('ssasymp_tables/temp.csv',sep=',')
runr(huc)
else:
print(huc + "is not sampled enough")
time.sleep(5)