-
Notifications
You must be signed in to change notification settings - Fork 0
/
ANSWER for Python_04 Question3.rtf
178 lines (168 loc) · 4.6 KB
/
ANSWER for Python_04 Question3.rtf
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
{\rtf1\ansi\ansicpg1252\cocoartf1404\cocoasubrtf470
{\fonttbl\f0\fnil\fcharset0 Menlo-Regular;\f1\fnil\fcharset0 Menlo-Bold;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww13420\viewh18000\viewkind0
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\f0\fs22 \cf0 \CocoaLigature0 Python_05 Question 3\
\
\
Have a FASTA file with new lines\
\
i want a \'91for loop\'92 so that when it encounters a >, it prints it\
\
then, if i encounter a line with no >, then it puts that sequence into an empty file named combined\
\
next, the next line with no >, will be also put into the combined file\
\
then i will print the combined file\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\fs26 \cf0 #!/usr/bin/env python3\
#Python_05 question 3\
\
\
fasta_read = open ("../Python_05.fasta", "r")\
fasta_write = open ("../test1.fasta", "w")\
\
combined = ""\
\
for dna in fasta_read:\
\
dna = dna.rstrip() \
\
if dna.startswith('>'):\
fasta_write.write(dna + "\\n")\
\
else:\
combined+=dna\
fasta_write.write(combined + "\\n")\
\
fasta_read.close()\
fasta_write.close()\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\fs22 \cf0 \
\f1\b PROBLEM
\f0\b0 that occurred was that the combined file contained line 1, and line1+line2\
\
and the next >seq was concatenating to the prior concatenation\
\
\
so! how to stop this? \
\
well, why not clear the combined file every time a > is encountered\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\fs26 \cf0 #!/usr/bin/env python3\
#Python_05 question 3\
\
\
fasta_read = open ("../Python_05.fasta", "r")\
fasta_write = open ("../test1.fasta", "w")\
\
combined = ""\
\
for dna in fasta_read:\
\
dna = dna.rstrip() \
\
if dna.startswith('>'):\
combined = ""\
fasta_write.write(dna + "\\n")\
\
else:\
combined+=dna\
fasta_write.write(combined + "\\n")\
\
fasta_read.close()\
fasta_write.close()\
\
\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\f1\b\fs22 \cf0 PROBLEM
\f0\b0 \
\
the next > doesn\'92t show the previous >\'92s sequence, but I\'92m still getting the first round printed\
so why not move when the concatenated thing prints\
\
print the concatenation only if the > is encountered? \
\
so, create an if statement such that if a > is encountered, then the combined file will be printed\
the combine file after\
\
if combined statement (bool - so if it\'92s empty, the answer is false, so then it won\'92t print anything; but if it encounters > and the combined file is full, it will print it\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\fs26 \cf0 #!/usr/bin/env python3\
#Python_05 question 3\
\
\
fasta_read = open ("../Python_05.fasta", "r")\
fasta_write = open ("../test1.fasta", "w")\
\
combined = ""\
\
for dna in fasta_read:\
\
dna = dna.rstrip() \
\
if dna.startswith('>'):\
fasta_write.write(combined + "\\n")\
combined = ""\
fasta_write.write(dna + "\\n")\
\
else:\
combined+=dna\
\
fasta_read.close()\
fasta_write.close()\
\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\fs22 \cf0 \
\
\
\
\f1\b \
PROBLEM
\f0\b0 \
\
the final sequence didn\'92t print out, but everything looks good!\
\
add the final print statement to the end of the script\
\
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\pardirnatural\partightenfactor0
\fs26 \cf0 \
\
\
#!/usr/bin/env python3\
#Python_05 question 3\
\
\
fasta_read = open ("../Python_05.fasta", "r")\
fasta_write = open ("../Python_05comb.fasta", "w")\
\
combined = ""\
\
\
for dna in fasta_read:\
dna = dna.rstrip()\
\
if dna.startswith('>'):\
if combined:\
fasta_write.write(combined + "\\n")\
combined = ""\
fasta_write.write(dna + "\\n")\
\
else:\
combined += dna\
\
fasta_write.write(combined + "\\n")\
\
fasta_read.close()\
fasta_write.close()
\fs22 \
\
\
}