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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
#Import needed modules (needed : comb for gmpy, corresponding at combinatory calculations)
import math
import decimal
import gmpy
import os
import time
decimal.getcontext().prec = 6
#Defined variables for the main program and for the calculation of the rarefaction curve
treated_files = list()
Cluster_analyzed = list()
tmp = list()
Matrix = list()
Nb_seq_MOTUs = 0
Result_tmp = decimal.Decimal(0)
Result,Result_fin = float(), float()
Result2 = ""
Nb_MOTUs,verif = 0,0
i,j,n = 0,0,0
N_Ni,x,result = 0,0,0
Rarefact_chose = ""
string_printed = "Sample\t"
tmp_printed = list()
Result_printed = [""]
Children = []
Child_pid = 0
#-------------------------------------------------------------------------------------------------------
#First part of the program, asking for data chosen by the user to analyze files in 'IN' directory
#and also estimators needed to be calculated and chose by the user
#-------------------------------------------------------------------------------------------------------
#Asking for the desired threshold of dissimilarity
print ("- Please give the chosen similarity threshold to select the clustering\n(in percent) [0-100] ?")
cutoff = raw_input()
cutoff = float(cutoff)
cutoff = (100-cutoff)/100
#Asking for the user if he wants the calculation of the rarefaction curve
print ("- Do you want to determine a rarefaction curve based on these data\n[yes, no] ?")
Rarefact_chose = raw_input()
#Asking for the user if he wants the determination of the Chao1 richness estimator
print ("- Do you want to calculate the full bias corrected Chao1 richness estimator\
\non these data [yes, no] ?")
Chao_chose = raw_input()
#Asking for the user if he wants the determination of the ACE richness estimator
print ("- Do you want to calculate the ACE richness estimator on these\ndata [yes, no] ?")
ACE_chose = raw_input()
#Asking for the user if he wants the determination of the ACE richness estimator
print ("- Do you want to calculate the bootstrap estimate and also shannon and simpson\
\nindexes on these data [yes, no] ?")
Index_chose = raw_input()
#Deleting the last character of the string and transforming it in integer
#-------------------------------------------------------------------------------------------------------
#Needed function
#-------------------------------------------------------------------------------------------------------
def read_folder(path): ##read_folder (to acquire all file names in a defined directory)
tab = []
tab = os.listdir(path)
return tab
def combinatory(N,n): ##combinatory (permit the calculation of combinatorial values)
X = math.factorial(N)
Xb = math.factorial(n)
Xc = math.factorial(N-n)
final = X/(Xb*Xc)
return final
#-------------------------------------------------------------------------------------------------------
#Main program : selection and storage of the chosen cluster
#-------------------------------------------------------------------------------------------------------
string_printed +="Nb_seqs\tNb_clusters\t"
if Chao_chose.lower() == "yes":
string_printed += "Chao1\tvar(Chao1)\tLCI95\tUCI95\t"
if ACE_chose.lower() == "yes":
string_printed += "ACE\tRare_ACE\tAbundant_ACE\t"
if Index_chose.lower() == "yes":
string_printed += "Bootstrap\tShannon\tvar(Shannon)\tSimpson\t1/Simpson"
Result_printed[0] = string_printed
#Automatic acquisition of file names in the target directory
treated_files = read_folder("IN")
#For each detected file in the target directory ("IN")
for i in range(len(treated_files)):
Result_printed.append(str(treated_files[i])+"\t")
#Defined variables for chao calculation
C_chao1,S_chao1,var_S_chao1 = 0,0,0
n1,n2 = 0,0
LCI95_chao1,UCI95_chao1 = 0,0
#Defined variables for ACE calculation
N_rare_ACE,C_ACE = 0,0
Gamma_ACE,S_rare_ACE = 0,0
S_ACE,S_abund_ACE,n1_ACE = 0,0,0
#Defined variables for shannon and simpson index calculations
S_bootstrap = 0
H_Shannon,var_H_Shannon = decimal.Decimal(0),decimal.Decimal(0)
D_Simpson = decimal.Decimal(0)
#Needed empty variables
verif = 0
tmp = list()
n = 0
Cluster_analyzed = list()
#open the targeted cluster file
Cluster_file = open("IN/"+treated_files[i], 'r')
#For each read line of the file (as the line is not empty)
Read_line = Cluster_file.readline()
while Read_line != "":
Read_line = Cluster_file.readline()
#We search for the line containg the number of sequences
if 0<=Read_line.find("Sequences", 0):
#The line is splitted based on "\t" and stored in tmp
tmp = Read_line.split("\t")
#The number of sequences in stored in Nb_seqs_tot after conversion in int
Nb_seqs_tot = int(tmp[1])
#We search also for the distance cut-off to clusterize sequences to found the corresponding
#cluster based on the choice of the user
if 0<=Read_line.find("distance", 0):
tmp = Read_line.split("\t")
#We verify that the defined threshold by the user is same or below the analyzed cluster
if float(tmp[1]) >= cutoff and verif == 0:
#Needed variable to 1 to stop the data acquisition
verif = 1
Read_line = Cluster_file.readline()
#Loop to copy needed data
while Read_line != "":
#Stop the analysis if we found the end of the cluster
if Read_line == "\n":
break
#Copy and storage of cluster number needed to calculate rarefact curve
if 0<=Read_line.find("Clusters", 0):
tmp = Read_line.split("\t")
Nb_clusters = int(tmp[1])
#Copy only the needed data (number of sequences in the cluster)
elif Read_line != "":
tmp = Read_line.split("\t")
Cluster_analyzed.append(int(tmp[2]))
Read_line = Cluster_file.readline()
#Close the treated file as it's not needed now
Cluster_file.close()
#-------------------------------------------------------------------------------------------------------
#After recuperation in Cluster_analyzed of needed data (MOTUs and sequences in each MOTUs), the next
#step is developed to create the needed matrix to calculate the rarefaction curve
#-------------------------------------------------------------------------------------------------------
#Rearrange the list by values (ascending 1 to n)
Cluster_analyzed.sort()
#For each line (correponding to each MOTU), creation of the matrix
for element in Cluster_analyzed:
#We count the number of MOTUs with the same number of sequences
if Nb_seq_MOTUs == element:
Nb_MOTUs += 1
#If we found a new number of sequences in a MOTU, we create a new line and store the
#data obtained for the previous MOTU type
else:
Matrix.append([Nb_seq_MOTUs, Nb_MOTUs])
Nb_seq_MOTUs = element
Nb_MOTUs = 1
#As the loop didn't verify the last data, a new step is dedicated to the analysis
tmp = Matrix[-1]
#Addition of the last if needed
if Nb_seq_MOTUs == tmp[0]:
tmp[1] +=1
Matrix[-1] = tmp
#Or creation of a new line
else:
Matrix.append([Nb_seq_MOTUs, 1])
#Close the file
Nb_seq_MOTUs = 0
Nb_MOTUs = 0
#Deletion of the first line not needed for the rarefaction curve
del Matrix[0]
#-------------------------------------------------------------------------------------------------------
#Calculation of the rarefaction curve based on the formula found in various publications if wanted
#for details : see Appendix.A
#-------------------------------------------------------------------------------------------------------
#Verification for the choice of the user for calculation of the rarefaction curve
if Rarefact_chose.lower() == "yes":
#Create a new file to store the rarefaction curve calculation data
Rarefact_curve = open("OUT/"+"rc_"+str(cutoff)+"_"+treated_files[i], 'w')
Rarefact_curve.close()
#A maximum of 1000 points are determined, verification of the number of sequences
#If the number of sequences if less than 1000, we treat each point
if Nb_seqs_tot < 1000:
Nb_steps_calc = 1
#Else, we calculate the needed step to do 1000 points
else:
Nb_steps_calc = math.floor(Nb_seqs_tot/1000)
#Loop to calculate the rarefaction curve
while n < Nb_seqs_tot:
Child_pid = os.fork()
if Child_pid == 0:
#Calculation of the rarefaction curve points (details in the formula)
for element in Matrix:
#For each element in the matrix, we calculate N_Ni
N_Ni = Nb_seqs_tot - element[0]
#Sum of combinatory values in the upper formula using N_Ni, n and Nb_seqs_tot
Result = Result + element[1]*gmpy.comb(gmpy.mpz(N_Ni),gmpy.mpz(n))
#Determination of the value of the rarefaction curve for n sequences in Nb_seqs_tot data
Result_fin = float(Nb_clusters) - Result/(gmpy.comb(gmpy.mpz(Nb_seqs_tot),gmpy.mpz(n)))
#Needed empty variable
Result = 0
#In the result file, storage of results for this step
Result2 = str(n) + "\t" + str(Result_fin) + "\n"
#Append the file to store results
Rarefact_curve = open("OUT/"+"rc_"+str(cutoff)+"_"+treated_files[i], 'a')
Rarefact_curve.write(Result2)
Rarefact_curve.close()
os._exit(0)
else:
Children.append(Child_pid)
#Increment n using the step calculated before
n += Nb_steps_calc
#Wait for all child processes to continue the program
for Child_pid in Children:
status = os.waitpid(Child_pid,0)
Result_printed[i+1]+= str(Nb_seqs_tot)+"\t"+str(Nb_clusters)+"\t" |
Partager