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
#22
Here is the final program. It Sorts and Selects in one module.

It is written so that you can run it from the command line using:
python SelsectAndSortFasta infilename outfilename min_seq_size
Or, you can import it into another program and call from within similar to:
# Add following import at top of your program
import SelectAndSortFasta

# In your initialization routine, add:
# If a class:
    self.sasf = SelectAndSortFasta.SelectAndSortFasta()
# If just a function:
    sasf = SelectAndSortFasta.SelectAndSortFasta()

# Then when you want to run a file
    sasf.sort_fasta_file(infile, outfile, minlen)
Here's the code:
from operator import itemgetter
import sys


class SelectAndSortFasta:
    """
    Sort in decreasing size order
    """
    def __init__(self):
        self.headers = []
        self.infile = None
        self.outfile = None
        self.minlen = 0

    def ExtractHeader(self):
        """
        Reads all headers, saves starting file position, header, and size in self.headers
        :return: None
        """
        seqlen = 0
        this_header = []
        firstseq = True

        f = open(self.infile, 'r')
        f.seek(0, 2)
        file_size = f.tell()
        f.seek(0)
        while True:
            fptr = f.tell()
            if fptr == file_size:
                break
            line = None
            line = f.readline()
            line = line.strip()

            # skip empty lines
            if len(line) > 0:
                # print(f'line len: {len(line)}')
                if line.startswith('>'):
                    if firstseq:
                        firstseq = False
                    else:
                        this_header.append(seqlen)
                        self.headers.append(this_header)

                    seqlen = 0
                    this_header = []
                    this_header.append(fptr)
                    this_header.append(line)
                else:
                    seqlen += len(line)
        this_header.append(seqlen)
        self.headers.append(this_header)
        f.close()
        # This is the sort routine, sorts on column 2 (size) of self.headers, in reverse order
        self.headers.sort(key=itemgetter(2), reverse=True)
        self.show_header()

    def show_header(self):
        """
        display self.headers list
        :return: None
        """
        for item in self.headers:
            print(f'File ptr: {item[0]}, Size: {item[2]}, Header: {item[1]}')

    def write_outfile(self):
        """
        Reads self.headers (which is sorted in reverse size order,
        seeks to that record in the inout file (from file pointer which i in column 0 of self.headers)
        and writes the output file
        :return:
        """
        with open(self.infile) as f, open(self.outfile, 'w') as fo:
            for item in self.headers:
                # Ignore entry if too small
                if item[2] < minlen:
                    continue
                f.seek(item[0], 0)
                fo.write(f.readline())
                while True:
                    buf = f.readline()
                    if buf.startswith('>'):
                        break
                    fo.write(buf)

    def sort_fasta_file(self, infile, outfile, minlen):
        """
        Launch pad for sort and select program
        :param infile: Name of input fasta file
        :param outfile: Name of output fasta file
        :param minlen: Minimun size of output record. (smaller inut records are ignored)
        :return: None
        """
        self.infile = infile
        self.outfile = outfile
        self.minlen = minlen
        self.ExtractHeader()
        self.write_outfile()


if __name__ == '__main__':
    # Test routine
    srtf = SelectAndSortFasta()
    numargs = len(sys.argv)
    if numargs > 1:
        infile = sys.argv[1]
        outfile = sys.argv[2]
        minlen = sys.argv[3]
    else:
        infile = 'data/fasta/AINZ01/AINZ01.1.fsa_nt'
        outfile = 'data/fasta/AINZ01/AINZ01sorted.1.fsa_nt'
        minlen = 1000

    srtf.sort_fasta_file(infile, outfile, minlen)
You may want to supress the printout on line 57 (self.show_header())
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,478 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,633 Aug-23-2020, 04:49 PM
Last Post: saravanatn
  No Scripts File present after python installation ag2207 5 4,977 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