-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslateSequencesByDict.py
executable file
·50 lines (40 loc) · 1.83 KB
/
translateSequencesByDict.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
#!/usr/bin/env python
import warnings
from CisRegModels import MYUTILS
import sys
import argparse
parser = argparse.ArgumentParser(description='Translates the contents of a file from one thing to another using a dictionary.')
parser.add_argument('-is',dest='inFPSeqs', metavar='<inFileSeqs>',help='Input file of sequences to translate.', required=True);
parser.add_argument('-id',dest='inFPDict', metavar='<inFileDictionary>',help='Input file of translations to use in the form from\tto', required=True);
parser.add_argument('-o',dest='outFP', metavar='<outFile>',help='Where to output results [default=stdout]', required=False);
parser.add_argument('-l',dest='logFP', metavar='<logFile>',help='Where to output errors/warnings [default=stderr]', required=False);
parser.add_argument('-v',dest='verbose', action='count',help='Verbose output?', required=False, default=0);
args = parser.parse_args();
inFileDict=MYUTILS.smartGZOpen(args.inFPDict,'r');
inFileSeqs=MYUTILS.smartGZOpen(args.inFPSeqs,'r');
if (args.logFP is not None):
logFile=MYUTILS.smartGZOpen(args.logFP,'w');
sys.stderr=logFile;
if (args.outFP is None):
outFile= sys.stdout;
else:
if args.verbose>0: sys.stderr.write("Outputting to file "+args.outFP+"\n");
outFile = MYUTILS.smartGZOpen(args.outFP,'w');
translationDict = {};
#raise Exception("Reached bad state=%d for '%s.%d' '%s' at line '%s'" %(state,mid,ver,tfid,line));
for line in inFileDict:
if line is None or line == "" or line[0]=="#": continue
data=line.rstrip().split("\t");
translationDict[data[0]] = data[1];
inFileDict.close();
for line in inFileSeqs:
if line is None or line == "" or line[0]=="#": continue
seq=line.rstrip();
if seq in translationDict:
outFile.write(translationDict[seq]+"\n");
else:
outFile.write(seq + "\n");
inFileSeqs.close();
outFile.close();
if (args.logFP is not None):
logFile.close();