Python Forum
How to link two python scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to link two python scripts
#36
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:
python SelectAndSortFasta.py -h
To 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()
Reply


Messages In This Thread
How to link two python scripts - by berthenet - Jan-26-2018, 10:18 AM
RE: How to link two python scripts - by Larz60+ - Jan-26-2018, 11:10 AM
RE: How to link two python scripts - by berthenet - Jan-26-2018, 11:49 AM
RE: How to link two python scripts - by Larz60+ - Jan-26-2018, 12:31 PM
RE: How to link two python scripts - by berthenet - Jan-26-2018, 12:42 PM
RE: How to link two python scripts - by Larz60+ - Jan-26-2018, 01:40 PM
RE: How to link two python scripts - by Larz60+ - Jan-26-2018, 02:03 PM
RE: How to link two python scripts - by berthenet - Jan-26-2018, 02:24 PM
RE: How to link two python scripts - by Larz60+ - Jan-26-2018, 07:58 PM
RE: How to link two python scripts - by berthenet - Jan-29-2018, 10:04 AM
RE: How to link two python scripts - by Larz60+ - Jan-29-2018, 12:11 PM
RE: How to link two python scripts - by berthenet - Jan-29-2018, 12:24 PM
RE: How to link two python scripts - by Larz60+ - Jan-29-2018, 01:29 PM
RE: How to link two python scripts - by berthenet - Jan-29-2018, 01:38 PM
RE: How to link two python scripts - by buran - Jan-29-2018, 01:43 PM
RE: How to link two python scripts - by Larz60+ - Jan-29-2018, 01:47 PM
RE: How to link two python scripts - by buran - Jan-29-2018, 01:50 PM
RE: How to link two python scripts - by berthenet - Jan-29-2018, 01:56 PM
RE: How to link two python scripts - by buran - Jan-29-2018, 02:03 PM
RE: How to link two python scripts - by Larz60+ - Jan-30-2018, 05:20 AM
RE: How to link two python scripts - by Larz60+ - Jan-30-2018, 09:45 PM
RE: How to link two python scripts - by Larz60+ - Jan-30-2018, 11:58 PM
RE: How to link two python scripts - by berthenet - Feb-01-2018, 10:26 AM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:02 PM
RE: How to link two python scripts - by berthenet - Feb-01-2018, 01:16 PM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:26 PM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:28 PM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:36 PM
RE: How to link two python scripts - by berthenet - Feb-01-2018, 01:39 PM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:47 PM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:48 PM
RE: How to link two python scripts - by Larz60+ - Feb-01-2018, 01:52 PM
RE: How to link two python scripts - by berthenet - Feb-02-2018, 09:16 AM
RE: How to link two python scripts - by Larz60+ - Feb-02-2018, 11:36 AM
RE: How to link two python scripts - by Larz60+ - Feb-02-2018, 09:21 PM
RE: How to link two python scripts - by Larz60+ - Feb-03-2018, 02:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to us python.exe from our network to run scripts cubangt 3 925 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  Link scripts from a different folder Extra 3 1,475 May-11-2022, 08:34 PM
Last Post: snippsat
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,413 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  I can't open a link with Selenium in Python jao 0 1,423 Jan-30-2022, 04:21 AM
Last Post: jao
  Parsing link from html tags with Python Melcu54 0 1,636 Jun-14-2021, 09:25 AM
Last Post: Melcu54
  How to link Sublime Text 3 Build system to Python 3.9 Using Windows 10 Fanman001 2 4,676 Mar-04-2021, 03:09 PM
Last Post: martpogs
  Running python scripts from github etc pacmyc 7 3,794 Mar-03-2021, 10:26 PM
Last Post: pacmyc
  How to skip LinkedIn signup link using python script? Mangesh121 0 1,821 Aug-26-2020, 01:22 PM
Last Post: Mangesh121
  Reading SQL scripts from excel file and run it using python saravanatn 2 2,632 Aug-23-2020, 04:49 PM
Last Post: saravanatn
  No Scripts File present after python installation ag2207 5 4,975 Jul-30-2020, 11:11 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020