-
Notifications
You must be signed in to change notification settings - Fork 1
/
retreat_haplotypes.tsv.py
executable file
·210 lines (188 loc) · 7.01 KB
/
retreat_haplotypes.tsv.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
########################################################################
## IMPORT
import sys, getopt, os,glob
from os import listdir
from os.path import isfile, join, islink
import gc
import gzip
########################################################################
## FUNCTIONS
def usage():
print 'retreat_haplotypes.tsv.py -i <input-haplotype> -o <output-haplotype> -s <sstacks_results_dir>'
sys.exit(2)
def research_ambiguous_haplotypes(sstacks_dir,dic_out):
for f in glob.glob(sstacks_dir+'/*.matches.tsv*') :
name=os.path.basename(f).split(".matches")[0]
dic_out[name]={}
tmpd={}
if f.endswith(".gz"):
handle = gzip.open( f, "r" )
else :
handle=open(f,"r")
for l in handle.readlines():
tmplist=l.split()
locus=tmplist[2]
if not locus in tmpd:
tmpd[locus]=1
else:
tmpd[locus]+=1
if not locus in dic_out[name] :
dic_out[name][locus]=2
else :
dic_out[name][locus]+=1
tmpd.clear()
gc.collect()
def rewrite_happlotypes(infile,dic_amb,outfile):
FO=open(outfile,"w")
FI=open(infile,"r")
header=FI.readline().strip()
nameOut=header.split("\t")[1:]
c=0
string_out=header+"\n"
i=1
for l in FI.readlines():
tmplist=l.strip().split()
locus=tmplist[0]
s=locus
for i in xrange(0,len(nameOut)):
sample=nameOut[i]
if not sample in dic_amb: # colonne de comptage d'individus génotypé (et de fréquence pour le programme genotype)
s+="\t"+tmplist[i+1]
elif tmplist[i+1]!="-": # si genotype connu on le conserve
s+="\t"+tmplist[i+1]
else :
if locus in dic_amb[sample] :
s+="\t?"
else :
s+="\t-"
string_out+=s+"\n"
i+=1
if i==2000:
c+=1
FO.write(string_out)
string_out=""
i=0
print "\trewrite haplotypes for "+`c*2000`+" locus "
if not string_out == "":
print "\trewrite haplotypes for "+`c*2000+i`+" locus"
FO.write(string_out)
string_out=""
i=0
# lecture du fichier d'haplotypage de Stacks et enregistrement dans un dictionnaire
# sortie = dict[locus][sample]=haplotypes dict[locus][Cnt]=nb_indiv_haplotypé
def read_haplotypes(infile,dicOut):
nameOut=[]
FI=open(infile,"r")
header=FI.readline().strip()
nameOut=header.split("\t")[1:]
for locus in FI.readlines():
tmplist=locus.split()
dicOut[int(tmplist[0])]=dict(zip(nameOut,tmplist[1:]))
FI.close()
#~ print nameOut
#~ print dicOut.keys()
#~ print dicOut["10258"]
return nameOut
# lectures des fichiers matches.tsv (sorties sstacks) et enregistrement du nombre de cluster d'un individus correspondant à un locus du catalog
# sortie = dict[sample][locus]=nb cluster
def read_sstacks(indir,dicOut) :
#~ print [ f for f in listdir(indir) if isfile(join(indir,f))]
matchesfiles = [ f for f in listdir(indir) if (isfile(join(indir,f)) or islink(join(indir,f))) and f.endswith(".matches.tsv") ]
#~ print matchesfiles
for f in matchesfiles:
FI=open(os.path.realpath(indir+"/"+f),"r")
sample=f.replace(".matches.tsv","")
d={}
for l in FI.readlines():
tmplist=l.split()
locus=int(tmplist[2])
if not locus in d:
d[locus]=1
else:
d[locus]+=1
dicOut[sample]=d
FI.close()
#~ print dicOut.keys()
#~ print dicOut["EFFICACE_11_1"]["10258"]
#~ def research_ambiguities_haplotypes(haplotypes_dict, header, matches_dict, outfile) :
#~ FO=open(outfile,"w")
#~
#~ FO.write("Catalog ID\t"+"\t".join(header)+"\n")
#~ string=""
#~ i=0
#~ amb=0
#~ c=0
#~ for locus in sorted(haplotypes_dict.keys()):
#~ #print c*2000+i
#~ s=`locus`
#~ for sample in header :
#~ if not sample in matches_dict.keys(): # colonne de comptage d'individus génotypé (et de fréquence pour le programme genotype)
#~ s+="\t"+haplotypes_dict[locus][sample]
#~ else:
#~ if not haplotypes_dict[locus][sample]=="-" : # si genotype connu on le conserve
#~ s+="\t"+haplotypes_dict[locus][sample]
#~ else: # genotype inconnu pour l'individus
#~ if locus in matches_dict[sample].keys(): #inconnu car ambigue
#~ s+="\t?"
#~ amb+=1
#~ else : # reellement inconnu
#~ s+="\t-"
#~ string+=s+"\n"
#~ i+=1
#~ if i==2000:
#~ c+=1
#~ FO.write(string)
#~ string=""
#~ i=0
#~ print "\tresearch ambiguous haplotypes for "+`c*2000`+" locus on "+`len(haplotypes_dict.keys())`
#~
#~ if not string == "":
#~ print "\tresearch ambiguous haplotypes for "+`c*2000+i`+" locus on "+`len(haplotypes_dict.keys())`
#~ FO.write(string)
#~ string=""
#~ i=0
#~ print "Found "+`amb`+" ambiguous haplotypes\n"
#~ FO.close()
########################################################################
## MAIN
SSTACKS_DIR = ''
HAPLOTYPES_IN= ''
HAPLOTYPES_OUT= ''
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:s:o:",["input=","sstacks-dir=","output="])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt == '-h':
usage()
elif opt in ("-i", "--input"):
HAPLOTYPES_IN = arg
elif opt in ("-o", "--output"):
HAPLOTYPES_OUT = arg
elif opt in ("-s", "--sstacks-dir"):
SSTACKS_DIR = arg
if HAPLOTYPES_IN =='' or not os.path.exists(HAPLOTYPES_IN):
print "infile :"+HAPLOTYPES_IN+" ,does not exist"
usage()
if SSTACKS_DIR =='' or not os.path.exists(SSTACKS_DIR):
print "sstacks results directory :"+SSTACKS_DIR+" ,does not exist"
usage()
if HAPLOTYPES_OUT=='':
tmplist=HAPLOTYPES_IN.split('.')
HAPLOTYPES_OUT='.'.join(tmplist[0:-1])+"_retreat."+tmplist[-1]
print "output haplotypes files will be : "+HAPLOTYPES_OUT
print "record amb matches"
amb_matches={}
research_ambiguous_haplotypes(SSTACKS_DIR,amb_matches)
print "rewrite haplotypes"
rewrite_happlotypes(HAPLOTYPES_IN,amb_matches,HAPLOTYPES_OUT)
#~ haplotypes_dict={}
#~ header=read_haplotypes(HAPLOTYPES_IN,haplotypes_dict)
#~ print "Found "+`len(haplotypes_dict.keys())`+" loci\n"
#~
#~ matches_dict={}
#~ read_sstacks(SSTACKS_DIR,matches_dict)
#~ print "Found "+`len(matches_dict.keys())`+" samples\n"
#~ research_ambiguities_haplotypes(haplotypes_dict, header, matches_dict, HAPLOTYPES_OUT)