forked from lasersonlab/phip-stat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parts2barcodes.py
executable file
·68 lines (61 loc) · 2.13 KB
/
parts2barcodes.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
#! /usr/bin/env python
# Copyright 2014 Uri Laserson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import argparse
import glob
import re
bcre = re.compile(r'#(.*)/')
def hamming1(s):
s = s.upper()
alts = {'A':'CGTN','C':'AGTN','G':'ACTN','T':'ACGN'}
mutants = []
for i in range(len(s)):
for alt in alts[s[i]]:
mutant = s[:i] + alt + s[i+1:]
mutants.append(mutant)
return mutants
argparser = argparse.ArgumentParser(description=None)
argparser.add_argument('-i','--input',required=True)
argparser.add_argument('-o','--output',required=True)
argparser.add_argument('-m','--mapping',required=True)
args = argparser.parse_args()
input_dir = os.path.abspath(args.input)
output_dir = os.path.abspath(args.output)
os.makedirs(output_dir,mode=0755)
mapping_file = args.mapping
# load barcode mapping and open outhandles
barcode2sample = {}
outhandles = {}
with open(mapping_file,'r') as ip:
for line in ip:
data = line.split()
bc = data[0]
sample = data[1]
barcode2sample[bc] = sample
for mut in hamming1(bc):
barcode2sample[mut] = sample
outhandles[sample] = open(os.path.join(output_dir,data[1]+'.aln'),'w')
# iterate through alignments
for infilename in glob.glob(os.path.join(input_dir,'*.aln')):
with open(infilename,'r') as ip:
for line in ip:
# read_name = line.split()[0]
# bc = bcre.search(read_name).group(1)
bc = line.split()[1].split(':')[-1]
try:
sample = barcode2sample[bc]
except KeyError:
continue
outhandles[sample].write(line)