Mercurial > repos > yufei-luo > s_mart
comparison SMART/bacteriaRegulatoryRegion_Detection/changeName.py @ 18:94ab73e8a190
Uploaded
author | m-zytnicki |
---|---|
date | Mon, 29 Apr 2013 03:20:15 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
17:b0e8584489e6 | 18:94ab73e8a190 |
---|---|
1 #! /usr/bin/env python | |
2 | |
3 import optparse, os, sys, subprocess, tempfile, shutil | |
4 from optparse import OptionParser | |
5 | |
6 def stop_err(msg): | |
7 sys.stderr.write('%s\n' % msg) | |
8 sys.exit() | |
9 | |
10 def changeName(fileName, format, name, outputName): | |
11 file = open(fileName, 'r') | |
12 line = file.readline() | |
13 if format == "fasta": | |
14 while not line.startswith('>'): | |
15 line = file.readline() | |
16 old_name = line[1:] | |
17 elif format == "gff": | |
18 while line.startswith('#'): | |
19 line = file.readline() | |
20 old_name = (line.split('\t'))[0] | |
21 elif format == "sam": | |
22 while line.startswith('@'): | |
23 line = file.readline() | |
24 old_name = (line.split('\t'))[2] | |
25 file.close() | |
26 cmd = "sed \"s/%s/%s/g\" %s >%s " % (old_name.strip(), name.strip(), fileName, outputName) | |
27 proc = subprocess.Popen(cmd, shell=True) | |
28 proc.communicate() | |
29 if proc.returncode != 0: | |
30 raise Exception("ERROR when launching '%s'" % cmd) | |
31 | |
32 def getName(fileName, format): | |
33 file = open(fileName, 'r') | |
34 line = file.readline() | |
35 if format == "gff": | |
36 while line.startswith('#'): | |
37 line = file.readline() | |
38 old_name = (line.split('\t'))[0] | |
39 elif format == "sam": | |
40 while line.startswith('@') or line.startswith('#'): | |
41 line = file.readline() | |
42 old_name = (line.split('\t'))[2] | |
43 file.close() | |
44 return old_name | |
45 | |
46 def __main__(): | |
47 #Parse Command Line | |
48 parser = optparse.OptionParser() | |
49 parser.add_option("", "--input1", dest="inputFile1", default=None, help="Choose a fasta file.") | |
50 parser.add_option("", "--input2", dest="inputFile2", default=None, help="Choose a gff file.") | |
51 parser.add_option("", "--input3", dest="inputFile3", default=None, help="Choose a sam file.") | |
52 parser.add_option("", "--name", dest="name", default=None, help="Change to a new name.[compulsory] if there is only one input.") | |
53 parser.add_option("", "--output1", dest="outputFile1", default=None, help="OutputFile1") | |
54 parser.add_option("", "--output2", dest="outputFile2", default=None, help="OutputFile2") | |
55 parser.add_option("", "--output3", dest="outputFile3", default=None, help="OutputFile3") | |
56 (options, args) = parser.parse_args() | |
57 | |
58 #TODO:write raise Exception!! | |
59 | |
60 #In case only one input | |
61 if options.name == None: | |
62 #find a default_name to unify the name for all input files | |
63 if options.inputFile1 != None: | |
64 if options.inputFile2 == None and options.inputFile3 == None: | |
65 raise Exception("ERROR, only one input, you should identify a new name to modify.") | |
66 elif options.inputFile2 != None and options.outputFile2 != None: | |
67 default_name = getName(options.inputFile2, 'gff') | |
68 changeName(options.inputFile1, 'fasta', default_name, options.outputFile1) | |
69 changeName(options.inputFile2, 'gff', default_name, options.outputFile2) | |
70 if options.inputFile3 != None and options.outputFile3 != None: | |
71 changeName(options.inputFile3, 'sam', default_name, options.outputFile3) | |
72 elif options.inputFile3 != None and options.outputFile3 != None: | |
73 default_name = getName(options.inputFile3, 'sam') | |
74 changeName(options.inputFile3, 'sam', default_name, options.outputFile3) | |
75 changeName(options.inputFile1, 'fasta', default_name, options.outputFile1) | |
76 if options.inputFile2 != None and options.outputFile2 != None: | |
77 changeName(options.inputFile2, 'gff', default_name, options.outputFile2) | |
78 else: | |
79 if options.inputFile1 != None and options.outputFile1 != None: | |
80 changeName(options.inputFile1, 'fasta', options.name, options.outputFile1) | |
81 if options.inputFile2 != None and options.outputFile2 != None: | |
82 changeName(options.inputFile2, 'gff', options.name, options.outputFile2) | |
83 if options.inputFile3 != None and options.outputFile3 != None: | |
84 changeName(options.inputFile3, 'sam', options.name, options.outputFile3) | |
85 | |
86 if __name__ == '__main__':__main__() | |
87 | |
88 | |
89 #test commands: | |
90 #only one input: | |
91 #python changeName.py --input1 NC_011744.fna --name NC_test --output1 out.fna | |
92 #several inputs: | |
93 #python changeName.py --input1 NC_011744.fna --input2 NC_011744.gff --output1 out.fna --output2 out.gff | |
94 #python changeName.py --input1 NC_011744.fna --input2 NC_011744.gff --name NC_test --output1 out.fna --output2 out.gff | |
95 #python changeName.py --input1 NC_011744.fna --input2 NC_011744.gff --input3 NC_011744.sam --name NC_test2 --output1 out.fna --output2 out.gff --output3 out.sam | |
96 #python changeName.py --input1 NC_011744.fna --input3 out.sam --output1 out.fna --output3 out.sam | |
97 | |
98 | |
99 |