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
#6
sorry, I gave you an ftp url

I was looking for something larger, 2 items is not a good test.
Nevertheless, take a look at the following and see if it does what you'd like
save the file as SplitFasta.py
#!/usr/bin/python
import sys


class SplitFasta:
    def __init__(self):
        self.lines_out = []
        self.header = None
        self.save_seq = ''

    def save_this(self, fo):
        fo.write('{}\n'.format(self.header))
        fo.write('{}\n'.format(self.save_seq))

    def split_file(self, in_filename, out_filename, min_seq_len):

        self.header = None
        seqlen = 0
        self.lines_out = []
        with open(in_filename, 'r') as fin, open(out_filename, '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 > min_seq_len:
                            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 > min_seq_len:
                    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)

if __name__ == '__main__':
    sf = SplitFasta()
    numargs = len(sys.argv)
    print(f'numargs: {numargs}')
    if numargs > 1:
        infile = sys.argv[1]
        outfile = sys.argv[2]
        minlen = sys.argv[3]
    else:
        infile = 'Example.fa'
        outfile = 'NewExample.fa'
        minlen = 1000
    sf.split_file(infile, outfile, int(minlen))
    sf.show_seq_out()
The bottom part 'if __name__ (etc.)' is for testing only, and woun be used if the module is imported into another program

you can import this into any program like:
import SplitFasta
and then use this way
just once at start of script, instantiate the class with:
sf = SplitFasta.SplitFasta()
Then to call, use:
sf.split_file(your_in_filename, your_out_filename, your_min_seq_len)
if you want to show what was written, call:
sf.show_seq_out()
of if you just want the list of output headers:
my_header_list = sf.lines_out
If this is what you are looking for, let me know, I'll be going to bed soon, but be back in about 4 - 5 hours.

Here's a sample run (from command line):
Output:
λ python SplitFasta.py Example.fa NewExample.fa 1000 numargs: 4 The following sequences are in the output file: >NODE_30_length_1090_cov_54656.2
and the output file (NewExample.fa:
Output:
>NODE_30_length_1090_cov_54656.2 TTATGGAATATCTATTAGAGCAAAAAAGAGATTTTACGCAATTAAAATTTAGCGATATAC AGCAAATGAAATCAGCTTATAGCATAAGAATTTATAATATGCTACTTTGTGAATTAAAAC AAAACAGACAAAATCTTAAAATAAATCTTTCAGTATTGCAAAATCTTTTAGAAGTTCCGA AAAATTATGAAGAAAGATGGGCTGATTTTAATCGTTTTGTATTAAAACAAGCAGAAAAAG ATATAAATAGCAAATCTAATTTAGTTTTATTAGATATTAAAACTTATAAAACAGGGCGTA AAATAACAGACTTAGAGTTTATTTTTGATTATAAAAATAACGATAAGCGTATCGCACAGG AAAAACTAAAAGAAGAAAATTTATTTAAAAAACTCAAAGAAATATTAAGTTCTTACATAG GCAAATCAATTTATGATGATAGATTTGGCGAAATGATTATAAGTCATTACGAACATAATG AAGAAAATAAAAAGATTTTAATTATCGCCCAGAGAAAAAGCGATGATAAATTTGTTTGCT TTGGTGTTAAAAACTTCAAAGATATTAAAAGTTTAGAAAAGCTAAAAGATAAAGCAGAAG AGTTGTTTTATTTAGATAAACAAAGAGTTTTAAAAGCAAAAGAAGCTCAAAAATATAGAA ATCTTTTTAATTGATTGTATTTTAAAAATTATAAAAATAAAAGAGATATTAAAAGGCTTG ATTGATAAAAATAATTCTTAAGCTCTAATATCTATGCTTTTTTGTGTAGAATTTAAAGAA AGAATTTTATTAAATTCCCCTGTATTATCATCGCTAAATTTCATACCAAAAAGAATTTCT AGCTCATCGCTTGTGCCAAATTTATTTTCCAGTAGCTTTTTTAAAAGCTCATTCATTTTA TTATCATCTTTATAGGTTTCGCTTTTACTTTCTGCTTGTATAGGTTTAAAAGGCTTTTTT TTGTCTTCTTCTGAAGTTTCTTTGTTATTTGTATTTTTTAAAGGATTGCTATAATCTACA CCTTTTGCCTTTTCTGCTTCTTCTAGTGATTTTACAAACCCATCGTGTCTTTGTTTAAAA TCAAGATATT
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 1,034 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  Link scripts from a different folder Extra 3 1,598 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,484 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  I can't open a link with Selenium in Python jao 0 1,493 Jan-30-2022, 04:21 AM
Last Post: jao
  Parsing link from html tags with Python Melcu54 0 1,668 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,820 Mar-04-2021, 03:09 PM
Last Post: martpogs
  Running python scripts from github etc pacmyc 7 3,930 Mar-03-2021, 10:26 PM
Last Post: pacmyc
  How to skip LinkedIn signup link using python script? Mangesh121 0 1,868 Aug-26-2020, 01:22 PM
Last Post: Mangesh121
  Reading SQL scripts from excel file and run it using python saravanatn 2 2,777 Aug-23-2020, 04:49 PM
Last Post: saravanatn
  No Scripts File present after python installation ag2207 5 5,188 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