/tools/fastq/fastq_paired_end_joiner.py

https://bitbucket.org/cistrome/cistrome-harvard/ · Python · 38 lines · 30 code · 6 blank · 2 comment · 9 complexity · 8e1a32d88a11f9075f4ef3a00fe37ff1 MD5 · raw file

  1. #Dan Blankenberg
  2. import sys, os, shutil
  3. from galaxy_utils.sequence.fastq import fastqReader, fastqNamedReader, fastqWriter, fastqJoiner
  4. def main():
  5. #Read command line arguments
  6. input1_filename = sys.argv[1]
  7. input1_type = sys.argv[2] or 'sanger'
  8. input2_filename = sys.argv[3]
  9. input2_type = sys.argv[4] or 'sanger'
  10. output_filename = sys.argv[5]
  11. if input1_type != input2_type:
  12. print "WARNING: You are trying to join files of two different types: %s and %s." % ( input1_type, input2_type )
  13. input2 = fastqNamedReader( open( input2_filename, 'rb' ), input2_type )
  14. joiner = fastqJoiner( input1_type )
  15. out = fastqWriter( open( output_filename, 'wb' ), format = input1_type )
  16. i = None
  17. skip_count = 0
  18. for i, fastq_read in enumerate( fastqReader( open( input1_filename, 'rb' ), format = input1_type ) ):
  19. identifier = joiner.get_paired_identifier( fastq_read )
  20. fastq_paired = input2.get( identifier )
  21. if fastq_paired is None:
  22. skip_count += 1
  23. else:
  24. out.write( joiner.join( fastq_read, fastq_paired ) )
  25. out.close()
  26. if i is None:
  27. print "Your file contains no valid FASTQ reads."
  28. else:
  29. print input2.has_data()
  30. print 'Joined %s of %s read pairs (%.2f%%).' % ( i - skip_count + 1, i + 1, float( i - skip_count + 1 ) / float( i + 1 ) * 100.0 )
  31. if __name__ == "__main__":
  32. main()