File: run_auto_All_for_web_multi_revise.py

package info (click to toggle)
seqsero 1.0.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,232 kB
  • sloc: python: 2,447; perl: 82; sh: 55; makefile: 7
file content (315 lines) | stat: -rw-r--r-- 13,090 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3



import os
from Bio import SeqIO
import sys
import itertools
from Initial_Conditions import phase1
from Initial_Conditions import phase2
from Initial_Conditions import phaseO
from Initial_Conditions import sero
import time
import multiprocessing
import string

#m=string.atoi(sys.argv[1])
m=1 #temperorily, m can be set as one, because we just need one core to deal with it
file_name=sys.argv[1]
mapping_mode=sys.argv[2]
if sys.argv[3] not in ("1","2","3"):
  additional_file=sys.argv[3]
  file_mode=sys.argv[4]
else:
  additional_file=""
  file_mode=sys.argv[3]
#mode=sys.argv[2]

def Combine(b,c):
  fliC_combinations=[]
  fliC_combinations.append(",".join(c))
  temp_combinations=[]
  for i in range(len(b)):
    for x in itertools.combinations(b,i+1):
      temp_combinations.append(",".join(x))
  for x in temp_combinations:
    temp=[]
    for y in c:
      temp.append(y)
    temp.append(x)
    temp=",".join(temp)
    temp=temp.split(",")
    temp.sort()
    temp=",".join(temp)
    fliC_combinations.append(temp)
  return fliC_combinations


def Test(file1,additional_file,file_mode,mapping_mode,z,q):
  fliC="?"
  fljB="?"
  Otype="?"
  file2=file1.replace(' ','_').replace(":","__").replace("[","").replace("]","").replace(".fq",".fastq")
  thename=file2#$$$$$$
  try:
    os.rename(file1, file2)
    real_file=file2
  except:
    real_file=file1
  file2=additional_file.replace(' ','_').replace(":","__").replace("[","").replace("]","").replace(".fq",".fastq")
  try:
    os.rename(additional_file, file2)
    additional_file=file2
  except:
    pass
  #print "###The genome name:",file1
  dirpath = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))###01/27/2015
  os.system('touch result.txt')
  database_path="database"###01/27/2015, because we add one directory to the result directory
  if file_mode=="1":
    os.system('python3 '+dirpath+'/BWA_analysis_O_new_dependent.py '+real_file+' '+database_path+'/special_new_O_genes.fasta '+mapping_mode+' 1 >temp_result_'+str(q)+'O.txt')###01/27/2015
  elif file_mode=="2":
    os.system('python3 '+dirpath+'/BWA_analysis_O_new_dependent.py '+real_file+' '+database_path+'/special_new_O_genes.fasta '+mapping_mode+" "+additional_file+' 2 >temp_result_'+str(q)+'O.txt')###01/27/2015
  elif file_mode=="3":
    os.system('python3 '+dirpath+'/BWA_analysis_O_new_dependent.py '+real_file+' '+database_path+'/special_new_O_genes.fasta '+mapping_mode+' 3 >temp_result_'+str(q)+'O.txt')###01/27/2015
  os.system('cat temp_result_'+str(q)+'O.txt>>data_log.txt')###01/28/2015
  handle=open('temp_result_'+str(q)+'O.txt',"r")
  handle=handle.readlines()
  sdf=""#
  for line in handle:
    if "$$$Most" in line:
      Otype=line.split("O-")[1].split(" ")[0]
      Otype=Otype.replace("\n","").strip()
      #print line,
    elif "$$$No" in line:
      Otype="-"
      if "O-9" in line:
        Otype="9"
      #print line,
    if "$$$" in line and "got a hit" in line and "sdf" in line:#
      sdf="+"#
  if sdf!="+":#
    sdf="-"#
  if file_mode=="1":
    os.system('python3 '+dirpath+'/BWA_analysis_H_update_new_family_dependent.py '+real_file+' '+database_path+'/H_combine_update_9_03_2014_new.fasta '+mapping_mode+' 1 >temp_result_'+str(q)+'H.txt')###01/27/2015
  if file_mode=="2":
    os.system('python3 '+dirpath+'/BWA_analysis_H_update_new_family_dependent.py '+real_file+" "+database_path+'/H_combine_update_9_03_2014_new.fasta '+mapping_mode+' '+additional_file+' 2 >temp_result_'+str(q)+'H.txt')###01/27/2015
  if file_mode=="3":
    os.system('python3 '+dirpath+'/BWA_analysis_H_update_new_family_dependent.py '+real_file+' '+database_path+'/H_combine_update_9_03_2014_new.fasta '+mapping_mode+' 3 >temp_result_'+str(q)+'H.txt')###01/27/2015
  os.system('cat temp_result_'+str(q)+'H.txt>>data_log.txt')
  handle2=open('temp_result_'+str(q)+'H.txt',"r")
  handle2=handle2.readlines()
  suspect="no" #for the first choice doesn't hit core sequence
  for line in handle2:
    if "$$$Most" in line and "fliC" in line:
      #print line,
      fliC=line.split("  ")[1].strip()
      if fliC=="g,m,p,s":
        fliC="g,m,s"
    elif "$$$No" in line and "fliC" in line:
      fliC="-"
      #print line,
    elif "$$$Most" in line and "fljB" in line:
      #print line,
      fljB=line.split("  ")[1].strip()
    elif "$$$No" in line and "fljB" in line:
      fljB="-"
      #print line,
    elif "$$$There" in line and "fliC" in line:
      fliC=line.split(":")[-1].replace("\n","").strip()
      #print line,
      suspect="yes"
    elif "$$$There" in line and "fljB" in line:
      fljB=line.split(":")[-1].replace("\n","").replace(" ","").strip()
      #print line,
      suspect="yes"
  seronames=[]
  for i in range(len(phase1)):
    fliC_combine=[]
    fljB_combine=[]
    if phaseO[i]==Otype:
      if phase1[i].count("[")==0:
        fliC_combine.append(phase1[i])
      elif phase1[i].count("[")>=1:
        c=[]
        b=[]
        if phase1[i][0]=="[" and phase1[i][-1]=="]" and phase1[i].count("[")==1:#for specific situations like [1,5]
          content=phase1[i].replace("[","").replace("]","")
          fliC_combine.append(content)
          fliC_combine.append("-")
        else:
          for x in phase1[i].split(","):
            if "[" in x:
              b.append(x.replace("[","").replace("]",""))
            else:
              c.append(x)
          fliC_combine=Combine(b,c) #Combine will offer every possible combinations of the formula, like f,[g],t: f,t  f,g,t
      if phase2[i].count("[")==0:
        fljB_combine.append(phase2[i])
      elif phase2[i].count("[")>=1:
        d=[]
        e=[]
        if phase2[i][0]=="[" and phase2[i][-1]=="]" and phase2[i].count("[")==1:
          content=phase2[i].replace("[","").replace("]","")
          fljB_combine.append(content)
          fljB_combine.append("-")
        else:
          for x in phase2[i].split(","):
            if "[" in x:
              d.append(x.replace("[","").replace("]",""))
            else:
              e.append(x)
          fljB_combine=Combine(d,e)
      new_fliC=fliC.split(",") #because some antigen like r,[i] not follow alphabetical order, so use this one to judge and can avoid missings
      new_fliC.sort()
      new_fliC=",".join(new_fliC)
      new_fljB=fljB.split(",")
      new_fljB.sort()
      new_fljB=",".join(new_fljB)
      if (new_fliC in fliC_combine or fliC in fliC_combine) and (new_fljB in fljB_combine or fljB in fljB_combine):
        seronames.append(sero[i])
  if len(seronames)==0:
    seronames=["N/A (The predicted antigenic profile does not exist in the White-Kauffmann-Le Minor scheme)"]
  star=""
  star_line=""
  if len(seronames)>1:
    star="*"
    star_line="The predicted serotypes share the same general formula:\t"+Otype+":"+fliC+":"+fljB+"\n"##
  #print "$$$The most possible formula is: (by the order O:H1:H2) ",Otype,":",fliC,":",fljB
  #print "$$$The possible serotyes are:",seronames
  m=0
  for y in seronames:
    if y in file1:
      #print "$$$ Is the judgement true? Answer:Yes!"       #here we use file1, because we want ":", while file2 turned it to "__"
      answer="Yes"
      m=1
  if m==0:
    #print "$$$ Is the judgement true? Answer: Need to check the records and file names"
    answer="Not sure"
  print("\n","\n")
  predict_form=Otype+":"+fliC+":"+fljB#
  predict_sero=(" or ").join(seronames)#
  if predict_form=="9:g,m:-":#
    predict_form=predict_form+"\nSdf prediction:"+sdf #
    if sdf=="-":#
      star="*"#
      star_line="Additional characterization is necessary to assign a serotype to this strain.  Commonly circulating strains of serotype Enteritidis are sdf+, although sdf- strains of serotype Enteritidis are known to exist. Serotype Gallinarum is typically sdf- but should be quite rare. Sdf- strains of serotype Enteritidis and serotype Gallinarum can be differentiated by phenotypic profile or genetic criteria.\n"#+##
      predict_sero="See comments below"#
  elif predict_form=="4:i:-":#03252016#
    predict_sero="potential monophasic variant of Typhimurium"#03252016#
  elif predict_form=="4:r:-":#03252016#
    predict_sero="potential monophasic variant of Heidelberg"#03252016# 
  elif predict_form=="4:b:-":#03252016#
    predict_sero="potential monophasic variant of Paratyphi B"#03252016#
  elif predict_form=="8:e,h:1,2":#03282016#
    predict_sero="Newport"#03282016#
    star="*"##03282016#
    star_line="Serotype Bardo shares the same antigenic profile with Newport, but Bardo is exceedingly rare."#03282016#
  claim="The serotype(s) is/are the only serotype(s) with the indicated antigenic profile currently recognized in the Kauffmann White Scheme.  New serotypes can emerge and the possibility exists that this antigenic profile may emerge in a different subspecies.  Identification of strains to the subspecies level should accompany serotype determination; the same antigenic profile in different subspecies is considered different serotypes."##
  if "N/A" in predict_sero:###added after standalone version, 2015/2/3
    claim=""###added after standalone version, 2015/2/3
  '''
  new_file=open(file2+".txt","w")
  new_file.write(file2+"\t"+"O-"+Otype+"\t"+fliC+"\t"+fljB+"\t"+Otype+":"+fliC+":"+fljB+"\t"+(" or ").join(seronames)+"\t"+answer+"\t"+suspect+"\n")
  new_file.close()
  '''
  if "Typhimurium" in predict_sero or predict_form=="4:i:-":#03252016#
    if file_mode=="1":
      os.system('python3 '+dirpath+'/deletion_compare.py '+real_file+' oafA '+mapping_mode+' 1 >temp_result_'+str(q)+'Typhim.txt')###02/06/2015
    if file_mode=="2":
      os.system('python3 '+dirpath+'/deletion_compare.py '+real_file+' oafA '+mapping_mode+" "+additional_file+' 2 >temp_result_'+str(q)+'Typhim.txt')###02/06/2015
    if file_mode=="3":
      os.system('python3 '+dirpath+'/deletion_compare.py '+real_file+' oafA '+mapping_mode+' 3 >temp_result_'+str(q)+'Typhim.txt')###02/06/2015
    os.system('cat temp_result_'+str(q)+'Typhim.txt>>data_log.txt')###01/28/2015
    handle=open('temp_result_'+str(q)+'Typhim.txt',"r")
    handle=handle.readlines()
    for line in handle:
      if "$$$Typhimurium_O5-" in line:
        predict_sero=predict_sero.strip()+"(O5-)"#03252016#
        star="*"#
        star_line="Detected the deletion of O5-."
  new_file=open("Seqsero_result.txt","w")
  new_file.write("Input files:\t"+thename+" "+additional_file+"\n"+"O antigen prediction:\t"+"O-"+Otype+"\n"+"H1 antigen prediction(fliC):\t"+fliC+"\n"+"H2 antigen prediction(fljB):\t"+fljB+"\n"+"Predicted antigenic profile:\t"+predict_form+"\n"+"Predicted serotype(s):\t"+predict_sero+star+"\n"+star+star_line+claim+"\n")#+##
  new_file.close()
  os.system("rm temp_result_"+str(q)+"*.txt")###01/28/2015
  os.system("rm result.txt")###01/28/2015
  os.system("rm -rf database")###01/28/2015
  os.system("rm -f *.fq *.fastq *.sam *.bam *.sai *.sra")###02/07/2015
  #os.system("rm "+for_fq)###01/28/2015
  #os.system("rm "+rev_fq)###01/28/2015
  #os.system("rm "+for_sai)
  #os.system("rm "+rev_sai)
  #os.system("rm "+bam)
  #os.system("rm "+sam)###01/28/2015
  #os.system("rm temp.txt")###01/28/2015
  #os.system("rm "+sam+".fasta"+"_db.*")###01/28/2015
  #os.system("rm temp"+sra_name+".fasta")###01/28/2015   

    
def main():
  file_pair=(file_name,additional_file)
  file_names=[]
  file_names.append(file_pair)
  for i in range(0,len(file_names),m):
    jobs=[]
    txt_names=[]
    if len(file_names)>=i+m:
      for j in range(m):
        p = multiprocessing.Process(target=Test,args=(file_names[j+i][0],file_names[j+i][1],file_mode,mapping_mode,i+j+1,i+j,))
        jobs.append(p)
        p.start()
    else:
      t=m+i-len(file_names)
      for j in range(m-t):
        p = multiprocessing.Process(target=Test,args=(file_names[j+i][0],file_names[j+i][1],file_mode,mapping_mode,i+j+1,i+j,))
        jobs.append(p)
        p.start()
'''
  files1=[]
  files1.append(file_name)
  file_names=[]
  fastq_names=[]
  for file1 in files1:
    if file1[-4:]=='.sra':
      file_names.append(file1)
    if file1[-9:]==".fastq.gz":#need to change
      fastq_names.append(file1)
    if file1[-6:]==".fastq":#need to change
      fastq_names.append(file1)
  fastq_names=list(set(fastq_names))
  file_names=file_names+fastq_names
  for i in range(0,len(file_names),m):
    jobs=[]
    txt_names=[]
    if len(file_names)>=i+m:
      for j in range(m):
        p = multiprocessing.Process(target=Test,args=(file_names[j+i],i+j+1,i+j,))
        jobs.append(p)
        p.start()
    else:
      t=m+i-len(file_names)
      for j in range(m-t):
        p = multiprocessing.Process(target=Test,args=(file_names[j+i],i+j+1,i+j,))
        jobs.append(p)
        p.start()
'''

'''
    for j in xrange(len(jobs)):
      jobs[j].join()
      txt_names.append(file_names[j+i].replace(' ','_').replace(":","__").replace("[","").replace("]","")+".txt")
    print txt_names
    for j in xrange(len(txt_names)):
      print i,"and",j
      print i+j+1
      file=open(txt_names[j],"r")
      handle=list(file)
      b=handle[0].split("\t")
      print b

  print "End time,",time.time()
'''

if __name__ == '__main__':
  main()