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
#1
Hi everyone !

This is my first post here. I am originally a biologist but I started doing bioinformatics. I have very basic knowledge in programming, in perl, python and shell.

I found online a small python script which prints a list of all contigs in a multi-fasta file, with their length.

#!/usr/bin/python
from Bio import SeqIO
import sys
cmdargs = str(sys.argv)
for seq_record in SeqIO.parse(str(sys.argv[1]), "fasta"):
 output_line = '%s\t%i' % \
(seq_record.id, len(seq_record))
 print(output_line)
I used this script to store the result in a file (that I named TestTailleContigs1.txt for now), and I wrote a script that uses this file and the original multi-fasta file to cut off all the contigs with a length < 1000 and store the result in a new file.

#!/usr/bin/python
from __future__ import division

length_file = open("TestTailleContigs1.txt")
contig_file = open("2013_1056H.contigs.fa")
output_file = open("2013_1056H.contigs_filtered.fa","w")

Contigs_over_1000 = 0
Contigs = 0
FirstContigToTrim = 0

for line in length_file:
	column = line.split("\t")
	contigsize = int(column[1])
	if contigsize > 1000:
		Contigs_over_1000 += 1
		Contigs += 1
	else :
		Contigs += 1
		if FirstContigToTrim == 0:
			FirstContigToTrim = column[0]

print("The first contig to filter is " + str(FirstContigToTrim))
print("The number of contigs in the original file is " + str(Contigs))
print("The number of contigs remaining after filtering is " + str(Contigs_over_1000))

testcontig = 0
for line in contig_file:
	present = line.count(str(FirstContigToTrim))
	testcontig = testcontig + present
	if testcontig == 0:
		output_file.write(line)


length_file.close
contig_file.close
output_file.close
My question is:
- How can I combine these two scripts into one ? I actually don't need the intermediate list of lengths, I only need to use it as an input for my second script, and as I will have quite a lot of files to process, I would quite like to avoid the accumulation of intermediate files and having to run two scripts instead of just one.

I would really like to understand how it works, more than just having the solution, as I think this is something very basic that I should know how to do.

Thanks in advance for your time and help. If there is anything I should explain differently in order for you to help me, please let me know. Smile
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 880 Aug-17-2023, 07:53 PM
Last Post: deanhystad
  Link scripts from a different folder Extra 3 1,436 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,382 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  I can't open a link with Selenium in Python jao 0 1,414 Jan-30-2022, 04:21 AM
Last Post: jao
  Parsing link from html tags with Python Melcu54 0 1,624 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,633 Mar-04-2021, 03:09 PM
Last Post: martpogs
  Running python scripts from github etc pacmyc 7 3,735 Mar-03-2021, 10:26 PM
Last Post: pacmyc
  How to skip LinkedIn signup link using python script? Mangesh121 0 1,804 Aug-26-2020, 01:22 PM
Last Post: Mangesh121
  Reading SQL scripts from excel file and run it using python saravanatn 2 2,586 Aug-23-2020, 04:49 PM
Last Post: saravanatn
  No Scripts File present after python installation ag2207 5 4,930 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