annotate change_fasta_deflines.py @ 0:a3130cc156dd draft default tip

planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
author public-health-bioinformatics
date Thu, 04 Jul 2019 19:41:09 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
1 #!/usr/bin/env python
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
2 import sys, argparse
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
3 '''Accepts either csv (default) or tab-delimited files with old/new sequence names, creating a dictionary of
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
4 respective key:value pairs. Parses an input fasta file for 'old' names, replacing them with 'new' names, writing
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
5 renamed sequences to a fasta file. NOTE: use of tab-delim text file for renaming requires '-t' on cmd line.'''
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
6 #USAGE EXAMPLE 1: python change_fasta_def_lines.py csv_rename_file.csv fasta_2_rename.fasta renamedSequences.fasta
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
7 #USAGE EXAMPLE 2: python change_fasta_def_lines.py tab_delim_rename_file.txt -t fasta_2_rename.fasta renamedSequences.fasta
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
8
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
9 '''Author: Diane Eisler, Molecular Microbiology & Genomics, BCCDC Public Health Laboratory,Sept 2017'''
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
10
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
11 #parse command line arguments
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
12 parser = argparse.ArgumentParser()
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
13 parser.add_argument ("-t", "--tab_delim", help = "name fasta definition lines from tab-delim file", action = "store_true")
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
14 parser.add_argument("inFileHandle") #csv file with current fasta file names in column 1 and desired names in col 2
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
15 parser.add_argument("inFileHandle2") #fasta file containing sequences requiring name replacement
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
16 parser.add_argument("outFileHandle") #user-specified output filename
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
17 args = parser.parse_args()
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
18
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
19 #open a writable output file that will be over-written if it already exists
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
20 outfile= open(args.outFileHandle,'w')
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
21 dict = {} #dictionary to hold old_name:new_name key:value pairs
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
22 splitter = ',' #default char to split lines at
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
23 #determine if input naming file is csv (default) or tab delim text
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
24 if args.tab_delim:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
25 splitter = '\t' #change splitter to tab if comd line args contain '-t'
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
26
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
27 #create dictionary using key/value pairs from csv file of old/new names
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
28 with open(args.inFileHandle,'r') as inputFile:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
29 #read in each line and split at comma into key:value pairs
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
30 for line in inputFile:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
31 #remove whitespace from end of lines, split at comma, assigning to key:value pairs
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
32 line2 = line.rstrip()
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
33 splitLine = line2.split(splitter)
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
34 old_name = splitLine[0]
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
35 new_name = splitLine[1]
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
36 dict[old_name] = new_name
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
37
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
38 #parse fasta deflines for 'old' names and, if found, replace with new names
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
39 with open(args.inFileHandle2,'r') as inputFile2:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
40 for line in inputFile2:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
41 #find the definition lines, remove trailing whitespace & '>'
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
42 if ">" in line:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
43 originalDefline = line.rstrip().replace(">","",1)
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
44 #check for a match to any of the dict key
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
45 if originalDefline in dict:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
46 #find the index of that item in the list
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
47 newDefline= dict[originalDefline]
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
48 #print("the new name"), newDefline
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
49 # print each item to make sure the right name is being entered
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
50 outfile.write(">" + newDefline + "\n")
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
51 else:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
52 #write out the original defline sequence name
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
53 print("Defline not in dictionary: ", originalDefline)
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
54 outfile.write(">" + originalDefline + "\n")
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
55 else:
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
56 #in lines without ">", write out sequence as it was
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
57 seq = line.rstrip()
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
58 outfile.write(seq+"\n")
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
59
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
60 inputFile.close()
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
61 inputFile2.close()
a3130cc156dd planemo upload for repository https://github.com/Public-Health-Bioinformatics/flu_classification_suite commit b96b6e06f6eaa6ae8ef4c24630dbb72a4aed7dbe
public-health-bioinformatics
parents:
diff changeset
62 outfile.close()