| 
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 	
 | 
| 
 | 
    25 
 | 
| 
 | 
    26 
 | 
| 
 | 
    27 currentSeq = ""
 | 
| 
 | 
    28 currentId = ""
 | 
| 
 | 
    29 
 | 
| 
 | 
    30 if end is 0:
 | 
| 
 | 
    31 	with open(args.input, 'r') as i:
 | 
| 
 | 
    32 		with open(args.output, 'w') as o:
 | 
| 
 | 
    33 			for line in i.readlines():
 | 
| 
 | 
    34 				if line[0] is ">":
 | 
| 
 | 
    35 					currentSeq = currentSeq[start:]
 | 
| 
 | 
    36 					if currentSeq is not "" and currentId is not "":
 | 
| 
 | 
    37 						o.write(currentId)
 | 
| 
 | 
    38 						o.write(currentSeq + "\n")
 | 
| 
 | 
    39 					currentId = line
 | 
| 
 | 
    40 					currentSeq = ""
 | 
| 
 | 
    41 				else:
 | 
| 
 | 
    42 					currentSeq += line.rstrip()
 | 
| 
 | 
    43 			o.write(currentId)
 | 
| 
 | 
    44 			o.write(currentSeq[start:] + "\n")
 | 
| 
 | 
    45 else:
 | 
| 
 | 
    46 	with open(args.input, 'r') as i:
 | 
| 
 | 
    47 		with open(args.output, 'w') as o:
 | 
| 
 | 
    48 			for line in i.readlines():
 | 
| 
 | 
    49 				if line[0] is ">":
 | 
| 
 | 
    50 					currentSeq = currentSeq[start:-end]
 | 
| 
 | 
    51 					if currentSeq is not "" and currentId is not "":
 | 
| 
 | 
    52 						o.write(currentId)
 | 
| 
 | 
    53 						o.write(currentSeq + "\n")
 | 
| 
 | 
    54 					currentId = line
 | 
| 
 | 
    55 					currentSeq = ""
 | 
| 
 | 
    56 				else:
 | 
| 
 | 
    57 					currentSeq += line.rstrip()
 | 
| 
 | 
    58 			o.write(currentId)
 | 
| 
 | 
    59 			o.write(currentSeq[start:-end] + "\n")
 |