Repository navigation
Expand file tree
/
Copy pathsplit_fasta.py
More file actions
58 lines (48 loc) · 1.74 KB
/
Copy pathsplit_fasta.py
File metadata and controls
58 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
##################################################################
#### Tessa Pierce
#### https://github.com/bluegenes/
#### 5.30.2013, edited 12.5.2014 and 1.13.2015
###################################################################
"""Function: Take in a FASTA file and a list of contig names: print
two fastas: one containing the fasta entries for contigs in the list;
the other containing the remaining contigs in the input FASTA.
"""
###################################################################
import sys, re
fastaIN = open(sys.argv[1], 'r')
name_list = open(sys.argv[2], 'r')
inListOUT= open(sys.argv[3], 'w')
notInListOUT= open(sys.argv[4], 'w')
fastaLines = [ x.strip() for x in fastaIN ]
names = [x.split('\t')[0] for x in name_list]
bp = ''
contig_name = re.compile('^(>\S*)')# match just to 1st whitespace. If want to match entire line, change (>\S*) --> (>.*)
nameList = []
# parse and store the list of contigs you want to separate out.
for contig in names:
if not contig.startswith('>'):
contig = '>' + contig
searchName = re.match(contig_name, contig).groups()[0]
nameList.append(searchName)
#go through the fasta file, writing contigs to the inList file or notInList file.
for line in fastaLines:
if line.startswith('>'):
if len(bp) > 0:
if name in nameList:
inListOUT.write(full_name + '\n' + bp + '\n')
else:
notInListOUT.write(full_name + '\n' + bp + '\n')
bp = ''
name = re.match(contig_name, line).groups()[0]
full_name = line
else:
bp = bp + line
#catch the last contig
if name in nameList:
inListOUT.write(full_name + '\n' + bp + '\n')
else:
notInListOUT.write(full_name + '\n' + bp + '\n')
fastaIN.close()
inListOUT.close()
notInListOUT.close()
name_list.close()