| 
0
 | 
     1 import argparse
 | 
| 
 | 
     2 
 | 
| 
 | 
     3 #docs.python.org/dev/library/argparse.html
 | 
| 
 | 
     4 parser = argparse.ArgumentParser()
 | 
| 
 | 
     5 parser.add_argument("--input", help="Input fasta")
 | 
| 
 | 
     6 parser.add_argument("--output", help="Output fasta")
 | 
| 
 | 
     7 parser.add_argument("--start", help="How many nucleotides to trim from the start", type=int)
 | 
| 
 | 
     8 parser.add_argument("--end", help="How many nucleotides to trim from the end", type=int)
 | 
| 
 | 
     9 
 | 
| 
 | 
    10 args = parser.parse_args()
 | 
| 
 | 
    11 start = int(args.start)
 | 
| 
 | 
    12 end = int(args.end)
 | 
| 
 | 
    13 
 | 
| 
 | 
    14 print args.input
 | 
| 
 | 
    15 print args.output
 | 
| 
 | 
    16 print start
 | 
| 
 | 
    17 print end
 | 
| 
 | 
    18 
 | 
| 
 | 
    19 if end <= 0 and start <= 0:
 | 
| 
 | 
    20 	import shutil
 | 
| 
 | 
    21 	shutil.copy(args.input, args.output)
 | 
| 
 | 
    22 	import sys
 | 
| 
 | 
    23 	sys.exit()
 | 
| 
 | 
    24 	
 | 
| 
3
 | 
    25 def trim(string, s, e):
 | 
| 
 | 
    26 	if e == 0:
 | 
| 
 | 
    27 		return string[s:]
 | 
| 
 | 
    28 	else:
 | 
| 
 | 
    29 		return string[s:-e]
 | 
| 
0
 | 
    30 
 | 
| 
 | 
    31 currentSeq = ""
 | 
| 
 | 
    32 currentId = ""
 | 
| 
3
 | 
    33 with open(args.input, 'r') as i, open(args.output, 'w') as o:
 | 
| 
 | 
    34 	for line in i:
 | 
| 
 | 
    35 		print "ID:", currentId
 | 
| 
 | 
    36 		if line[0] == ">":
 | 
| 
 | 
    37 			currentSeq = trim(currentSeq, start, end)
 | 
| 
 | 
    38 			if len(currentId) > 0 and len(currentSeq) > 0:
 | 
| 
 | 
    39 				o.write(currentId)
 | 
| 
 | 
    40 				o.write(currentSeq + "\n")
 | 
| 
 | 
    41 			currentId = line
 | 
| 
 | 
    42 			currentSeq = ""
 | 
| 
 | 
    43 		else:
 | 
| 
 | 
    44 			currentSeq += line.rstrip()
 | 
| 
 | 
    45 	o.write(currentId)
 | 
| 
 | 
    46 	o.write(currentSeq + "\n")
 |