python - How to treat stdin like a text file -


i have program reads parses text file , analysis on it. want modify can take parameters via command line. reading file when designated stdin.

the parser looks this:

class fastareader :     '''     class provide reading of file containing 1 or more fasta     formatted sequences:     object instantiation:     fastareader(<file name>):      object attributes:     fname: initial file name      methods:     readfasta() : returns header , sequence 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         2 strings - header , sequence.         whitespace removed, no adjustment made sequence contents.         initial '>' removed header.         '''         header = ''         sequence = ''          open(self.fname) fileh:             # initialize return containers             header = ''             sequence = ''              # skip first fasta header             line = fileh.readline()             while not line.startswith('>') :                 line = fileh.readline()             header = line[1:].rstrip()              # header saved, rest of sequence             # until next header found             # yield results , wait next call.             # next call resume @ yield point             # have next header             line in fileh:                 if line.startswith ('>'):                     yield header,sequence                     header = line[1:].rstrip()                     sequence = ''                 else :                     sequence += ''.join(line.rstrip().split()).upper()         # final header , sequence seen end of file         # clause terminate, final yield of data         yield header,sequence  # presumed object instantiation , example usage # myreader = fastareader ('testtiny.fa'); # head, seq in myreader.readfasta() : #     print (head,seq) 

it parses files this:

>test atgaaatag >test2 aatgatgtaa >test3 aaatgatgtaa  >test-1 tta cat cat  >test-2 tta cat cat  >test-3 tta cat cat aa  >test1a atgatgtaaa >test2a aatgatgtaaa >test3a aaatgatgtaaa  >test-1a tta cat cat  >test-2a aa tta cat cat  >test-3a aa tta cat cat aa 

my test program looks this:

import argparse import sequenceanalysis 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)         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 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 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() 

my commandline input looks this:

python testcommand2.py -s atg <tass2.fa >out.txt 

where tass2.fa filewhich can parsed fastareader. can pass paramters start , them output text file when try parse input file should stdin prints instead of parsing , instead of outputting designated textfile should stdout prints directly commandline.

when use i/o redirection (i.e. have < or | or > or << in command line), handled shell before program runs. when python runs, standard input connected file or pipe redirecting from, , standard output connected file or pipe redirecting to, , file names not (directly) visible python because dealing open()ed file handles, not file names. argument parser returns nothing, because there no file name arguments.

to correctly cope this, should adapt code work file handles directly -- instead of, or in addition to, explicit file names.

for latter scenario, common convention have special case file name - , when passed in, use standard input (or standard output, depending on context) instead of opening file. (you can still have files named simple workaround of using relative path ./- name not single dash.)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -