-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tabular and multi/single fasta conversion
- Loading branch information
1 parent
43ef427
commit 17a8a48
Showing
5 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# python3 | ||
from gooey import * | ||
import os | ||
from Bio import SeqIO | ||
import pandas as pd | ||
# imput parameters | ||
@Gooey(required_cols=2, program_name='single-fastas to tabular txt file', header_bg_color= '#DCDCDC', terminal_font_color= '#DCDCDC', terminal_panel_color= '#DCDCDC') | ||
def main(): | ||
ap = GooeyParser() | ||
ap.add_argument("-dir", "--directory", required=True, type=str, widget='DirChooser', help="directory to search for fasta files") | ||
ap.add_argument("-out","--output", required=True, widget='FileSaver', help="output txt file") | ||
args = vars(ap.parse_args()) | ||
# main | ||
seqs = [] | ||
ids = [] # setup empty lists | ||
# import each fasta file from the working directory | ||
for filename in sorted(os.listdir(os.chdir(args['directory']))): | ||
if filename.endswith(".fa") or filename.endswith(".fasta"): | ||
for record in SeqIO.parse(filename, "fasta"): | ||
ids.append(record.id) | ||
seqs.append(record.seq) | ||
# put the 2 list in a data frame of 2 columns | ||
dfasta = pd.DataFrame() | ||
dfasta['id'] = ids | ||
dfasta['seq'] = seqs | ||
# export data frame to a tabular txt file | ||
with open(args['output'], 'a') as f: | ||
f.write( | ||
dfasta.to_csv(header = False, index = False, sep= "\t", line_terminator= '\n') | ||
) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# python3 | ||
import itertools | ||
from gooey import * | ||
from Bio import SeqIO | ||
from Bio.Seq import Seq | ||
from Bio.SeqRecord import SeqRecord | ||
import pandas as pd | ||
# input arguments | ||
@Gooey(required_cols=1, program_name='tabular to single-fasta files', header_bg_color= '#DCDCDC', terminal_font_color= '#DCDCDC', terminal_panel_color= '#DCDCDC') | ||
def main(): | ||
ap = GooeyParser(description="convert each row of a tabular file with the fasta headers and sequences in each row in single-fasta files") | ||
ap.add_argument("-in", "--input", required=True, widget="FileChooser", help="input txt file") | ||
args = vars(ap.parse_args()) | ||
# main | ||
df = pd.read_csv(args['input'], header=None, sep="\t") | ||
# select ids and sequence columns, convert to lists | ||
headers = df.iloc[:,0].values.tolist() | ||
sequences = df.iloc[:,1].values.tolist() | ||
# iter elements on pairs to export to single-fasta files | ||
for (ids, seq) in zip(headers, sequences): | ||
seq_for_fasta=SeqRecord(Seq(str(seq)),id=str(ids),description="") | ||
SeqIO.write(seq_for_fasta, "".join([str(ids),".fasta"]), "fasta") | ||
|
||
if __name__ == '__main__': | ||
main() |