我有一個程序,讀取分析一個文本文件,並對它做一些分析。我想修改它,以便它可以通過命令行獲取參數。當它被指定爲stdin時從文件讀取。如何看待stdin像一個文本文件
解析器是這樣的:
class FastAreader :
'''
Class to provide reading of a file containing one or more FASTA
formatted sequences:
object instantiation:
FastAreader(<file name>):
object attributes:
fname: the initial file name
methods:
readFasta() : returns header and sequence as strings.
Author: David Bernick
Date: April 19, 2013
'''
def __init__ (self, fname):
'''contructor: saves attribute fname '''
self.fname = fname
def readFasta (self):
'''
using filename given in init, returns each included FastA record
as 2 strings - header and sequence.
whitespace is removed, no adjustment is made to sequence contents.
The initial '>' is removed from the header.
'''
header = ''
sequence = ''
with open(self.fname) as fileH:
# initialize return containers
header = ''
sequence = ''
# skip to first fasta header
line = fileH.readline()
while not line.startswith('>') :
line = fileH.readline()
header = line[1:].rstrip()
# header is saved, get the rest of the sequence
# up until the next header is found
# then yield the results and wait for the next call.
# next call will resume at the yield point
# which is where we have the next header
for line in fileH:
if line.startswith ('>'):
yield header,sequence
header = line[1:].rstrip()
sequence = ''
else :
sequence += ''.join(line.rstrip().split()).upper()
# final header and sequence will be seen with an end of file
# with clause will terminate, so we do the final yield of the data
yield header,sequence
# presumed object instantiation and example usage
# myReader = FastAreader ('testTiny.fa');
# for head, seq in myReader.readFasta() :
# print (head,seq)
它解析看起來像這樣的文件:
>test
ATGAAATAG
>test2
AATGATGTAA
>test3
AAATGATGTAA
>test-1
TTA CAT CAT
>test-2
TTA CAT CAT A
>test-3
TTA CAT CAT AA
>test1A
ATGATGTAAA
>test2A
AATGATGTAAA
>test3A
AAATGATGTAAA
>test-1A
A TTA CAT CAT
>test-2A
AA TTA CAT CAT A
>test-3A
AA TTA CAT CAT AA
我的測試程序是這樣的:
import argparse
import sequenceAnalysis as s
import sys
class Test:
def __init__(self, infile, longest, min, start):
self.longest = longest
self.start = set(start)
self.infile = infile
self.data = sys.stdin.read()
self.fasta = s.FastAreader(self.data)
for head, seq in self.fasta.readFasta():
self.head = head
self.seq = "".join(seq).strip()
self.test()
def test(self):
print("YUP", self.start, self.head)
def main():
parser = argparse.ArgumentParser(description = 'Program prolog',
epilog = 'Program epilog',
add_help = True, #default is True
prefix_chars = '-',
usage = '%(prog)s [options] -option1[default] <input >output')
parser.add_argument('-i', '--inFile', action = 'store', help='input file name')
parser.add_argument('-o', '--outFile', action = 'store', help='output file name')
parser.add_argument('-lG', '--longestGene', action = 'store', nargs='?', const=True, default=True, help='longest Gene in an ORF')
parser.add_argument('-mG', '--minGene', type=int, choices= range(0, 2000), action = 'store', help='minimum Gene length')
parser.add_argument('-s', '--start', action = 'append', nargs='?', help='start Codon') #allows multiple list options
parser.add_argument('-v', '--version', action='version', version='%(prog)s 0.1')
args = parser.parse_args()
test = Test(args.inFile, args.longestGene, args.minGene, args.start)
if __name__ == '__main__':
main()
我的命令行輸入長相像這樣:
python testcommand2.py -s ATG <tass2.fa >out.txt
其中tass2.fa是一個可由FastAreader解析的文件。我可以像start一樣傳遞參數,並讓它們輸出到文本文件,但是當我嘗試解析輸入文件時應該是stdin,它會打印所有內容而不是解析它,而不是輸出到指定的文本文件,該文件應該是標準輸出文件直接到命令行。
您的命令行是這樣的:'python test.py < in.txt > parsed.txt' ?你可以顯示你使用的命令行(每[問])? – boardrider
@boardrider對不起,我的文章已被相應編輯。但是,這是我的命令輸入的樣子。 – Sam