0

I'm trying to remove the import sys and import os lines from one hunk of my script:

diff --git a/GFF/fastas2GFF.py b/GFF/fastas2GFF.py
old mode 100644
new mode 100755
index 88850cd..8226717
--- a/GFF/fastas2GFF.py
+++ b/GFF/fastas2GFF.py
@@ -1,13 +1,22 @@
+#!/usr/bin/env python
+#fastas2GFF.py gets a multisequence FASTA file and outputs a GFF file
+import sys
+import os
 from Bio import SeqIO
 from Bio.Seq import Seq

-def map_bowtie(f, index_name):
-    handle = open(f, 'rU')
+if len(sys.argv) != 3:
+    sys.exit('Usage: %s <fasta_file> <bt2_index_name>' % sys.argv[0])
+
+def map_bowtie(input_file, index_name):
+    handle = open(input_file, 'rU')
     dna = list(SeqIO.parse(handle, 'fasta'))
     handle.close()
     map_dict = {}
     counter = 1 #To comply with the GFF format file specifications needs to start at 1
-    cat_genes = open(f + '.seq', 'a')
+
+    output_fasta_file = os.path.splitext(input_file)[0] + '.concatenated.fasta'
+    cat_genes = open(output_fasta_file, 'a')
     cat_genes.write('>' + index_name + '\n') #FASTA header

     seqid_counter = 1 #To order the output of the sequence
@@ -16,7 +25,6 @@ def map_bowtie(f, index_name):
         gene_len = len(str(sequence.seq))

         description = list()
-        
         description.append(index_name) #bowtie index name
         description.append('beja_lab') #source of the sequence
         description.append('CDS') #Feature type: This can be exon, promotor, etc for us CDS it's ok i guess
@@ -25,8 +33,7 @@ def map_bowtie(f, index_name):
         description.append('.') #score ???
         description.append('+') #strand
         description.append('.') #score ???
-        description.append('GeneID '+ sequence.id + ' ' + str(seqid_counter).zfill(4)) #CDS0001, CDS0002, etc
-        
+        description.append('GeneID '+ sequence.id)# + ' ' + str(seqid_counter).zfill(4)) #CDS0001, CDS0002, etc
         description.append('\n')

         map_dict[seqid_counter] = description
@@ -34,7 +41,13 @@ def map_bowtie(f, index_name):
         counter += (gene_len)
         cat_genes.write(str(sequence.seq))

-    with open(f + '.map', 'w') as m:
+    output_gff_file = os.path.splitext(input_file)[0] + '.GFF2'
+    with open(output_gff_file, 'w') as m:
         m.write('##gff-version 2\n')
         m.writelines('\t'.join(map(str,map_dict[k])) for k in map_dict.keys())
     return
+
+fasta_file = sys.argv[1]
+bt2_index = sys.argv[2]
+
+map_bowtie(fasta_file, bt2_index)

After git add -p I ignore the mode change. Then I split this hunk:

@@ -1,13 +1,22 @@
+#!/usr/bin/env python
+#fastas2GFF.py gets a multisequence FASTA file and outputs a GFF file
+import sys
+import os
 from Bio import SeqIO
 from Bio.Seq import Seq

-def map_bowtie(f, index_name):
-    handle = open(f, 'rU')
+if len(sys.argv) != 3:
+    sys.exit('Usage: %s <fasta_file> <bt2_index_name>' % sys.argv[0])
+
+def map_bowtie(input_file, index_name):
+    handle = open(input_file, 'rU')
     dna = list(SeqIO.parse(handle, 'fasta'))
     handle.close()
     map_dict = {}
     counter = 1 #To comply with the GFF format file specifications needs to start at 1
-    cat_genes = open(f + '.seq', 'a')
+
+    output_fasta_file = os.path.splitext(input_file)[0] + '.concatenated.fasta'
+    cat_genes = open(output_fasta_file, 'a')
     cat_genes.write('>' + index_name + '\n') #FASTA header

     seqid_counter = 1 #To order the output of the sequence

And get the one that I want to modify (remove the import sys and import os):

Split into 3 hunks.
@@ -1,3 +1,7 @@
+#!/usr/bin/env python
+#fastas2GFF.py gets a multisequence FASTA file and outputs a GFF file
+import sys
+import os
 from Bio import SeqIO
 from Bio.Seq import Seq

Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? 

The edited hunk that is being rejected is this one:

# Manual hunk edit mode -- see bottom for a quick guide
@@ -1,3 +1,5 @@
+#!/usr/bin/env python
+#fastas2GFF.py gets a multisequence FASTA file and outputs a GFF file
 from Bio import SeqIO
 from Bio.Seq import Seq

# ---

From reading this answer I suppose my problem is with the @@ from-file-range to-file-range @@ line, but I can't set the correct ranges.

Community
  • 1
  • 1
ppflrs
  • 311
  • 4
  • 11
  • Sorry I didn't include the complete code, which is the one not working when I try to do the hunk edition. – ppflrs Nov 18 '15 at 13:30
  • It does not make sense to post working stuff and ask why it is not working ;) – hek2mgl Nov 18 '15 at 13:33
  • 1
    I didn't knew the isolated piece of work wouldn't make a proper example, sorry. I just added the entire code. – ppflrs Nov 18 '15 at 13:37

0 Answers0