-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get_batch_information_leftover.py
executable file
·141 lines (130 loc) · 4.51 KB
/
Get_batch_information_leftover.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
#!/usr/bin/python
#import math
import sys
import os
import commands
import numpy as np
from collections import defaultdict
def fasta_iterator(fh):
while True:
line = fh.readline()
if line.startswith('>'): break
while True:
header = line[1:-1].rstrip()
sequence = fh.readline().rstrip()
while True:
line = fh.readline()
if not line: break
if line.startswith('>'): break
sequence += line.rstrip()
yield(header, sequence)
if not line: return
def Get_sample_files(batch_file):
id, dir = [],[]
fh=open(batch_file,"r")
ind = 0
for l in fh:
ind = ind+1
if(l[0]!="#" and len(l)>3):
l=l.strip().split()
id.append(l[0])
dir.append(l[7])
fh.close()
return(id, dir)
class Tree(defaultdict):
def __init__(self, value=None):
super(Tree, self).__init__(Tree)
self.value = value
def Get_run_ids():
file = "/gpfs3/well/immune-rep/shared/MISEQ/FMS/ORIENTATED_SEQUENCES/ANNOTATIONS/IMGT_RAW/IMGT_BCR1/1_Summary.txt"
done = Tree()
fh=open(file, "r")
for l in fh:
l=l.strip().split()
if(l[0]!="Sequence"):
seq = l[1].split("__")[0]
done[seq].value = 1
fh.close()
print len(done), "done"
return(done)
def Batch_sequences_for_IMGT(id,dir):
done = Get_run_ids()
file_out_pre = dir[0]+"ORIENTATED_SEQUENCES/NETWORKS/Fully_reduced_"+batch_file.replace(".txt","").replace("Samples_","")+"_"
out, ind = '',0
batch_size,batch = 1000000-10,1
n = 0
fh=open(file_out_pre+str(batch)+".fasta","w")
fh.close()
for s in range(len(id)):
print s
sample,dir_use = id[s],dir[s]
fasta_file = dir_use+"ORIENTATED_SEQUENCES/NETWORKS/Fully_reduced_"+sample+".fasta"
if(os.path.exists(fasta_file)):
fh= open(fasta_file,"r")
for header,sequence in fasta_iterator(fh):
if(header.split("__")[0] not in done):
n,ind=n+1,ind+1
out=out+">"+header+"\n"+sequence+"\n"
if(ind>1000 or n>=batch_size):
Write_out(out, file_out_pre+str(batch)+".fasta")
out, ind = '',0
if(n>=batch_size):
n,batch = 0,batch+1
fh=open(file_out_pre+str(batch)+".fasta","w")
fh.close()
fh.close()
Write_out(out, file_out_pre+str(batch)+".fasta")
out, ind = '',0
return()
def Write_out(out, file):
fh=open(file,"a")
fh.write(out)
fh.close()
return()
def Get_isotype_depth(id,dir):
isotypes_uniq,isotypes_total = {},{}
all_uniq, all_total = {},{}
for s in range(len(id)):
sample,dir_use = id[s],dir[s]
isotype_file = dir_use+"ORIENTATED_SEQUENCES/ANNOTATIONS/IsoTyper_chain_repertoire_statistics_file_"+sample+".txt"
if(os.path.exists(isotype_file)):
fh=open(isotype_file,"r")
for l in fh:
if(l[0]!="#"):
l=l.strip().split()
iso, umis, uniqs = l[1],int(l[2]), int(l[3])
if(iso in ["TRBC1","TRBC2"]):iso = "TRBC"
if(iso in ["TRGC1","TRGC2"]):iso = "TRGC"
if(iso in isotypes_uniq):
isotypes_uniq[iso], isotypes_total[iso] = isotypes_uniq[iso]+[uniqs], isotypes_total[iso] +[umis]
else:isotypes_uniq[iso], isotypes_total[iso] = [uniqs],[umis]
if(sample in all_uniq):
all_uniq[sample],all_total[sample] = all_uniq[sample]+uniqs,all_total[sample]+umis
else:all_uniq[sample],all_total[sample] = uniqs,umis
fh.close()
array_uniq, array_total = [],[]
for s in all_uniq:
array_uniq, array_total = array_uniq +[all_uniq[s]], array_total + [all_total[s]]
isotypes_uniq["all"] = array_uniq
isotypes_total["all"] = array_total
out = "#isotype\ttype\tmin\t5th percentile\t10th percentile\t20th percentile\n"
for iso in isotypes_uniq:
print iso
quantiles = [np.percentile(isotypes_uniq[iso],5), np.percentile(isotypes_uniq[iso],10),np.percentile(isotypes_uniq[iso],20)]
out = out+"\t".join(map(str, [iso,"UNIQ",min(isotypes_uniq[iso])]+quantiles))+"\n"
for iso in isotypes_total:
quantiles = [np.percentile(isotypes_total[iso],5), np.percentile(isotypes_total[iso],10),np.percentile(isotypes_total[iso],20)]
out = out+"\t".join(map(str, [iso,"TOTAL",min(isotypes_total[iso])]+quantiles))+"\n"
out_file = dir_use+"ORIENTATED_SEQUENCES/ANNOTATIONS/Sampling_depth_per_isotype_"+batch_file.replace(".txt","")+".txt"
print out_file
fh=open(out_file, "w")
fh.write(out)
fh.close()
return()
################################################
args=sys.argv
batch_file = args[1]
print batch_file
id, dir = Get_sample_files(batch_file)
#Get_isotype_depth(id,dir)
Batch_sequences_for_IMGT(id,dir)