OK, this version uses argparse which I should have used in the first one.
now you can use filename with or without quotes.
There is a new command line structure, it now uses flags, so arguments can be passed in any order, so long as they have the proper flags,
you can type:
You can still import the module into an other program as explained previously.
just make sure you use argument names like:
now you can use filename with or without quotes.
There is a new command line structure, it now uses flags, so arguments can be passed in any order, so long as they have the proper flags,
you can type:
python SelectAndSortFasta.py -hTo get help
Output:λ python SelectAndSortFasta.py -h
usage: SelectAndSortFasta.py [-h] [-i INFILE_NAME] [-o OUFILE_NAME]
[-s MINSIZE]
SortFasta
optional arguments:
-h, --help show this help message and exit
-i INFILE_NAME Name of input file
-o OUFILE_NAME Name of output file
-s MINSIZE Minimum sequence length
so a proper command line argument would look like:python SelectAndSortFasta.py -o data/fasta/AINZ01/AINZ01sorted.1.fsa_nt -s 1000 -i data/fasta/AINZ01/AINZ01.1.fsa_nt # or python SelectAndSortFasta.py -o 'data/fasta/AINZ01/AINZ01sorted.1.fsa_nt' -s 1000 -i 'data/fasta/AINZ01/AINZ01.1.fsa_nt'Assuming your data was in a sub-directory 'data/fasta/AINZ01'
You can still import the module into an other program as explained previously.
just make sure you use argument names like:
#At top of program import SelectAndSortFasta # In Initialization: ssf = SelectAndSortFasta.SelectAndSortFasta() # or if in calss: self.ssf = SelectAndSortFasta.SelectAndSortFasta() # Then when you want to convert a file: ssf.split_file(self, in_filename='YourInfileName', out_filename='YourOutfileName', min_seq_len=1000) # or if in class: self.ssf.split_file(self, in_filename='YourInfileName', out_filename='YourOutfileName', min_seq_len=1000)Here's the new (and final?) code.
import sys import argparse class SelectAndSortFasta: def __init__(self): self.lines_out = [] self.header = None self.save_seq = '' self.infilename = None self.outfilename = None self.minsize = None def save_this(self, fo): fo.write('{}\n'.format(self.header)) fo.write('{}\n'.format(self.save_seq)) def split_file(self, from_args=False, in_filename=None, out_filename=None, min_seq_len=None): iname = None oname = None mlen = None if from_args: iname = self.infilename oname = self.outfilename mlen = self.minsize else: iname = in_filename oname = out_filename mlen = min_seq_len self.header = None seqlen = 0 self.lines_out = [] with open(iname, 'r') as fin, open(oname, 'w') as fo: firstline = True for line in fin: line = line.strip() if line.startswith('>'): if firstline: firstline = False self.header = line else: if seqlen > mlen: self.save_this(fo) self.save_seq = line self.lines_out.append(self.header) self.header = line seqlen = 0 else: self.save_seq = '{}{}\n'.format(self.save_seq, line) seqlen += len(line) if self.header is not None: if seqlen > mlen: self.save_this(fo) def show_seq_out(self): print('The following sequences are in the output file:') plist = "\n".join(self.lines_out) print(plist) def parse_args(self): parser = argparse.ArgumentParser(description='SortFasta') parser.add_argument('-i', action='store', dest='infile_name', help='Name of input file') parser.add_argument('-o', action='store', dest='oufile_name', help='Name of output file') parser.add_argument('-s', action='store', dest='minsize', help='Minimum sequence length') args = parser.parse_args() action = results = parser.parse_args() self.infilename = action.infile_name.strip('\'"') self.outfilename = action.oufile_name.strip('\'"') self.minsize = int(action.minsize) if __name__ == '__main__': sf = SelectAndSortFasta() if len(sys.argv) > 1: sf.parse_args() sf.split_file(from_args=True) else: infile = 'data/fasta/AINZ01/AINZ01.1.fsa_nt' outfile = 'data/fasta/AINZ01/AINZ01sorted.1.fsa_nt' minlen = 1000 sf.split_file(from_args=False, in_filename=infile, out_filename=outfile, min_seq_len=minlen) sf.show_seq_out()